u-form-item.vue 5.9 KB

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