u-form-item.vue 5.8 KB

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