@fairys/valtio-form-basic 0.0.12 → 1.0.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/README.md +935 -2
- package/esm/{form → common}/hooks/index.d.ts +1 -0
- package/esm/{form → common}/hooks/index.js +9 -1
- package/esm/common/index.d.ts +4 -0
- package/esm/common/index.js +4 -0
- package/esm/{form → common}/instance/index.d.ts +34 -7
- package/esm/{form → common}/instance/index.js +45 -11
- package/esm/{form → common}/utils/index.d.ts +7 -0
- package/esm/{form → common}/utils/index.js +11 -1
- package/esm/form/form.d.ts +14 -8
- package/esm/form/form.item.d.ts +18 -7
- package/esm/form/form.item.js +56 -32
- package/esm/form/form.js +7 -4
- package/esm/form/layout.d.ts +4 -2591
- package/esm/form/layout.js +7 -4
- package/esm/index.d.ts +1 -2
- package/esm/index.js +1 -2
- package/esm/styles/index.css +20 -4
- package/lib/common/hooks/index.d.ts +35 -0
- package/lib/common/hooks/index.js +117 -0
- package/lib/common/index.d.ts +4 -0
- package/lib/common/index.js +87 -0
- package/lib/common/instance/index.d.ts +108 -0
- package/lib/common/instance/index.js +269 -0
- package/lib/common/interface.d.ts +4 -0
- package/lib/common/interface.js +18 -0
- package/lib/common/utils/index.d.ts +31 -0
- package/lib/common/utils/index.js +119 -0
- package/lib/form/form.d.ts +82 -0
- package/lib/form/form.item.d.ts +228 -0
- package/lib/form/form.item.js +327 -0
- package/lib/form/form.js +54 -0
- package/lib/form/layout.d.ts +153 -0
- package/lib/form/layout.js +196 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +18 -27
- package/lib/styles/index.css +308 -0
- package/package.json +5 -5
- package/src/{form → common}/hooks/index.tsx +9 -1
- package/src/common/index.ts +4 -0
- package/src/{form → common}/instance/index.ts +110 -21
- package/src/{form → common}/utils/index.ts +21 -0
- package/src/form/form.item.tsx +87 -40
- package/src/form/form.tsx +23 -9
- package/src/form/layout.tsx +9 -3
- package/src/index.tsx +4 -5
- /package/esm/{interface.d.ts → common/interface.d.ts} +0 -0
- /package/esm/{interface.js → common/interface.js} +0 -0
- /package/src/{interface.ts → common/interface.ts} +0 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
*, :before, :after, ::backdrop {
|
|
2
|
+
--un-rotate: 0;
|
|
3
|
+
--un-rotate-x: 0;
|
|
4
|
+
--un-rotate-y: 0;
|
|
5
|
+
--un-rotate-z: 0;
|
|
6
|
+
--un-scale-x: 1;
|
|
7
|
+
--un-scale-y: 1;
|
|
8
|
+
--un-scale-z: 1;
|
|
9
|
+
--un-skew-x: 0;
|
|
10
|
+
--un-skew-y: 0;
|
|
11
|
+
--un-translate-x: 0;
|
|
12
|
+
--un-translate-y: 0;
|
|
13
|
+
--un-translate-z: 0;
|
|
14
|
+
--un-pan-x: ;
|
|
15
|
+
--un-pan-y: ;
|
|
16
|
+
--un-pinch-zoom: ;
|
|
17
|
+
--un-scroll-snap-strictness: proximity;
|
|
18
|
+
--un-ordinal: ;
|
|
19
|
+
--un-slashed-zero: ;
|
|
20
|
+
--un-numeric-figure: ;
|
|
21
|
+
--un-numeric-spacing: ;
|
|
22
|
+
--un-numeric-fraction: ;
|
|
23
|
+
--un-border-spacing-x: 0;
|
|
24
|
+
--un-border-spacing-y: 0;
|
|
25
|
+
--un-ring-offset-shadow: 0 0 #0000;
|
|
26
|
+
--un-ring-shadow: 0 0 #0000;
|
|
27
|
+
--un-shadow-inset: ;
|
|
28
|
+
--un-shadow: 0 0 #0000;
|
|
29
|
+
--un-ring-inset: ;
|
|
30
|
+
--un-ring-offset-width: 0px;
|
|
31
|
+
--un-ring-offset-color: #fff;
|
|
32
|
+
--un-ring-width: 0px;
|
|
33
|
+
--un-ring-color: #93c5fd80;
|
|
34
|
+
--un-blur: ;
|
|
35
|
+
--un-brightness: ;
|
|
36
|
+
--un-contrast: ;
|
|
37
|
+
--un-drop-shadow: ;
|
|
38
|
+
--un-grayscale: ;
|
|
39
|
+
--un-hue-rotate: ;
|
|
40
|
+
--un-invert: ;
|
|
41
|
+
--un-saturate: ;
|
|
42
|
+
--un-sepia: ;
|
|
43
|
+
--un-backdrop-blur: ;
|
|
44
|
+
--un-backdrop-brightness: ;
|
|
45
|
+
--un-backdrop-contrast: ;
|
|
46
|
+
--un-backdrop-grayscale: ;
|
|
47
|
+
--un-backdrop-hue-rotate: ;
|
|
48
|
+
--un-backdrop-invert: ;
|
|
49
|
+
--un-backdrop-opacity: ;
|
|
50
|
+
--un-backdrop-saturate: ;
|
|
51
|
+
--un-backdrop-sepia: ;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
.fairystaroform__absolute {
|
|
55
|
+
position: absolute;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.fairystaroform__relative {
|
|
59
|
+
position: relative;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.fairystaroform__bottom-_zkh1_-10px_zhk2_ {
|
|
63
|
+
bottom: -10px;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.fairystaroform__bottom-_zkh1_-14px_zhk2_ {
|
|
67
|
+
bottom: -14px;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
.fairystaroform__bottom-_zkh1_-2px_zhk2_ {
|
|
71
|
+
bottom: -2px;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
.fairystaroform__left-0 {
|
|
75
|
+
left: 0;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
.fairystaroform__right-0 {
|
|
79
|
+
right: 0;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
.fairystaroform__top-_zkh1_-4px_zhk2_ {
|
|
83
|
+
top: -4px;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
.fairystaroform__z-10 {
|
|
87
|
+
z-index: 10;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
.fairystaroform__grid {
|
|
91
|
+
display: grid;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
.fairystaroform__box-border {
|
|
95
|
+
box-sizing: border-box;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
.fairystaroform__h-full {
|
|
99
|
+
height: 100%;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.fairystaroform__min-h-_zkh1_32px_zhk2_ {
|
|
103
|
+
min-height: 32px;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.fairystaroform__w-full {
|
|
107
|
+
width: 100%;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
.fairystaroform__flex {
|
|
111
|
+
display: flex;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
.fairystaroform__flex-1 {
|
|
115
|
+
flex: 1;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
.fairystaroform__flex-row {
|
|
119
|
+
flex-direction: row;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
.fairystaroform__flex-col {
|
|
123
|
+
flex-direction: column;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
.fairystaroform__items-start {
|
|
127
|
+
align-items: flex-start;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.fairystaroform__items-center {
|
|
131
|
+
align-items: center;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
.fairystaroform__justify-start {
|
|
135
|
+
justify-content: flex-start;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
.fairystaroform__justify-end {
|
|
139
|
+
justify-content: flex-end;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
.fairystaroform__justify-center {
|
|
143
|
+
justify-content: center;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
.fairystaroform__justify-between {
|
|
147
|
+
justify-content: space-between;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
.fairystaroform__gap-_zkh1_4px_zhk2_ {
|
|
151
|
+
gap: 4px;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
.fairystaroform__gap-_zkh1_6px_zhk2_ {
|
|
155
|
+
gap: 6px;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
.fairystaroform__gap-_zkh1_8px_zhk2_ {
|
|
159
|
+
gap: 8px;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
.fairystaroform__break-all {
|
|
163
|
+
word-break: break-all;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
.fairystaroform__border {
|
|
167
|
+
border-width: 1px;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
.fairystaroform__border-b {
|
|
171
|
+
border-bottom-width: 1px;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
.fairystaroform__border-gray-200 {
|
|
175
|
+
--un-border-opacity: 1;
|
|
176
|
+
border-color: rgb(229 231 235 / var(--un-border-opacity));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
.fairystaroform__border-b-gray-200 {
|
|
180
|
+
--un-border-opacity: 1;
|
|
181
|
+
--un-border-bottom-opacity: var(--un-border-opacity);
|
|
182
|
+
border-bottom-color: rgb(229 231 235 / var(--un-border-bottom-opacity));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
.fairystaroform__rounded-_zkh1_8px_zhk2_ {
|
|
186
|
+
border-radius: 8px;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
.fairystaroform__border-solid {
|
|
190
|
+
border-style: solid;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
.fairystaroform__border-b-solid {
|
|
194
|
+
border-bottom-style: solid;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
.fairystaroform__p-_zkh1_4px_zhk2_ {
|
|
198
|
+
padding: 4px;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
.fairystaroform__px-_zkh1_4px_zhk2_ {
|
|
202
|
+
padding-left: 4px;
|
|
203
|
+
padding-right: 4px;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
.fairystaroform__px-_zkh1_8px_zhk2_ {
|
|
207
|
+
padding-left: 8px;
|
|
208
|
+
padding-right: 8px;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
.fairystaroform__py-_zkh1_10px_zhk2_ {
|
|
212
|
+
padding-top: 10px;
|
|
213
|
+
padding-bottom: 10px;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
.fairystaroform__pb-_zkh1_12px_zhk2_ {
|
|
217
|
+
padding-bottom: 12px;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
.fairystaroform__pt-_zkh1_8px_zhk2_ {
|
|
221
|
+
padding-top: 8px;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
.fairystaroform__text-left {
|
|
225
|
+
text-align: left;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
.fairystaroform__text-right {
|
|
229
|
+
text-align: right;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
.fairystaroform__text-_zkh1_10px_zhk2_ {
|
|
233
|
+
font-size: 10px;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
.fairystaroform__text-_zkh1_12px_zhk2_ {
|
|
237
|
+
font-size: 12px;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
.fairystaroform__text-_zkh1_14px_zhk2_ {
|
|
241
|
+
font-size: 14px;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
.fairystaroform__text-gray-800 {
|
|
245
|
+
--un-text-opacity: 1;
|
|
246
|
+
color: rgb(31 41 55 / var(--un-text-opacity));
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
.fairystaroform__text-red {
|
|
250
|
+
--un-text-opacity: 1;
|
|
251
|
+
color: rgb(248 113 113 / var(--un-text-opacity));
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
.fairystaroform__font-bold {
|
|
255
|
+
font-weight: 700;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
.fairystaroform__transition-all {
|
|
259
|
+
transition-property: all;
|
|
260
|
+
transition-duration: .15s;
|
|
261
|
+
transition-timing-function: cubic-bezier(.4, 0, .2, 1);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
.fairystaroform__duration-300 {
|
|
265
|
+
transition-duration: .3s;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
.fairys-valtio-form-item-label.show-colon:after {
|
|
269
|
+
content: ":";
|
|
270
|
+
text-align: center;
|
|
271
|
+
margin: 0;
|
|
272
|
+
box-sizing: border-box;
|
|
273
|
+
margin-inline: .1rem;
|
|
274
|
+
display: inline-block;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
.fairys-valtio-form-item-label.required:before {
|
|
278
|
+
content: "*";
|
|
279
|
+
color: red;
|
|
280
|
+
box-sizing: border-box;
|
|
281
|
+
margin-inline-end: .2rem;
|
|
282
|
+
display: inline-block;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
.fairys-valtio-form-layout.fairys-valtio-form-layout-all-col-span {
|
|
286
|
+
grid-column: 1 / -1;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
.fairys-valtio-form-layout.fairys-valtio-form-layout-box-shadow {
|
|
290
|
+
box-shadow: 0 6px 16px -8px #00000014, 0 9px 28px #0000000d, 0 12px 48px 16px #00000008;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
.fairys-valtio-form-layout + .fairys-valtio-form-layout {
|
|
294
|
+
margin-top: 12px;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
.fairys-valtio-form-layout-last-item-no-border > .fairys-valtio-form-layout-body > .fairys-valtio-form-item:last-child:not(.fairys-valtio-form-item-invalid-border-red), .fairys-valtio-form-layout-last-item-no-border > .fairys-valtio-form-layout-body > .fairys-valtio-form-item:last-child > .fairys-valtio-form-item-container > .fairys-valtio-form-item-body:not(.fairys-valtio-form-item-invalid-border-red) {
|
|
298
|
+
border-bottom: 0;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
.fairys-valtio-form-item-invalid-border-red {
|
|
302
|
+
border-bottom-color: red !important;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
.fairys-valtio-form-item-invalid-text-red > .fairys-valtio-form-item-container > .fairys-valtio-form-item-label, .fairys-valtio-form-item-invalid-text-red > .fairys-valtio-form-item-container > .fairys-valtio-form-item-body > .fairys-valtio-form-item-body-input {
|
|
306
|
+
color: red !important;
|
|
307
|
+
}
|
|
308
|
+
|
package/package.json
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
"name": "@fairys/valtio-form-basic",
|
|
3
3
|
"author": "SunLxy <1011771396@qq.com>",
|
|
4
4
|
"description": "使用 valtio 实现的表单基础库, 使其更加便捷,同时支持`PC`、`H5`、`Taro`,同时也更加灵活。",
|
|
5
|
-
"homepage": "https://github.com/autumn-fairy-tales/
|
|
6
|
-
"version": "0.0
|
|
5
|
+
"homepage": "https://github.com/autumn-fairy-tales/valtio-form-basic",
|
|
6
|
+
"version": "1.0.0",
|
|
7
7
|
"main": "lib/index.js",
|
|
8
8
|
"types": "esm/index.d.ts",
|
|
9
9
|
"module": "esm/index.js",
|
|
@@ -17,15 +17,15 @@
|
|
|
17
17
|
},
|
|
18
18
|
"repository": {
|
|
19
19
|
"type": "git",
|
|
20
|
-
"url": "git+https://github.com/autumn-fairy-tales/
|
|
20
|
+
"url": "git+https://github.com/autumn-fairy-tales/valtio-form-basic.git",
|
|
21
21
|
"directory": "packages/valtio-form-basic"
|
|
22
22
|
},
|
|
23
23
|
"files": [
|
|
24
24
|
"src",
|
|
25
|
-
"esm"
|
|
25
|
+
"esm",
|
|
26
|
+
"lib"
|
|
26
27
|
],
|
|
27
28
|
"dependencies": {
|
|
28
|
-
"@nutui/nutui-react-taro": "^3.0.18",
|
|
29
29
|
"async-validator": "~4.2.5",
|
|
30
30
|
"clsx": "2.1.1",
|
|
31
31
|
"fast-copy": "~4.0.2",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useRef, createContext, useContext, useMemo } from 'react';
|
|
2
2
|
import { proxy, useSnapshot } from 'valtio';
|
|
3
|
-
import { formatePath, formateName } from '
|
|
3
|
+
import { formatePath, formateName } from 'common/utils';
|
|
4
4
|
export interface FairysValtioFormParentAttrsState {
|
|
5
5
|
name?: string;
|
|
6
6
|
}
|
|
@@ -76,3 +76,11 @@ export const useFairysValtioFormAttrsName = (options: FairysValtioFormAttrsNameO
|
|
|
76
76
|
paths: _paths,
|
|
77
77
|
};
|
|
78
78
|
};
|
|
79
|
+
|
|
80
|
+
let localId = 0;
|
|
81
|
+
export const useId = (suffix: string = 'form-item-input') => {
|
|
82
|
+
const count = useRef(localId++);
|
|
83
|
+
return useMemo(() => {
|
|
84
|
+
return `fairys-valtio-form_${count.current.toString(32)}_${suffix}`;
|
|
85
|
+
}, [count.current, suffix]);
|
|
86
|
+
};
|
|
@@ -1,12 +1,42 @@
|
|
|
1
|
-
import { MObject } from 'interface';
|
|
1
|
+
import { MObject } from 'common/interface';
|
|
2
2
|
import { createContext, useContext, useRef } from 'react';
|
|
3
|
-
import { proxy, ref, snapshot, useSnapshot } from 'valtio';
|
|
3
|
+
import { proxy, ref, snapshot, useSnapshot, unstable_getInternalStates } from 'valtio';
|
|
4
4
|
import AsyncValidator, { RuleItem, ValidateFieldsError, Values } from 'async-validator';
|
|
5
5
|
import { copy } from 'fast-copy';
|
|
6
|
-
import { formatePath, get, set } from '
|
|
6
|
+
import { formatePath, get, set, isObject, removeValueByPaths } from 'common/utils';
|
|
7
|
+
import { FairysValtioFormAttrsProps } from 'form/form';
|
|
7
8
|
|
|
8
9
|
/**表单实例*/
|
|
9
10
|
export class FairysValtioFormInstance<T extends MObject<T> = Record<string, any>> {
|
|
11
|
+
/**
|
|
12
|
+
* 表单值改变时回调
|
|
13
|
+
* @param path 表单项路径
|
|
14
|
+
* @param value 表单项值
|
|
15
|
+
*/
|
|
16
|
+
onValuesChange?: (path: PropertyKey, value: any) => void;
|
|
17
|
+
/***
|
|
18
|
+
* 判断值是否为代理对象
|
|
19
|
+
* @param value 值
|
|
20
|
+
* @returns 是否为代理对象
|
|
21
|
+
*/
|
|
22
|
+
isValtioProxy = (value: any) => {
|
|
23
|
+
const { refSet } = unstable_getInternalStates();
|
|
24
|
+
const canProxyDefault = (x: unknown): boolean =>
|
|
25
|
+
isObject(x) &&
|
|
26
|
+
!refSet.has(x) &&
|
|
27
|
+
(Array.isArray(x) || !(Symbol.iterator in x)) &&
|
|
28
|
+
!(x instanceof WeakMap) &&
|
|
29
|
+
!(x instanceof WeakSet) &&
|
|
30
|
+
!(x instanceof Error) &&
|
|
31
|
+
!(x instanceof Number) &&
|
|
32
|
+
!(x instanceof Date) &&
|
|
33
|
+
!(x instanceof String) &&
|
|
34
|
+
!(x instanceof RegExp) &&
|
|
35
|
+
!(x instanceof ArrayBuffer) &&
|
|
36
|
+
!(x instanceof Promise);
|
|
37
|
+
return canProxyDefault(value);
|
|
38
|
+
};
|
|
39
|
+
|
|
10
40
|
/**状态*/
|
|
11
41
|
state = proxy<T>({} as T);
|
|
12
42
|
/**
|
|
@@ -19,18 +49,30 @@ export class FairysValtioFormInstance<T extends MObject<T> = Record<string, any>
|
|
|
19
49
|
ctor = (options?: {
|
|
20
50
|
formData?: Partial<T>;
|
|
21
51
|
hideState?: Record<PropertyKey, boolean>;
|
|
22
|
-
initFormDataType?: '
|
|
52
|
+
initFormDataType?: FairysValtioFormAttrsProps['initFormDataType'];
|
|
23
53
|
}) => {
|
|
24
54
|
const { formData, hideState, initFormDataType = 'deepCopy' } = options || {};
|
|
25
|
-
|
|
26
|
-
this.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
55
|
+
this.clear();
|
|
56
|
+
this.updatedHideInfo(hideState ? copy(hideState) : {});
|
|
57
|
+
// 判断是否是代理对象
|
|
58
|
+
const isValtioProxy = this.isValtioProxy(formData);
|
|
59
|
+
if (isValtioProxy) {
|
|
60
|
+
if (initFormDataType === 'deepCopy') {
|
|
61
|
+
// this.state = proxy(copy(snapshot(formData)) as T);
|
|
62
|
+
this.updated(copy(snapshot(formData)) as T, false);
|
|
63
|
+
} else {
|
|
64
|
+
// this.state = formData as T;
|
|
65
|
+
this.updated(formData as T, false);
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
if (initFormDataType === 'deepCopy') {
|
|
69
|
+
// this.state = proxy(copy(formData || {}) as T);
|
|
70
|
+
this.updated(copy(formData || {}) as T, false);
|
|
71
|
+
}
|
|
72
|
+
{
|
|
73
|
+
// this.state = proxy((formData || {}) as T);
|
|
74
|
+
this.updated((formData || {}) as T, false);
|
|
75
|
+
}
|
|
34
76
|
}
|
|
35
77
|
};
|
|
36
78
|
/**
|
|
@@ -56,7 +98,28 @@ export class FairysValtioFormInstance<T extends MObject<T> = Record<string, any>
|
|
|
56
98
|
updatedValueByPaths = (path: PropertyKey, value: any) => {
|
|
57
99
|
set(this.state, formatePath(path), value);
|
|
58
100
|
this.validate([path], false);
|
|
101
|
+
this.onValuesChange?.(path, value);
|
|
59
102
|
};
|
|
103
|
+
|
|
104
|
+
/**表单项卸载移除保存值*/
|
|
105
|
+
removeValueByPaths = (path: PropertyKey) => {
|
|
106
|
+
removeValueByPaths(this.state, formatePath(path));
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* 清理值
|
|
110
|
+
*/
|
|
111
|
+
clearValue = (fields?: PropertyKey[]) => {
|
|
112
|
+
let _fields = fields;
|
|
113
|
+
if (!Array.isArray(fields)) {
|
|
114
|
+
_fields = Object.keys(this.state) as PropertyKey[];
|
|
115
|
+
}
|
|
116
|
+
for (let index = 0; index < _fields.length; index++) {
|
|
117
|
+
const field = _fields[index];
|
|
118
|
+
delete this.state[field];
|
|
119
|
+
}
|
|
120
|
+
return this;
|
|
121
|
+
};
|
|
122
|
+
|
|
60
123
|
// ===================================================隐藏状态================================================================
|
|
61
124
|
/**
|
|
62
125
|
* 更新行数据的隐藏信息
|
|
@@ -119,9 +182,12 @@ export class FairysValtioFormInstance<T extends MObject<T> = Record<string, any>
|
|
|
119
182
|
* 清理所有数据
|
|
120
183
|
*/
|
|
121
184
|
clear = () => {
|
|
122
|
-
this.
|
|
123
|
-
this.
|
|
124
|
-
this.
|
|
185
|
+
this.clearValue();
|
|
186
|
+
this.clearHideInfo();
|
|
187
|
+
this.clearErrorInfo();
|
|
188
|
+
this.mountRules = {}; //由表单项挂载规则,(根据表单项的字段存储路径对应校验规则)
|
|
189
|
+
this.rules = {}; //表单项规则
|
|
190
|
+
this.nameToPaths = {}; //表单项名称到路径映射
|
|
125
191
|
};
|
|
126
192
|
|
|
127
193
|
// ===================================================规则处理================================================================
|
|
@@ -139,7 +205,10 @@ export class FairysValtioFormInstance<T extends MObject<T> = Record<string, any>
|
|
|
139
205
|
* @param fields 要验证的字段(可选)
|
|
140
206
|
* @param isReturn 是否返回验证结果(可选)
|
|
141
207
|
*/
|
|
142
|
-
validate = async (
|
|
208
|
+
validate = async (
|
|
209
|
+
fields?: PropertyKey[],
|
|
210
|
+
isReturn: boolean = true,
|
|
211
|
+
): Promise<ValidateFieldsError | Values | Partial<T>> => {
|
|
143
212
|
const rules = {
|
|
144
213
|
...this.rules,
|
|
145
214
|
...this.mountRules,
|
|
@@ -213,7 +282,9 @@ export class FairysValtioFormInstance<T extends MObject<T> = Record<string, any>
|
|
|
213
282
|
}
|
|
214
283
|
|
|
215
284
|
/**声明实例*/
|
|
216
|
-
export function useFairysValtioFormInstance<T extends MObject<T> =
|
|
285
|
+
export function useFairysValtioFormInstance<T extends MObject<T> = Record<string, any>>(
|
|
286
|
+
instance?: FairysValtioFormInstance<T>,
|
|
287
|
+
) {
|
|
217
288
|
const ref = useRef<FairysValtioFormInstance<T>>();
|
|
218
289
|
if (!ref.current) {
|
|
219
290
|
if (instance) {
|
|
@@ -231,12 +302,12 @@ export const FairysValtioFormInstanceContext = createContext<FairysValtioFormIns
|
|
|
231
302
|
);
|
|
232
303
|
|
|
233
304
|
/**表单实例上下文*/
|
|
234
|
-
export function useFairysValtioFormInstanceContext<T extends MObject<T> =
|
|
305
|
+
export function useFairysValtioFormInstanceContext<T extends MObject<T> = Record<string, any>>() {
|
|
235
306
|
return useContext(FairysValtioFormInstanceContext) as FairysValtioFormInstance<T>;
|
|
236
307
|
}
|
|
237
308
|
|
|
238
309
|
/**状态取值*/
|
|
239
|
-
export function useFairysValtioFormInstanceContextState<T extends MObject<T> =
|
|
310
|
+
export function useFairysValtioFormInstanceContextState<T extends MObject<T> = Record<string, any>>() {
|
|
240
311
|
const instance = useFairysValtioFormInstanceContext<T>();
|
|
241
312
|
const state = useSnapshot(instance.state) as T;
|
|
242
313
|
const errorState = useSnapshot(instance.errorState) as Record<PropertyKey, string[]>;
|
|
@@ -250,7 +321,7 @@ export function useFairysValtioFormInstanceContextState<T extends MObject<T> = o
|
|
|
250
321
|
}
|
|
251
322
|
|
|
252
323
|
/**隐藏组件状态取值*/
|
|
253
|
-
export function useFairysValtioFormInstanceContextHideState<T extends MObject<T> =
|
|
324
|
+
export function useFairysValtioFormInstanceContextHideState<T extends MObject<T> = Record<string, any>>() {
|
|
254
325
|
const instance = useFairysValtioFormInstanceContext<T>();
|
|
255
326
|
const hideState = useSnapshot(instance.hideState) as Record<PropertyKey, boolean>;
|
|
256
327
|
return [hideState, instance, (hideState as any).__defaultValue] as [
|
|
@@ -259,3 +330,21 @@ export function useFairysValtioFormInstanceContextHideState<T extends MObject<T>
|
|
|
259
330
|
any,
|
|
260
331
|
];
|
|
261
332
|
}
|
|
333
|
+
/**
|
|
334
|
+
* 传递表单实例获取状态
|
|
335
|
+
*/
|
|
336
|
+
export function useFairysValtioFormInstanceToState<T extends MObject<T> = Record<string, any>>(
|
|
337
|
+
formInstance: FairysValtioFormInstance<T>,
|
|
338
|
+
) {
|
|
339
|
+
const state = useSnapshot(formInstance.state) as T;
|
|
340
|
+
return state as T;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* 传递表单实例获取隐藏状态
|
|
344
|
+
*/
|
|
345
|
+
export function useFairysValtioFormInstanceToHideState<T extends MObject<T> = Record<string, any>>(
|
|
346
|
+
formInstance: FairysValtioFormInstance<T>,
|
|
347
|
+
) {
|
|
348
|
+
const hideState = useSnapshot(formInstance.hideState) as Record<PropertyKey, boolean>;
|
|
349
|
+
return hideState as Record<PropertyKey, boolean>;
|
|
350
|
+
}
|
|
@@ -50,6 +50,25 @@ export function get<TDefault = unknown>(value: any, segments: PropertyKey[]): TD
|
|
|
50
50
|
return current;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
/***
|
|
54
|
+
* 移除值
|
|
55
|
+
* @param value 任意值
|
|
56
|
+
* @param segments 键路径
|
|
57
|
+
*/
|
|
58
|
+
export function removeValueByPaths(value: any, segments: PropertyKey[]) {
|
|
59
|
+
// 移除字段
|
|
60
|
+
let current: any = value;
|
|
61
|
+
const lg = segments.length;
|
|
62
|
+
for (let index = 0; index < lg; index++) {
|
|
63
|
+
const key = segments[index];
|
|
64
|
+
if (index === lg - 1) {
|
|
65
|
+
delete current[key];
|
|
66
|
+
} else {
|
|
67
|
+
current = current?.[key];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
53
72
|
/***
|
|
54
73
|
* 格式化路径,将路径中的数组索引转换为数字
|
|
55
74
|
* @param path 路径
|
|
@@ -106,3 +125,5 @@ export function formateName(name?: string, parentName?: string) {
|
|
|
106
125
|
}
|
|
107
126
|
return '';
|
|
108
127
|
}
|
|
128
|
+
|
|
129
|
+
export const isObject = (x: unknown): x is object => typeof x === 'object' && x !== null;
|