uni-events-helper-wx
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
5.3 KiB

3 years ago
  1. <template>
  2. <view class="u-form-item">
  3. <view
  4. class="u-form-item__body"
  5. @tap="clickHandler"
  6. :style="[$u.addStyle(customStyle), {
  7. flexDirection: parentData.labelPosition === 'left' ? 'row' : 'column'
  8. }]"
  9. >
  10. <!-- 微信小程序中将一个参数设置空字符串结果会变成字符串"true" -->
  11. <slot name="label">
  12. <view
  13. class="u-form-item__body__left"
  14. v-if="required || leftIcon || label"
  15. :style="{
  16. width: $u.addUnit(labelWidth || parentData.labelWidth),
  17. marginBottom: parentData.labelPosition === 'left' ? 0 : '5px',
  18. }"
  19. >
  20. <!-- 为了块对齐 -->
  21. <view class="u-form-item__body__left__content">
  22. <!-- nvue不支持伪元素before -->
  23. <text
  24. v-if="required"
  25. class="u-form-item__body__left__content__required"
  26. >*</text>
  27. <view
  28. class="u-form-item__body__left__content__icon"
  29. v-if="leftIcon"
  30. >
  31. <u-icon
  32. :name="leftIcon"
  33. :custom-style="leftIconStyle"
  34. ></u-icon>
  35. </view>
  36. <text
  37. class="u-form-item__body__left__content__label"
  38. :style="[parentData.labelStyle, {
  39. justifyContent: parentData.labelAlign === 'left' ? 'flex-start' : elLabelAlign === 'center' ? 'center' : 'flex-end'
  40. }]"
  41. >{{ label }}</text>
  42. </view>
  43. </view>
  44. </slot>
  45. <view class="u-form-item__body__right">
  46. <view class="u-form-item__body__right__content">
  47. <view class="u-form-item__body__right__content__slot">
  48. <slot />
  49. </view>
  50. <view
  51. class="item__body__right__content__icon"
  52. v-if="$slots.right"
  53. >
  54. <slot name="right" />
  55. </view>
  56. </view>
  57. </view>
  58. </view>
  59. <slot name="error">
  60. <text
  61. v-if="!!message"
  62. class="u-form-item__body__right__message"
  63. :style="{
  64. marginLeft: $u.addUnit(labelWidth || parentData.labelWidth)
  65. }"
  66. >{{ message }}</text>
  67. </slot>
  68. <u-line
  69. v-if="borderBottom"
  70. :customStyle="`margin-top: ${message ? '5px' : 0}`"
  71. ></u-line>
  72. </view>
  73. </template>
  74. <script>
  75. import props from './props.js';
  76. /**
  77. * Form 表单
  78. * @description 此组件一般用于表单场景可以配置Input输入框Select弹出框进行表单验证等
  79. * @tutorial https://www.uviewui.com/components/form.html
  80. * @property {String} label input的label提示语
  81. * @property {String} prop 绑定的值
  82. * @property {String | Boolean} borderBottom 是否显示表单域的下划线边框
  83. * @property {String | Number} labelWidth label的宽度单位px
  84. * @property {String} rightIcon 右侧图标
  85. * @property {String} leftIcon 左侧图标
  86. * @property {Boolean} required 是否显示左边的必填星号只作显示用具体校验必填的逻辑请在rules中配置 (默认 false )
  87. *
  88. * @example <u-form-item label="姓名" prop="userInfo.name" borderBottom ref="item1"></u-form-item>
  89. */
  90. export default {
  91. name: 'u-form-item',
  92. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  93. data() {
  94. return {
  95. // 错误提示语
  96. message: '',
  97. parentData: {
  98. // 提示文本的位置
  99. labelPosition: 'left',
  100. // 提示文本对齐方式
  101. labelAlign: 'left',
  102. // 提示文本的样式
  103. labelStyle: {},
  104. // 提示文本的宽度
  105. labelWidth: 45
  106. }
  107. }
  108. },
  109. // 组件创建完成时,将当前实例保存到u-form中
  110. mounted() {
  111. this.init()
  112. },
  113. methods: {
  114. init() {
  115. // 父组件的实例
  116. this.updateParentData()
  117. if (!this.parent) {
  118. uni.$u.error('u-form-item需要结合u-form组件使用')
  119. }
  120. },
  121. // 获取父组件的参数
  122. updateParentData() {
  123. // 此方法写在mixin中
  124. this.getParentData('u-form');
  125. },
  126. // 移除u-form-item的校验结果
  127. clearValidate() {
  128. this.message = null
  129. },
  130. // 清空当前的组件的校验结果,并重置为初始值
  131. resetField() {
  132. // 找到原始值
  133. const value = uni.$u.getProperty(this.parent.originalModel, this.prop)
  134. // 将u-form的model的prop属性链还原原始值
  135. uni.$u.setProperty(this.parent.model, this.prop, value)
  136. // 移除校验结果
  137. this.message = null
  138. },
  139. // 点击组件
  140. clickHandler() {
  141. this.$emit('click')
  142. }
  143. },
  144. }
  145. </script>
  146. <style lang="scss">
  147. @import "../../libs/css/components.scss";
  148. .u-form-item {
  149. @include flex(column);
  150. font-size: 14px;
  151. color: $u-main-color;
  152. &__body {
  153. @include flex;
  154. padding: 10px 0;
  155. &__left {
  156. @include flex;
  157. align-items: center;
  158. &__content {
  159. position: relative;
  160. @include flex;
  161. align-items: center;
  162. padding-right: 10rpx;
  163. flex: 1;
  164. &__icon {
  165. margin-right: 8rpx;
  166. }
  167. &__required {
  168. position: absolute;
  169. left: -9px;
  170. color: $u-error;
  171. line-height: 20px;
  172. font-size: 20px;
  173. top: 3px;
  174. }
  175. &__label {
  176. @include flex;
  177. align-items: center;
  178. flex: 1;
  179. color: $u-main-color;
  180. font-size: 15px;
  181. }
  182. }
  183. }
  184. &__right {
  185. flex: 1;
  186. &__content {
  187. @include flex;
  188. align-items: center;
  189. flex: 1;
  190. &__slot {
  191. flex: 1;
  192. /* #ifndef MP */
  193. @include flex;
  194. align-items: center;
  195. /* #endif */
  196. }
  197. &__icon {
  198. margin-left: 10rpx;
  199. color: $u-light-color;
  200. font-size: 30rpx;
  201. }
  202. }
  203. &__message {
  204. font-size: 12px;
  205. line-height: 12px;
  206. color: $u-error;
  207. }
  208. }
  209. }
  210. }
  211. </style>