@fairys/valtio-form-basic 0.0.13 → 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.
@@ -3,6 +3,12 @@ import { RuleItem, ValidateFieldsError, Values } from 'async-validator';
3
3
  import { FairysValtioFormAttrsProps } from '../../form/form';
4
4
  /**表单实例*/
5
5
  export declare class FairysValtioFormInstance<T extends MObject<T> = Record<string, any>> {
6
+ /**
7
+ * 表单值改变时回调
8
+ * @param path 表单项路径
9
+ * @param value 表单项值
10
+ */
11
+ onValuesChange?: (path: PropertyKey, value: any) => void;
6
12
  /***
7
13
  * 判断值是否为代理对象
8
14
  * @param value 值
@@ -34,6 +40,12 @@ export declare class FairysValtioFormInstance<T extends MObject<T> = Record<stri
34
40
  * @param value 值
35
41
  */
36
42
  updatedValueByPaths: (path: PropertyKey, value: any) => void;
43
+ /**表单项卸载移除保存值*/
44
+ removeValueByPaths: (path: PropertyKey) => void;
45
+ /**
46
+ * 清理值
47
+ */
48
+ clearValue: (fields?: PropertyKey[]) => this;
37
49
  /**
38
50
  * 更新行数据的隐藏信息
39
51
  * @param objectHideInfo 行数据隐藏信息对象
@@ -68,7 +80,7 @@ export declare class FairysValtioFormInstance<T extends MObject<T> = Record<stri
68
80
  * @param fields 要验证的字段(可选)
69
81
  * @param isReturn 是否返回验证结果(可选)
70
82
  */
71
- validate: (fields?: PropertyKey[], isReturn?: boolean) => Promise<ValidateFieldsError | Values>;
83
+ validate: (fields?: PropertyKey[], isReturn?: boolean) => Promise<ValidateFieldsError | Values | Partial<T>>;
72
84
  /**
73
85
  * 验证某些前缀的字段
74
86
  * @param prefix 前缀字段数组
@@ -77,12 +89,20 @@ export declare class FairysValtioFormInstance<T extends MObject<T> = Record<stri
77
89
  validatePrefixFields: (prefix: string[], isReturn?: boolean) => Promise<ValidateFieldsError | Values>;
78
90
  }
79
91
  /**声明实例*/
80
- export declare function useFairysValtioFormInstance<T extends MObject<T> = object>(instance?: FairysValtioFormInstance<T>): FairysValtioFormInstance<T>;
92
+ export declare function useFairysValtioFormInstance<T extends MObject<T> = Record<string, any>>(instance?: FairysValtioFormInstance<T>): FairysValtioFormInstance<T>;
81
93
  /**表单实例上下文*/
82
94
  export declare const FairysValtioFormInstanceContext: import("react").Context<FairysValtioFormInstance<any>>;
83
95
  /**表单实例上下文*/
84
- export declare function useFairysValtioFormInstanceContext<T extends MObject<T> = object>(): FairysValtioFormInstance<T>;
96
+ export declare function useFairysValtioFormInstanceContext<T extends MObject<T> = Record<string, any>>(): FairysValtioFormInstance<T>;
85
97
  /**状态取值*/
86
- export declare function useFairysValtioFormInstanceContextState<T extends MObject<T> = object>(): [T, Record<PropertyKey, string[]>, FairysValtioFormInstance<T>, any, any];
98
+ export declare function useFairysValtioFormInstanceContextState<T extends MObject<T> = Record<string, any>>(): [T, Record<PropertyKey, string[]>, FairysValtioFormInstance<T>, any, any];
87
99
  /**隐藏组件状态取值*/
88
- export declare function useFairysValtioFormInstanceContextHideState<T extends MObject<T> = object>(): [Record<PropertyKey, boolean>, FairysValtioFormInstance<T>, any];
100
+ export declare function useFairysValtioFormInstanceContextHideState<T extends MObject<T> = Record<string, any>>(): [Record<PropertyKey, boolean>, FairysValtioFormInstance<T>, any];
101
+ /**
102
+ * 传递表单实例获取状态
103
+ */
104
+ export declare function useFairysValtioFormInstanceToState<T extends MObject<T> = Record<string, any>>(formInstance: FairysValtioFormInstance<T>): T;
105
+ /**
106
+ * 传递表单实例获取隐藏状态
107
+ */
108
+ export declare function useFairysValtioFormInstanceToHideState<T extends MObject<T> = Record<string, any>>(formInstance: FairysValtioFormInstance<T>): Record<PropertyKey, boolean>;
@@ -2,8 +2,9 @@ import { createContext, useContext, useRef } from "react";
2
2
  import { proxy, ref as external_valtio_ref, snapshot, unstable_getInternalStates, useSnapshot } from "valtio";
3
3
  import async_validator from "async-validator";
4
4
  import { copy } from "fast-copy";
5
- import { formatePath, get, isObject, set } from "../utils/index.js";
5
+ import { formatePath, get, isObject, removeValueByPaths, set } from "../utils/index.js";
6
6
  class FairysValtioFormInstance {
7
+ onValuesChange;
7
8
  isValtioProxy = (value)=>{
8
9
  const { refSet } = unstable_getInternalStates();
9
10
  const canProxyDefault = (x)=>isObject(x) && !refSet.has(x) && (Array.isArray(x) || !(Symbol.iterator in x)) && !(x instanceof WeakMap) && !(x instanceof WeakSet) && !(x instanceof Error) && !(x instanceof Number) && !(x instanceof Date) && !(x instanceof String) && !(x instanceof RegExp) && !(x instanceof ArrayBuffer) && !(x instanceof Promise);
@@ -14,16 +15,14 @@ class FairysValtioFormInstance {
14
15
  hideState = proxy({});
15
16
  ctor = (options)=>{
16
17
  const { formData, hideState, initFormDataType = 'deepCopy' } = options || {};
17
- this.mountRules = {};
18
- this.nameToPaths = {};
19
- this.errorState = proxy({});
20
- this.hideState = proxy(hideState ? copy(hideState) : {});
18
+ this.clear();
19
+ this.updatedHideInfo(hideState ? copy(hideState) : {});
21
20
  const isValtioProxy = this.isValtioProxy(formData);
22
- if (isValtioProxy) if ('deepCopy' === initFormDataType) this.state = proxy(copy(snapshot(formData)));
23
- else this.state = formData;
21
+ if (isValtioProxy) if ('deepCopy' === initFormDataType) this.updated(copy(snapshot(formData)), false);
22
+ else this.updated(formData, false);
24
23
  else {
25
- if ('deepCopy' === initFormDataType) this.state = proxy(copy(formData || {}));
26
- this.state = proxy(formData || {});
24
+ if ('deepCopy' === initFormDataType) this.updated(copy(formData || {}), false);
25
+ this.updated(formData || {}, false);
27
26
  }
28
27
  };
29
28
  updated = (state, isValidate = true)=>{
@@ -39,6 +38,19 @@ class FairysValtioFormInstance {
39
38
  this.validate([
40
39
  path
41
40
  ], false);
41
+ this.onValuesChange?.(path, value);
42
+ };
43
+ removeValueByPaths = (path)=>{
44
+ removeValueByPaths(this.state, formatePath(path));
45
+ };
46
+ clearValue = (fields)=>{
47
+ let _fields = fields;
48
+ if (!Array.isArray(fields)) _fields = Object.keys(this.state);
49
+ for(let index = 0; index < _fields.length; index++){
50
+ const field = _fields[index];
51
+ delete this.state[field];
52
+ }
53
+ return this;
42
54
  };
43
55
  updatedHideInfo = (objectHideInfo)=>{
44
56
  const keys = Object.keys(objectHideInfo);
@@ -75,9 +87,9 @@ class FairysValtioFormInstance {
75
87
  return this;
76
88
  };
77
89
  clear = ()=>{
78
- this.state = proxy({});
79
- this.errorState = proxy({});
80
- this.hideState = proxy({});
90
+ this.clearValue();
91
+ this.clearHideInfo();
92
+ this.clearErrorInfo();
81
93
  this.mountRules = {};
82
94
  this.rules = {};
83
95
  this.nameToPaths = {};
@@ -181,4 +193,12 @@ function useFairysValtioFormInstanceContextHideState() {
181
193
  hideState.__defaultValue
182
194
  ];
183
195
  }
184
- export { FairysValtioFormInstance, FairysValtioFormInstanceContext, useFairysValtioFormInstance, useFairysValtioFormInstanceContext, useFairysValtioFormInstanceContextHideState, useFairysValtioFormInstanceContextState };
196
+ function useFairysValtioFormInstanceToState(formInstance) {
197
+ const state = useSnapshot(formInstance.state);
198
+ return state;
199
+ }
200
+ function useFairysValtioFormInstanceToHideState(formInstance) {
201
+ const hideState = useSnapshot(formInstance.hideState);
202
+ return hideState;
203
+ }
204
+ export { FairysValtioFormInstance, FairysValtioFormInstanceContext, useFairysValtioFormInstance, useFairysValtioFormInstanceContext, useFairysValtioFormInstanceContextHideState, useFairysValtioFormInstanceContextState, useFairysValtioFormInstanceToHideState, useFairysValtioFormInstanceToState };
@@ -14,6 +14,12 @@ export declare function set<T>(state: any, paths: PropertyKey[], nextValue: T):
14
14
  * @param segments 键路径
15
15
  */
16
16
  export declare function get<TDefault = unknown>(value: any, segments: PropertyKey[]): TDefault;
17
+ /***
18
+ * 移除值
19
+ * @param value 任意值
20
+ * @param segments 键路径
21
+ */
22
+ export declare function removeValueByPaths(value: any, segments: PropertyKey[]): void;
17
23
  /***
18
24
  * 格式化路径,将路径中的数组索引转换为数字
19
25
  * @param path 路径
@@ -20,6 +20,15 @@ function get(value, segments) {
20
20
  for (const key of segments)current = current?.[key];
21
21
  return current;
22
22
  }
23
+ function removeValueByPaths(value, segments) {
24
+ let current = value;
25
+ const lg = segments.length;
26
+ for(let index = 0; index < lg; index++){
27
+ const key = segments[index];
28
+ if (index === lg - 1) delete current[key];
29
+ else current = current?.[key];
30
+ }
31
+ }
23
32
  function formatePath(path) {
24
33
  if ('string' != typeof path) return [
25
34
  path
@@ -58,4 +67,4 @@ function formateName(name, parentName) {
58
67
  return '';
59
68
  }
60
69
  const isObject = (x)=>'object' == typeof x && null !== x;
61
- export { formateName, formatePath, get, isObject, set };
70
+ export { formateName, formatePath, get, isObject, removeValueByPaths, set };
@@ -3,7 +3,7 @@ import { FairysValtioFormInstance } from '../common/instance';
3
3
  import { type ReactNode } from 'react';
4
4
  import { FairysValtioFormLayoutAttrsProps } from './layout';
5
5
  import { RuleItem } from 'async-validator';
6
- export interface FairysValtioFormAttrsProps<T extends MObject<T> = object> extends FairysValtioFormLayoutAttrsProps {
6
+ export interface FairysValtioFormAttrsProps<T extends MObject<T> = Record<string, any>> extends FairysValtioFormLayoutAttrsProps {
7
7
  /**表单实例*/
8
8
  form?: FairysValtioFormInstance<T>;
9
9
  /**子元素*/
@@ -20,6 +20,12 @@ export interface FairysValtioFormAttrsProps<T extends MObject<T> = object> exten
20
20
  * - immutable:直接使用对象(注意:当传递的不是`valtio`的`proxy`对象时,会使用`valtio`中的`proxy`声明)
21
21
  */
22
22
  initFormDataType?: 'deepCopy' | 'immutable';
23
+ /**
24
+ * 表单值改变时回调
25
+ * @param path 表单项路径
26
+ * @param value 表单项值
27
+ */
28
+ onValuesChange?: (path: PropertyKey, value: any) => void;
23
29
  }
24
30
  /**
25
31
  * 表单属性处理
@@ -41,7 +47,7 @@ export const Form = (props: FormProps) => {
41
47
  }
42
48
  * ```
43
49
  */
44
- export declare function useFairysValtioForm<T extends MObject<T> = object>(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>>): {
45
51
  formInstance: FairysValtioFormInstance<T>;
46
52
  /**子元素*/
47
53
  children: ReactNode;
@@ -5,7 +5,7 @@ import { FairysValtioFormInstance } from '../common/instance';
5
5
  import { FairysValtioFormLayoutContextOptions } from './layout';
6
6
  import { FairysValtioFormParentAttrs } from '../common/hooks';
7
7
  import { RuleItem } from 'async-validator';
8
- export interface FairysValtioFormItemAttrsProps<T extends MObject<T> = object> {
8
+ export interface FairysValtioFormItemAttrsProps<T extends MObject<T> = Record<string, any>> {
9
9
  /**平台*/
10
10
  platform?: 'pc' | 'rn' | 'taro';
11
11
  /**
@@ -77,6 +77,10 @@ export interface FairysValtioFormItemAttrsProps<T extends MObject<T> = object> {
77
77
  isJoinParentField?: boolean;
78
78
  /**校验规则*/
79
79
  rules?: RuleItem[];
80
+ /**卸载移除数据值
81
+ * @default true
82
+ */
83
+ isRemoveValueOnUnmount?: boolean;
80
84
  }
81
85
  /**
82
86
  * 处理表单表单项属性
@@ -121,8 +125,8 @@ export const FormItem = (props: FormItemProps) => {
121
125
  * ```
122
126
  *
123
127
  */
124
- export declare function useFairysValtioFormItemAttrs<T extends MObject<T> = object>(props: FairysValtioFormItemAttrsProps<T>): FairysValtioFormItemAttrsReturn<T>;
125
- export interface FairysValtioFormItemAttrsReturn<T extends MObject<T> = object> {
128
+ export declare function useFairysValtioFormItemAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>): FairysValtioFormItemAttrsReturn<T>;
129
+ export interface FairysValtioFormItemAttrsReturn<T extends MObject<T> = Record<string, any>> {
126
130
  /**表单项值*/
127
131
  value?: any;
128
132
  /**是否校验错误*/
@@ -207,7 +211,7 @@ export const FormItem = (props: FormItemProps) => {
207
211
  }
208
212
  * ```
209
213
  */
210
- export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = object>(props: FairysValtioFormItemAttrsProps<T>): {
214
+ export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>): {
211
215
  value: unknown;
212
216
  error: string[];
213
217
  onValueChange: (event: any) => void;
@@ -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 } = props;
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 = true } = props;
25
25
  const { name: _name, paths, parentName, formAttrsNameInstance } = useFairysValtioFormAttrsName({
26
26
  name,
27
27
  isJoinParentField
@@ -54,6 +54,9 @@ function useFairysValtioFormItemAttrs(props) {
54
54
  formInstance.updatedValueByPaths(_name, _value);
55
55
  if ('function' == typeof onAfterUpdate) onAfterUpdate(_value, formInstance, event);
56
56
  };
57
+ useEffect(()=>()=>{
58
+ if (isRemoveValueOnUnmount) formInstance.removeValueByPaths(_name);
59
+ }, []);
57
60
  const baseControl = {
58
61
  ...attrs,
59
62
  name,
@@ -213,7 +216,7 @@ function useFairysValtioFormItemAttrs(props) {
213
216
  };
214
217
  }
215
218
  function useFairysValtioFormItemNoStyleAttrs(props) {
216
- const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', children, attrs = {}, isJoinParentField = true, rules } = props;
219
+ const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', children, attrs = {}, isJoinParentField = true, rules, isRemoveValueOnUnmount = true } = props;
217
220
  const [state, errorState, formInstance] = useFairysValtioFormInstanceContextState();
218
221
  const { name: _name, paths, parentName, formAttrsNameInstance } = useFairysValtioFormAttrsName({
219
222
  name,
@@ -245,6 +248,9 @@ function useFairysValtioFormItemNoStyleAttrs(props) {
245
248
  formInstance.updatedValueByPaths(_name, _value);
246
249
  if ('function' == typeof onAfterUpdate) onAfterUpdate(_value, formInstance, event);
247
250
  };
251
+ useEffect(()=>()=>{
252
+ if (isRemoveValueOnUnmount) formInstance.removeValueByPaths(_name);
253
+ }, []);
248
254
  const baseControl = {
249
255
  ...attrs,
250
256
  name,
package/esm/form/form.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import { useFairysValtioFormInstance } from "../common/instance/index.js";
2
2
  import { useEffect, useImperativeHandle, useMemo } from "react";
3
3
  function useFairysValtioForm(props, ref) {
4
- const { form, rules, formData, hideState, initFormDataType = 'deepCopy', ...rest } = props;
4
+ const { form, rules, formData, hideState, initFormDataType = 'deepCopy', onValuesChange, ...rest } = props;
5
5
  const formInstance = useFairysValtioFormInstance(form);
6
6
  formInstance.rules = rules;
7
+ formInstance.onValuesChange = onValuesChange;
7
8
  useMemo(()=>formInstance.ctor({
8
9
  formData,
9
10
  hideState,
@@ -3,6 +3,12 @@ import { RuleItem, ValidateFieldsError, Values } from 'async-validator';
3
3
  import { FairysValtioFormAttrsProps } from '../../form/form';
4
4
  /**表单实例*/
5
5
  export declare class FairysValtioFormInstance<T extends MObject<T> = Record<string, any>> {
6
+ /**
7
+ * 表单值改变时回调
8
+ * @param path 表单项路径
9
+ * @param value 表单项值
10
+ */
11
+ onValuesChange?: (path: PropertyKey, value: any) => void;
6
12
  /***
7
13
  * 判断值是否为代理对象
8
14
  * @param value 值
@@ -34,6 +40,12 @@ export declare class FairysValtioFormInstance<T extends MObject<T> = Record<stri
34
40
  * @param value 值
35
41
  */
36
42
  updatedValueByPaths: (path: PropertyKey, value: any) => void;
43
+ /**表单项卸载移除保存值*/
44
+ removeValueByPaths: (path: PropertyKey) => void;
45
+ /**
46
+ * 清理值
47
+ */
48
+ clearValue: (fields?: PropertyKey[]) => this;
37
49
  /**
38
50
  * 更新行数据的隐藏信息
39
51
  * @param objectHideInfo 行数据隐藏信息对象
@@ -68,7 +80,7 @@ export declare class FairysValtioFormInstance<T extends MObject<T> = Record<stri
68
80
  * @param fields 要验证的字段(可选)
69
81
  * @param isReturn 是否返回验证结果(可选)
70
82
  */
71
- validate: (fields?: PropertyKey[], isReturn?: boolean) => Promise<ValidateFieldsError | Values>;
83
+ validate: (fields?: PropertyKey[], isReturn?: boolean) => Promise<ValidateFieldsError | Values | Partial<T>>;
72
84
  /**
73
85
  * 验证某些前缀的字段
74
86
  * @param prefix 前缀字段数组
@@ -77,12 +89,20 @@ export declare class FairysValtioFormInstance<T extends MObject<T> = Record<stri
77
89
  validatePrefixFields: (prefix: string[], isReturn?: boolean) => Promise<ValidateFieldsError | Values>;
78
90
  }
79
91
  /**声明实例*/
80
- export declare function useFairysValtioFormInstance<T extends MObject<T> = object>(instance?: FairysValtioFormInstance<T>): FairysValtioFormInstance<T>;
92
+ export declare function useFairysValtioFormInstance<T extends MObject<T> = Record<string, any>>(instance?: FairysValtioFormInstance<T>): FairysValtioFormInstance<T>;
81
93
  /**表单实例上下文*/
82
94
  export declare const FairysValtioFormInstanceContext: import("react").Context<FairysValtioFormInstance<any>>;
83
95
  /**表单实例上下文*/
84
- export declare function useFairysValtioFormInstanceContext<T extends MObject<T> = object>(): FairysValtioFormInstance<T>;
96
+ export declare function useFairysValtioFormInstanceContext<T extends MObject<T> = Record<string, any>>(): FairysValtioFormInstance<T>;
85
97
  /**状态取值*/
86
- export declare function useFairysValtioFormInstanceContextState<T extends MObject<T> = object>(): [T, Record<PropertyKey, string[]>, FairysValtioFormInstance<T>, any, any];
98
+ export declare function useFairysValtioFormInstanceContextState<T extends MObject<T> = Record<string, any>>(): [T, Record<PropertyKey, string[]>, FairysValtioFormInstance<T>, any, any];
87
99
  /**隐藏组件状态取值*/
88
- export declare function useFairysValtioFormInstanceContextHideState<T extends MObject<T> = object>(): [Record<PropertyKey, boolean>, FairysValtioFormInstance<T>, any];
100
+ export declare function useFairysValtioFormInstanceContextHideState<T extends MObject<T> = Record<string, any>>(): [Record<PropertyKey, boolean>, FairysValtioFormInstance<T>, any];
101
+ /**
102
+ * 传递表单实例获取状态
103
+ */
104
+ export declare function useFairysValtioFormInstanceToState<T extends MObject<T> = Record<string, any>>(formInstance: FairysValtioFormInstance<T>): T;
105
+ /**
106
+ * 传递表单实例获取隐藏状态
107
+ */
108
+ export declare function useFairysValtioFormInstanceToHideState<T extends MObject<T> = Record<string, any>>(formInstance: FairysValtioFormInstance<T>): Record<PropertyKey, boolean>;
@@ -33,12 +33,14 @@ var __webpack_require__ = {};
33
33
  var __webpack_exports__ = {};
34
34
  __webpack_require__.r(__webpack_exports__);
35
35
  __webpack_require__.d(__webpack_exports__, {
36
- FairysValtioFormInstance: ()=>FairysValtioFormInstance,
37
36
  useFairysValtioFormInstanceContextState: ()=>useFairysValtioFormInstanceContextState,
38
- useFairysValtioFormInstance: ()=>useFairysValtioFormInstance,
39
- FairysValtioFormInstanceContext: ()=>FairysValtioFormInstanceContext,
40
37
  useFairysValtioFormInstanceContext: ()=>useFairysValtioFormInstanceContext,
41
- useFairysValtioFormInstanceContextHideState: ()=>useFairysValtioFormInstanceContextHideState
38
+ useFairysValtioFormInstanceContextHideState: ()=>useFairysValtioFormInstanceContextHideState,
39
+ useFairysValtioFormInstanceToHideState: ()=>useFairysValtioFormInstanceToHideState,
40
+ useFairysValtioFormInstanceToState: ()=>useFairysValtioFormInstanceToState,
41
+ FairysValtioFormInstance: ()=>FairysValtioFormInstance,
42
+ useFairysValtioFormInstance: ()=>useFairysValtioFormInstance,
43
+ FairysValtioFormInstanceContext: ()=>FairysValtioFormInstanceContext
42
44
  });
43
45
  const external_react_namespaceObject = require("react");
44
46
  const external_valtio_namespaceObject = require("valtio");
@@ -47,6 +49,7 @@ var external_async_validator_default = /*#__PURE__*/ __webpack_require__.n(exter
47
49
  const external_fast_copy_namespaceObject = require("fast-copy");
48
50
  const index_js_namespaceObject = require("../utils/index.js");
49
51
  class FairysValtioFormInstance {
52
+ onValuesChange;
50
53
  isValtioProxy = (value)=>{
51
54
  const { refSet } = (0, external_valtio_namespaceObject.unstable_getInternalStates)();
52
55
  const canProxyDefault = (x)=>(0, index_js_namespaceObject.isObject)(x) && !refSet.has(x) && (Array.isArray(x) || !(Symbol.iterator in x)) && !(x instanceof WeakMap) && !(x instanceof WeakSet) && !(x instanceof Error) && !(x instanceof Number) && !(x instanceof Date) && !(x instanceof String) && !(x instanceof RegExp) && !(x instanceof ArrayBuffer) && !(x instanceof Promise);
@@ -57,16 +60,14 @@ class FairysValtioFormInstance {
57
60
  hideState = (0, external_valtio_namespaceObject.proxy)({});
58
61
  ctor = (options)=>{
59
62
  const { formData, hideState, initFormDataType = 'deepCopy' } = options || {};
60
- this.mountRules = {};
61
- this.nameToPaths = {};
62
- this.errorState = (0, external_valtio_namespaceObject.proxy)({});
63
- this.hideState = (0, external_valtio_namespaceObject.proxy)(hideState ? (0, external_fast_copy_namespaceObject.copy)(hideState) : {});
63
+ this.clear();
64
+ this.updatedHideInfo(hideState ? (0, external_fast_copy_namespaceObject.copy)(hideState) : {});
64
65
  const isValtioProxy = this.isValtioProxy(formData);
65
- if (isValtioProxy) if ('deepCopy' === initFormDataType) this.state = (0, external_valtio_namespaceObject.proxy)((0, external_fast_copy_namespaceObject.copy)((0, external_valtio_namespaceObject.snapshot)(formData)));
66
- else this.state = formData;
66
+ if (isValtioProxy) if ('deepCopy' === initFormDataType) this.updated((0, external_fast_copy_namespaceObject.copy)((0, external_valtio_namespaceObject.snapshot)(formData)), false);
67
+ else this.updated(formData, false);
67
68
  else {
68
- if ('deepCopy' === initFormDataType) this.state = (0, external_valtio_namespaceObject.proxy)((0, external_fast_copy_namespaceObject.copy)(formData || {}));
69
- this.state = (0, external_valtio_namespaceObject.proxy)(formData || {});
69
+ if ('deepCopy' === initFormDataType) this.updated((0, external_fast_copy_namespaceObject.copy)(formData || {}), false);
70
+ this.updated(formData || {}, false);
70
71
  }
71
72
  };
72
73
  updated = (state, isValidate = true)=>{
@@ -82,6 +83,19 @@ class FairysValtioFormInstance {
82
83
  this.validate([
83
84
  path
84
85
  ], false);
86
+ this.onValuesChange?.(path, value);
87
+ };
88
+ removeValueByPaths = (path)=>{
89
+ (0, index_js_namespaceObject.removeValueByPaths)(this.state, (0, index_js_namespaceObject.formatePath)(path));
90
+ };
91
+ clearValue = (fields)=>{
92
+ let _fields = fields;
93
+ if (!Array.isArray(fields)) _fields = Object.keys(this.state);
94
+ for(let index = 0; index < _fields.length; index++){
95
+ const field = _fields[index];
96
+ delete this.state[field];
97
+ }
98
+ return this;
85
99
  };
86
100
  updatedHideInfo = (objectHideInfo)=>{
87
101
  const keys = Object.keys(objectHideInfo);
@@ -118,9 +132,9 @@ class FairysValtioFormInstance {
118
132
  return this;
119
133
  };
120
134
  clear = ()=>{
121
- this.state = (0, external_valtio_namespaceObject.proxy)({});
122
- this.errorState = (0, external_valtio_namespaceObject.proxy)({});
123
- this.hideState = (0, external_valtio_namespaceObject.proxy)({});
135
+ this.clearValue();
136
+ this.clearHideInfo();
137
+ this.clearErrorInfo();
124
138
  this.mountRules = {};
125
139
  this.rules = {};
126
140
  this.nameToPaths = {};
@@ -224,19 +238,31 @@ function useFairysValtioFormInstanceContextHideState() {
224
238
  hideState.__defaultValue
225
239
  ];
226
240
  }
241
+ function useFairysValtioFormInstanceToState(formInstance) {
242
+ const state = (0, external_valtio_namespaceObject.useSnapshot)(formInstance.state);
243
+ return state;
244
+ }
245
+ function useFairysValtioFormInstanceToHideState(formInstance) {
246
+ const hideState = (0, external_valtio_namespaceObject.useSnapshot)(formInstance.hideState);
247
+ return hideState;
248
+ }
227
249
  exports.FairysValtioFormInstance = __webpack_exports__.FairysValtioFormInstance;
228
250
  exports.FairysValtioFormInstanceContext = __webpack_exports__.FairysValtioFormInstanceContext;
229
251
  exports.useFairysValtioFormInstance = __webpack_exports__.useFairysValtioFormInstance;
230
252
  exports.useFairysValtioFormInstanceContext = __webpack_exports__.useFairysValtioFormInstanceContext;
231
253
  exports.useFairysValtioFormInstanceContextHideState = __webpack_exports__.useFairysValtioFormInstanceContextHideState;
232
254
  exports.useFairysValtioFormInstanceContextState = __webpack_exports__.useFairysValtioFormInstanceContextState;
255
+ exports.useFairysValtioFormInstanceToHideState = __webpack_exports__.useFairysValtioFormInstanceToHideState;
256
+ exports.useFairysValtioFormInstanceToState = __webpack_exports__.useFairysValtioFormInstanceToState;
233
257
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
234
258
  "FairysValtioFormInstance",
235
259
  "FairysValtioFormInstanceContext",
236
260
  "useFairysValtioFormInstance",
237
261
  "useFairysValtioFormInstanceContext",
238
262
  "useFairysValtioFormInstanceContextHideState",
239
- "useFairysValtioFormInstanceContextState"
263
+ "useFairysValtioFormInstanceContextState",
264
+ "useFairysValtioFormInstanceToHideState",
265
+ "useFairysValtioFormInstanceToState"
240
266
  ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
241
267
  Object.defineProperty(exports, '__esModule', {
242
268
  value: true
@@ -14,6 +14,12 @@ export declare function set<T>(state: any, paths: PropertyKey[], nextValue: T):
14
14
  * @param segments 键路径
15
15
  */
16
16
  export declare function get<TDefault = unknown>(value: any, segments: PropertyKey[]): TDefault;
17
+ /***
18
+ * 移除值
19
+ * @param value 任意值
20
+ * @param segments 键路径
21
+ */
22
+ export declare function removeValueByPaths(value: any, segments: PropertyKey[]): void;
17
23
  /***
18
24
  * 格式化路径,将路径中的数组索引转换为数字
19
25
  * @param path 路径
@@ -28,6 +28,7 @@ __webpack_require__.d(__webpack_exports__, {
28
28
  formatePath: ()=>formatePath,
29
29
  get: ()=>get,
30
30
  isObject: ()=>isObject,
31
+ removeValueByPaths: ()=>removeValueByPaths,
31
32
  set: ()=>set
32
33
  });
33
34
  function set(state, paths, nextValue) {
@@ -52,6 +53,15 @@ function get(value, segments) {
52
53
  for (const key of segments)current = current?.[key];
53
54
  return current;
54
55
  }
56
+ function removeValueByPaths(value, segments) {
57
+ let current = value;
58
+ const lg = segments.length;
59
+ for(let index = 0; index < lg; index++){
60
+ const key = segments[index];
61
+ if (index === lg - 1) delete current[key];
62
+ else current = current?.[key];
63
+ }
64
+ }
55
65
  function formatePath(path) {
56
66
  if ('string' != typeof path) return [
57
67
  path
@@ -94,12 +104,14 @@ exports.formateName = __webpack_exports__.formateName;
94
104
  exports.formatePath = __webpack_exports__.formatePath;
95
105
  exports.get = __webpack_exports__.get;
96
106
  exports.isObject = __webpack_exports__.isObject;
107
+ exports.removeValueByPaths = __webpack_exports__.removeValueByPaths;
97
108
  exports.set = __webpack_exports__.set;
98
109
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
99
110
  "formateName",
100
111
  "formatePath",
101
112
  "get",
102
113
  "isObject",
114
+ "removeValueByPaths",
103
115
  "set"
104
116
  ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
105
117
  Object.defineProperty(exports, '__esModule', {
@@ -3,7 +3,7 @@ import { FairysValtioFormInstance } from '../common/instance';
3
3
  import { type ReactNode } from 'react';
4
4
  import { FairysValtioFormLayoutAttrsProps } from './layout';
5
5
  import { RuleItem } from 'async-validator';
6
- export interface FairysValtioFormAttrsProps<T extends MObject<T> = object> extends FairysValtioFormLayoutAttrsProps {
6
+ export interface FairysValtioFormAttrsProps<T extends MObject<T> = Record<string, any>> extends FairysValtioFormLayoutAttrsProps {
7
7
  /**表单实例*/
8
8
  form?: FairysValtioFormInstance<T>;
9
9
  /**子元素*/
@@ -20,6 +20,12 @@ export interface FairysValtioFormAttrsProps<T extends MObject<T> = object> exten
20
20
  * - immutable:直接使用对象(注意:当传递的不是`valtio`的`proxy`对象时,会使用`valtio`中的`proxy`声明)
21
21
  */
22
22
  initFormDataType?: 'deepCopy' | 'immutable';
23
+ /**
24
+ * 表单值改变时回调
25
+ * @param path 表单项路径
26
+ * @param value 表单项值
27
+ */
28
+ onValuesChange?: (path: PropertyKey, value: any) => void;
23
29
  }
24
30
  /**
25
31
  * 表单属性处理
@@ -41,7 +47,7 @@ export const Form = (props: FormProps) => {
41
47
  }
42
48
  * ```
43
49
  */
44
- export declare function useFairysValtioForm<T extends MObject<T> = object>(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>>): {
45
51
  formInstance: FairysValtioFormInstance<T>;
46
52
  /**子元素*/
47
53
  children: ReactNode;
@@ -5,7 +5,7 @@ import { FairysValtioFormInstance } from '../common/instance';
5
5
  import { FairysValtioFormLayoutContextOptions } from './layout';
6
6
  import { FairysValtioFormParentAttrs } from '../common/hooks';
7
7
  import { RuleItem } from 'async-validator';
8
- export interface FairysValtioFormItemAttrsProps<T extends MObject<T> = object> {
8
+ export interface FairysValtioFormItemAttrsProps<T extends MObject<T> = Record<string, any>> {
9
9
  /**平台*/
10
10
  platform?: 'pc' | 'rn' | 'taro';
11
11
  /**
@@ -77,6 +77,10 @@ export interface FairysValtioFormItemAttrsProps<T extends MObject<T> = object> {
77
77
  isJoinParentField?: boolean;
78
78
  /**校验规则*/
79
79
  rules?: RuleItem[];
80
+ /**卸载移除数据值
81
+ * @default true
82
+ */
83
+ isRemoveValueOnUnmount?: boolean;
80
84
  }
81
85
  /**
82
86
  * 处理表单表单项属性
@@ -121,8 +125,8 @@ export const FormItem = (props: FormItemProps) => {
121
125
  * ```
122
126
  *
123
127
  */
124
- export declare function useFairysValtioFormItemAttrs<T extends MObject<T> = object>(props: FairysValtioFormItemAttrsProps<T>): FairysValtioFormItemAttrsReturn<T>;
125
- export interface FairysValtioFormItemAttrsReturn<T extends MObject<T> = object> {
128
+ export declare function useFairysValtioFormItemAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>): FairysValtioFormItemAttrsReturn<T>;
129
+ export interface FairysValtioFormItemAttrsReturn<T extends MObject<T> = Record<string, any>> {
126
130
  /**表单项值*/
127
131
  value?: any;
128
132
  /**是否校验错误*/
@@ -207,7 +211,7 @@ export const FormItem = (props: FormItemProps) => {
207
211
  }
208
212
  * ```
209
213
  */
210
- export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = object>(props: FairysValtioFormItemAttrsProps<T>): {
214
+ export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>): {
211
215
  value: unknown;
212
216
  error: string[];
213
217
  onValueChange: (event: any) => void;
@@ -61,7 +61,7 @@ function useFairysValtioFormItemAttrs(props) {
61
61
  const parent_isInvalidTextRed = layoutAttrs.isInvalidTextRed;
62
62
  const parent_showColon = layoutAttrs.showColon;
63
63
  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 } = props;
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 = true } = props;
65
65
  const { name: _name, paths, parentName, formAttrsNameInstance } = (0, hooks_index_js_namespaceObject.useFairysValtioFormAttrsName)({
66
66
  name,
67
67
  isJoinParentField
@@ -94,6 +94,9 @@ function useFairysValtioFormItemAttrs(props) {
94
94
  formInstance.updatedValueByPaths(_name, _value);
95
95
  if ('function' == typeof onAfterUpdate) onAfterUpdate(_value, formInstance, event);
96
96
  };
97
+ (0, external_react_namespaceObject.useEffect)(()=>()=>{
98
+ if (isRemoveValueOnUnmount) formInstance.removeValueByPaths(_name);
99
+ }, []);
97
100
  const baseControl = {
98
101
  ...attrs,
99
102
  name,
@@ -253,7 +256,7 @@ function useFairysValtioFormItemAttrs(props) {
253
256
  };
254
257
  }
255
258
  function useFairysValtioFormItemNoStyleAttrs(props) {
256
- const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', children, attrs = {}, isJoinParentField = true, rules } = props;
259
+ const { name, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', children, attrs = {}, isJoinParentField = true, rules, isRemoveValueOnUnmount = true } = props;
257
260
  const [state, errorState, formInstance] = (0, index_js_namespaceObject.useFairysValtioFormInstanceContextState)();
258
261
  const { name: _name, paths, parentName, formAttrsNameInstance } = (0, hooks_index_js_namespaceObject.useFairysValtioFormAttrsName)({
259
262
  name,
@@ -285,6 +288,9 @@ function useFairysValtioFormItemNoStyleAttrs(props) {
285
288
  formInstance.updatedValueByPaths(_name, _value);
286
289
  if ('function' == typeof onAfterUpdate) onAfterUpdate(_value, formInstance, event);
287
290
  };
291
+ (0, external_react_namespaceObject.useEffect)(()=>()=>{
292
+ if (isRemoveValueOnUnmount) formInstance.removeValueByPaths(_name);
293
+ }, []);
288
294
  const baseControl = {
289
295
  ...attrs,
290
296
  name,