@fairys/valtio-form-basic 1.0.0 → 1.0.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 +40 -42
- package/esm/form/form.d.ts +1 -31
- package/esm/form/form.item.d.ts +34 -10
- package/esm/form/form.item.js +33 -3
- package/lib/form/form.d.ts +1 -31
- package/lib/form/form.item.d.ts +34 -10
- package/lib/form/form.item.js +36 -3
- package/package.json +1 -1
- package/src/form/form.item.tsx +83 -4
- 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
|
@@ -81,6 +81,8 @@ export interface FairysValtioFormItemAttrsProps<T extends MObject<T> = Record<st
|
|
|
81
81
|
* @default true
|
|
82
82
|
*/
|
|
83
83
|
isRemoveValueOnUnmount?: boolean;
|
|
84
|
+
/**隐藏表单项移除数据*/
|
|
85
|
+
isHideRemoveValue?: boolean;
|
|
84
86
|
}
|
|
85
87
|
/**
|
|
86
88
|
* 处理表单表单项属性
|
|
@@ -211,18 +213,40 @@ export const FormItem = (props: FormItemProps) => {
|
|
|
211
213
|
}
|
|
212
214
|
* ```
|
|
213
215
|
*/
|
|
214
|
-
export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>):
|
|
215
|
-
|
|
216
|
-
|
|
216
|
+
export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>): FairysValtioFormItemNoStyleAttrsReturn<T>;
|
|
217
|
+
export interface FairysValtioFormItemNoStyleAttrsReturn<T extends MObject<T> = Record<string, any>> {
|
|
218
|
+
/**表单项值*/
|
|
219
|
+
value?: any;
|
|
220
|
+
/**是否校验错误*/
|
|
221
|
+
isInvalid: boolean;
|
|
222
|
+
/**是否必填*/
|
|
223
|
+
isRequired: boolean;
|
|
224
|
+
/**错误信息*/
|
|
225
|
+
error?: string[];
|
|
226
|
+
/**值改变事件*/
|
|
217
227
|
onValueChange: (event: any) => void;
|
|
228
|
+
/**表单状态*/
|
|
218
229
|
state: T;
|
|
230
|
+
/**错误状态*/
|
|
219
231
|
errorState: Record<PropertyKey, string[]>;
|
|
232
|
+
/**表单实例*/
|
|
220
233
|
formInstance: FairysValtioFormInstance<T>;
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
234
|
+
/**拼接父级字段名后得到的表单项名称*/
|
|
235
|
+
_name?: string;
|
|
236
|
+
/**表单项名称*/
|
|
237
|
+
name?: string;
|
|
238
|
+
/**表单项ID*/
|
|
239
|
+
id?: string;
|
|
240
|
+
/**表单项路径*/
|
|
241
|
+
paths?: (string | number)[];
|
|
242
|
+
/**父级字段名*/
|
|
243
|
+
parentName?: string;
|
|
244
|
+
/**表单属性名实例*/
|
|
226
245
|
formAttrsNameInstance: FairysValtioFormParentAttrs;
|
|
227
|
-
|
|
228
|
-
|
|
246
|
+
/**子元素*/
|
|
247
|
+
children?: React.ReactNode;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* 处理表单表单项属性,隐藏表单项时移除数据值
|
|
251
|
+
*/
|
|
252
|
+
export declare function useFairysValtioFormItemHideAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>): void;
|
package/esm/form/form.item.js
CHANGED
|
@@ -21,7 +21,7 @@ function useFairysValtioFormItemAttrs(props) {
|
|
|
21
21
|
const parent_isInvalidTextRed = layoutAttrs.isInvalidTextRed;
|
|
22
22
|
const parent_showColon = layoutAttrs.showColon;
|
|
23
23
|
const parent_platform = layoutAttrs.platform;
|
|
24
|
-
const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', className, style, labelClassName, labelStyle, bodyClassName, bodyStyle, children, labelMode = parent_labelMode, errorLayout = parent_errorLayout, colSpan = 1, rowSpan = 1, isRequired: _isRequired, itemBorderType = parent_borderedType, attrs = {}, showColon = parent_showColon, itemBorderColor = parent_itemBorderColor, isInvalidBorderRed = parent_isInvalidBorderRed, isInvalidTextRed = parent_isInvalidTextRed, isJoinParentField = true, rules, platform = parent_platform, isRemoveValueOnUnmount =
|
|
24
|
+
const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', className, style, labelClassName, labelStyle, bodyClassName, bodyStyle, children, labelMode = parent_labelMode, errorLayout = parent_errorLayout, colSpan = 1, rowSpan = 1, isRequired: _isRequired, itemBorderType = parent_borderedType, attrs = {}, showColon = parent_showColon, itemBorderColor = parent_itemBorderColor, isInvalidBorderRed = parent_isInvalidBorderRed, isInvalidTextRed = parent_isInvalidTextRed, isJoinParentField = true, rules, platform = parent_platform, isRemoveValueOnUnmount = false } = props;
|
|
25
25
|
const { name: _name, paths, parentName, formAttrsNameInstance } = useFairysValtioFormAttrsName({
|
|
26
26
|
name,
|
|
27
27
|
isJoinParentField
|
|
@@ -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 =
|
|
219
|
+
const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', children, attrs = {}, isJoinParentField = true, rules, isRemoveValueOnUnmount = false, 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,
|
|
@@ -276,4 +295,15 @@ function useFairysValtioFormItemNoStyleAttrs(props) {
|
|
|
276
295
|
}) : children
|
|
277
296
|
};
|
|
278
297
|
}
|
|
279
|
-
|
|
298
|
+
function useFairysValtioFormItemHideAttrs(props) {
|
|
299
|
+
const { name, isJoinParentField = true, isHideRemoveValue = true } = props;
|
|
300
|
+
const [, , formInstance] = useFairysValtioFormInstanceContextState();
|
|
301
|
+
const { name: _name } = useFairysValtioFormAttrsName({
|
|
302
|
+
name,
|
|
303
|
+
isJoinParentField
|
|
304
|
+
});
|
|
305
|
+
useEffect(()=>()=>{
|
|
306
|
+
if (isHideRemoveValue) formInstance.removeValueByPaths(_name);
|
|
307
|
+
}, []);
|
|
308
|
+
}
|
|
309
|
+
export { useFairysValtioFormItemAttrs, useFairysValtioFormItemHideAttrs, useFairysValtioFormItemNoStyleAttrs };
|
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
|
@@ -81,6 +81,8 @@ export interface FairysValtioFormItemAttrsProps<T extends MObject<T> = Record<st
|
|
|
81
81
|
* @default true
|
|
82
82
|
*/
|
|
83
83
|
isRemoveValueOnUnmount?: boolean;
|
|
84
|
+
/**隐藏表单项移除数据*/
|
|
85
|
+
isHideRemoveValue?: boolean;
|
|
84
86
|
}
|
|
85
87
|
/**
|
|
86
88
|
* 处理表单表单项属性
|
|
@@ -211,18 +213,40 @@ export const FormItem = (props: FormItemProps) => {
|
|
|
211
213
|
}
|
|
212
214
|
* ```
|
|
213
215
|
*/
|
|
214
|
-
export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>):
|
|
215
|
-
|
|
216
|
-
|
|
216
|
+
export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>): FairysValtioFormItemNoStyleAttrsReturn<T>;
|
|
217
|
+
export interface FairysValtioFormItemNoStyleAttrsReturn<T extends MObject<T> = Record<string, any>> {
|
|
218
|
+
/**表单项值*/
|
|
219
|
+
value?: any;
|
|
220
|
+
/**是否校验错误*/
|
|
221
|
+
isInvalid: boolean;
|
|
222
|
+
/**是否必填*/
|
|
223
|
+
isRequired: boolean;
|
|
224
|
+
/**错误信息*/
|
|
225
|
+
error?: string[];
|
|
226
|
+
/**值改变事件*/
|
|
217
227
|
onValueChange: (event: any) => void;
|
|
228
|
+
/**表单状态*/
|
|
218
229
|
state: T;
|
|
230
|
+
/**错误状态*/
|
|
219
231
|
errorState: Record<PropertyKey, string[]>;
|
|
232
|
+
/**表单实例*/
|
|
220
233
|
formInstance: FairysValtioFormInstance<T>;
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
234
|
+
/**拼接父级字段名后得到的表单项名称*/
|
|
235
|
+
_name?: string;
|
|
236
|
+
/**表单项名称*/
|
|
237
|
+
name?: string;
|
|
238
|
+
/**表单项ID*/
|
|
239
|
+
id?: string;
|
|
240
|
+
/**表单项路径*/
|
|
241
|
+
paths?: (string | number)[];
|
|
242
|
+
/**父级字段名*/
|
|
243
|
+
parentName?: string;
|
|
244
|
+
/**表单属性名实例*/
|
|
226
245
|
formAttrsNameInstance: FairysValtioFormParentAttrs;
|
|
227
|
-
|
|
228
|
-
|
|
246
|
+
/**子元素*/
|
|
247
|
+
children?: React.ReactNode;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* 处理表单表单项属性,隐藏表单项时移除数据值
|
|
251
|
+
*/
|
|
252
|
+
export declare function useFairysValtioFormItemHideAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>): void;
|
package/lib/form/form.item.js
CHANGED
|
@@ -34,7 +34,8 @@ var __webpack_exports__ = {};
|
|
|
34
34
|
__webpack_require__.r(__webpack_exports__);
|
|
35
35
|
__webpack_require__.d(__webpack_exports__, {
|
|
36
36
|
useFairysValtioFormItemNoStyleAttrs: ()=>useFairysValtioFormItemNoStyleAttrs,
|
|
37
|
-
useFairysValtioFormItemAttrs: ()=>useFairysValtioFormItemAttrs
|
|
37
|
+
useFairysValtioFormItemAttrs: ()=>useFairysValtioFormItemAttrs,
|
|
38
|
+
useFairysValtioFormItemHideAttrs: ()=>useFairysValtioFormItemHideAttrs
|
|
38
39
|
});
|
|
39
40
|
const external_react_namespaceObject = require("react");
|
|
40
41
|
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
|
|
@@ -61,7 +62,7 @@ function useFairysValtioFormItemAttrs(props) {
|
|
|
61
62
|
const parent_isInvalidTextRed = layoutAttrs.isInvalidTextRed;
|
|
62
63
|
const parent_showColon = layoutAttrs.showColon;
|
|
63
64
|
const parent_platform = layoutAttrs.platform;
|
|
64
|
-
const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', className, style, labelClassName, labelStyle, bodyClassName, bodyStyle, children, labelMode = parent_labelMode, errorLayout = parent_errorLayout, colSpan = 1, rowSpan = 1, isRequired: _isRequired, itemBorderType = parent_borderedType, attrs = {}, showColon = parent_showColon, itemBorderColor = parent_itemBorderColor, isInvalidBorderRed = parent_isInvalidBorderRed, isInvalidTextRed = parent_isInvalidTextRed, isJoinParentField = true, rules, platform = parent_platform, isRemoveValueOnUnmount =
|
|
65
|
+
const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', className, style, labelClassName, labelStyle, bodyClassName, bodyStyle, children, labelMode = parent_labelMode, errorLayout = parent_errorLayout, colSpan = 1, rowSpan = 1, isRequired: _isRequired, itemBorderType = parent_borderedType, attrs = {}, showColon = parent_showColon, itemBorderColor = parent_itemBorderColor, isInvalidBorderRed = parent_isInvalidBorderRed, isInvalidTextRed = parent_isInvalidTextRed, isJoinParentField = true, rules, platform = parent_platform, isRemoveValueOnUnmount = false } = props;
|
|
65
66
|
const { name: _name, paths, parentName, formAttrsNameInstance } = (0, hooks_index_js_namespaceObject.useFairysValtioFormAttrsName)({
|
|
66
67
|
name,
|
|
67
68
|
isJoinParentField
|
|
@@ -256,7 +257,7 @@ function useFairysValtioFormItemAttrs(props) {
|
|
|
256
257
|
};
|
|
257
258
|
}
|
|
258
259
|
function useFairysValtioFormItemNoStyleAttrs(props) {
|
|
259
|
-
const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', children, attrs = {}, isJoinParentField = true, rules, isRemoveValueOnUnmount =
|
|
260
|
+
const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', children, attrs = {}, isJoinParentField = true, rules, isRemoveValueOnUnmount = false, isRequired: _isRequired } = props;
|
|
260
261
|
const [state, errorState, formInstance] = (0, index_js_namespaceObject.useFairysValtioFormInstanceContextState)();
|
|
261
262
|
const { name: _name, paths, parentName, formAttrsNameInstance } = (0, hooks_index_js_namespaceObject.useFairysValtioFormAttrsName)({
|
|
262
263
|
name,
|
|
@@ -269,6 +270,7 @@ function useFairysValtioFormItemNoStyleAttrs(props) {
|
|
|
269
270
|
]);
|
|
270
271
|
const error = errorState[_name];
|
|
271
272
|
formInstance.nameToPaths[_name] = paths;
|
|
273
|
+
const _formItemRules = formInstance.rules?.[_name];
|
|
272
274
|
(0, external_react_namespaceObject.useEffect)(()=>{
|
|
273
275
|
if (Array.isArray(rules) && rules.length) formInstance.mountRules[_name] = rules;
|
|
274
276
|
return ()=>{
|
|
@@ -298,8 +300,26 @@ function useFairysValtioFormItemNoStyleAttrs(props) {
|
|
|
298
300
|
[valuePropName]: value,
|
|
299
301
|
[trigger]: onValueChange
|
|
300
302
|
};
|
|
303
|
+
const isRequired = (0, external_react_namespaceObject.useMemo)(()=>{
|
|
304
|
+
if (_isRequired) return _isRequired;
|
|
305
|
+
if (Array.isArray(rules) && rules.length) return rules.some((rule)=>rule.required);
|
|
306
|
+
if (_formItemRules && Array.isArray(_formItemRules) && _formItemRules.length) return _formItemRules.some((rule)=>rule.required);
|
|
307
|
+
return false;
|
|
308
|
+
}, [
|
|
309
|
+
rules,
|
|
310
|
+
formInstance,
|
|
311
|
+
_formItemRules
|
|
312
|
+
]);
|
|
313
|
+
const isInvalid = (0, external_react_namespaceObject.useMemo)(()=>{
|
|
314
|
+
if (Array.isArray(error) && error.length) return true;
|
|
315
|
+
return false;
|
|
316
|
+
}, [
|
|
317
|
+
error
|
|
318
|
+
]);
|
|
301
319
|
return {
|
|
302
320
|
value,
|
|
321
|
+
isInvalid,
|
|
322
|
+
isRequired,
|
|
303
323
|
error,
|
|
304
324
|
onValueChange,
|
|
305
325
|
state,
|
|
@@ -316,10 +336,23 @@ function useFairysValtioFormItemNoStyleAttrs(props) {
|
|
|
316
336
|
}) : children
|
|
317
337
|
};
|
|
318
338
|
}
|
|
339
|
+
function useFairysValtioFormItemHideAttrs(props) {
|
|
340
|
+
const { name, isJoinParentField = true, isHideRemoveValue = true } = props;
|
|
341
|
+
const [, , formInstance] = (0, index_js_namespaceObject.useFairysValtioFormInstanceContextState)();
|
|
342
|
+
const { name: _name } = (0, hooks_index_js_namespaceObject.useFairysValtioFormAttrsName)({
|
|
343
|
+
name,
|
|
344
|
+
isJoinParentField
|
|
345
|
+
});
|
|
346
|
+
(0, external_react_namespaceObject.useEffect)(()=>()=>{
|
|
347
|
+
if (isHideRemoveValue) formInstance.removeValueByPaths(_name);
|
|
348
|
+
}, []);
|
|
349
|
+
}
|
|
319
350
|
exports.useFairysValtioFormItemAttrs = __webpack_exports__.useFairysValtioFormItemAttrs;
|
|
351
|
+
exports.useFairysValtioFormItemHideAttrs = __webpack_exports__.useFairysValtioFormItemHideAttrs;
|
|
320
352
|
exports.useFairysValtioFormItemNoStyleAttrs = __webpack_exports__.useFairysValtioFormItemNoStyleAttrs;
|
|
321
353
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
322
354
|
"useFairysValtioFormItemAttrs",
|
|
355
|
+
"useFairysValtioFormItemHideAttrs",
|
|
323
356
|
"useFairysValtioFormItemNoStyleAttrs"
|
|
324
357
|
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
325
358
|
Object.defineProperty(exports, '__esModule', {
|
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.2",
|
|
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,11 +81,12 @@ 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
|
*/
|
|
88
87
|
isRemoveValueOnUnmount?: boolean;
|
|
88
|
+
/**隐藏表单项移除数据*/
|
|
89
|
+
isHideRemoveValue?: boolean;
|
|
89
90
|
}
|
|
90
91
|
|
|
91
92
|
/**
|
|
@@ -180,7 +181,7 @@ export function useFairysValtioFormItemAttrs<T extends MObject<T> = Record<strin
|
|
|
180
181
|
isJoinParentField = true,
|
|
181
182
|
rules,
|
|
182
183
|
platform = parent_platform,
|
|
183
|
-
isRemoveValueOnUnmount =
|
|
184
|
+
isRemoveValueOnUnmount = false,
|
|
184
185
|
} = props;
|
|
185
186
|
|
|
186
187
|
const {
|
|
@@ -538,7 +539,8 @@ export function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Recor
|
|
|
538
539
|
attrs = {},
|
|
539
540
|
isJoinParentField = true,
|
|
540
541
|
rules,
|
|
541
|
-
isRemoveValueOnUnmount =
|
|
542
|
+
isRemoveValueOnUnmount = false,
|
|
543
|
+
isRequired: _isRequired,
|
|
542
544
|
} = props;
|
|
543
545
|
const [state, errorState, formInstance] = useFairysValtioFormInstanceContextState<T>();
|
|
544
546
|
const {
|
|
@@ -551,6 +553,8 @@ export function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Recor
|
|
|
551
553
|
const value = useMemo(() => get(state, paths), [state, paths]);
|
|
552
554
|
const error = errorState[_name];
|
|
553
555
|
formInstance.nameToPaths[_name] = paths;
|
|
556
|
+
// 使用从 Form 中设置的规则
|
|
557
|
+
const _formItemRules = formInstance.rules?.[_name];
|
|
554
558
|
|
|
555
559
|
useEffect(() => {
|
|
556
560
|
if (Array.isArray(rules) && rules.length) {
|
|
@@ -580,6 +584,7 @@ export function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Recor
|
|
|
580
584
|
onAfterUpdate(_value, formInstance, event);
|
|
581
585
|
}
|
|
582
586
|
};
|
|
587
|
+
|
|
583
588
|
useEffect(() => {
|
|
584
589
|
return () => {
|
|
585
590
|
if (isRemoveValueOnUnmount) {
|
|
@@ -587,6 +592,7 @@ export function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Recor
|
|
|
587
592
|
}
|
|
588
593
|
};
|
|
589
594
|
}, []);
|
|
595
|
+
|
|
590
596
|
/**基础组件参数*/
|
|
591
597
|
const baseControl = {
|
|
592
598
|
...attrs,
|
|
@@ -595,8 +601,30 @@ export function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Recor
|
|
|
595
601
|
[valuePropName]: value,
|
|
596
602
|
[trigger]: onValueChange,
|
|
597
603
|
};
|
|
604
|
+
|
|
605
|
+
/**判断是否必填*/
|
|
606
|
+
const isRequired = useMemo(() => {
|
|
607
|
+
if (_isRequired) {
|
|
608
|
+
return _isRequired;
|
|
609
|
+
} else if (Array.isArray(rules) && rules.length) {
|
|
610
|
+
return rules.some((rule) => rule.required);
|
|
611
|
+
} else if (_formItemRules && Array.isArray(_formItemRules) && _formItemRules.length) {
|
|
612
|
+
return _formItemRules.some((rule) => rule.required);
|
|
613
|
+
}
|
|
614
|
+
return false;
|
|
615
|
+
}, [rules, formInstance, _formItemRules]);
|
|
616
|
+
|
|
617
|
+
/**校验是否存在错误信息*/
|
|
618
|
+
const isInvalid = useMemo(() => {
|
|
619
|
+
if (Array.isArray(error) && error.length) {
|
|
620
|
+
return true;
|
|
621
|
+
}
|
|
622
|
+
return false;
|
|
623
|
+
}, [error]);
|
|
598
624
|
return {
|
|
599
625
|
value,
|
|
626
|
+
isInvalid,
|
|
627
|
+
isRequired,
|
|
600
628
|
error,
|
|
601
629
|
onValueChange,
|
|
602
630
|
state,
|
|
@@ -609,5 +637,56 @@ export function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Recor
|
|
|
609
637
|
parentName,
|
|
610
638
|
formAttrsNameInstance,
|
|
611
639
|
children: React.isValidElement(children) ? React.cloneElement(children, { ...baseControl }) : children,
|
|
612
|
-
}
|
|
640
|
+
} as FairysValtioFormItemNoStyleAttrsReturn<T>;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
export interface FairysValtioFormItemNoStyleAttrsReturn<T extends MObject<T> = Record<string, any>> {
|
|
644
|
+
/**表单项值*/
|
|
645
|
+
value?: any;
|
|
646
|
+
/**是否校验错误*/
|
|
647
|
+
isInvalid: boolean;
|
|
648
|
+
/**是否必填*/
|
|
649
|
+
isRequired: boolean;
|
|
650
|
+
/**错误信息*/
|
|
651
|
+
error?: string[];
|
|
652
|
+
/**值改变事件*/
|
|
653
|
+
onValueChange: (event: any) => void;
|
|
654
|
+
/**表单状态*/
|
|
655
|
+
state: T;
|
|
656
|
+
/**错误状态*/
|
|
657
|
+
errorState: Record<PropertyKey, string[]>;
|
|
658
|
+
/**表单实例*/
|
|
659
|
+
formInstance: FairysValtioFormInstance<T>;
|
|
660
|
+
/**拼接父级字段名后得到的表单项名称*/
|
|
661
|
+
_name?: string;
|
|
662
|
+
/**表单项名称*/
|
|
663
|
+
name?: string;
|
|
664
|
+
/**表单项ID*/
|
|
665
|
+
id?: string;
|
|
666
|
+
/**表单项路径*/
|
|
667
|
+
paths?: (string | number)[];
|
|
668
|
+
/**父级字段名*/
|
|
669
|
+
parentName?: string;
|
|
670
|
+
/**表单属性名实例*/
|
|
671
|
+
formAttrsNameInstance: FairysValtioFormParentAttrs;
|
|
672
|
+
/**子元素*/
|
|
673
|
+
children?: React.ReactNode;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* 处理表单表单项属性,隐藏表单项时移除数据值
|
|
678
|
+
*/
|
|
679
|
+
export function useFairysValtioFormItemHideAttrs<T extends MObject<T> = Record<string, any>>(
|
|
680
|
+
props: FairysValtioFormItemAttrsProps<T>,
|
|
681
|
+
) {
|
|
682
|
+
const { name, isJoinParentField = true, isHideRemoveValue = true } = props;
|
|
683
|
+
const [, , formInstance] = useFairysValtioFormInstanceContextState<T>();
|
|
684
|
+
const { name: _name } = useFairysValtioFormAttrsName({ name, isJoinParentField });
|
|
685
|
+
useEffect(() => {
|
|
686
|
+
return () => {
|
|
687
|
+
if (isHideRemoveValue) {
|
|
688
|
+
formInstance.removeValueByPaths(_name);
|
|
689
|
+
}
|
|
690
|
+
};
|
|
691
|
+
}, []);
|
|
613
692
|
}
|
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
|
}
|