uv-form-item.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <view class="uv-form-item">
  3. <view
  4. class="uv-form-item__body"
  5. @tap="clickHandler"
  6. :style="[$uv.addStyle(customStyle), {
  7. flexDirection: (labelPosition || parentData.labelPosition) === 'left' ? 'row' : 'column'
  8. }]"
  9. >
  10. <!-- 微信小程序中,将一个参数设置空字符串,结果会变成字符串"true" -->
  11. <slot name="label">
  12. <view
  13. class="uv-form-item__body__left"
  14. v-if="required || leftIcon || label"
  15. :style="{
  16. width: $uv.addUnit(labelWidth || parentData.labelWidth),
  17. marginBottom: parentData.labelPosition === 'left' ? 0 : '5px',
  18. }"
  19. >
  20. <!-- 为了块对齐 -->
  21. <view class="uv-form-item__body__left__content">
  22. <!-- nvue不支持伪元素before -->
  23. <text
  24. v-if="required"
  25. class="uv-form-item__body__left__content__required"
  26. >*</text>
  27. <view
  28. class="uv-form-item__body__left__content__icon"
  29. v-if="leftIcon"
  30. >
  31. <uv-icon
  32. :name="leftIcon"
  33. :custom-style="leftIconStyle"
  34. ></uv-icon>
  35. </view>
  36. <text
  37. class="uv-form-item__body__left__content__label"
  38. :style="[parentData.labelStyle, {
  39. justifyContent: parentData.labelAlign === 'left' ? 'flex-start' : parentData.labelAlign === 'center' ? 'center' : 'flex-end'
  40. }]"
  41. >{{ label }}</text>
  42. </view>
  43. </view>
  44. </slot>
  45. <view class="uv-form-item__body__right">
  46. <view class="uv-form-item__body__right__content">
  47. <view class="uv-form-item__body__right__content__slot">
  48. <slot />
  49. </view>
  50. <view class="item__body__right__content__icon">
  51. <slot name="right" />
  52. </view>
  53. </view>
  54. </view>
  55. </view>
  56. <slot name="error">
  57. <uv-transition
  58. :show="true"
  59. :duration="100"
  60. mode="fade"
  61. v-if="!!message && parentData.errorType === 'message'"
  62. >
  63. <text
  64. class="uv-form-item__body__right__message"
  65. :style="{
  66. marginLeft: $uv.addUnit(parentData.labelPosition === 'top' ? 0 : (labelWidth || parentData.labelWidth))
  67. }"
  68. >{{ message }}</text>
  69. </uv-transition>
  70. </slot>
  71. <uv-line
  72. v-if="borderBottom"
  73. :color="message && parentData.errorType === 'border-bottom' ? '#f56c6c' : '#d6d7d9'"
  74. ></uv-line>
  75. </view>
  76. </template>
  77. <script>
  78. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  79. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  80. import props from './props.js';
  81. /**
  82. * Form 表单
  83. * @description 此组件一般用于表单场景,可以配置Input输入框,Select弹出框,进行表单验证等。
  84. * @tutorial https://www.uvui.cn/components/form.html
  85. * @property {String} label input的label提示语
  86. * @property {String} prop 绑定的值
  87. * @property {String | Boolean} borderBottom 是否显示表单域的下划线边框
  88. * @property {String | Number} labelWidth label的宽度,单位px
  89. * @property {String} rightIcon 右侧图标
  90. * @property {String} leftIcon 左侧图标
  91. * @property {String | Object} leftIconStyle 左侧图标的样式
  92. * @property {Boolean} required 是否显示左边的必填星号,只作显示用,具体校验必填的逻辑,请在rules中配置 (默认 false )
  93. *
  94. * @example <uv-form-item label="姓名" prop="userInfo.name" borderBottom ref="item1"></uv-form-item>
  95. */
  96. export default {
  97. name: 'uv-form-item',
  98. emits: ['click'],
  99. mixins: [mpMixin, mixin, props],
  100. data() {
  101. return {
  102. // 错误提示语
  103. message: '',
  104. parentData: {
  105. // 提示文本的位置
  106. labelPosition: 'left',
  107. // 提示文本对齐方式
  108. labelAlign: 'left',
  109. // 提示文本的样式
  110. labelStyle: {},
  111. // 提示文本的宽度
  112. labelWidth: 45,
  113. // 错误提示方式
  114. errorType: 'message'
  115. }
  116. }
  117. },
  118. created() {
  119. this.init()
  120. },
  121. methods: {
  122. init() {
  123. // 父组件的实例
  124. this.updateParentData()
  125. if (!this.parent) {
  126. this.$uv.error('uv-form-item需要结合uv-form组件使用')
  127. }
  128. },
  129. // 获取父组件的参数
  130. updateParentData() {
  131. // 此方法写在mixin中
  132. this.getParentData('uv-form');
  133. },
  134. // 移除uv-form-item的校验结果
  135. clearValidate() {
  136. this.message = null
  137. },
  138. // 清空当前的组件的校验结果,并重置为初始值
  139. resetField() {
  140. // 找到原始值
  141. const value = this.$uv.getProperty(this.parent.originalModel, this.prop)
  142. // 将uv-form的model的prop属性链还原原始值
  143. this.$uv.setProperty(this.parent.model, this.prop, value)
  144. // 移除校验结果
  145. this.message = null
  146. },
  147. // 点击组件
  148. clickHandler() {
  149. this.$emit('click')
  150. }
  151. }
  152. }
  153. </script>
  154. <style lang="scss" scoped>
  155. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  156. @import '@/uni_modules/uv-ui-tools/libs/css/color.scss';
  157. .uv-form-item {
  158. @include flex(column);
  159. font-size: 14px;
  160. color: $uv-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: $uv-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: $uv-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: $uv-light-color;
  209. font-size: 30rpx;
  210. }
  211. }
  212. &__message__box {
  213. height: 16px;
  214. line-height: 16px;
  215. }
  216. &__message {
  217. margin-top: -6px;
  218. line-height: 24px;
  219. font-size: 12px;
  220. color: $uv-error;
  221. }
  222. }
  223. }
  224. }
  225. </style>