@fairys/valtio-form-basic 1.0.0 → 1.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.
- package/README.md +40 -42
- package/esm/form/form.d.ts +1 -31
- package/esm/form/form.item.d.ts +28 -10
- package/esm/form/form.item.js +20 -1
- package/lib/form/form.d.ts +1 -31
- package/lib/form/form.item.d.ts +28 -10
- package/lib/form/form.item.js +20 -1
- package/package.json +1 -1
- package/src/form/form.item.tsx +61 -2
- package/src/form/form.tsx +5 -0
package/README.md
CHANGED
|
@@ -251,40 +251,9 @@ export const Form = (props: FormProps) => {
|
|
|
251
251
|
}
|
|
252
252
|
* ```
|
|
253
253
|
*/
|
|
254
|
-
export declare function useFairysValtioForm<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormAttrsProps<T>, ref: React.Ref<FairysValtioFormInstance<T>>): {
|
|
254
|
+
export declare function useFairysValtioForm<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormAttrsProps<T>, ref: React.Ref<FairysValtioFormInstance<T>>): Omit<FairysValtioFormAttrsProps<T>, "initFormDataType" | "form" | "rules" | "formData" | "hideState" | "onValuesChange"> & {
|
|
255
255
|
formInstance: FairysValtioFormInstance<T>;
|
|
256
|
-
/**子元素*/
|
|
257
|
-
children: ReactNode;
|
|
258
|
-
gap?: string | number;
|
|
259
|
-
title?: React.ReactNode;
|
|
260
|
-
extra?: React.ReactNode;
|
|
261
|
-
isAllColSpan?: boolean;
|
|
262
|
-
className?: string;
|
|
263
|
-
style?: React.CSSProperties;
|
|
264
|
-
headerClassName?: string;
|
|
265
|
-
headerStyle?: React.CSSProperties;
|
|
266
|
-
bodyClassName?: string;
|
|
267
|
-
bodyStyle?: React.CSSProperties;
|
|
268
|
-
bordered?: boolean;
|
|
269
|
-
boxShadow?: boolean;
|
|
270
|
-
lastItemBordered?: boolean;
|
|
271
|
-
platform?: "pc" | "rn" | "taro";
|
|
272
|
-
colCount?: number;
|
|
273
|
-
errorLayout?: "bottom-left" | "bottom-right" | "top-right" | "top-left" | "left-border-top" | "right-border-top";
|
|
274
|
-
labelMode?: "left" | "top" | "between";
|
|
275
|
-
formItemClassName?: string;
|
|
276
|
-
formItemStyle?: React.CSSProperties;
|
|
277
|
-
formItemLabelClassName?: string;
|
|
278
|
-
formItemLabelStyle?: React.CSSProperties;
|
|
279
|
-
formItemBodyClassName?: string;
|
|
280
|
-
formItemBodyStyle?: React.CSSProperties;
|
|
281
|
-
itemBorderType?: "bottom" | "body" | "none";
|
|
282
|
-
itemBorderColor?: React.CSSProperties["borderColor"];
|
|
283
|
-
isInvalidBorderRed?: boolean;
|
|
284
|
-
isInvalidTextRed?: boolean;
|
|
285
|
-
showColon?: boolean;
|
|
286
256
|
};
|
|
287
|
-
|
|
288
257
|
```
|
|
289
258
|
|
|
290
259
|
### formItem
|
|
@@ -369,6 +338,10 @@ export interface FairysValtioFormItemAttrsProps<T extends MObject<T> = Record<st
|
|
|
369
338
|
isJoinParentField?: boolean;
|
|
370
339
|
/**校验规则*/
|
|
371
340
|
rules?: RuleItem[];
|
|
341
|
+
/**卸载移除数据值
|
|
342
|
+
* @default true
|
|
343
|
+
*/
|
|
344
|
+
isRemoveValueOnUnmount?: boolean;
|
|
372
345
|
}
|
|
373
346
|
|
|
374
347
|
```
|
|
@@ -511,21 +484,40 @@ export const FormItem = (props: FormItemProps) => {
|
|
|
511
484
|
}
|
|
512
485
|
* ```
|
|
513
486
|
*/
|
|
514
|
-
export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>):
|
|
515
|
-
|
|
516
|
-
|
|
487
|
+
export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>): FairysValtioFormItemNoStyleAttrsReturn<T>;
|
|
488
|
+
export interface FairysValtioFormItemNoStyleAttrsReturn<T extends MObject<T> = Record<string, any>> {
|
|
489
|
+
/**表单项值*/
|
|
490
|
+
value?: any;
|
|
491
|
+
/**是否校验错误*/
|
|
492
|
+
isInvalid: boolean;
|
|
493
|
+
/**是否必填*/
|
|
494
|
+
isRequired: boolean;
|
|
495
|
+
/**错误信息*/
|
|
496
|
+
error?: string[];
|
|
497
|
+
/**值改变事件*/
|
|
517
498
|
onValueChange: (event: any) => void;
|
|
499
|
+
/**表单状态*/
|
|
518
500
|
state: T;
|
|
501
|
+
/**错误状态*/
|
|
519
502
|
errorState: Record<PropertyKey, string[]>;
|
|
503
|
+
/**表单实例*/
|
|
520
504
|
formInstance: FairysValtioFormInstance<T>;
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
505
|
+
/**拼接父级字段名后得到的表单项名称*/
|
|
506
|
+
_name?: string;
|
|
507
|
+
/**表单项名称*/
|
|
508
|
+
name?: string;
|
|
509
|
+
/**表单项ID*/
|
|
510
|
+
id?: string;
|
|
511
|
+
/**表单项路径*/
|
|
512
|
+
paths?: (string | number)[];
|
|
513
|
+
/**父级字段名*/
|
|
514
|
+
parentName?: string;
|
|
515
|
+
/**表单属性名实例*/
|
|
526
516
|
formAttrsNameInstance: FairysValtioFormParentAttrs;
|
|
527
|
-
|
|
528
|
-
|
|
517
|
+
/**子元素*/
|
|
518
|
+
children?: React.ReactNode;
|
|
519
|
+
}
|
|
520
|
+
|
|
529
521
|
```
|
|
530
522
|
|
|
531
523
|
### layout
|
|
@@ -730,6 +722,12 @@ export declare function get<TDefault = unknown>(value: any, segments: PropertyKe
|
|
|
730
722
|
* @returns 格式化后的路径
|
|
731
723
|
*/
|
|
732
724
|
export declare function formatePath(path: PropertyKey): (number | symbol)[] | (string | number)[];
|
|
725
|
+
/***
|
|
726
|
+
* 移除值
|
|
727
|
+
* @param value 任意值
|
|
728
|
+
* @param segments 键路径
|
|
729
|
+
*/
|
|
730
|
+
export declare function removeValueByPaths(value: any, segments: PropertyKey[]): void;
|
|
733
731
|
/**格式化属性名*/
|
|
734
732
|
export declare function formateName(name?: string, parentName?: string): string;
|
|
735
733
|
/***
|
package/esm/form/form.d.ts
CHANGED
|
@@ -47,36 +47,6 @@ export const Form = (props: FormProps) => {
|
|
|
47
47
|
}
|
|
48
48
|
* ```
|
|
49
49
|
*/
|
|
50
|
-
export declare function useFairysValtioForm<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormAttrsProps<T>, ref: React.Ref<FairysValtioFormInstance<T>>): {
|
|
50
|
+
export declare function useFairysValtioForm<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormAttrsProps<T>, ref: React.Ref<FairysValtioFormInstance<T>>): Omit<FairysValtioFormAttrsProps<T>, "initFormDataType" | "form" | "rules" | "formData" | "hideState" | "onValuesChange"> & {
|
|
51
51
|
formInstance: FairysValtioFormInstance<T>;
|
|
52
|
-
/**子元素*/
|
|
53
|
-
children: ReactNode;
|
|
54
|
-
gap?: string | number;
|
|
55
|
-
title?: React.ReactNode;
|
|
56
|
-
extra?: React.ReactNode;
|
|
57
|
-
isAllColSpan?: boolean;
|
|
58
|
-
className?: string;
|
|
59
|
-
style?: React.CSSProperties;
|
|
60
|
-
headerClassName?: string;
|
|
61
|
-
headerStyle?: React.CSSProperties;
|
|
62
|
-
bodyClassName?: string;
|
|
63
|
-
bodyStyle?: React.CSSProperties;
|
|
64
|
-
bordered?: boolean;
|
|
65
|
-
boxShadow?: boolean;
|
|
66
|
-
lastItemBordered?: boolean;
|
|
67
|
-
platform?: "pc" | "rn" | "taro";
|
|
68
|
-
colCount?: number;
|
|
69
|
-
errorLayout?: "bottom-left" | "bottom-right" | "top-right" | "top-left" | "left-border-top" | "right-border-top";
|
|
70
|
-
labelMode?: "left" | "top" | "between";
|
|
71
|
-
formItemClassName?: string;
|
|
72
|
-
formItemStyle?: React.CSSProperties;
|
|
73
|
-
formItemLabelClassName?: string;
|
|
74
|
-
formItemLabelStyle?: React.CSSProperties;
|
|
75
|
-
formItemBodyClassName?: string;
|
|
76
|
-
formItemBodyStyle?: React.CSSProperties;
|
|
77
|
-
itemBorderType?: "bottom" | "body" | "none";
|
|
78
|
-
itemBorderColor?: React.CSSProperties["borderColor"];
|
|
79
|
-
isInvalidBorderRed?: boolean;
|
|
80
|
-
isInvalidTextRed?: boolean;
|
|
81
|
-
showColon?: boolean;
|
|
82
52
|
};
|
package/esm/form/form.item.d.ts
CHANGED
|
@@ -211,18 +211,36 @@ export const FormItem = (props: FormItemProps) => {
|
|
|
211
211
|
}
|
|
212
212
|
* ```
|
|
213
213
|
*/
|
|
214
|
-
export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>):
|
|
215
|
-
|
|
216
|
-
|
|
214
|
+
export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>): FairysValtioFormItemNoStyleAttrsReturn<T>;
|
|
215
|
+
export interface FairysValtioFormItemNoStyleAttrsReturn<T extends MObject<T> = Record<string, any>> {
|
|
216
|
+
/**表单项值*/
|
|
217
|
+
value?: any;
|
|
218
|
+
/**是否校验错误*/
|
|
219
|
+
isInvalid: boolean;
|
|
220
|
+
/**是否必填*/
|
|
221
|
+
isRequired: boolean;
|
|
222
|
+
/**错误信息*/
|
|
223
|
+
error?: string[];
|
|
224
|
+
/**值改变事件*/
|
|
217
225
|
onValueChange: (event: any) => void;
|
|
226
|
+
/**表单状态*/
|
|
218
227
|
state: T;
|
|
228
|
+
/**错误状态*/
|
|
219
229
|
errorState: Record<PropertyKey, string[]>;
|
|
230
|
+
/**表单实例*/
|
|
220
231
|
formInstance: FairysValtioFormInstance<T>;
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
232
|
+
/**拼接父级字段名后得到的表单项名称*/
|
|
233
|
+
_name?: string;
|
|
234
|
+
/**表单项名称*/
|
|
235
|
+
name?: string;
|
|
236
|
+
/**表单项ID*/
|
|
237
|
+
id?: string;
|
|
238
|
+
/**表单项路径*/
|
|
239
|
+
paths?: (string | number)[];
|
|
240
|
+
/**父级字段名*/
|
|
241
|
+
parentName?: string;
|
|
242
|
+
/**表单属性名实例*/
|
|
226
243
|
formAttrsNameInstance: FairysValtioFormParentAttrs;
|
|
227
|
-
|
|
228
|
-
|
|
244
|
+
/**子元素*/
|
|
245
|
+
children?: React.ReactNode;
|
|
246
|
+
}
|
package/esm/form/form.item.js
CHANGED
|
@@ -216,7 +216,7 @@ function useFairysValtioFormItemAttrs(props) {
|
|
|
216
216
|
};
|
|
217
217
|
}
|
|
218
218
|
function useFairysValtioFormItemNoStyleAttrs(props) {
|
|
219
|
-
const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', children, attrs = {}, isJoinParentField = true, rules, isRemoveValueOnUnmount = true } = props;
|
|
219
|
+
const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', children, attrs = {}, isJoinParentField = true, rules, isRemoveValueOnUnmount = true, isRequired: _isRequired } = props;
|
|
220
220
|
const [state, errorState, formInstance] = useFairysValtioFormInstanceContextState();
|
|
221
221
|
const { name: _name, paths, parentName, formAttrsNameInstance } = useFairysValtioFormAttrsName({
|
|
222
222
|
name,
|
|
@@ -229,6 +229,7 @@ function useFairysValtioFormItemNoStyleAttrs(props) {
|
|
|
229
229
|
]);
|
|
230
230
|
const error = errorState[_name];
|
|
231
231
|
formInstance.nameToPaths[_name] = paths;
|
|
232
|
+
const _formItemRules = formInstance.rules?.[_name];
|
|
232
233
|
useEffect(()=>{
|
|
233
234
|
if (Array.isArray(rules) && rules.length) formInstance.mountRules[_name] = rules;
|
|
234
235
|
return ()=>{
|
|
@@ -258,8 +259,26 @@ function useFairysValtioFormItemNoStyleAttrs(props) {
|
|
|
258
259
|
[valuePropName]: value,
|
|
259
260
|
[trigger]: onValueChange
|
|
260
261
|
};
|
|
262
|
+
const isRequired = useMemo(()=>{
|
|
263
|
+
if (_isRequired) return _isRequired;
|
|
264
|
+
if (Array.isArray(rules) && rules.length) return rules.some((rule)=>rule.required);
|
|
265
|
+
if (_formItemRules && Array.isArray(_formItemRules) && _formItemRules.length) return _formItemRules.some((rule)=>rule.required);
|
|
266
|
+
return false;
|
|
267
|
+
}, [
|
|
268
|
+
rules,
|
|
269
|
+
formInstance,
|
|
270
|
+
_formItemRules
|
|
271
|
+
]);
|
|
272
|
+
const isInvalid = useMemo(()=>{
|
|
273
|
+
if (Array.isArray(error) && error.length) return true;
|
|
274
|
+
return false;
|
|
275
|
+
}, [
|
|
276
|
+
error
|
|
277
|
+
]);
|
|
261
278
|
return {
|
|
262
279
|
value,
|
|
280
|
+
isInvalid,
|
|
281
|
+
isRequired,
|
|
263
282
|
error,
|
|
264
283
|
onValueChange,
|
|
265
284
|
state,
|
package/lib/form/form.d.ts
CHANGED
|
@@ -47,36 +47,6 @@ export const Form = (props: FormProps) => {
|
|
|
47
47
|
}
|
|
48
48
|
* ```
|
|
49
49
|
*/
|
|
50
|
-
export declare function useFairysValtioForm<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormAttrsProps<T>, ref: React.Ref<FairysValtioFormInstance<T>>): {
|
|
50
|
+
export declare function useFairysValtioForm<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormAttrsProps<T>, ref: React.Ref<FairysValtioFormInstance<T>>): Omit<FairysValtioFormAttrsProps<T>, "initFormDataType" | "form" | "rules" | "formData" | "hideState" | "onValuesChange"> & {
|
|
51
51
|
formInstance: FairysValtioFormInstance<T>;
|
|
52
|
-
/**子元素*/
|
|
53
|
-
children: ReactNode;
|
|
54
|
-
gap?: string | number;
|
|
55
|
-
title?: React.ReactNode;
|
|
56
|
-
extra?: React.ReactNode;
|
|
57
|
-
isAllColSpan?: boolean;
|
|
58
|
-
className?: string;
|
|
59
|
-
style?: React.CSSProperties;
|
|
60
|
-
headerClassName?: string;
|
|
61
|
-
headerStyle?: React.CSSProperties;
|
|
62
|
-
bodyClassName?: string;
|
|
63
|
-
bodyStyle?: React.CSSProperties;
|
|
64
|
-
bordered?: boolean;
|
|
65
|
-
boxShadow?: boolean;
|
|
66
|
-
lastItemBordered?: boolean;
|
|
67
|
-
platform?: "pc" | "rn" | "taro";
|
|
68
|
-
colCount?: number;
|
|
69
|
-
errorLayout?: "bottom-left" | "bottom-right" | "top-right" | "top-left" | "left-border-top" | "right-border-top";
|
|
70
|
-
labelMode?: "left" | "top" | "between";
|
|
71
|
-
formItemClassName?: string;
|
|
72
|
-
formItemStyle?: React.CSSProperties;
|
|
73
|
-
formItemLabelClassName?: string;
|
|
74
|
-
formItemLabelStyle?: React.CSSProperties;
|
|
75
|
-
formItemBodyClassName?: string;
|
|
76
|
-
formItemBodyStyle?: React.CSSProperties;
|
|
77
|
-
itemBorderType?: "bottom" | "body" | "none";
|
|
78
|
-
itemBorderColor?: React.CSSProperties["borderColor"];
|
|
79
|
-
isInvalidBorderRed?: boolean;
|
|
80
|
-
isInvalidTextRed?: boolean;
|
|
81
|
-
showColon?: boolean;
|
|
82
52
|
};
|
package/lib/form/form.item.d.ts
CHANGED
|
@@ -211,18 +211,36 @@ export const FormItem = (props: FormItemProps) => {
|
|
|
211
211
|
}
|
|
212
212
|
* ```
|
|
213
213
|
*/
|
|
214
|
-
export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>):
|
|
215
|
-
|
|
216
|
-
|
|
214
|
+
export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>): FairysValtioFormItemNoStyleAttrsReturn<T>;
|
|
215
|
+
export interface FairysValtioFormItemNoStyleAttrsReturn<T extends MObject<T> = Record<string, any>> {
|
|
216
|
+
/**表单项值*/
|
|
217
|
+
value?: any;
|
|
218
|
+
/**是否校验错误*/
|
|
219
|
+
isInvalid: boolean;
|
|
220
|
+
/**是否必填*/
|
|
221
|
+
isRequired: boolean;
|
|
222
|
+
/**错误信息*/
|
|
223
|
+
error?: string[];
|
|
224
|
+
/**值改变事件*/
|
|
217
225
|
onValueChange: (event: any) => void;
|
|
226
|
+
/**表单状态*/
|
|
218
227
|
state: T;
|
|
228
|
+
/**错误状态*/
|
|
219
229
|
errorState: Record<PropertyKey, string[]>;
|
|
230
|
+
/**表单实例*/
|
|
220
231
|
formInstance: FairysValtioFormInstance<T>;
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
232
|
+
/**拼接父级字段名后得到的表单项名称*/
|
|
233
|
+
_name?: string;
|
|
234
|
+
/**表单项名称*/
|
|
235
|
+
name?: string;
|
|
236
|
+
/**表单项ID*/
|
|
237
|
+
id?: string;
|
|
238
|
+
/**表单项路径*/
|
|
239
|
+
paths?: (string | number)[];
|
|
240
|
+
/**父级字段名*/
|
|
241
|
+
parentName?: string;
|
|
242
|
+
/**表单属性名实例*/
|
|
226
243
|
formAttrsNameInstance: FairysValtioFormParentAttrs;
|
|
227
|
-
|
|
228
|
-
|
|
244
|
+
/**子元素*/
|
|
245
|
+
children?: React.ReactNode;
|
|
246
|
+
}
|
package/lib/form/form.item.js
CHANGED
|
@@ -256,7 +256,7 @@ function useFairysValtioFormItemAttrs(props) {
|
|
|
256
256
|
};
|
|
257
257
|
}
|
|
258
258
|
function useFairysValtioFormItemNoStyleAttrs(props) {
|
|
259
|
-
const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', children, attrs = {}, isJoinParentField = true, rules, isRemoveValueOnUnmount = true } = props;
|
|
259
|
+
const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', children, attrs = {}, isJoinParentField = true, rules, isRemoveValueOnUnmount = true, isRequired: _isRequired } = props;
|
|
260
260
|
const [state, errorState, formInstance] = (0, index_js_namespaceObject.useFairysValtioFormInstanceContextState)();
|
|
261
261
|
const { name: _name, paths, parentName, formAttrsNameInstance } = (0, hooks_index_js_namespaceObject.useFairysValtioFormAttrsName)({
|
|
262
262
|
name,
|
|
@@ -269,6 +269,7 @@ function useFairysValtioFormItemNoStyleAttrs(props) {
|
|
|
269
269
|
]);
|
|
270
270
|
const error = errorState[_name];
|
|
271
271
|
formInstance.nameToPaths[_name] = paths;
|
|
272
|
+
const _formItemRules = formInstance.rules?.[_name];
|
|
272
273
|
(0, external_react_namespaceObject.useEffect)(()=>{
|
|
273
274
|
if (Array.isArray(rules) && rules.length) formInstance.mountRules[_name] = rules;
|
|
274
275
|
return ()=>{
|
|
@@ -298,8 +299,26 @@ function useFairysValtioFormItemNoStyleAttrs(props) {
|
|
|
298
299
|
[valuePropName]: value,
|
|
299
300
|
[trigger]: onValueChange
|
|
300
301
|
};
|
|
302
|
+
const isRequired = (0, external_react_namespaceObject.useMemo)(()=>{
|
|
303
|
+
if (_isRequired) return _isRequired;
|
|
304
|
+
if (Array.isArray(rules) && rules.length) return rules.some((rule)=>rule.required);
|
|
305
|
+
if (_formItemRules && Array.isArray(_formItemRules) && _formItemRules.length) return _formItemRules.some((rule)=>rule.required);
|
|
306
|
+
return false;
|
|
307
|
+
}, [
|
|
308
|
+
rules,
|
|
309
|
+
formInstance,
|
|
310
|
+
_formItemRules
|
|
311
|
+
]);
|
|
312
|
+
const isInvalid = (0, external_react_namespaceObject.useMemo)(()=>{
|
|
313
|
+
if (Array.isArray(error) && error.length) return true;
|
|
314
|
+
return false;
|
|
315
|
+
}, [
|
|
316
|
+
error
|
|
317
|
+
]);
|
|
301
318
|
return {
|
|
302
319
|
value,
|
|
320
|
+
isInvalid,
|
|
321
|
+
isRequired,
|
|
303
322
|
error,
|
|
304
323
|
onValueChange,
|
|
305
324
|
state,
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"author": "SunLxy <1011771396@qq.com>",
|
|
4
4
|
"description": "使用 valtio 实现的表单基础库, 使其更加便捷,同时支持`PC`、`H5`、`Taro`,同时也更加灵活。",
|
|
5
5
|
"homepage": "https://github.com/autumn-fairy-tales/valtio-form-basic",
|
|
6
|
-
"version": "1.0.
|
|
6
|
+
"version": "1.0.1",
|
|
7
7
|
"main": "lib/index.js",
|
|
8
8
|
"types": "esm/index.d.ts",
|
|
9
9
|
"module": "esm/index.js",
|
package/src/form/form.item.tsx
CHANGED
|
@@ -81,7 +81,6 @@ export interface FairysValtioFormItemAttrsProps<T extends MObject<T> = Record<st
|
|
|
81
81
|
isJoinParentField?: boolean;
|
|
82
82
|
/**校验规则*/
|
|
83
83
|
rules?: RuleItem[];
|
|
84
|
-
|
|
85
84
|
/**卸载移除数据值
|
|
86
85
|
* @default true
|
|
87
86
|
*/
|
|
@@ -539,6 +538,7 @@ export function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Recor
|
|
|
539
538
|
isJoinParentField = true,
|
|
540
539
|
rules,
|
|
541
540
|
isRemoveValueOnUnmount = true,
|
|
541
|
+
isRequired: _isRequired,
|
|
542
542
|
} = props;
|
|
543
543
|
const [state, errorState, formInstance] = useFairysValtioFormInstanceContextState<T>();
|
|
544
544
|
const {
|
|
@@ -551,6 +551,8 @@ export function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Recor
|
|
|
551
551
|
const value = useMemo(() => get(state, paths), [state, paths]);
|
|
552
552
|
const error = errorState[_name];
|
|
553
553
|
formInstance.nameToPaths[_name] = paths;
|
|
554
|
+
// 使用从 Form 中设置的规则
|
|
555
|
+
const _formItemRules = formInstance.rules?.[_name];
|
|
554
556
|
|
|
555
557
|
useEffect(() => {
|
|
556
558
|
if (Array.isArray(rules) && rules.length) {
|
|
@@ -580,6 +582,7 @@ export function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Recor
|
|
|
580
582
|
onAfterUpdate(_value, formInstance, event);
|
|
581
583
|
}
|
|
582
584
|
};
|
|
585
|
+
|
|
583
586
|
useEffect(() => {
|
|
584
587
|
return () => {
|
|
585
588
|
if (isRemoveValueOnUnmount) {
|
|
@@ -587,6 +590,7 @@ export function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Recor
|
|
|
587
590
|
}
|
|
588
591
|
};
|
|
589
592
|
}, []);
|
|
593
|
+
|
|
590
594
|
/**基础组件参数*/
|
|
591
595
|
const baseControl = {
|
|
592
596
|
...attrs,
|
|
@@ -595,8 +599,30 @@ export function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Recor
|
|
|
595
599
|
[valuePropName]: value,
|
|
596
600
|
[trigger]: onValueChange,
|
|
597
601
|
};
|
|
602
|
+
|
|
603
|
+
/**判断是否必填*/
|
|
604
|
+
const isRequired = useMemo(() => {
|
|
605
|
+
if (_isRequired) {
|
|
606
|
+
return _isRequired;
|
|
607
|
+
} else if (Array.isArray(rules) && rules.length) {
|
|
608
|
+
return rules.some((rule) => rule.required);
|
|
609
|
+
} else if (_formItemRules && Array.isArray(_formItemRules) && _formItemRules.length) {
|
|
610
|
+
return _formItemRules.some((rule) => rule.required);
|
|
611
|
+
}
|
|
612
|
+
return false;
|
|
613
|
+
}, [rules, formInstance, _formItemRules]);
|
|
614
|
+
|
|
615
|
+
/**校验是否存在错误信息*/
|
|
616
|
+
const isInvalid = useMemo(() => {
|
|
617
|
+
if (Array.isArray(error) && error.length) {
|
|
618
|
+
return true;
|
|
619
|
+
}
|
|
620
|
+
return false;
|
|
621
|
+
}, [error]);
|
|
598
622
|
return {
|
|
599
623
|
value,
|
|
624
|
+
isInvalid,
|
|
625
|
+
isRequired,
|
|
600
626
|
error,
|
|
601
627
|
onValueChange,
|
|
602
628
|
state,
|
|
@@ -609,5 +635,38 @@ export function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Recor
|
|
|
609
635
|
parentName,
|
|
610
636
|
formAttrsNameInstance,
|
|
611
637
|
children: React.isValidElement(children) ? React.cloneElement(children, { ...baseControl }) : children,
|
|
612
|
-
}
|
|
638
|
+
} as FairysValtioFormItemNoStyleAttrsReturn<T>;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
export interface FairysValtioFormItemNoStyleAttrsReturn<T extends MObject<T> = Record<string, any>> {
|
|
642
|
+
/**表单项值*/
|
|
643
|
+
value?: any;
|
|
644
|
+
/**是否校验错误*/
|
|
645
|
+
isInvalid: boolean;
|
|
646
|
+
/**是否必填*/
|
|
647
|
+
isRequired: boolean;
|
|
648
|
+
/**错误信息*/
|
|
649
|
+
error?: string[];
|
|
650
|
+
/**值改变事件*/
|
|
651
|
+
onValueChange: (event: any) => void;
|
|
652
|
+
/**表单状态*/
|
|
653
|
+
state: T;
|
|
654
|
+
/**错误状态*/
|
|
655
|
+
errorState: Record<PropertyKey, string[]>;
|
|
656
|
+
/**表单实例*/
|
|
657
|
+
formInstance: FairysValtioFormInstance<T>;
|
|
658
|
+
/**拼接父级字段名后得到的表单项名称*/
|
|
659
|
+
_name?: string;
|
|
660
|
+
/**表单项名称*/
|
|
661
|
+
name?: string;
|
|
662
|
+
/**表单项ID*/
|
|
663
|
+
id?: string;
|
|
664
|
+
/**表单项路径*/
|
|
665
|
+
paths?: (string | number)[];
|
|
666
|
+
/**父级字段名*/
|
|
667
|
+
parentName?: string;
|
|
668
|
+
/**表单属性名实例*/
|
|
669
|
+
formAttrsNameInstance: FairysValtioFormParentAttrs;
|
|
670
|
+
/**子元素*/
|
|
671
|
+
children?: React.ReactNode;
|
|
613
672
|
}
|
package/src/form/form.tsx
CHANGED
|
@@ -68,5 +68,10 @@ export function useFairysValtioForm<T extends MObject<T> = Record<string, any>>(
|
|
|
68
68
|
return {
|
|
69
69
|
...rest,
|
|
70
70
|
formInstance,
|
|
71
|
+
} as Omit<
|
|
72
|
+
FairysValtioFormAttrsProps<T>,
|
|
73
|
+
'initFormDataType' | 'form' | 'rules' | 'formData' | 'hideState' | 'onValuesChange'
|
|
74
|
+
> & {
|
|
75
|
+
formInstance: FairysValtioFormInstance<T>;
|
|
71
76
|
};
|
|
72
77
|
}
|