You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
6.1 KiB

5 months ago
  1. <template>
  2. <view class="u-form-item">
  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 {String} rule 绑定的规则
  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. }
  120. },
  121. // 组件创建完成时,将当前实例保存到u-form中
  122. computed: {
  123. propsLine() {
  124. return defProps.line
  125. }
  126. },
  127. mounted() {
  128. this.init()
  129. },
  130. emits: ["click"],
  131. methods: {
  132. addStyle,
  133. addUnit,
  134. init() {
  135. // 父组件的实例
  136. this.updateParentData()
  137. if (!this.parent) {
  138. error('u-form-item需要结合u-form组件使用')
  139. }
  140. },
  141. // 获取父组件的参数
  142. updateParentData() {
  143. // 此方法写在mixin中
  144. this.getParentData('u-form');
  145. },
  146. // 移除u-form-item的校验结果
  147. clearValidate() {
  148. this.message = null
  149. },
  150. // 清空当前的组件的校验结果,并重置为初始值
  151. resetField() {
  152. // 找到原始值
  153. const value = getProperty(this.parent.originalModel, this.prop)
  154. // 将u-form的model的prop属性链还原原始值
  155. setProperty(this.parent.model, this.prop, value)
  156. // 移除校验结果
  157. this.message = null
  158. },
  159. // 点击组件
  160. clickHandler() {
  161. this.$emit('click')
  162. }
  163. },
  164. }
  165. </script>
  166. <style lang="scss" scoped>
  167. @import "../../libs/css/components.scss";
  168. .u-form-item {
  169. @include flex(column);
  170. font-size: 14px;
  171. color: $u-main-color;
  172. &__body {
  173. @include flex;
  174. padding: 10px 0;
  175. &__left {
  176. @include flex;
  177. align-items: center;
  178. &__content {
  179. position: relative;
  180. @include flex;
  181. align-items: center;
  182. padding-right: 10rpx;
  183. flex: 1;
  184. &__icon {
  185. margin-right: 8rpx;
  186. }
  187. &__required {
  188. position: absolute;
  189. left: -9px;
  190. color: $u-error;
  191. line-height: 20px;
  192. font-size: 20px;
  193. top: 3px;
  194. }
  195. &__label {
  196. @include flex;
  197. align-items: center;
  198. flex: 1;
  199. color: $u-main-color;
  200. font-size: 15px;
  201. }
  202. }
  203. }
  204. &__right {
  205. flex: 1;
  206. &__content {
  207. @include flex;
  208. align-items: center;
  209. flex: 1;
  210. &__slot {
  211. flex: 1;
  212. /* #ifndef MP */
  213. @include flex;
  214. align-items: center;
  215. /* #endif */
  216. }
  217. &__icon {
  218. margin-left: 10rpx;
  219. color: $u-light-color;
  220. font-size: 30rpx;
  221. }
  222. }
  223. &__message {
  224. font-size: 12px;
  225. line-height: 12px;
  226. color: $u-error;
  227. }
  228. }
  229. }
  230. }
  231. </style>