hy-app 0.3.0 → 0.3.2
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/README.md +6 -3
- package/common/shakeService.ts +31 -29
- package/components/avatar.zip +0 -0
- package/components/hy-action-sheet/hy-action-sheet.vue +71 -46
- package/components/hy-address-picker/hy-address-picker.vue +94 -83
- package/components/hy-avatar/hy-avatar.vue +84 -85
- package/components/hy-back-top/hy-back-top.vue +8 -6
- package/components/hy-badge/hy-badge.vue +47 -46
- package/components/hy-button/hy-button.vue +117 -93
- package/components/hy-calendar/hy-calendar.vue +168 -160
- package/components/hy-card/hy-card.vue +50 -43
- package/components/hy-card/typing.d.ts +33 -32
- package/components/hy-cell/hy-cell.vue +73 -51
- package/components/hy-check-button/hy-check-button.vue +54 -47
- package/components/hy-checkbox/hy-checkbox.vue +97 -105
- package/components/hy-code-input/hy-code-input.vue +80 -89
- package/components/hy-config-provider/hy-config-provider.vue +20 -21
- package/components/hy-count-down/hy-count-down.vue +66 -67
- package/components/hy-count-to/hy-count-to.vue +105 -99
- package/components/hy-count-to/typing.d.ts +13 -12
- package/components/hy-datetime-picker/hy-datetime-picker.vue +261 -253
- package/components/hy-datetime-picker/typing.d.ts +42 -40
- package/components/hy-divider/hy-divider.vue +68 -73
- package/components/hy-dropdown/hy-dropdown.vue +20 -19
- package/components/hy-dropdown-item/hy-dropdown-item.vue +66 -61
- package/components/hy-dropdown-item/typing.d.ts +9 -9
- package/components/hy-empty/hy-empty.vue +42 -42
- package/components/hy-flex/hy-flex.vue +99 -0
- package/components/hy-flex/index.scss +8 -0
- package/components/hy-flex/typing.d.ts +23 -0
- package/components/hy-float-button/hy-float-button.vue +218 -210
- package/components/hy-folding-panel/hy-folding-panel.vue +32 -33
- package/components/hy-form/hy-form.vue +264 -252
- package/components/hy-form/typing.d.ts +4 -0
- package/components/hy-form-group/hy-form-group.vue +114 -183
- package/components/hy-form-item/hy-form-item.vue +12 -10
- package/components/hy-form-item/index.scss +2 -2
- package/components/hy-form-item/typing.d.ts +3 -6
- package/components/hy-grid/hy-grid.vue +44 -43
- package/components/hy-icon/hy-icon.vue +61 -67
- package/components/hy-image/hy-image.vue +112 -88
- package/components/hy-image/typing.d.ts +27 -23
- package/components/hy-input/hy-input.vue +157 -127
- package/components/hy-input/typing.d.ts +53 -47
- package/components/hy-line/hy-line.vue +26 -26
- package/components/hy-line-progress/hy-line-progress.vue +42 -35
- package/components/hy-list/hy-list.vue +76 -85
- package/components/hy-loading/hy-loading.vue +26 -23
- package/components/hy-login/TheUserLogin.vue +1 -1
- package/components/hy-menu/hy-menu.vue +48 -43
- package/components/hy-menu/typing.d.ts +18 -17
- package/components/hy-modal/hy-modal.vue +39 -35
- package/components/hy-navbar/hy-navbar.vue +25 -25
- package/components/hy-navbar/typing.d.ts +24 -22
- package/components/hy-notice-bar/hy-notice-bar.vue +26 -27
- package/components/hy-notify/hy-notify.vue +53 -53
- package/components/hy-number-step/hy-number-step.vue +134 -146
- package/components/hy-number-step/typing.d.ts +35 -35
- package/components/hy-overlay/hy-overlay.vue +23 -21
- package/components/hy-pagination/hy-pagination.vue +41 -36
- package/components/hy-picker/hy-picker.vue +184 -154
- package/components/hy-picker/typing.d.ts +39 -39
- package/components/hy-popover/hy-popover.vue +97 -77
- package/components/hy-popup/hy-popup.vue +107 -98
- package/components/hy-price/hy-price.vue +38 -34
- package/components/hy-qrcode/hy-qrcode.vue +50 -51
- package/components/hy-radio/hy-radio.vue +101 -113
- package/components/hy-rate/hy-rate.vue +107 -88
- package/components/hy-read-more/hy-read-more.vue +64 -49
- package/components/hy-scroll-list/hy-scroll-list.vue +45 -48
- package/components/hy-search/hy-search.vue +73 -66
- package/components/hy-search/typing.d.ts +36 -35
- package/components/hy-signature/hy-signature.vue +282 -240
- package/components/hy-slider/hy-slider.vue +195 -153
- package/components/hy-slider/typing.d.ts +21 -21
- package/components/hy-steps/hy-steps.vue +118 -90
- package/components/hy-steps/index.scss +31 -21
- package/components/hy-submit-bar/hy-submit-bar.vue +61 -70
- package/components/hy-subsection/hy-subsection.vue +99 -102
- package/components/hy-subsection/typing.d.ts +19 -19
- package/components/hy-swipe-action/hy-swipe-action.vue +131 -118
- package/components/hy-swiper/hy-swiper.vue +85 -71
- package/components/hy-switch/hy-switch.vue +67 -72
- package/components/hy-switch/typing.d.ts +21 -19
- package/components/hy-tabs/hy-tabs.vue +168 -113
- package/components/hy-tag/hy-tag.vue +90 -86
- package/components/hy-tag/typing.d.ts +26 -21
- package/components/hy-text/hy-text.vue +119 -111
- package/components/hy-textarea/hy-textarea.vue +100 -93
- package/components/hy-textarea/typing.d.ts +36 -31
- package/components/hy-toast/hy-toast.vue +77 -67
- package/components/hy-tooltip/hy-tooltip.vue +109 -91
- package/components/hy-transition/hy-transition.vue +62 -66
- package/components/hy-upload/hy-upload.vue +294 -152
- package/components/hy-upload/typing.d.ts +41 -36
- package/components/hy-warn/hy-warn.vue +34 -27
- package/components/hy-waterfall/hy-waterfall.vue +83 -74
- package/components/hy-watermark/hy-watermark.vue +134 -115
- package/components/index.ts +1 -1
- package/composables/usePopover.ts +236 -221
- package/composables/useQueue.ts +53 -52
- package/global.d.ts +1 -0
- package/package.json +2 -2
- package/store/index.ts +9 -1
- package/theme.scss +5 -5
- package/typing/index.ts +0 -1
- package/typing/modules/common.d.ts +0 -2
- package/web-types.json +1 -1
|
@@ -40,30 +40,30 @@
|
|
|
40
40
|
|
|
41
41
|
<script lang="ts">
|
|
42
42
|
export default {
|
|
43
|
-
name:
|
|
43
|
+
name: "hy-folding-panel",
|
|
44
44
|
options: {
|
|
45
45
|
addGlobalClass: true,
|
|
46
46
|
virtualHost: true,
|
|
47
|
-
styleIsolation:
|
|
47
|
+
styleIsolation: "shared",
|
|
48
48
|
},
|
|
49
|
-
}
|
|
49
|
+
};
|
|
50
50
|
</script>
|
|
51
51
|
|
|
52
52
|
<script setup lang="ts">
|
|
53
|
-
import { toRefs, ref, watch } from
|
|
54
|
-
import type { CSSProperties, PropType } from
|
|
55
|
-
import type { IFoldingPanel, PanelVo } from
|
|
56
|
-
import { addUnit } from
|
|
57
|
-
import { ColorConfig } from
|
|
58
|
-
|
|
59
|
-
import HyCell from
|
|
60
|
-
import HyLine from
|
|
53
|
+
import { toRefs, ref, watch } from "vue";
|
|
54
|
+
import type { CSSProperties, PropType } from "vue";
|
|
55
|
+
import type { IFoldingPanel, PanelVo } from "./typing";
|
|
56
|
+
import { addUnit } from "../../utils";
|
|
57
|
+
import { ColorConfig } from "../../config";
|
|
58
|
+
// 组件
|
|
59
|
+
import HyCell from "../hy-cell/hy-cell.vue";
|
|
60
|
+
import HyLine from "../hy-line/hy-line.vue";
|
|
61
61
|
|
|
62
62
|
/**
|
|
63
63
|
* 通过折叠面板收纳内容区域。
|
|
64
64
|
* @displayName hy-folding-panel
|
|
65
65
|
*/
|
|
66
|
-
defineOptions({})
|
|
66
|
+
defineOptions({});
|
|
67
67
|
|
|
68
68
|
// const props = withDefaults(defineProps<IProps>(), defaultProps);
|
|
69
69
|
const props = defineProps({
|
|
@@ -110,7 +110,7 @@ const props = defineProps({
|
|
|
110
110
|
* */
|
|
111
111
|
size: {
|
|
112
112
|
type: String,
|
|
113
|
-
default:
|
|
113
|
+
default: "medium",
|
|
114
114
|
},
|
|
115
115
|
/** 内容面板高度 */
|
|
116
116
|
contentHeight: {
|
|
@@ -121,45 +121,44 @@ const props = defineProps({
|
|
|
121
121
|
customStyle: {
|
|
122
122
|
type: Object as PropType<CSSProperties>,
|
|
123
123
|
},
|
|
124
|
-
})
|
|
125
|
-
const
|
|
126
|
-
const emit = defineEmits<IFoldingPanel>()
|
|
124
|
+
});
|
|
125
|
+
const emit = defineEmits<IFoldingPanel>();
|
|
127
126
|
|
|
128
|
-
const lists = ref<PanelVo[]>([])
|
|
127
|
+
const lists = ref<PanelVo[]>([]);
|
|
129
128
|
|
|
130
129
|
watch(
|
|
131
|
-
() => list
|
|
130
|
+
() => props.list,
|
|
132
131
|
(newValue: PanelVo[]) => {
|
|
133
132
|
lists.value = newValue.map((item) => ({
|
|
134
133
|
...item,
|
|
135
|
-
arrowDirection:
|
|
134
|
+
arrowDirection: "down",
|
|
136
135
|
spread: false,
|
|
137
|
-
}))
|
|
136
|
+
}));
|
|
138
137
|
},
|
|
139
138
|
{ immediate: true },
|
|
140
|
-
)
|
|
139
|
+
);
|
|
141
140
|
|
|
142
141
|
const clickHandler = (temp: PanelVo, index: number) => {
|
|
143
142
|
// if (temp?.disabled && temp?.animating) return;
|
|
144
|
-
lists.value = list.
|
|
145
|
-
if (accordion
|
|
143
|
+
lists.value = props.list.map((item, i) => {
|
|
144
|
+
if (props.accordion) {
|
|
146
145
|
// 判断是否是收起来
|
|
147
|
-
item.spread = i === index ? !item.spread : false
|
|
146
|
+
item.spread = i === index ? !item.spread : false;
|
|
148
147
|
} else {
|
|
149
148
|
if (i === index) {
|
|
150
|
-
item.spread = !item.spread
|
|
149
|
+
item.spread = !item.spread;
|
|
151
150
|
}
|
|
152
151
|
}
|
|
153
152
|
|
|
154
|
-
item.arrowDirection = item.spread ?
|
|
155
|
-
return item
|
|
156
|
-
})
|
|
157
|
-
const event = temp.spread ?
|
|
158
|
-
emit(
|
|
159
|
-
emit(event, temp, index)
|
|
160
|
-
}
|
|
153
|
+
item.arrowDirection = item.spread ? "up" : "down";
|
|
154
|
+
return item;
|
|
155
|
+
});
|
|
156
|
+
const event: "open" | "close" = temp.spread ? "open" : "close";
|
|
157
|
+
emit("change", temp, index);
|
|
158
|
+
emit(event, temp, index);
|
|
159
|
+
};
|
|
161
160
|
</script>
|
|
162
161
|
|
|
163
162
|
<style lang="scss" scoped>
|
|
164
|
-
@import
|
|
163
|
+
@import "./index.scss";
|
|
165
164
|
</style>
|
|
@@ -1,252 +1,264 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<view class="hy-form
|
|
3
|
-
<slot></slot>
|
|
4
|
-
</view>
|
|
5
|
-
</template>
|
|
6
|
-
|
|
7
|
-
<script lang="ts">
|
|
8
|
-
export default {
|
|
9
|
-
name:
|
|
10
|
-
options: {
|
|
11
|
-
addGlobalClass: true,
|
|
12
|
-
virtualHost: true,
|
|
13
|
-
styleIsolation:
|
|
14
|
-
},
|
|
15
|
-
}
|
|
16
|
-
</script>
|
|
17
|
-
|
|
18
|
-
<script setup lang="ts">
|
|
19
|
-
import {
|
|
20
|
-
import type {
|
|
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
|
-
const
|
|
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
|
-
if (
|
|
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
|
-
if (!
|
|
143
|
-
errorMessage = rule.message ||
|
|
144
|
-
isValid = false
|
|
145
|
-
break
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
if (
|
|
153
|
-
errorMessage =
|
|
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
|
-
|
|
1
|
+
<template>
|
|
2
|
+
<view class="hy-form">
|
|
3
|
+
<slot></slot>
|
|
4
|
+
</view>
|
|
5
|
+
</template>
|
|
6
|
+
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
export default {
|
|
9
|
+
name: "hy-form",
|
|
10
|
+
options: {
|
|
11
|
+
addGlobalClass: true,
|
|
12
|
+
virtualHost: true,
|
|
13
|
+
styleIsolation: "shared",
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
<script setup lang="ts">
|
|
19
|
+
import { provide, reactive, ref } from "vue";
|
|
20
|
+
import type { PropType } from "vue";
|
|
21
|
+
import type { FormItemRule } from "./typing";
|
|
22
|
+
import { clearVal, isArray } from "../../utils";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 表单组件父组件,需要搭配hy-form-item
|
|
26
|
+
* @displayName hy-form
|
|
27
|
+
*/
|
|
28
|
+
defineOptions({});
|
|
29
|
+
|
|
30
|
+
const props = defineProps({
|
|
31
|
+
/** 表单数据对象 */
|
|
32
|
+
model: Object as PropType<AnyObject>,
|
|
33
|
+
/** 表单校验规则 */
|
|
34
|
+
rules: Object as PropType<FormItemRule>,
|
|
35
|
+
/** 表单底部边框 */
|
|
36
|
+
border: {
|
|
37
|
+
type: Boolean,
|
|
38
|
+
default: false,
|
|
39
|
+
},
|
|
40
|
+
/** label标签的宽度,单位rpx */
|
|
41
|
+
labelWidth: {
|
|
42
|
+
type: String,
|
|
43
|
+
default: "auto",
|
|
44
|
+
},
|
|
45
|
+
/**
|
|
46
|
+
* 标签位置
|
|
47
|
+
* @values left,top
|
|
48
|
+
* */
|
|
49
|
+
labelPosition: {
|
|
50
|
+
type: String,
|
|
51
|
+
default: "left",
|
|
52
|
+
},
|
|
53
|
+
/**
|
|
54
|
+
* 标签位置
|
|
55
|
+
* @values left,center,right
|
|
56
|
+
* */
|
|
57
|
+
labelAlign: {
|
|
58
|
+
type: String,
|
|
59
|
+
default: "left",
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const emit = defineEmits<{
|
|
64
|
+
submit: [data: Record<string, any>];
|
|
65
|
+
validate: [valid: boolean, errors: Record<string, string>];
|
|
66
|
+
}>();
|
|
67
|
+
|
|
68
|
+
// 表单数据
|
|
69
|
+
const formData = reactive(props.model || {});
|
|
70
|
+
const formItems = ref<any[]>([]);
|
|
71
|
+
const errors = reactive<Record<string, string>>({});
|
|
72
|
+
|
|
73
|
+
// 表单上下文
|
|
74
|
+
const formContext = {
|
|
75
|
+
formData,
|
|
76
|
+
errors,
|
|
77
|
+
rules: props.rules,
|
|
78
|
+
border: props.border,
|
|
79
|
+
labelWidth: props.labelWidth,
|
|
80
|
+
labelPosition: props.labelPosition,
|
|
81
|
+
labelAlign: props.labelAlign,
|
|
82
|
+
addFormItem: (item: any) => {
|
|
83
|
+
formItems.value.push(item);
|
|
84
|
+
},
|
|
85
|
+
removeFormItem: (item: any) => {
|
|
86
|
+
const index = formItems.value.indexOf(item);
|
|
87
|
+
if (index > -1) {
|
|
88
|
+
formItems.value.splice(index, 1);
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
validateField: (field: string, value: any, trigger?: "blur" | "change") => {
|
|
92
|
+
const fieldRules = props.rules?.[field];
|
|
93
|
+
if (!fieldRules) return true;
|
|
94
|
+
|
|
95
|
+
const rules = isArray(fieldRules) ? fieldRules : [fieldRules];
|
|
96
|
+
let isValid = true;
|
|
97
|
+
let errorMessage = "";
|
|
98
|
+
|
|
99
|
+
for (const rule of rules) {
|
|
100
|
+
// 检查触发时机
|
|
101
|
+
if (
|
|
102
|
+
(trigger && !rule.trigger) ||
|
|
103
|
+
(trigger && rule.trigger && !rule.trigger.includes(trigger))
|
|
104
|
+
) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// 必填校验
|
|
109
|
+
if (rule.required && (!value || value === "")) {
|
|
110
|
+
errorMessage = rule.message || `${field} 是必填项`;
|
|
111
|
+
isValid = false;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// 长度校验
|
|
116
|
+
if (rule.min && String(value).length < rule.min) {
|
|
117
|
+
errorMessage =
|
|
118
|
+
rule.message || `${field} 长度不能少于 ${rule.min} 个字符`;
|
|
119
|
+
isValid = false;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (rule.max && String(value).length > rule.max) {
|
|
124
|
+
errorMessage =
|
|
125
|
+
rule.message || `${field} 长度不能超过 ${rule.max} 个字符`;
|
|
126
|
+
isValid = false;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// 类型校验
|
|
131
|
+
if (rule.type === "phone") {
|
|
132
|
+
const phoneRegex = /^1[3-9]\d{9}$/;
|
|
133
|
+
if (!phoneRegex.test(String(value))) {
|
|
134
|
+
errorMessage = rule.message || "请输入正确的手机号";
|
|
135
|
+
isValid = false;
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (rule.type === "email") {
|
|
141
|
+
const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
|
|
142
|
+
if (!emailRegex.test(String(value))) {
|
|
143
|
+
errorMessage = rule.message || "请输入正确的邮箱格式";
|
|
144
|
+
isValid = false;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (rule.type === "password") {
|
|
150
|
+
const passwordRegex =
|
|
151
|
+
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$/;
|
|
152
|
+
if (!passwordRegex.test(String(value))) {
|
|
153
|
+
errorMessage =
|
|
154
|
+
rule.message || "密码至少8个字符,包含大小写字母、数字和特殊符号";
|
|
155
|
+
isValid = false;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// 自定义校验
|
|
161
|
+
if (rule.validator) {
|
|
162
|
+
const result = rule.validator(value);
|
|
163
|
+
if (result === false || typeof result === "string") {
|
|
164
|
+
errorMessage =
|
|
165
|
+
typeof result === "string"
|
|
166
|
+
? result
|
|
167
|
+
: rule.message || `${field} 校验失败`;
|
|
168
|
+
isValid = false;
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (isValid) {
|
|
175
|
+
delete errors[field];
|
|
176
|
+
} else {
|
|
177
|
+
errors[field] = errorMessage;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return isValid;
|
|
181
|
+
},
|
|
182
|
+
setFieldValue: (field: string, value: any) => {
|
|
183
|
+
formData[field] = value;
|
|
184
|
+
},
|
|
185
|
+
getFieldValue: (field: string) => {
|
|
186
|
+
return formData[field];
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// 提供表单上下文给子组件
|
|
191
|
+
provide("formContext", formContext);
|
|
192
|
+
|
|
193
|
+
// 验证所有字段
|
|
194
|
+
const validate = () => {
|
|
195
|
+
return new Promise((resolve, reject) => {
|
|
196
|
+
let isValid = true;
|
|
197
|
+
const allErrors: Record<string, string> = {};
|
|
198
|
+
|
|
199
|
+
formItems.value.forEach((item) => {
|
|
200
|
+
const field = item.props?.prop;
|
|
201
|
+
if (field) {
|
|
202
|
+
const value = formData[field];
|
|
203
|
+
const fieldValid = formContext.validateField(field, value);
|
|
204
|
+
if (!fieldValid) {
|
|
205
|
+
isValid = false;
|
|
206
|
+
allErrors[field] = errors[field];
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
emit("validate", isValid, allErrors);
|
|
212
|
+
if (isValid) {
|
|
213
|
+
resolve(isValid);
|
|
214
|
+
} else {
|
|
215
|
+
reject(allErrors);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// 重置表单
|
|
221
|
+
const resetFields = () => {
|
|
222
|
+
clearVal(formData);
|
|
223
|
+
Object.keys(errors).forEach((key) => {
|
|
224
|
+
delete errors[key];
|
|
225
|
+
});
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
// 清除验证
|
|
229
|
+
const clearValidate = (fields?: string[]) => {
|
|
230
|
+
if (fields) {
|
|
231
|
+
fields.forEach((field) => {
|
|
232
|
+
delete errors[field];
|
|
233
|
+
});
|
|
234
|
+
} else {
|
|
235
|
+
Object.keys(errors).forEach((key) => {
|
|
236
|
+
delete errors[key];
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
// 提交表单
|
|
242
|
+
const submit = async () => {
|
|
243
|
+
if (await validate()) {
|
|
244
|
+
emit("submit", { ...formData });
|
|
245
|
+
return formData;
|
|
246
|
+
}
|
|
247
|
+
return false;
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
defineExpose({
|
|
251
|
+
validate,
|
|
252
|
+
resetFields,
|
|
253
|
+
clearValidate,
|
|
254
|
+
submit,
|
|
255
|
+
formData,
|
|
256
|
+
errors,
|
|
257
|
+
});
|
|
258
|
+
</script>
|
|
259
|
+
|
|
260
|
+
<style lang="scss" scoped>
|
|
261
|
+
.hy-form {
|
|
262
|
+
width: 100%;
|
|
263
|
+
}
|
|
264
|
+
</style>
|