uv-form-item.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. >*
  27. </text>
  28. <view
  29. class="uv-form-item__body__left__content__icon"
  30. v-if="leftIcon"
  31. >
  32. <uv-icon
  33. :name="leftIcon"
  34. :custom-style="leftIconStyle"
  35. ></uv-icon>
  36. </view>
  37. <text
  38. class="uv-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 }}
  43. </text>
  44. </view>
  45. </view>
  46. </slot>
  47. <view class="uv-form-item__body__right">
  48. <view class="uv-form-item__body__right__content">
  49. <view class="uv-form-item__body__right__content__slot">
  50. <slot/>
  51. </view>
  52. <view class="item__body__right__content__icon">
  53. <slot name="right"/>
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. <slot name="error">
  59. <uv-transition
  60. :show="true"
  61. :duration="100"
  62. mode="fade"
  63. v-if="!!message && parentData.errorType === 'message'"
  64. >
  65. <text
  66. class="uv-form-item__body__right__message"
  67. :style="{
  68. marginLeft: $uv.addUnit(parentData.labelPosition === 'top' ? 0 : (labelWidth || parentData.labelWidth))
  69. }"
  70. >{{ message }}
  71. </text>
  72. </uv-transition>
  73. </slot>
  74. <uv-line
  75. v-if="borderBottom"
  76. :color="message && parentData.errorType === 'border-bottom' ? '#f56c6c' : '#d6d7d9'"
  77. ></uv-line>
  78. </view>
  79. </template>
  80. <script>
  81. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  82. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  83. import props from './props.js';
  84. /**
  85. * Form 表单
  86. * @description 此组件一般用于表单场景,可以配置Input输入框,Select弹出框,进行表单验证等。
  87. * @tutorial https://www.uvui.cn/components/form.html
  88. * @property {String} label input的label提示语
  89. * @property {String} prop 绑定的值
  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 <uv-form-item label="姓名" prop="userInfo.name" borderBottom ref="item1"></uv-form-item>
  98. */
  99. export default {
  100. name: 'uv-form-item',
  101. emits: ['click'],
  102. mixins: [mpMixin, mixin, props],
  103. data() {
  104. return {
  105. // 错误提示语
  106. message: '',
  107. parentData: {
  108. // 提示文本的位置
  109. labelPosition: 'left',
  110. // 提示文本对齐方式
  111. labelAlign: 'left',
  112. // 提示文本的样式
  113. labelStyle: {},
  114. // 提示文本的宽度
  115. labelWidth: 45,
  116. // 错误提示方式
  117. errorType: 'message'
  118. }
  119. }
  120. },
  121. created() {
  122. this.init()
  123. },
  124. methods: {
  125. init() {
  126. // 父组件的实例
  127. this.updateParentData()
  128. if (!this.parent) {
  129. this.$uv.error('uv-form-item需要结合uv-form组件使用')
  130. }
  131. },
  132. // 获取父组件的参数
  133. updateParentData() {
  134. // 此方法写在mixin中
  135. this.getParentData('uv-form');
  136. },
  137. // 移除uv-form-item的校验结果
  138. clearValidate() {
  139. this.message = null
  140. },
  141. // 清空当前的组件的校验结果,并重置为初始值
  142. resetField() {
  143. // 找到原始值
  144. const value = this.$uv.getProperty(this.parent.originalModel, this.prop)
  145. // 将uv-form的model的prop属性链还原原始值
  146. this.$uv.setProperty(this.parent.model, this.prop, value)
  147. // 移除校验结果
  148. this.message = null
  149. },
  150. // 点击组件
  151. clickHandler() {
  152. this.$emit('click')
  153. }
  154. }
  155. }
  156. </script>
  157. <style lang="scss" scoped>
  158. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  159. @import '@/uni_modules/uv-ui-tools/libs/css/color.scss';
  160. .uv-form-item {
  161. @include flex(column);
  162. font-size: 14px;
  163. color: $uv-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: $uv-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: $uv-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: $uv-light-color;
  212. font-size: 30rpx;
  213. }
  214. }
  215. &__message__box {
  216. height: 16px;
  217. line-height: 16px;
  218. }
  219. &__message {
  220. margin-top: -6px;
  221. line-height: 24px;
  222. font-size: 12px;
  223. color: $uv-error;
  224. }
  225. }
  226. }
  227. }
  228. </style>