hy-app 0.2.16 → 0.3.0
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.
- package/components/hy-action-sheet/hy-action-sheet.vue +5 -5
- package/components/hy-action-sheet/index.scss +6 -1
- package/components/hy-address-picker/hy-address-picker.vue +5 -5
- package/components/hy-avatar/hy-avatar.vue +5 -5
- package/components/hy-back-top/hy-back-top.vue +5 -5
- package/components/hy-badge/hy-badge.vue +5 -5
- package/components/hy-button/hy-button.vue +5 -5
- package/components/hy-calendar/hy-calendar.vue +22 -18
- package/components/hy-calendar/typing.d.ts +1 -1
- package/components/hy-card/hy-card.vue +5 -5
- package/components/hy-cell/hy-cell.vue +10 -11
- package/components/hy-check-button/hy-check-button.vue +7 -6
- package/components/hy-checkbox/hy-checkbox.vue +14 -14
- package/components/hy-code-input/hy-code-input.vue +8 -15
- package/components/hy-code-input/index.scss +1 -1
- package/components/hy-config-provider/hy-config-provider.vue +8 -6
- package/components/hy-count-down/hy-count-down.vue +7 -6
- package/components/hy-count-to/hy-count-to.vue +6 -5
- package/components/hy-datetime-picker/hy-datetime-picker.vue +7 -7
- package/components/hy-divider/hy-divider.vue +10 -7
- package/components/hy-dropdown/hy-dropdown.vue +6 -5
- package/components/hy-dropdown-item/hy-dropdown-item.vue +7 -5
- package/components/hy-empty/hy-empty.vue +6 -5
- package/components/hy-float-button/hy-float-button.vue +6 -6
- package/components/hy-folding-panel/hy-folding-panel.vue +6 -5
- package/components/hy-form/README.md +301 -0
- package/components/hy-form/hy-form.vue +252 -533
- package/components/hy-form/index.ts +5 -0
- package/components/hy-form/typing.d.ts +57 -77
- package/components/hy-form-group/hy-form-group.vue +533 -0
- package/components/hy-form-group/typing.d.ts +77 -0
- package/components/hy-form-item/hy-form-item.vue +199 -0
- package/components/hy-form-item/index.scss +41 -0
- package/components/hy-form-item/typing.d.ts +43 -0
- package/components/hy-grid/hy-grid.vue +11 -13
- package/components/hy-grid/typing.d.ts +1 -1
- package/components/hy-icon/hy-icon.vue +15 -15
- package/components/hy-image/hy-image.vue +15 -9
- package/components/hy-input/hy-input.vue +17 -11
- package/components/hy-line/hy-line.vue +8 -6
- package/components/hy-line-progress/hy-line-progress.vue +9 -16
- package/components/hy-list/hy-list.vue +6 -5
- package/components/hy-loading/hy-loading.vue +9 -10
- package/components/hy-login/TheUserLogin.vue +82 -90
- package/components/hy-menu/hy-menu.vue +11 -8
- package/components/hy-modal/hy-modal.vue +6 -6
- package/components/hy-modal/index.scss +1 -1
- package/components/hy-navbar/hy-navbar.vue +6 -6
- package/components/hy-notice-bar/hy-notice-bar.vue +14 -9
- package/components/hy-notify/hy-notify.vue +6 -6
- package/components/hy-number-step/hy-number-step.vue +8 -6
- package/components/hy-overlay/hy-overlay.vue +8 -9
- package/components/hy-pagination/hy-pagination.vue +11 -8
- package/components/hy-picker/hy-picker.vue +9 -9
- package/components/hy-popover/hy-popover.vue +11 -7
- package/components/hy-popup/hy-popup.vue +9 -7
- package/components/hy-price/hy-price.vue +6 -5
- package/components/hy-qrcode/hy-qrcode.vue +6 -8
- package/components/hy-radio/hy-radio.vue +10 -12
- package/components/hy-rate/hy-rate.vue +7 -7
- package/components/hy-read-more/hy-read-more.vue +6 -5
- package/components/hy-scroll-list/hy-scroll-list.vue +6 -5
- package/components/hy-search/hy-search.vue +8 -6
- package/components/hy-signature/hy-signature.vue +6 -5
- package/components/hy-slider/hy-slider.vue +8 -7
- package/components/hy-steps/hy-steps.vue +10 -17
- package/components/hy-submit-bar/hy-submit-bar.vue +6 -6
- package/components/hy-subsection/hy-subsection.vue +16 -19
- package/components/hy-swipe-action/hy-swipe-action.vue +31 -25
- package/components/hy-swipe-action/typing.d.ts +1 -1
- package/components/hy-swiper/hy-swiper.vue +15 -15
- package/components/hy-swiper/typing.d.ts +13 -1
- package/components/hy-switch/hy-switch.vue +17 -10
- package/components/hy-tabBar/hy-tabBar.vue +35 -26
- package/components/hy-tabs/hy-tabs.vue +28 -21
- package/components/hy-tag/hy-tag.vue +13 -17
- package/components/hy-tag/typing.d.ts +1 -1
- package/components/hy-text/hy-text.vue +12 -14
- package/components/hy-textarea/hy-textarea.vue +16 -13
- package/components/hy-textarea/index.scss +7 -2
- package/components/hy-textarea/typing.d.ts +1 -1
- package/components/hy-toast/hy-toast.vue +72 -75
- package/components/hy-tooltip/hy-tooltip.vue +8 -14
- package/components/hy-transition/hy-transition.vue +8 -6
- package/components/hy-upload/hy-upload.vue +6 -5
- package/components/hy-warn/hy-warn.vue +6 -6
- package/components/hy-waterfall/hy-waterfall.vue +8 -7
- package/components/hy-watermark/hy-watermark.vue +20 -10
- package/components/index.ts +7 -1
- package/global.d.ts +3 -1
- package/libs/css/common.scss +5 -0
- package/libs/css/mixin.scss +2 -1
- package/libs/css/vars.css +1 -1
- package/package.json +2 -2
- package/theme.scss +1 -1
- package/utils/inspect.ts +34 -34
- package/utils/utils.ts +1 -19
- package/web-types.json +1 -1
- /package/components/{hy-form → hy-form-group}/index.scss +0 -0
- /package/components/{hy-form → hy-form-group}/props.ts +0 -0
|
@@ -1,533 +1,252 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<view class="hy-form">
|
|
3
|
-
<
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
shape: item?.picker?.input?.shape || picker?.input?.shape,
|
|
254
|
-
border: item?.picker?.input?.border || picker?.input?.border,
|
|
255
|
-
customStyle: errorStyle(!!errors[item.field]),
|
|
256
|
-
}"
|
|
257
|
-
></HyDatetimePicker>
|
|
258
|
-
</template>
|
|
259
|
-
<!-- 时间选择器 -->
|
|
260
|
-
|
|
261
|
-
<!-- 地址选择器 -->
|
|
262
|
-
<template v-if="item.type === FormTypeEnum.ADDRESS">
|
|
263
|
-
<HyAddressPicker
|
|
264
|
-
v-model="formData[item.field]"
|
|
265
|
-
has-input
|
|
266
|
-
:separator="item?.picker?.separator || picker?.separator"
|
|
267
|
-
:itemHeight="item?.picker?.itemHeight || picker?.itemHeight"
|
|
268
|
-
:cancelText="item?.picker?.cancelText || picker?.cancelText"
|
|
269
|
-
:confirmText="item?.picker?.confirmText || picker?.confirmText"
|
|
270
|
-
:cancelColor="item?.picker?.cancelColor || picker?.cancelColor"
|
|
271
|
-
:confirmColor="item?.picker?.confirmColor || picker?.confirmColor"
|
|
272
|
-
:visibleItemCount="
|
|
273
|
-
item?.picker?.visibleItemCount || picker?.visibleItemCount
|
|
274
|
-
"
|
|
275
|
-
:closeOnClickOverlay="
|
|
276
|
-
item?.picker?.closeOnClickOverlay || picker?.closeOnClickOverlay
|
|
277
|
-
"
|
|
278
|
-
:title="item?.picker?.title || picker?.title"
|
|
279
|
-
:showToolbar="item?.picker?.showToolbar || picker?.showToolbar"
|
|
280
|
-
:customStyle="errorStyle(!!errors[item.field])"
|
|
281
|
-
:input="{
|
|
282
|
-
disabled:
|
|
283
|
-
item?.picker?.input?.disabled || picker?.input?.disabled,
|
|
284
|
-
placeholder: item.picker?.input?.placeholder,
|
|
285
|
-
shape: item?.picker?.input?.shape || picker?.input?.shape,
|
|
286
|
-
border: item?.picker?.input?.border || picker?.input?.border,
|
|
287
|
-
customStyle: errorStyle(!!errors[item.field]),
|
|
288
|
-
}"
|
|
289
|
-
></HyAddressPicker>
|
|
290
|
-
</template>
|
|
291
|
-
<!-- 地址选择器 -->
|
|
292
|
-
|
|
293
|
-
<!-- 自定义插槽 -->
|
|
294
|
-
<view class="flex" v-if="item.type === FormTypeEnum.CUSTOM">
|
|
295
|
-
<slot
|
|
296
|
-
:name="item.field"
|
|
297
|
-
:record="item"
|
|
298
|
-
:errorStyle="errorStyle(!!errors[item.field])"
|
|
299
|
-
></slot>
|
|
300
|
-
</view>
|
|
301
|
-
<!-- 自定义插槽 -->
|
|
302
|
-
</view>
|
|
303
|
-
|
|
304
|
-
<!-- 提示信息 -->
|
|
305
|
-
<HyTransition :show="!!errors[item.field]" mode="slide-left">
|
|
306
|
-
<view class="hy-form--item__container-warning">{{
|
|
307
|
-
errors[item.field]
|
|
308
|
-
}}</view>
|
|
309
|
-
</HyTransition>
|
|
310
|
-
<!-- 提示信息 -->
|
|
311
|
-
</view>
|
|
312
|
-
</view>
|
|
313
|
-
</view>
|
|
314
|
-
</template>
|
|
315
|
-
|
|
316
|
-
<script lang="ts">
|
|
317
|
-
export default {
|
|
318
|
-
name: 'hy-form',
|
|
319
|
-
options: {
|
|
320
|
-
addGlobalClass: true,
|
|
321
|
-
virtualHost: true,
|
|
322
|
-
styleIsolation: 'shared'
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
</script>
|
|
326
|
-
|
|
327
|
-
<script setup lang="ts">
|
|
328
|
-
import { computed, type CSSProperties, reactive, toRefs } from "vue";
|
|
329
|
-
import type IProps from "./typing";
|
|
330
|
-
import defaultProps from "./props";
|
|
331
|
-
import { addUnit, error } from "../../utils";
|
|
332
|
-
import type { FormColumnsType, RulesVo } from "../../typing";
|
|
333
|
-
import { FormTypeEnum } from "../../typing";
|
|
334
|
-
|
|
335
|
-
// 组件
|
|
336
|
-
import HyInput from "../hy-input/hy-input.vue";
|
|
337
|
-
import HyPicker from "../hy-picker/hy-picker.vue";
|
|
338
|
-
import HyUpload from "../hy-upload/hy-upload.vue";
|
|
339
|
-
import HyTextarea from "../hy-textarea/hy-textarea.vue";
|
|
340
|
-
import HySwitch from "../hy-switch/hy-switch.vue";
|
|
341
|
-
import HyRadio from "../hy-radio/hy-radio.vue";
|
|
342
|
-
import HyDatetimePicker from "../hy-datetime-picker/hy-datetime-picker.vue";
|
|
343
|
-
import HyAddressPicker from "../hy-address-picker/hy-address-picker.vue";
|
|
344
|
-
import HyTransition from "../hy-transition/hy-transition.vue";
|
|
345
|
-
import HyCheckButton from "../hy-check-button/hy-check-button.vue";
|
|
346
|
-
|
|
347
|
-
const props = withDefaults(defineProps<IProps>(), defaultProps);
|
|
348
|
-
const {
|
|
349
|
-
formData,
|
|
350
|
-
columns,
|
|
351
|
-
labelWidth,
|
|
352
|
-
labelAlign,
|
|
353
|
-
labelPosition,
|
|
354
|
-
borderBottom,
|
|
355
|
-
} = toRefs(props);
|
|
356
|
-
const emit = defineEmits(["click"]);
|
|
357
|
-
|
|
358
|
-
const labelPos = labelPosition.value === "top" ? "column" : "row";
|
|
359
|
-
const isInput = (type: FormTypeEnum) =>
|
|
360
|
-
type === FormTypeEnum.TEXT ||
|
|
361
|
-
type === FormTypeEnum.NUMBER ||
|
|
362
|
-
type === FormTypeEnum.PASSWORD;
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
|
-
* @description 错误输入框样式
|
|
366
|
-
* */
|
|
367
|
-
const errorStyle = computed(() => {
|
|
368
|
-
return (err: boolean) => {
|
|
369
|
-
const style: CSSProperties = {};
|
|
370
|
-
if (err) {
|
|
371
|
-
style.background = "#dd6161";
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
return style;
|
|
375
|
-
};
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
const isRequired = computed(() => {
|
|
379
|
-
return (temp: any) => {
|
|
380
|
-
if (Array.isArray(temp)) {
|
|
381
|
-
return temp.some((item) => item?.required);
|
|
382
|
-
} else {
|
|
383
|
-
return temp?.required;
|
|
384
|
-
}
|
|
385
|
-
};
|
|
386
|
-
});
|
|
387
|
-
|
|
388
|
-
const errors: AnyObject = reactive({});
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* @description 标题行内样式
|
|
392
|
-
* */
|
|
393
|
-
const labelStyle = computed(() => {
|
|
394
|
-
return {
|
|
395
|
-
textAlign: labelAlign.value,
|
|
396
|
-
width: addUnit(labelWidth.value),
|
|
397
|
-
};
|
|
398
|
-
});
|
|
399
|
-
|
|
400
|
-
/**
|
|
401
|
-
* @description 错误信息校验
|
|
402
|
-
* */
|
|
403
|
-
const errorMsg = (rule: RulesVo, value: string): string => {
|
|
404
|
-
// 手机正则
|
|
405
|
-
const phoneExpression = /^1[3-9]\d{9}$/;
|
|
406
|
-
// 邮箱正则
|
|
407
|
-
const emitExpression = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
|
|
408
|
-
// 复杂密码正则
|
|
409
|
-
const passwordExpression =
|
|
410
|
-
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$/;
|
|
411
|
-
if (rule.required && !value) {
|
|
412
|
-
return rule.message!;
|
|
413
|
-
} else if (
|
|
414
|
-
(rule?.min || rule?.max) &&
|
|
415
|
-
(value.length < rule?.min! || value.length > rule?.max!)
|
|
416
|
-
) {
|
|
417
|
-
return rule.message!;
|
|
418
|
-
} else if (rule.type === "phone" && !phoneExpression.test(String(value))) {
|
|
419
|
-
return rule.message || "请输入正确的手机号";
|
|
420
|
-
} else if (rule.type === "email" && !emitExpression.test(String(value))) {
|
|
421
|
-
return rule.message || "请输入正确的邮箱格式";
|
|
422
|
-
} else if (
|
|
423
|
-
rule.type === "password" &&
|
|
424
|
-
!passwordExpression.test(String(value))
|
|
425
|
-
) {
|
|
426
|
-
return (
|
|
427
|
-
rule.message || "至少8个字符串,并且包含大、小写字母、数字和特殊符号"
|
|
428
|
-
);
|
|
429
|
-
} else if (rule?.validator && !rule.validator(rule, value)) {
|
|
430
|
-
return rule.message || "";
|
|
431
|
-
} else {
|
|
432
|
-
return "";
|
|
433
|
-
}
|
|
434
|
-
};
|
|
435
|
-
|
|
436
|
-
/**
|
|
437
|
-
* @description 校验字段
|
|
438
|
-
* */
|
|
439
|
-
const validateField = (
|
|
440
|
-
rules: RulesVo | RulesVo[] | undefined,
|
|
441
|
-
value: string,
|
|
442
|
-
field: string,
|
|
443
|
-
event?: "blur" | "change",
|
|
444
|
-
) => {
|
|
445
|
-
if (!rules) return;
|
|
446
|
-
|
|
447
|
-
let errorMessage = "";
|
|
448
|
-
|
|
449
|
-
if (Array.isArray(rules)) {
|
|
450
|
-
// 遍历所有规则
|
|
451
|
-
for (const index in rules) {
|
|
452
|
-
// 判断是否有change事件或者blur事件
|
|
453
|
-
if (event && !rules[index]?.trigger?.includes(event)) continue;
|
|
454
|
-
errorMessage = errorMsg(rules[index], value);
|
|
455
|
-
if (errorMessage) {
|
|
456
|
-
errors[field] = errorMessage;
|
|
457
|
-
return;
|
|
458
|
-
}
|
|
459
|
-
// 可以继续添加更多规则
|
|
460
|
-
}
|
|
461
|
-
} else {
|
|
462
|
-
// 必填校验
|
|
463
|
-
if (event && !rules?.trigger?.includes(event)) return;
|
|
464
|
-
errorMessage = errorMsg(rules, value);
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
// 自定义校验规则
|
|
468
|
-
// if (rules.custom) {
|
|
469
|
-
// const customError = rules.custom(value);
|
|
470
|
-
// if (customError) {
|
|
471
|
-
// errorMessage = customError;
|
|
472
|
-
// }
|
|
473
|
-
// }
|
|
474
|
-
|
|
475
|
-
errors[field] = errorMessage;
|
|
476
|
-
};
|
|
477
|
-
|
|
478
|
-
/**
|
|
479
|
-
* @description 提交表单
|
|
480
|
-
* */
|
|
481
|
-
const handleSubmit = () => {
|
|
482
|
-
return new Promise((resolve, reject) => {
|
|
483
|
-
let isValid = true;
|
|
484
|
-
|
|
485
|
-
// 校验所有字段
|
|
486
|
-
props.columns.forEach((col) => {
|
|
487
|
-
validateField(col.rules, formData.value[col.field], col.field);
|
|
488
|
-
if (errors[col.field]) {
|
|
489
|
-
isValid = false;
|
|
490
|
-
}
|
|
491
|
-
});
|
|
492
|
-
|
|
493
|
-
if (isValid) {
|
|
494
|
-
// alert("表单提交成功!");
|
|
495
|
-
resolve(formData.value);
|
|
496
|
-
} else {
|
|
497
|
-
reject();
|
|
498
|
-
error("表单校验失败,请检查输入!");
|
|
499
|
-
}
|
|
500
|
-
});
|
|
501
|
-
};
|
|
502
|
-
|
|
503
|
-
/**
|
|
504
|
-
* @description 输入值触发
|
|
505
|
-
* */
|
|
506
|
-
const handleChange = (event: string, temp: FormColumnsType) => {
|
|
507
|
-
if (isInput(temp.type) && temp?.input?.onChange) {
|
|
508
|
-
temp.input.onChange(event, temp);
|
|
509
|
-
}
|
|
510
|
-
validateField(temp.rules, event, temp.field, "change");
|
|
511
|
-
};
|
|
512
|
-
/**
|
|
513
|
-
* @description 输入值触发
|
|
514
|
-
* */
|
|
515
|
-
const handleBlur = (event: string, temp: FormColumnsType) => {
|
|
516
|
-
if (isInput(temp.type) && temp?.input?.onBlur) {
|
|
517
|
-
temp.input.onBlur(event, temp);
|
|
518
|
-
}
|
|
519
|
-
validateField(temp.rules, event, temp.field, "blur");
|
|
520
|
-
};
|
|
521
|
-
|
|
522
|
-
defineExpose({
|
|
523
|
-
validateField,
|
|
524
|
-
handleSubmit,
|
|
525
|
-
});
|
|
526
|
-
</script>
|
|
527
|
-
|
|
528
|
-
<style lang="scss" scoped>
|
|
529
|
-
@import "./index.scss";
|
|
530
|
-
.hy-form--item {
|
|
531
|
-
flex-direction: v-bind(labelPos);
|
|
532
|
-
}
|
|
533
|
-
</style>
|
|
1
|
+
<template>
|
|
2
|
+
<view class="hy-form-simple">
|
|
3
|
+
<slot></slot>
|
|
4
|
+
</view>
|
|
5
|
+
</template>
|
|
6
|
+
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
export default {
|
|
9
|
+
name: 'hy-form-simple',
|
|
10
|
+
options: {
|
|
11
|
+
addGlobalClass: true,
|
|
12
|
+
virtualHost: true,
|
|
13
|
+
styleIsolation: 'shared',
|
|
14
|
+
},
|
|
15
|
+
}
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
<script setup lang="ts">
|
|
19
|
+
import { PropType, provide, reactive, ref, toRefs } from 'vue'
|
|
20
|
+
import type { FormItemRule } from './typing'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 表单组件父组件,需要搭配hy-form-item
|
|
24
|
+
* @displayName hy-form
|
|
25
|
+
*/
|
|
26
|
+
defineOptions({})
|
|
27
|
+
|
|
28
|
+
const props = defineProps({
|
|
29
|
+
/** 表单数据对象 */
|
|
30
|
+
model: Object as PropType<AnyObject>,
|
|
31
|
+
/** 表单校验规则 */
|
|
32
|
+
rules: Object as unknown as PropType<FormItemRule>,
|
|
33
|
+
/** label标签的宽度,单位rpx */
|
|
34
|
+
labelWidth: {
|
|
35
|
+
type: String,
|
|
36
|
+
default: 'auto',
|
|
37
|
+
},
|
|
38
|
+
/**
|
|
39
|
+
* 标签位置
|
|
40
|
+
* @values left,top
|
|
41
|
+
* */
|
|
42
|
+
labelPosition: {
|
|
43
|
+
type: String,
|
|
44
|
+
default: 'left',
|
|
45
|
+
},
|
|
46
|
+
/**
|
|
47
|
+
* 标签位置
|
|
48
|
+
* @values left,center,right
|
|
49
|
+
* */
|
|
50
|
+
labelAlign: {
|
|
51
|
+
type: String,
|
|
52
|
+
default: 'left',
|
|
53
|
+
},
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const emit = defineEmits<{
|
|
57
|
+
submit: [data: Record<string, any>]
|
|
58
|
+
validate: [valid: boolean, errors: Record<string, string>]
|
|
59
|
+
}>()
|
|
60
|
+
|
|
61
|
+
// 表单数据
|
|
62
|
+
const formData = reactive(props.model || {})
|
|
63
|
+
const formItems = ref<any[]>([])
|
|
64
|
+
const errors = reactive<Record<string, string>>({})
|
|
65
|
+
|
|
66
|
+
// 表单上下文
|
|
67
|
+
const formContext = {
|
|
68
|
+
formData,
|
|
69
|
+
errors,
|
|
70
|
+
rules: toRefs(props).rules,
|
|
71
|
+
labelWidth: toRefs(props).labelWidth,
|
|
72
|
+
labelPosition: toRefs(props).labelPosition,
|
|
73
|
+
labelAlign: toRefs(props).labelAlign,
|
|
74
|
+
addFormItem: (item: any) => {
|
|
75
|
+
formItems.value.push(item)
|
|
76
|
+
},
|
|
77
|
+
removeFormItem: (item: any) => {
|
|
78
|
+
const index = formItems.value.indexOf(item)
|
|
79
|
+
if (index > -1) {
|
|
80
|
+
formItems.value.splice(index, 1)
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
validateField: (field: string, value: any, trigger?: 'blur' | 'change') => {
|
|
84
|
+
const fieldRules = props.rules?.[field]
|
|
85
|
+
if (!fieldRules) return true
|
|
86
|
+
|
|
87
|
+
const rules = Array.isArray(fieldRules) ? fieldRules : [fieldRules]
|
|
88
|
+
let isValid = true
|
|
89
|
+
let errorMessage = ''
|
|
90
|
+
|
|
91
|
+
for (const rule of rules) {
|
|
92
|
+
// 检查触发时机
|
|
93
|
+
if (
|
|
94
|
+
(trigger && !rule.trigger) ||
|
|
95
|
+
(trigger && rule.trigger && !rule.trigger.includes(trigger))
|
|
96
|
+
) {
|
|
97
|
+
continue
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// 必填校验
|
|
101
|
+
if (rule.required && (!value || value === '')) {
|
|
102
|
+
errorMessage = rule.message || `${field} 是必填项`
|
|
103
|
+
isValid = false
|
|
104
|
+
break
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 长度校验
|
|
108
|
+
if (rule.min && String(value).length < rule.min) {
|
|
109
|
+
errorMessage = rule.message || `${field} 长度不能少于 ${rule.min} 个字符`
|
|
110
|
+
isValid = false
|
|
111
|
+
break
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (rule.max && String(value).length > rule.max) {
|
|
115
|
+
errorMessage = rule.message || `${field} 长度不能超过 ${rule.max} 个字符`
|
|
116
|
+
isValid = false
|
|
117
|
+
break
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// 类型校验
|
|
121
|
+
if (rule.type === 'phone') {
|
|
122
|
+
const phoneRegex = /^1[3-9]\d{9}$/
|
|
123
|
+
if (!phoneRegex.test(String(value))) {
|
|
124
|
+
errorMessage = rule.message || '请输入正确的手机号'
|
|
125
|
+
isValid = false
|
|
126
|
+
break
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (rule.type === 'email') {
|
|
131
|
+
const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/
|
|
132
|
+
if (!emailRegex.test(String(value))) {
|
|
133
|
+
errorMessage = rule.message || '请输入正确的邮箱格式'
|
|
134
|
+
isValid = false
|
|
135
|
+
break
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (rule.type === 'password') {
|
|
140
|
+
const passwordRegex =
|
|
141
|
+
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$/
|
|
142
|
+
if (!passwordRegex.test(String(value))) {
|
|
143
|
+
errorMessage = rule.message || '密码至少8个字符,包含大小写字母、数字和特殊符号'
|
|
144
|
+
isValid = false
|
|
145
|
+
break
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// 自定义校验
|
|
150
|
+
if (rule.validator) {
|
|
151
|
+
const result = rule.validator(value)
|
|
152
|
+
if (result === false || typeof result === 'string') {
|
|
153
|
+
errorMessage = typeof result === 'string' ? result : rule.message || `${field} 校验失败`
|
|
154
|
+
isValid = false
|
|
155
|
+
break
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (isValid) {
|
|
161
|
+
delete errors[field]
|
|
162
|
+
} else {
|
|
163
|
+
errors[field] = errorMessage
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return isValid
|
|
167
|
+
},
|
|
168
|
+
setFieldValue: (field: string, value: any) => {
|
|
169
|
+
formData[field] = value
|
|
170
|
+
},
|
|
171
|
+
getFieldValue: (field: string) => {
|
|
172
|
+
return formData[field]
|
|
173
|
+
},
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// 提供表单上下文给子组件
|
|
177
|
+
provide('formContext', formContext)
|
|
178
|
+
|
|
179
|
+
// 验证所有字段
|
|
180
|
+
const validate = () => {
|
|
181
|
+
return new Promise((resolve, reject) => {
|
|
182
|
+
let isValid = true
|
|
183
|
+
const allErrors: Record<string, string> = {}
|
|
184
|
+
|
|
185
|
+
formItems.value.forEach((item) => {
|
|
186
|
+
const field = item.props?.prop
|
|
187
|
+
if (field) {
|
|
188
|
+
const value = formData[field]
|
|
189
|
+
const fieldValid = formContext.validateField(field, value)
|
|
190
|
+
if (!fieldValid) {
|
|
191
|
+
isValid = false
|
|
192
|
+
allErrors[field] = errors[field]
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
emit('validate', isValid, allErrors)
|
|
198
|
+
if (isValid) {
|
|
199
|
+
resolve(isValid)
|
|
200
|
+
} else {
|
|
201
|
+
reject(allErrors)
|
|
202
|
+
}
|
|
203
|
+
})
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// 重置表单
|
|
207
|
+
const resetFields = () => {
|
|
208
|
+
Object.keys(formData).forEach((key) => {
|
|
209
|
+
formData[key] = undefined
|
|
210
|
+
})
|
|
211
|
+
Object.keys(errors).forEach((key) => {
|
|
212
|
+
delete errors[key]
|
|
213
|
+
})
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// 清除验证
|
|
217
|
+
const clearValidate = (fields?: string[]) => {
|
|
218
|
+
if (fields) {
|
|
219
|
+
fields.forEach((field) => {
|
|
220
|
+
delete errors[field]
|
|
221
|
+
})
|
|
222
|
+
} else {
|
|
223
|
+
Object.keys(errors).forEach((key) => {
|
|
224
|
+
delete errors[key]
|
|
225
|
+
})
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// 提交表单
|
|
230
|
+
const submit = async () => {
|
|
231
|
+
if (await validate()) {
|
|
232
|
+
emit('submit', { ...formData })
|
|
233
|
+
return formData
|
|
234
|
+
}
|
|
235
|
+
return false
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
defineExpose({
|
|
239
|
+
validate,
|
|
240
|
+
resetFields,
|
|
241
|
+
clearValidate,
|
|
242
|
+
submit,
|
|
243
|
+
formData,
|
|
244
|
+
errors,
|
|
245
|
+
})
|
|
246
|
+
</script>
|
|
247
|
+
|
|
248
|
+
<style lang="scss" scoped>
|
|
249
|
+
.hy-form-simple {
|
|
250
|
+
width: 100%;
|
|
251
|
+
}
|
|
252
|
+
</style>
|