u-form-item.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <view class="u-form-item" :class="{'u-form-item--error':(!!message && parentData.errorType === 'message')}">
  3. <view
  4. class="u-form-item__body"
  5. @tap="clickHandler"
  6. :style="[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: addUnit(labelWidth || parentData.labelWidth),
  18. marginBottom: 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">
  61. <text
  62. v-if="!!message && parentData.errorType === 'message'"
  63. class="u-form-item__body__right__message"
  64. :style="{
  65. marginLeft: addUnit(parentData.labelPosition === 'top' ? 0 : (labelWidth || parentData.labelWidth))
  66. }"
  67. >{{ message }}</text>
  68. </slot>
  69. <u-line
  70. v-if="borderBottom"
  71. :color="message && parentData.errorType === 'border-bottom' ? color.error : propsLine.color"
  72. :customStyle="`margin-top: ${message && parentData.errorType === 'message' ? '5px' : 0}`"
  73. ></u-line>
  74. </view>
  75. </template>
  76. <script>
  77. import { props } from './props';
  78. import { mpMixin } from '../../libs/mixin/mpMixin';
  79. import { mixin } from '../../libs/mixin/mixin';
  80. import defProps from '../../libs/config/props.js'
  81. import color from '../../libs/config/color';
  82. import { addStyle, addUnit, getProperty, setProperty, error } from '../../libs/function/index';
  83. /**
  84. * Form 表单
  85. * @description 此组件一般用于表单场景,可以配置Input输入框,Select弹出框,进行表单验证等。
  86. * @tutorial https://ijry.github.io/uview-plus/components/form.html
  87. * @property {String} label input的label提示语
  88. * @property {String} prop 绑定的值
  89. * @property {Array} rules 绑定的规则
  90. * @property {String | Boolean} borderBottom 是否显示表单域的下划线边框
  91. * @property {String | Number} labelWidth label的宽度,单位px
  92. * @property {String} rightIcon 右侧图标
  93. * @property {String} leftIcon 左侧图标
  94. * @property {String | Object} leftIconStyle 左侧图标的样式
  95. * @property {Boolean} required 是否显示左边的必填星号,只作显示用,具体校验必填的逻辑,请在rules中配置 (默认 false )
  96. *
  97. * @example <u-form-item label="姓名" prop="userInfo.name" borderBottom ref="item1"></u-form-item>
  98. */
  99. export default {
  100. name: 'u-form-item',
  101. mixins: [mpMixin, mixin, props],
  102. data() {
  103. return {
  104. // 错误提示语
  105. message: '',
  106. parentData: {
  107. // 提示文本的位置
  108. labelPosition: 'left',
  109. // 提示文本对齐方式
  110. labelAlign: 'left',
  111. // 提示文本的样式
  112. labelStyle: {},
  113. // 提示文本的宽度
  114. labelWidth: 45,
  115. // 错误提示方式
  116. errorType: 'message'
  117. },
  118. color: color,
  119. itemRules: []
  120. }
  121. },
  122. // 组件创建完成时,将当前实例保存到u-form中
  123. computed: {
  124. propsLine() {
  125. return defProps.line
  126. }
  127. },
  128. mounted() {
  129. this.init()
  130. },
  131. emits: ["click"],
  132. watch: {
  133. // 监听规则的变化
  134. rules: {
  135. immediate: true,
  136. handler(n) {
  137. this.setRules(n);
  138. },
  139. },
  140. },
  141. methods: {
  142. addStyle,
  143. addUnit,
  144. init() {
  145. // 父组件的实例
  146. this.updateParentData()
  147. if (!this.parent) {
  148. error('u-form-item需要结合u-form组件使用')
  149. }
  150. },
  151. // 手动设置校验的规则,如果规则中有函数的话,微信小程序中会过滤掉,所以只能手动调用设置规则
  152. setRules(rules) {
  153. // 判断是否有规则
  154. if (rules.length === 0) {
  155. this.itemRules = [];
  156. return
  157. };
  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 = getProperty(this.parent.originalModel, this.prop)
  173. // 将u-form的model的prop属性链还原原始值
  174. 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: 10rpx;
  202. flex: 1;
  203. &__icon {
  204. margin-right: 8rpx;
  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. /* #ifndef MP */
  232. @include flex;
  233. align-items: center;
  234. /* #endif */
  235. }
  236. &__icon {
  237. margin-left: 10rpx;
  238. color: $u-light-color;
  239. font-size: 30rpx;
  240. }
  241. }
  242. &__message {
  243. font-size: 12px;
  244. line-height: 12px;
  245. color: $u-error;
  246. }
  247. }
  248. }
  249. }
  250. </style>