uview-pro 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/changelog.md +6 -0
  2. package/components/u-action-sheet/u-action-sheet.vue +205 -0
  3. package/components/u-alert-tips/u-alert-tips.vue +241 -0
  4. package/components/u-avatar/u-avatar.vue +220 -0
  5. package/components/u-avatar-cropper/u-avatar-cropper.vue +329 -0
  6. package/components/u-avatar-cropper/weCropper.d.ts +54 -0
  7. package/components/u-avatar-cropper/weCropper.js +1267 -0
  8. package/components/u-avatar-cropper/weCropper.ts +1254 -0
  9. package/components/u-back-top/u-back-top.vue +156 -0
  10. package/components/u-badge/u-badge.vue +189 -0
  11. package/components/u-button/u-button.vue +562 -0
  12. package/components/u-calendar/u-calendar.vue +725 -0
  13. package/components/u-car-keyboard/u-car-keyboard.vue +236 -0
  14. package/components/u-card/u-card.vue +240 -0
  15. package/components/u-cell-group/u-cell-group.vue +56 -0
  16. package/components/u-cell-item/u-cell-item.vue +245 -0
  17. package/components/u-checkbox/u-checkbox.vue +310 -0
  18. package/components/u-checkbox-group/u-checkbox-group.vue +134 -0
  19. package/components/u-circle-progress/u-circle-progress.vue +210 -0
  20. package/components/u-col/u-col.vue +135 -0
  21. package/components/u-collapse/u-collapse.vue +82 -0
  22. package/components/u-collapse-item/u-collapse-item.vue +190 -0
  23. package/components/u-column-notice/u-column-notice.vue +264 -0
  24. package/components/u-count-down/u-count-down.vue +333 -0
  25. package/components/u-count-to/u-count-to.vue +297 -0
  26. package/components/u-divider/u-divider.vue +141 -0
  27. package/components/u-dropdown/u-dropdown.vue +311 -0
  28. package/components/u-dropdown-item/u-dropdown-item.vue +135 -0
  29. package/components/u-empty/u-empty.vue +111 -0
  30. package/components/u-field/u-field.vue +469 -0
  31. package/components/u-form/u-form.vue +162 -0
  32. package/components/u-form-item/u-form-item.vue +476 -0
  33. package/components/u-full-screen/u-full-screen.vue +80 -0
  34. package/components/u-gap/u-gap.vue +48 -0
  35. package/components/u-grid/u-grid.vue +101 -0
  36. package/components/u-grid-item/u-grid-item.vue +136 -0
  37. package/components/u-icon/u-icon.vue +389 -0
  38. package/components/u-image/types.ts +48 -0
  39. package/components/u-image/u-image.vue +218 -0
  40. package/components/u-index-anchor/u-index-anchor.vue +101 -0
  41. package/components/u-index-list/u-index-list.vue +376 -0
  42. package/components/u-input/u-input.vue +462 -0
  43. package/components/u-keyboard/u-keyboard.vue +188 -0
  44. package/components/u-lazy-load/u-lazy-load.vue +288 -0
  45. package/components/u-line/u-line.vue +71 -0
  46. package/components/u-line-progress/u-line-progress.vue +128 -0
  47. package/components/u-link/u-link.vue +87 -0
  48. package/components/u-loading/u-loading.vue +111 -0
  49. package/components/u-loadmore/u-loadmore.vue +205 -0
  50. package/components/u-mask/u-mask.vue +137 -0
  51. package/components/u-message-input/u-message-input.vue +315 -0
  52. package/components/u-modal/u-modal.vue +284 -0
  53. package/components/u-navbar/u-navbar.vue +314 -0
  54. package/components/u-no-network/image.ts +2 -0
  55. package/components/u-no-network/u-no-network.vue +311 -0
  56. package/components/u-notice-bar/u-notice-bar.vue +274 -0
  57. package/components/u-number-box/u-number-box.vue +344 -0
  58. package/components/u-number-keyboard/u-number-keyboard.vue +170 -0
  59. package/components/u-parse/libs/CssHandler.js +100 -0
  60. package/components/u-parse/libs/MpHtmlParser.js +580 -0
  61. package/components/u-parse/libs/config.js +80 -0
  62. package/components/u-parse/libs/handler.wxs +22 -0
  63. package/components/u-parse/libs/trees.vue +505 -0
  64. package/components/u-parse/u-parse.vue +645 -0
  65. package/components/u-picker/u-picker.vue +808 -0
  66. package/components/u-popup/u-popup.vue +404 -0
  67. package/components/u-radio/u-radio.vue +272 -0
  68. package/components/u-radio-group/u-radio-group.vue +116 -0
  69. package/components/u-rate/u-rate.vue +349 -0
  70. package/components/u-read-more/u-read-more.vue +199 -0
  71. package/components/u-row/u-row.vue +95 -0
  72. package/components/u-row-notice/u-row-notice.vue +273 -0
  73. package/components/u-search/u-search.vue +298 -0
  74. package/components/u-section/u-section.vue +175 -0
  75. package/components/u-select/u-select.vue +387 -0
  76. package/components/u-skeleton/u-skeleton.vue +230 -0
  77. package/components/u-slider/u-slider.vue +293 -0
  78. package/components/u-steps/u-steps.vue +200 -0
  79. package/components/u-sticky/u-sticky.vue +189 -0
  80. package/components/u-subsection/u-subsection.vue +388 -0
  81. package/components/u-swipe-action/u-swipe-action.vue +289 -0
  82. package/components/u-swiper/u-swiper.vue +305 -0
  83. package/components/u-switch/u-switch.vue +146 -0
  84. package/components/u-tabbar/u-tabbar.vue +347 -0
  85. package/components/u-table/u-table.vue +104 -0
  86. package/components/u-tabs/u-tabs.vue +322 -0
  87. package/components/u-tabs-swiper/u-tabs-swiper.vue +426 -0
  88. package/components/u-tag/u-tag.vue +270 -0
  89. package/components/u-td/u-td.vue +76 -0
  90. package/components/u-th/u-th.vue +70 -0
  91. package/components/u-time-line/u-time-line.vue +39 -0
  92. package/components/u-time-line-item/u-time-line-item.vue +88 -0
  93. package/components/u-toast/types.ts +4 -0
  94. package/components/u-toast/u-toast.vue +238 -0
  95. package/components/u-top-tips/u-top-tips.vue +118 -0
  96. package/components/u-tr/u-tr.vue +24 -0
  97. package/components/u-upload/u-upload.vue +600 -0
  98. package/components/u-verification-code/u-verification-code.vue +194 -0
  99. package/components/u-waterfall/u-waterfall.vue +186 -0
  100. package/iconfont.css +910 -0
  101. package/index.scss +23 -0
  102. package/index.ts +166 -0
  103. package/libs/config/config.ts +26 -0
  104. package/libs/config/zIndex.ts +37 -0
  105. package/libs/css/color.scss +155 -0
  106. package/libs/css/common.scss +176 -0
  107. package/libs/css/style.components.scss +7 -0
  108. package/libs/css/style.h5.scss +8 -0
  109. package/libs/css/style.mp.scss +72 -0
  110. package/libs/css/style.nvue.scss +3 -0
  111. package/libs/css/style.vue.scss +175 -0
  112. package/libs/function/$parent.ts +22 -0
  113. package/libs/function/addUnit.ts +13 -0
  114. package/libs/function/color.ts +37 -0
  115. package/libs/function/colorGradient.ts +123 -0
  116. package/libs/function/debounce.ts +28 -0
  117. package/libs/function/deepClone.ts +39 -0
  118. package/libs/function/deepMerge.ts +34 -0
  119. package/libs/function/getParent.ts +59 -0
  120. package/libs/function/getRect.ts +26 -0
  121. package/libs/function/guid.ts +42 -0
  122. package/libs/function/md5.ts +397 -0
  123. package/libs/function/parent.ts +21 -0
  124. package/libs/function/queryParams.ts +60 -0
  125. package/libs/function/random.ts +16 -0
  126. package/libs/function/randomArray.ts +11 -0
  127. package/libs/function/route.ts +118 -0
  128. package/libs/function/sys.ts +15 -0
  129. package/libs/function/test.ts +229 -0
  130. package/libs/function/throttle.ts +31 -0
  131. package/libs/function/timeFormat.ts +54 -0
  132. package/libs/function/timeFrom.ts +48 -0
  133. package/libs/function/toast.ts +14 -0
  134. package/libs/function/trim.ts +21 -0
  135. package/libs/function/type2icon.ts +36 -0
  136. package/libs/hooks/useEmitter.ts +77 -0
  137. package/libs/hooks/useParent.ts +29 -0
  138. package/libs/request/index.ts +237 -0
  139. package/libs/store/index.ts +88 -0
  140. package/libs/util/area.ts +1 -0
  141. package/libs/util/async-validator.js +1356 -0
  142. package/libs/util/city.ts +1 -0
  143. package/libs/util/emitter.ts +112 -0
  144. package/libs/util/mitt.ts +118 -0
  145. package/libs/util/parent.ts +20 -0
  146. package/libs/util/province.ts +1 -0
  147. package/package.json +98 -0
  148. package/readme.md +165 -0
  149. package/theme.scss +38 -0
