u-form-item.vue 6.8 KB

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