u-form-item.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. <!-- {{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: 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: $u.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' ? $u.color.error : propsLine.color"
  72. :customStyle="`margin-top: ${message ? '5px' : 0}`"
  73. ></u-line>
  74. </view>
  75. </template>
  76. <script>
  77. import props from './props.js';
  78. /**
  79. * Form 表单
  80. * @description 此组件一般用于表单场景,可以配置Input输入框,Select弹出框,进行表单验证等。
  81. * @tutorial https://www.uviewui.com/components/form.html
  82. * @property {String} label input的label提示语
  83. * @property {String} prop 绑定的值
  84. * @property {String | Boolean} borderBottom 是否显示表单域的下划线边框
  85. * @property {String | Number} labelWidth label的宽度,单位px
  86. * @property {String} rightIcon 右侧图标
  87. * @property {String} leftIcon 左侧图标
  88. * @property {Boolean} required 是否显示左边的必填星号,只作显示用,具体校验必填的逻辑,请在rules中配置 (默认 false )
  89. *
  90. * @example <u-form-item label="姓名" prop="userInfo.name" borderBottom ref="item1"></u-form-item>
  91. */
  92. export default {
  93. name: 'u-form-item',
  94. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  95. data() {
  96. return {
  97. // 错误提示语
  98. message: '',
  99. parentData: {
  100. // 提示文本的位置
  101. labelPosition: 'left',
  102. // 提示文本对齐方式
  103. labelAlign: 'left',
  104. // 提示文本的样式
  105. labelStyle: {},
  106. // 提示文本的宽度
  107. labelWidth: 45,
  108. // 错误提示方式
  109. errorType: 'message'
  110. }
  111. }
  112. },
  113. // 组件创建完成时,将当前实例保存到u-form中
  114. computed: {
  115. propsLine() {
  116. return uni.$u.props.line
  117. }
  118. },
  119. mounted() {
  120. this.init()
  121. },
  122. methods: {
  123. init() {
  124. // 父组件的实例
  125. this.updateParentData()
  126. if (!this.parent) {
  127. uni.$u.error('u-form-item需要结合u-form组件使用')
  128. }
  129. },
  130. // 获取父组件的参数
  131. updateParentData() {
  132. // 此方法写在mixin中
  133. this.getParentData('u-form');
  134. },
  135. // 移除u-form-item的校验结果
  136. clearValidate() {
  137. this.message = null
  138. },
  139. // 清空当前的组件的校验结果,并重置为初始值
  140. resetField() {
  141. // 找到原始值
  142. const value = uni.$u.getProperty(this.parent.originalModel, this.prop)
  143. // 将u-form的model的prop属性链还原原始值
  144. uni.$u.setProperty(this.parent.model, this.prop, value)
  145. // 移除校验结果
  146. this.message = null
  147. },
  148. // 点击组件
  149. clickHandler() {
  150. this.$emit('click')
  151. }
  152. },
  153. }
  154. </script>
  155. <style lang="scss" scoped>
  156. @import "../../libs/css/components.scss";
  157. .u-form-item {
  158. @include flex(column);
  159. font-size: 14px;
  160. color: $u-main-color;
  161. &__body {
  162. @include flex;
  163. padding: 10px 0;
  164. &__left {
  165. @include flex;
  166. align-items: center;
  167. &__content {
  168. position: relative;
  169. @include flex;
  170. align-items: center;
  171. padding-right: 10rpx;
  172. flex: 1;
  173. &__icon {
  174. margin-right: 8rpx;
  175. }
  176. &__required {
  177. position: absolute;
  178. left: -9px;
  179. color: $u-error;
  180. line-height: 20px;
  181. font-size: 20px;
  182. top: 3px;
  183. }
  184. &__label {
  185. @include flex;
  186. align-items: center;
  187. flex: 1;
  188. color: $u-main-color;
  189. font-size: 15px;
  190. }
  191. }
  192. }
  193. &__right {
  194. flex: 1;
  195. &__content {
  196. @include flex;
  197. align-items: center;
  198. flex: 1;
  199. &__slot {
  200. flex: 1;
  201. /* #ifndef MP */
  202. @include flex;
  203. align-items: center;
  204. /* #endif */
  205. }
  206. &__icon {
  207. margin-left: 10rpx;
  208. color: $u-light-color;
  209. font-size: 30rpx;
  210. }
  211. }
  212. &__message {
  213. font-size: 12px;
  214. line-height: 12px;
  215. color: $u-error;
  216. }
  217. }
  218. }
  219. }
  220. </style>