@@ -0,0 +1,469 @@
1
+ <template>
2
+ <view class="u-field" :class="{ 'u-border-top': props.borderTop, 'u-border-bottom': props.borderBottom }">
3
+ <view class="u-field-inner" :class="[props.type === 'textarea' ? 'u-textarea-inner' : '', 'u-label-postion-' + props.labelPosition]">
4
+ <view
5
+ class="u-label"
6
+ :class="[props.required ? 'u-required' : '']"
7
+ :style="{
8
+ justifyContent: justifyContent,
9
+ flex: props.labelPosition === 'left' ? `0 0 ${props.labelWidth}rpx` : '1'
10
+ }"
11
+ >
12
+ <view class="u-icon-wrap" v-if="props.icon">
13
+ <u-icon size="32" :custom-style="props.iconStyle" :name="props.icon" :color="props.iconColor" class="u-icon"></u-icon>
14
+ </view>
15
+ <slot name="icon"></slot>
16
+ <text class="u-label-text" :class="[$slots.icon || props.icon ? 'u-label-left-gap' : '']">{{ props.label }}</text>
17
+ </view>
18
+ <view class="fild-body">
19
+ <view class="u-flex-1 u-flex" :style="[inputWrapStyle]">
20
+ <textarea
21
+ v-if="props.type === 'textarea'"
22
+ class="u-flex-1 u-textarea-class"
23
+ :style="[props.fieldStyle]"
24
+ :value="props.modelValue"
25
+ :placeholder="props.placeholder"
26
+ :placeholderStyle="props.placeholderStyle"
27
+ :disabled="props.disabled"
28
+ :maxlength="inputMaxlength"
29
+ :focus="props.focus"
30
+ :autoHeight="props.autoHeight"
31
+ :fixed="props.fixed"
32
+ @input="onInput"
33
+ @blur="onBlur"
34
+ @focus="onFocus"
35
+ @confirm="onConfirm"
36
+ @tap="fieldClick"
37
+ />
38
+ <input
39
+ v-else
40
+ :style="[props.fieldStyle]"
41
+ :type="props.type"
42
+ class="u-flex-1 u-field__input-wrap"
43
+ :value="props.modelValue"
44
+ :password="props.password || props.type === 'password'"
45
+ :placeholder="props.placeholder"
46
+ :placeholderStyle="props.placeholderStyle"
47
+ :disabled="props.disabled"
48
+ :maxlength="inputMaxlength"
49
+ :focus="props.focus"
50
+ :confirmType="props.confirmType"
51
+ @focus="onFocus"
52
+ @blur="onBlur"
53
+ @input="onInput"
54
+ @confirm="onConfirm"
55
+ @tap="fieldClick"
56
+ />
57
+ </view>
58
+ <u-icon
59
+ :size="props.clearSize"
60
+ v-if="props.clearable && props.modelValue != '' && focused"
61
+ name="close-circle-fill"
62
+ color="#c0c4cc"
63
+ class="u-clear-icon"
64
+ @click="onClear"
65
+ />
66
+ <view class="u-button-wrap"><slot name="right" /></view>
67
+ <u-icon v-if="props.rightIcon" @click="rightIconClick" :name="props.rightIcon" color="#c0c4cc" :style="[rightIconStyle]" size="26" class="u-arror-right" />
68
+ </view>
69
+ </view>
70
+ <view
71
+ v-if="props.errorMessage !== false && props.errorMessage != ''"
72
+ class="u-error-message"
73
+ :style="{
74
+ paddingLeft: props.labelWidth + 'rpx'
75
+ }"
76
+ >{{ props.errorMessage }}</view
77
+ >
78
+ </view>
79
+ </template>
80
+
81
+ <script setup lang="ts">
82
+ import { ref, computed } from 'vue';
83
+ import { $u } from '../..';
84
+
85
+ defineOptions({ name: 'u-field' });
86
+
87
+ /**
88
+ * field 输入框
89
+ * @description 借助此组件,可以实现表单的输入, 有"text"和"textarea"类型的,此外,借助uView的picker和actionSheet组件可以快速实现上拉菜单,时间,地区选择等, 为表单解决方案的利器。
90
+ * @tutorial https://www.uviewui.com/components/field.html
91
+ * @property {String} type 输入框的类型(默认text)
92
+ * @property {String} icon label左边的图标,限uView的图标名称
93
+ * @property {Object} icon-style 左边图标的样式,对象形式
94
+ * @property {Boolean} right-icon 输入框右边的图标名称,限uView的图标名称(默认false)
95
+ * @property {Boolean} required 是否必填,左侧显示红色"*"号(默认false)
96
+ * @property {String} label 输入框左边的文字提示
97
+ * @property {Boolean} password 是否密码输入方式(用点替换文字),type为text时有效(默认false)
98
+ * @property {Boolean} clearable 是否显示右侧清空内容的图标控件(输入框有内容,且获得焦点时才显示),点击可清空输入框内容(默认true)
99
+ * @property {Number | String} label-width label的宽度,单位rpx(默认130)
100
+ * @property {String} label-align label的文字对齐方式(默认left)
101
+ * @property {Object} field-style 自定义输入框的样式,对象形式
102
+ * @property {Number | String} clear-size 清除图标的大小,单位rpx(默认30)
103
+ * @property {String} input-align 输入框内容对齐方式(默认left)
104
+ * @property {Boolean} border-bottom 是否显示field的下边框(默认true)
105
+ * @property {Boolean} border-top 是否显示field的上边框(默认false)
106
+ * @property {String} icon-color 左边通过icon配置的图标的颜色(默认#606266)
107
+ * @property {Boolean} auto-height 是否自动增高输入区域,type为textarea时有效(默认true)
108
+ * @property {String Boolean} error-message 显示的错误提示内容,如果为空字符串或者false,则不显示错误信息
109
+ * @property {String} placeholder 输入框的提示文字
110
+ * @property {String} placeholder-style placeholder的样式(内联样式,字符串),如"color: #ddd"
111
+ * @property {Boolean} focus 是否自动获得焦点(默认false)
112
+ * @property {Boolean} fixed 如果type为textarea,且在一个"position:fixed"的区域,需要指明为true(默认false)
113
+ * @property {Boolean} disabled 是否不可输入(默认false)
114
+ * @property {Number String} maxlength 最大输入长度,设置为 -1 的时候不限制最大长度(默认140)
115
+ * @property {String} confirm-type 设置键盘右下角按钮的文字,仅在type="text"时生效(默认done)
116
+ * @event {Function} input 输入框内容发生变化时触发
117
+ * @event {Function} focus 输入框获得焦点时触发
118
+ * @event {Function} blur 输入框失去焦点时触发
119
+ * @event {Function} confirm 点击完成按钮时触发
120
+ * @event {Function} right-icon-click 通过right-icon生成的图标被点击时触发
121
+ * @event {Function} click 输入框被点击或者通过right-icon生成的图标被点击时触发,这样设计是考虑到传递右边的图标,一般都为需要弹出"picker"等操作时的场景,点击倒三角图标,理应发出此事件,见上方说明
122
+ * @example <u-field v-model="mobile" label="手机号" required :error-message="errorMessage"></u-field>
123
+ */
124
+
125
+ const emit = defineEmits(['update:modelValue', 'focus', 'blur', 'confirm', 'right-icon-click', 'click']);
126
+
127
+ /**
128
+ * field 输入框
129
+ * @description 借助此组件,可以实现表单的输入, 有"text"和"textarea"类型的,此外,借助uView的picker和actionSheet组件可以快速实现上拉菜单,时间,地区选择等, 为表单解决方案的利器。
130
+ * @property {string} icon label左边的图标,限uView的图标名称
131
+ * @property {string} rightIcon 输入框右边的图标名称,限uView的图标名称(默认false)
132
+ * @property {boolean} required 是否必填,左边显示红色"*"号(默认false)
133
+ * @property {string} label 输入框左边的文字提示
134
+ * @property {boolean} password 是否密码输入方式(用点替换文字),type为text时有效(默认false)
135
+ * @property {boolean} clearable 是否显示右侧清空内容的图标控件(默认true)
136
+ * @property {number|string} labelWidth label的宽度,单位rpx(默认130)
137
+ * @property {string} labelAlign label的文字对齐方式(默认left)
138
+ * @property {string} inputAlign 输入框内容对齐方式(默认left)
139
+ * @property {string} iconColor 左边通过icon配置的图标的颜色(默认#606266)
140
+ * @property {boolean} autoHeight 是否自动增高输入区域,type为textarea时有效(默认true)
141
+ * @property {string|boolean} errorMessage 显示的错误提示内容,如果为空字符串或者false,则不显示错误信息
142
+ * @property {string} placeholder 输入框的提示文字
143
+ * @property {string} placeholderStyle placeholder的样式(内联样式,字符串),如"color: #ddd"
144
+ * @property {boolean} focus 是否自动获得焦点(默认false)
145
+ * @property {boolean} fixed 如果type为textarea,且在一个"position:fixed"的区域,需要指明为true(默认false)
146
+ * @property {boolean} disabled 是否不可输入(默认false)
147
+ * @property {number|string} maxlength 最大输入长度,设置为 -1 的时候不限制最大长度(默认140)
148
+ * @property {string} confirmType 设置键盘右下角按钮的文字,仅在type="text"时生效(默认done)
149
+ * @property {string} labelPosition label位置(默认left)
150
+ * @property {Record<string, any>} fieldStyle 自定义输入框的样式,对象形式
151
+ * @property {number|string} clearSize 清除图标的大小,单位rpx(默认30)
152
+ * @property {Record<string, any>} iconStyle 左侧图标样式
153
+ * @property {boolean} borderTop 是否显示field的上边框(默认false)
154
+ * @property {boolean} borderBottom 是否显示field的下边框(默认true)
155
+ * @property {boolean} trim 是否自动去除输入内容首尾空格(默认true)
156
+ * @property {string|number} value 输入框绑定值
157
+ * @property {string} type 输入框类型(text/textarea/password等,默认text)
158
+ */
159
+ const props = defineProps({
160
+ /** label左边的图标,限uView的图标名称 */
161
+ icon: String,
162
+ /** 输入框右边的图标名称,限uView的图标名称(默认false) */
163
+ rightIcon: String,
164
+ /** 方向属性 */
165
+ arrowDirection: {
166
+ type: String,
167
+ default: 'right'
168
+ },
169
+ /** 是否必填,左边显示红色"*"号(默认false) */
170
+ required: Boolean,
171
+ /** 输入框左边的文字提示 */
172
+ label: String,
173
+ /** 是否密码输入方式(用点替换文字),type为text时有效(默认false) */
174
+ password: Boolean,
175
+ /** 是否显示右侧清空内容的图标控件(默认true) */
176
+ clearable: {
177
+ type: Boolean,
178
+ default: true
179
+ },
180
+ /** label的宽度,单位rpx(默认130) */
181
+ labelWidth: {
182
+ type: [Number, String],
183
+ default: 130
184
+ },
185
+ /** label的文字对齐方式(默认left) */
186
+ labelAlign: {
187
+ type: String,
188
+ default: 'left'
189
+ },
190
+ /** 输入框内容对齐方式(默认left) */
191
+ inputAlign: {
192
+ type: String,
193
+ default: 'left'
194
+ },
195
+ /** 左边通过icon配置的图标的颜色(默认#606266) */
196
+ iconColor: {
197
+ type: String,
198
+ default: '#606266'
199
+ },
200
+ /** 是否自动增高输入区域,type为textarea时有效(默认true) */
201
+ autoHeight: {
202
+ type: Boolean,
203
+ default: true
204
+ },
205
+ /** 显示的错误提示内容,如果为空字符串或者false,则不显示错误信息 */
206
+ errorMessage: {
207
+ type: [String, Boolean],
208
+ default: ''
209
+ },
210
+ /** 输入框的提示文字 */
211
+ placeholder: String,
212
+ /** placeholder的样式(内联样式,字符串),如"color: #ddd" */
213
+ placeholderStyle: String,
214
+ /** 是否自动获得焦点(默认false) */
215
+ focus: Boolean,
216
+ /** 如果type为textarea,且在一个"position:fixed"的区域,需要指明为true(默认false) */
217
+ fixed: Boolean,
218
+ /** 输入框绑定值 */
219
+ modelValue: [Number, String],
220
+ /** 输入框类型(text/textarea/password等,默认text) */
221
+ type: {
222
+ type: String,
223
+ default: 'text'
224
+ },
225
+ /** 是否不可输入(默认false) */
226
+ disabled: {
227
+ type: Boolean,
228
+ default: false
229
+ },
230
+ /** 最大输入长度,设置为 -1 的时候不限制最大长度(默认140) */
231
+ maxlength: {
232
+ type: [Number, String],
233
+ default: 140
234
+ },
235
+ /** 设置键盘右下角按钮的文字,仅在type="text"时生效(默认done) */
236
+ confirmType: {
237
+ type: String,
238
+ default: 'done'
239
+ },
240
+ /** label位置(默认left)left-左边,top-上边 */
241
+ labelPosition: {
242
+ type: String,
243
+ default: 'left'
244
+ },
245
+ /** 自定义输入框的样式,对象形式 */
246
+ fieldStyle: {
247
+ type: Object as () => Record<string, any>,
248
+ default: () => ({})
249
+ },
250
+ /** 清除图标的大小,单位rpx(默认30) */
251
+ clearSize: {
252
+ type: [Number, String],
253
+ default: 30
254
+ },
255
+ /** 左侧图标样式 */
256
+ iconStyle: {
257
+ type: Object as () => Record<string, any>,
258
+ default: () => ({})
259
+ },
260
+ /** 是否显示field的上边框(默认false) */
261
+ borderTop: {
262
+ type: Boolean,
263
+ default: false
264
+ },
265
+ /** 是否显示field的下边框(默认true) */
266
+ borderBottom: {
267
+ type: Boolean,
268
+ default: true
269
+ },
270
+ /** 是否自动去除输入内容首尾空格(默认true) */
271
+ trim: {
272
+ type: Boolean,
273
+ default: true
274
+ }
275
+ });
276
+
277
+ const focused = ref(false);
278
+ const itemIndex = ref(0);
279
+
280
+ const inputWrapStyle = computed(() => {
281
+ const style: Record<string, string> = {};
282
+ style.textAlign = props.inputAlign;
283
+ // 判断lable的位置,如果是left的话,让input左边两边有间隙
284
+ if (props.labelPosition === 'left') {
285
+ style.margin = '0 8rpx';
286
+ } else {
287
+ // 如果lable是top的,input的左边就没必要有间隙了
288
+ style.marginRight = '8rpx';
289
+ }
290
+ return style;
291
+ });
292
+
293
+ const rightIconStyle = computed(() => {
294
+ const style: Record<string, string> = {};
295
+ // 方向属性已注释,保留原有逻辑
296
+ if (props.arrowDirection === 'top') style.transform = 'rotate(-90deg)';
297
+ if (props.arrowDirection === 'bottom') style.transform = 'rotate(90deg)';
298
+ else style.transform = 'rotate(0deg)';
299
+ return style;
300
+ });
301
+
302
+ const labelStyle = computed(() => {
303
+ const style: Record<string, string> = {};
304
+ if (props.labelAlign === 'left') style.justifyContent = 'flex-start';
305
+ if (props.labelAlign === 'center') style.justifyContent = 'center';
306
+ if (props.labelAlign === 'right') style.justifyContent = 'flex-end';
307
+ return style;
308
+ });
309
+
310
+ // uni不支持在computed中写style.justifyContent = 'center'的形式,故用此方法
311
+ const justifyContent = computed(() => {
312
+ if (props.labelAlign === 'left') return 'flex-start';
313
+ if (props.labelAlign === 'center') return 'center';
314
+ if (props.labelAlign === 'right') return 'flex-end';
315
+ return 'flex-start';
316
+ });
317
+
318
+ // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,给用户可以传入字符串数值
319
+ const inputMaxlength = computed(() => Number(props.maxlength));
320
+
321
+ // label的位置
322
+ const fieldInnerStyle = computed(() => {
323
+ const style: Record<string, string> = {};
324
+ style.flexDirection = props.labelPosition === 'left' ? 'row' : 'column';
325
+ return style;
326
+ });
327
+
328
+ function onInput(event: any) {
329
+ // 兼容 uni-app input/textarea 事件参数
330
+ let value = event?.detail?.value ?? '';
331
+ // 判断是否去除空格
332
+ if (props.trim) value = $u.trim(value);
333
+ emit('update:modelValue', value);
334
+ }
335
+
336
+ function onFocus(event: any) {
337
+ focused.value = true;
338
+ emit('focus', event);
339
+ }
340
+
341
+ function onBlur(event: any) {
342
+ // 最开始使用的是监听图标@touchstart事件,自从hx2.8.4后,此方法在微信小程序出错
343
+ // 这里改为监听点击事件,手点击清除图标时,同时也发生了@blur事件,导致图标消失而无法点击,这里做一个延时
344
+ setTimeout(() => {
345
+ focused.value = false;
346
+ }, 100);
347
+ emit('blur', event);
348
+ }
349
+
350
+ function onConfirm(e: any) {
351
+ emit('confirm', e?.detail?.value ?? '');
352
+ }
353
+
354
+ function onClear() {
355
+ emit('update:modelValue', '');
356
+ }
357
+
358
+ function rightIconClick() {
359
+ emit('right-icon-click');
360
+ emit('click');
361
+ }
362
+
363
+ function fieldClick() {
364
+ emit('click');
365
+ }
366
+ </script>
367
+
368
+ <style lang="scss" scoped>
369
+ @import '../../libs/css/style.components.scss';
370
+
371
+ .u-field {
372
+ font-size: 28rpx;
373
+ padding: 20rpx 28rpx;
374
+ text-align: left;
375
+ position: relative;
376
+ color: $u-main-color;
377
+ }
378
+
379
+ .u-field-inner {
380
+ @include vue-flex;
381
+ align-items: center;
382
+ }
383
+
384
+ .u-textarea-inner {
385
+ align-items: flex-start;
386
+ }
387
+
388
+ .u-textarea-class {
389
+ min-height: 96rpx;
390
+ width: auto;
391
+ font-size: 28rpx;
392
+ }
393
+
394
+ .fild-body {
395
+ @include vue-flex;
396
+ flex: 1;
397
+ align-items: center;
398
+ }
399
+
400
+ .u-arror-right {
401
+ margin-left: 8rpx;
402
+ }
403
+
404
+ .u-label-text {
405
+ /* #ifndef APP-NVUE */
406
+ display: inline-flex;
407
+ /* #endif */
408
+ }
409
+
410
+ .u-label-left-gap {
411
+ margin-left: 6rpx;
412
+ }
413
+
414
+ .u-label-postion-top {
415
+ flex-direction: column;
416
+ align-items: flex-start;
417
+ }
418
+
419
+ .u-label {
420
+ width: 130rpx;
421
+ flex: 1 1 130rpx;
422
+ text-align: left;
423
+ position: relative;
424
+ @include vue-flex;
425
+ align-items: center;
426
+ }
427
+
428
+ .u-required::before {
429
+ content: '*';
430
+ position: absolute;
431
+ left: -16rpx;
432
+ font-size: 14px;
433
+ color: $u-type-error;
434
+ height: 9px;
435
+ line-height: 1;
436
+ }
437
+
438
+ .u-field__input-wrap {
439
+ position: relative;
440
+ overflow: hidden;
441
+ font-size: 28rpx;
442
+ height: 48rpx;
443
+ flex: 1;
444
+ width: auto;
445
+ }
446
+
447
+ .u-clear-icon {
448
+ @include vue-flex;
449
+ align-items: center;
450
+ }
451
+
452
+ .u-error-message {
453
+ color: $u-type-error;
454
+ font-size: 26rpx;
455
+ text-align: left;
456
+ }
457
+
458
+ .placeholder-style {
459
+ color: rgb(150, 151, 153);
460
+ }
461
+
462
+ .u-input-class {
463
+ font-size: 28rpx;
464
+ }
465
+
466
+ .u-button-wrap {
467
+ margin-left: 8rpx;
468
+ }
469
+ </style>
@@ -0,0 +1,162 @@
1
+ <template>
2
+ <view class="u-form"><slot /></view>
3
+ </template>
4
+
5
+ <script setup lang="ts">
6
+ import { ref, provide } from 'vue';
7
+ import { $u } from '../..';
8
+
9
+ defineOptions({
10
+ name: 'u-form'
11
+ });
12
+
13
+ /**
14
+ * form 表单
15
+ * @description 此组件一般用于表单场景,可以配置Input输入框,Select弹出框,进行表单验证等。
16
+ * @tutorial http://uviewui.com/components/form.html
17
+ * @property {Object} model 表单数据对象
18
+ * @property {Boolean} border-bottom 是否显示表单域的下划线边框
19
+ * @property {String} label-position 表单域提示文字的位置,left-左侧,top-上方
20
+ * @property {String Number} label-width 提示文字的宽度,单位rpx(默认90)
21
+ * @property {Object} label-style lable的样式,对象形式
22
+ * @property {String} label-align lable的对齐方式
23
+ * @property {Object} rules 通过ref设置,见官网说明
24
+ * @property {Array} error-type 错误的提示方式,数组形式,见上方说明(默认['message'])
25
+ * @example <u-form :model="form" ref="uForm"></u-form>
26
+ */
27
+
28
+ const props = defineProps({
29
+ /** 当前form的需要验证字段的集合 */
30
+ model: {
31
+ type: Object,
32
+ default: () => ({})
33
+ },
34
+ // 验证规则
35
+ // rules: {
36
+ // type: [Object, Function, Array],
37
+ // default() {
38
+ // return {};
39
+ // }
40
+ // },
41
+ /**
42
+ * 有错误时的提示方式,message-提示信息,border-如果input设置了边框,变成呈红色,border-bottom-下边框呈现红色,none-无提示
43
+ */
44
+ errorType: {
45
+ type: Array,
46
+ default: () => ['message', 'toast']
47
+ },
48
+ /** 是否显示表单域的下划线边框 */
49
+ borderBottom: {
50
+ type: Boolean,
51
+ default: true
52
+ },
53
+ /** label的位置,left-左边,top-上边 */
54
+ labelPosition: {
55
+ type: String,
56
+ default: 'left'
57
+ },
58
+ /** label的宽度,单位rpx */
59
+ labelWidth: {
60
+ type: [String, Number],
61
+ default: 90
62
+ },
63
+ /** lable字体的对齐方式 */
64
+ labelAlign: {
65
+ type: String,
66
+ default: 'left'
67
+ },
68
+ /** lable的样式,对象形式 */
69
+ labelStyle: {
70
+ type: Object,
71
+ default: () => ({})
72
+ }
73
+ });
74
+
75
+ // 存储当前form下的所有u-form-item的实例
76
+ const fields = ref<any[]>([]);
77
+
78
+ // 校验规则
79
+ const rules = ref<Record<string, any>>({});
80
+
81
+ // 提供 uForm 实例给子组件(注册方法供 u-form-item 调用)
82
+ const uForm = {
83
+ addField(field: any) {
84
+ if (!fields.value.includes(field)) fields.value.push(field);
85
+ },
86
+ removeField(field: any) {
87
+ fields.value = fields.value.filter(f => f !== field);
88
+ },
89
+ // 兼容老用法
90
+ setRules,
91
+ resetFields,
92
+ validate,
93
+ fields,
94
+ rules,
95
+ props,
96
+ model: props.model
97
+ };
98
+ provide('u-form', uForm);
99
+
100
+ /**
101
+ * 设置校验规则
102
+ * @param newRules 校验规则对象
103
+ */
104
+ function setRules(newRules: Record<string, any>) {
105
+ rules.value = newRules;
106
+ }
107
+
108
+ /**
109
+ * 清空所有u-form-item组件的内容,本质上是调用了u-form-item组件中的resetField()方法
110
+ */
111
+ function resetFields() {
112
+ fields.value.forEach((field: any) => {
113
+ field.resetField && field.resetField();
114
+ });
115
+ }
116
+
117
+ /**
118
+ * 校验全部数据
119
+ * @param callback 校验回调
120
+ * @returns Promise<boolean>
121
+ */
122
+ function validate(callback?: (valid: boolean) => void): Promise<boolean> {
123
+ return new Promise(resolve => {
124
+ // 对所有的u-form-item进行校验
125
+ let valid = true; // 默认通过
126
+ let count = 0; // 用于标记是否检查完毕
127
+ let errorArr: any[] = []; // 存放错误信息
128
+ if (fields.value.length === 0) {
129
+ resolve(true);
130
+ if (typeof callback === 'function') callback(true);
131
+ return;
132
+ }
133
+ // 调用每一个u-form-item实例的validation的校验方法
134
+ fields.value.forEach((field: any) => {
135
+ // 如果任意一个u-form-item校验不通过,就意味着整个表单不通过
136
+ field.validation &&
137
+ field.validation('', (error: any) => {
138
+ if (error) {
139
+ valid = false;
140
+ errorArr.push(error);
141
+ }
142
+ // 当历遍了所有的u-form-item时,调用promise的then方法
143
+ if (++count === fields.value.length) {
144
+ resolve(valid); // 进入promise的then方法
145
+ // 判断是否设置了toast的提示方式,只提示最前面的表单域的第一个错误信息
146
+ if (props.errorType.indexOf('none') === -1 && props.errorType.indexOf('toast') >= 0 && errorArr.length) {
147
+ $u.toast(errorArr[0]);
148
+ }
149
+ // 调用回调方法
150
+ if (typeof callback === 'function') callback(valid);
151
+ }
152
+ });
153
+ });
154
+ });
155
+ }
156
+
157
+ defineExpose({ setRules, resetFields, validate });
158
+ </script>
159
+
160
+ <style scoped lang="scss">
161
+ @import '../../libs/css/style.components.scss';
162
+ </style>