租房小程序前端代码
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.

227 lines
6.0 KiB

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