@fairys/valtio-form-basic 0.0.12 → 0.0.13

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 (48) hide show
  1. package/esm/{form → common}/hooks/index.d.ts +1 -0
  2. package/esm/{form → common}/hooks/index.js +9 -1
  3. package/esm/common/index.d.ts +4 -0
  4. package/esm/common/index.js +4 -0
  5. package/esm/{form → common}/instance/index.d.ts +9 -2
  6. package/esm/{form → common}/instance/index.js +19 -5
  7. package/esm/{form → common}/utils/index.d.ts +1 -0
  8. package/esm/{form → common}/utils/index.js +2 -1
  9. package/esm/form/form.d.ts +7 -7
  10. package/esm/form/form.item.d.ts +10 -3
  11. package/esm/form/form.item.js +50 -32
  12. package/esm/form/form.js +5 -3
  13. package/esm/form/layout.d.ts +4 -2591
  14. package/esm/form/layout.js +7 -4
  15. package/esm/index.d.ts +1 -2
  16. package/esm/index.js +1 -2
  17. package/esm/styles/index.css +20 -4
  18. package/lib/common/hooks/index.d.ts +35 -0
  19. package/lib/common/hooks/index.js +117 -0
  20. package/lib/common/index.d.ts +4 -0
  21. package/lib/common/index.js +87 -0
  22. package/lib/common/instance/index.d.ts +88 -0
  23. package/lib/common/instance/index.js +243 -0
  24. package/lib/common/interface.d.ts +4 -0
  25. package/lib/common/interface.js +18 -0
  26. package/lib/common/utils/index.d.ts +25 -0
  27. package/lib/common/utils/index.js +107 -0
  28. package/lib/form/form.d.ts +76 -0
  29. package/lib/form/form.item.d.ts +224 -0
  30. package/lib/form/form.item.js +321 -0
  31. package/lib/form/form.js +53 -0
  32. package/lib/form/layout.d.ts +153 -0
  33. package/lib/form/layout.js +196 -0
  34. package/lib/index.d.ts +4 -0
  35. package/lib/index.js +18 -27
  36. package/lib/styles/index.css +308 -0
  37. package/package.json +5 -5
  38. package/src/{form → common}/hooks/index.tsx +9 -1
  39. package/src/common/index.ts +4 -0
  40. package/src/{form → common}/instance/index.ts +51 -13
  41. package/src/{form → common}/utils/index.ts +2 -0
  42. package/src/form/form.item.tsx +59 -36
  43. package/src/form/form.tsx +12 -7
  44. package/src/form/layout.tsx +9 -3
  45. package/src/index.tsx +4 -5
  46. /package/esm/{interface.d.ts → common/interface.d.ts} +0 -0
  47. /package/esm/{interface.js → common/interface.js} +0 -0
  48. /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/fairys-taro-react",
6
- "version": "0.0.12",
5
+ "homepage": "https://github.com/autumn-fairy-tales/valtio-form-basic",
6
+ "version": "0.0.13",
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/fairys-taro-react.git",
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 'form/utils';
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
+ };
@@ -0,0 +1,4 @@
1
+ export * from './hooks';
2
+ export * from './instance';
3
+ export * from './utils';
4
+ export * from './interface';
@@ -1,12 +1,36 @@
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 'form/utils';
6
+ import { formatePath, get, set, isObject } 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 value 值
14
+ * @returns 是否为代理对象
15
+ */
16
+ isValtioProxy = (value: any) => {
17
+ const { refSet } = unstable_getInternalStates();
18
+ const canProxyDefault = (x: unknown): boolean =>
19
+ isObject(x) &&
20
+ !refSet.has(x) &&
21
+ (Array.isArray(x) || !(Symbol.iterator in x)) &&
22
+ !(x instanceof WeakMap) &&
23
+ !(x instanceof WeakSet) &&
24
+ !(x instanceof Error) &&
25
+ !(x instanceof Number) &&
26
+ !(x instanceof Date) &&
27
+ !(x instanceof String) &&
28
+ !(x instanceof RegExp) &&
29
+ !(x instanceof ArrayBuffer) &&
30
+ !(x instanceof Promise);
31
+ return canProxyDefault(value);
32
+ };
33
+
10
34
  /**状态*/
11
35
  state = proxy<T>({} as T);
12
36
  /**
@@ -19,18 +43,29 @@ export class FairysValtioFormInstance<T extends MObject<T> = Record<string, any>
19
43
  ctor = (options?: {
20
44
  formData?: Partial<T>;
21
45
  hideState?: Record<PropertyKey, boolean>;
22
- initFormDataType?: 'deepCopy' | 'proxy' | 'immutable';
46
+ initFormDataType?: FairysValtioFormAttrsProps['initFormDataType'];
23
47
  }) => {
24
48
  const { formData, hideState, initFormDataType = 'deepCopy' } = options || {};
49
+ this.mountRules = {}; //由表单项挂载规则,(根据表单项的字段存储路径对应校验规则)
50
+ this.nameToPaths = {}; //表单项名称到路径映射
25
51
  // 如果是 isProxy,则直接赋值
26
52
  this.errorState = proxy<Record<PropertyKey, string[]>>({});
27
53
  this.hideState = proxy<Record<PropertyKey, boolean>>(hideState ? copy(hideState) : {});
28
- if (initFormDataType === 'deepCopy') {
29
- this.state = proxy((formData ? copy(formData) : {}) as T);
30
- } else if (initFormDataType === 'proxy') {
31
- this.state = proxy(formData as T);
32
- } else if (initFormDataType === 'immutable') {
33
- this.state = formData as T;
54
+ // 判断是否是代理对象
55
+ const isValtioProxy = this.isValtioProxy(formData);
56
+ if (isValtioProxy) {
57
+ if (initFormDataType === 'deepCopy') {
58
+ this.state = proxy(copy(snapshot(formData)) as T);
59
+ } else {
60
+ this.state = formData as T;
61
+ }
62
+ } else {
63
+ if (initFormDataType === 'deepCopy') {
64
+ this.state = proxy(copy(formData || {}) as T);
65
+ }
66
+ {
67
+ this.state = proxy((formData || {}) as T);
68
+ }
34
69
  }
35
70
  };
36
71
  /**
@@ -119,9 +154,12 @@ export class FairysValtioFormInstance<T extends MObject<T> = Record<string, any>
119
154
  * 清理所有数据
120
155
  */
121
156
  clear = () => {
122
- this.state = proxy<T>({} as T);
123
- this.errorState = proxy<Record<PropertyKey, string[]>>({});
124
- this.hideState = proxy<Record<PropertyKey, boolean>>({});
157
+ this.state = proxy<T>({} as T); //表单值
158
+ this.errorState = proxy<Record<PropertyKey, string[]>>({}); //错误信息
159
+ this.hideState = proxy<Record<PropertyKey, boolean>>({}); //隐藏状态
160
+ this.mountRules = {}; //由表单项挂载规则,(根据表单项的字段存储路径对应校验规则)
161
+ this.rules = {}; //表单项规则
162
+ this.nameToPaths = {}; //表单项名称到路径映射
125
163
  };
126
164
 
127
165
  // ===================================================规则处理================================================================
@@ -106,3 +106,5 @@ export function formateName(name?: string, parentName?: string) {
106
106
  }
107
107
  return '';
108
108
  }
109
+
110
+ export const isObject = (x: unknown): x is object => typeof x === 'object' && x !== null;