u-form-item.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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) return;
  155. this.itemRules = rules;
  156. },
  157. // 获取父组件的参数
  158. updateParentData() {
  159. // 此方法写在mixin中
  160. this.getParentData('u-form');
  161. },
  162. // 移除u-form-item的校验结果
  163. clearValidate() {
  164. this.message = null
  165. },
  166. // 清空当前的组件的校验结果,并重置为初始值
  167. resetField() {
  168. // 找到原始值
  169. const value = getProperty(this.parent.originalModel, this.prop)
  170. // 将u-form的model的prop属性链还原原始值
  171. setProperty(this.parent.model, this.prop, value)
  172. // 移除校验结果
  173. this.message = null
  174. },
  175. // 点击组件
  176. clickHandler() {
  177. this.$emit('click')
  178. }
  179. },
  180. }
  181. </script>
  182. <style lang="scss" scoped>
  183. @import "../../libs/css/components.scss";
  184. .u-form-item {
  185. @include flex(column);
  186. font-size: 14px;
  187. color: $u-main-color;
  188. &__body {
  189. @include flex;
  190. padding: 10px 0;
  191. &__left {
  192. @include flex;
  193. align-items: center;
  194. &__content {
  195. position: relative;
  196. @include flex;
  197. align-items: center;
  198. padding-right: 10rpx;
  199. flex: 1;
  200. &__icon {
  201. margin-right: 8rpx;
  202. }
  203. &__required {
  204. position: absolute;
  205. left: -9px;
  206. color: $u-error;
  207. line-height: 20px;
  208. font-size: 20px;
  209. top: 3px;
  210. }
  211. &__label {
  212. @include flex;
  213. align-items: center;
  214. flex: 1;
  215. color: $u-main-color;
  216. font-size: 15px;
  217. }
  218. }
  219. }
  220. &__right {
  221. flex: 1;
  222. &__content {
  223. @include flex;
  224. align-items: center;
  225. flex: 1;
  226. &__slot {
  227. flex: 1;
  228. /* #ifndef MP */
  229. @include flex;
  230. align-items: center;
  231. /* #endif */
  232. }
  233. &__icon {
  234. margin-left: 10rpx;
  235. color: $u-light-color;
  236. font-size: 30rpx;
  237. }
  238. }
  239. &__message {
  240. font-size: 12px;
  241. line-height: 12px;
  242. color: $u-error;
  243. }
  244. }
  245. }
  246. }
  247. </style>