@mapples/form 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 +411 -0
- package/dist/components/Form.d.ts +37 -0
- package/dist/components/Form.d.ts.map +1 -0
- package/dist/components/Form.js +111 -0
- package/dist/components/Form.js.map +1 -0
- package/dist/components/index.d.ts +3 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +3 -0
- package/dist/components/index.js.map +1 -0
- package/dist/context/FormContext.d.ts +19 -0
- package/dist/context/FormContext.d.ts.map +1 -0
- package/dist/context/FormContext.js +28 -0
- package/dist/context/FormContext.js.map +1 -0
- package/dist/context/index.d.ts +4 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +4 -0
- package/dist/context/index.js.map +1 -0
- package/dist/hooks/index.d.ts +11 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +11 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/useBindFormField.d.ts +43 -0
- package/dist/hooks/useBindFormField.d.ts.map +1 -0
- package/dist/hooks/useBindFormField.js +93 -0
- package/dist/hooks/useBindFormField.js.map +1 -0
- package/dist/hooks/useFormArrayField.d.ts +43 -0
- package/dist/hooks/useFormArrayField.d.ts.map +1 -0
- package/dist/hooks/useFormArrayField.js +88 -0
- package/dist/hooks/useFormArrayField.js.map +1 -0
- package/dist/hooks/useFormContext.d.ts +28 -0
- package/dist/hooks/useFormContext.d.ts.map +1 -0
- package/dist/hooks/useFormContext.js +32 -0
- package/dist/hooks/useFormContext.js.map +1 -0
- package/dist/hooks/useFormField.d.ts +42 -0
- package/dist/hooks/useFormField.d.ts.map +1 -0
- package/dist/hooks/useFormField.js +79 -0
- package/dist/hooks/useFormField.js.map +1 -0
- package/dist/hooks/useFormFieldToggle.d.ts +33 -0
- package/dist/hooks/useFormFieldToggle.d.ts.map +1 -0
- package/dist/hooks/useFormFieldToggle.js +49 -0
- package/dist/hooks/useFormFieldToggle.js.map +1 -0
- package/dist/hooks/useFormHistory.d.ts +30 -0
- package/dist/hooks/useFormHistory.d.ts.map +1 -0
- package/dist/hooks/useFormHistory.js +57 -0
- package/dist/hooks/useFormHistory.js.map +1 -0
- package/dist/hooks/useFormState.d.ts +34 -0
- package/dist/hooks/useFormState.d.ts.map +1 -0
- package/dist/hooks/useFormState.js +54 -0
- package/dist/hooks/useFormState.js.map +1 -0
- package/dist/hooks/useFormSubmit.d.ts +27 -0
- package/dist/hooks/useFormSubmit.d.ts.map +1 -0
- package/dist/hooks/useFormSubmit.js +31 -0
- package/dist/hooks/useFormSubmit.js.map +1 -0
- package/dist/hooks/useFormValue.d.ts +28 -0
- package/dist/hooks/useFormValue.d.ts.map +1 -0
- package/dist/hooks/useFormValue.js +43 -0
- package/dist/hooks/useFormValue.js.map +1 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/dist/state/index.d.ts +128 -0
- package/dist/state/index.d.ts.map +1 -0
- package/dist/state/index.js +249 -0
- package/dist/state/index.js.map +1 -0
- package/dist/types/index.d.ts +71 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/index.d.ts +40 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +95 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +40 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAClD,OAAO,iBAAiB,MAAM,qBAAqB,CAAC;AACpD,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAC9C,OAAO,YAAY,MAAM,gBAAgB,CAAC;AAC1C,OAAO,kBAAkB,MAAM,sBAAsB,CAAC;AACtD,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAC9C,OAAO,YAAY,MAAM,gBAAgB,CAAC;AAC1C,OAAO,aAAa,MAAM,iBAAiB,CAAC;AAC5C,OAAO,YAAY,MAAM,gBAAgB,CAAC;AAE1C,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,iBAAiB,EACjB,kBAAkB,EAClB,cAAc,EACd,aAAa,EACb,gBAAgB,GACjB,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook to bind a form field with optional fallback path synchronization.
|
|
3
|
+
* This hook is similar to useFormField but allows binding a field to a fallback path,
|
|
4
|
+
* useful for creating field aliases or synchronized fields.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type of the field value
|
|
7
|
+
* @param fieldPath - Primary dot notation path to the field
|
|
8
|
+
* @param fallBackPath - Optional fallback path to synchronize with
|
|
9
|
+
* @param fallbackValue - Default value to use if field is undefined
|
|
10
|
+
* @param subscribe - Whether to subscribe to real-time value changes (default: true)
|
|
11
|
+
* @returns Object containing field value, error state, and control methods
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* const NameField = () => {
|
|
16
|
+
* const { value, error, setValue } = useBindFormField<string>(
|
|
17
|
+
* 'user.name',
|
|
18
|
+
* 'displayName', // Also update displayName when user.name changes
|
|
19
|
+
* ''
|
|
20
|
+
* );
|
|
21
|
+
*
|
|
22
|
+
* return (
|
|
23
|
+
* <input
|
|
24
|
+
* value={value}
|
|
25
|
+
* onChange={(e) => setValue(e.target.value)}
|
|
26
|
+
* />
|
|
27
|
+
* );
|
|
28
|
+
* };
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
declare const useBindFormField: <T>(fieldPath: string, fallBackPath?: string, fallbackValue?: T, subscribe?: boolean) => {
|
|
34
|
+
value: T | undefined;
|
|
35
|
+
error: string | undefined;
|
|
36
|
+
errors: import("yup").ValidationError[] | undefined;
|
|
37
|
+
isTouched: boolean;
|
|
38
|
+
validate: () => void;
|
|
39
|
+
touch: (fieldName: string) => void;
|
|
40
|
+
setValue: (newValue: T) => void;
|
|
41
|
+
};
|
|
42
|
+
export default useBindFormField;
|
|
43
|
+
//# sourceMappingURL=useBindFormField.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useBindFormField.d.ts","sourceRoot":"","sources":["../../src/hooks/useBindFormField.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,QAAA,MAAM,gBAAgB,GAAI,CAAC,EACzB,WAAW,MAAM,EACjB,eAAe,MAAM,EACrB,gBAAgB,CAAC,EACjB,YAAY,OAAO;;;;;;;yBAmDN,CAAC;CAqBf,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import { first } from 'lodash';
|
|
3
|
+
import FormContext from '../context';
|
|
4
|
+
/**
|
|
5
|
+
* Hook to bind a form field with optional fallback path synchronization.
|
|
6
|
+
* This hook is similar to useFormField but allows binding a field to a fallback path,
|
|
7
|
+
* useful for creating field aliases or synchronized fields.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of the field value
|
|
10
|
+
* @param fieldPath - Primary dot notation path to the field
|
|
11
|
+
* @param fallBackPath - Optional fallback path to synchronize with
|
|
12
|
+
* @param fallbackValue - Default value to use if field is undefined
|
|
13
|
+
* @param subscribe - Whether to subscribe to real-time value changes (default: true)
|
|
14
|
+
* @returns Object containing field value, error state, and control methods
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* const NameField = () => {
|
|
19
|
+
* const { value, error, setValue } = useBindFormField<string>(
|
|
20
|
+
* 'user.name',
|
|
21
|
+
* 'displayName', // Also update displayName when user.name changes
|
|
22
|
+
* ''
|
|
23
|
+
* );
|
|
24
|
+
*
|
|
25
|
+
* return (
|
|
26
|
+
* <input
|
|
27
|
+
* value={value}
|
|
28
|
+
* onChange={(e) => setValue(e.target.value)}
|
|
29
|
+
* />
|
|
30
|
+
* );
|
|
31
|
+
* };
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
const useBindFormField = (fieldPath, fallBackPath, fallbackValue, subscribe) => {
|
|
37
|
+
const { form, touched, validationErrors, validate, touch } = useContext(FormContext);
|
|
38
|
+
const [value, setValue] = useState(fallbackValue);
|
|
39
|
+
const errors = validationErrors?.[fieldPath];
|
|
40
|
+
const error = useMemo(() => {
|
|
41
|
+
return first(errors)?.message;
|
|
42
|
+
}, [errors]);
|
|
43
|
+
const isTouched = useMemo(() => {
|
|
44
|
+
return !!touched[fieldPath];
|
|
45
|
+
}, [fieldPath, touched]);
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
setValue(() => {
|
|
48
|
+
const value = form?.getValue(fieldPath) || fallbackValue;
|
|
49
|
+
if (fallBackPath) {
|
|
50
|
+
form?.setValue(fallBackPath, value);
|
|
51
|
+
}
|
|
52
|
+
return value;
|
|
53
|
+
});
|
|
54
|
+
}, [fallBackPath, fallbackValue, fieldPath, form]);
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
if (!subscribe) {
|
|
57
|
+
const unsubscribe = form?.subscribeOnHistoryChange(() => {
|
|
58
|
+
const newValue = form?.getValue(fieldPath) || fallbackValue;
|
|
59
|
+
if (fallBackPath) {
|
|
60
|
+
form?.setValue(fallBackPath, newValue);
|
|
61
|
+
}
|
|
62
|
+
setValue(newValue);
|
|
63
|
+
});
|
|
64
|
+
return () => {
|
|
65
|
+
unsubscribe?.();
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
const unsubscribe = form?.subscribeOnChange(setValue, fieldPath);
|
|
69
|
+
return () => {
|
|
70
|
+
unsubscribe?.();
|
|
71
|
+
};
|
|
72
|
+
}, [fallBackPath, fallbackValue, fieldPath, form, subscribe]);
|
|
73
|
+
const handleSetValue = useCallback((newValue) => {
|
|
74
|
+
form?.setValue(fieldPath, newValue);
|
|
75
|
+
if (!subscribe) {
|
|
76
|
+
setValue(newValue);
|
|
77
|
+
if (fallBackPath) {
|
|
78
|
+
form?.setValue(fallBackPath, newValue);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}, [fallBackPath, fieldPath, form, subscribe]);
|
|
82
|
+
return {
|
|
83
|
+
value,
|
|
84
|
+
error,
|
|
85
|
+
errors,
|
|
86
|
+
isTouched,
|
|
87
|
+
validate,
|
|
88
|
+
touch,
|
|
89
|
+
setValue: handleSetValue,
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
export default useBindFormField;
|
|
93
|
+
//# sourceMappingURL=useBindFormField.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useBindFormField.js","sourceRoot":"","sources":["../../src/hooks/useBindFormField.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAE9E,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAE/B,OAAO,WAAW,MAAM,YAAY,CAAC;AAErC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,gBAAgB,GAAG,CACvB,SAAiB,EACjB,YAAqB,EACrB,aAAiB,EACjB,SAAmB,EACnB,EAAE;IACF,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,KAAK,EAAE,GACxD,UAAU,CAAC,WAAW,CAAC,CAAC;IAE1B,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAgB,aAAa,CAAC,CAAC;IAEjE,MAAM,MAAM,GAAG,gBAAgB,EAAE,CAAC,SAAS,CAAC,CAA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C,CAAC;QAEpE,OAAO,GAAG,EAAE;YACV,WAAW,EAAE,EAAE,CAAC;QAClB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,YAAY,EAAE,aAAa,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC;IAE9D,MAAM,cAAc,GAAG,WAAW,CAChC,CAAC,QAAW,EAAE,EAAE;QACd,IAAI,EAAE,QAAQ,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QACpC,IAAI,CAAC,SAAS,EAAE,CAAC;YACf,QAAQ,CAAC,QAAQ,CAAC,CAAC;YACnB,IAAI,YAAY,EAAE,CAAC;gBACjB,IAAI,EAAE,QAAQ,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;YACzC,CAAC;QACH,CAAC;IACH,CAAC,EACD,CAAC,YAAY,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,CAAC,CAC3C,CAAC;IAEF,OAAO;QACL,KAAK;QACL,KAAK;QACL,MAAM;QACN,SAAS;QACT,QAAQ;QACR,KAAK;QACL,QAAQ,EAAE,cAAc;KACzB,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook to manage array fields with specialized array manipulation methods.
|
|
3
|
+
* This hook provides methods to add, remove, and manipulate array items in a form field.
|
|
4
|
+
*
|
|
5
|
+
* @template T - The type of items in the array
|
|
6
|
+
* @param fieldPath - Dot notation path to the array field
|
|
7
|
+
* @param fallbackValue - Default array value to use if field is undefined
|
|
8
|
+
* @returns Object containing array value and manipulation methods
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* const TagsField = () => {
|
|
13
|
+
* const { value, api } = useFormArrayField<string>('tags', []);
|
|
14
|
+
*
|
|
15
|
+
* return (
|
|
16
|
+
* <div>
|
|
17
|
+
* {value.map((tag, index) => (
|
|
18
|
+
* <div key={index}>
|
|
19
|
+
* {tag}
|
|
20
|
+
* <button onClick={() => api.remove(index)}>Remove</button>
|
|
21
|
+
* </div>
|
|
22
|
+
* ))}
|
|
23
|
+
* <button onClick={() => api.push('New Tag')}>Add Tag</button>
|
|
24
|
+
* </div>
|
|
25
|
+
* );
|
|
26
|
+
* };
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
declare const useFormArrayField: <T>(fieldPath: string, fallbackValue?: T[]) => {
|
|
32
|
+
value: T[] | undefined;
|
|
33
|
+
api: {
|
|
34
|
+
push: (item: T) => void;
|
|
35
|
+
dropLeft: (count?: number) => void;
|
|
36
|
+
dropRight: (count?: number) => void;
|
|
37
|
+
remove: (index: number) => void;
|
|
38
|
+
paste: (item: T, index: number) => void;
|
|
39
|
+
toggle: (item: T) => void;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export default useFormArrayField;
|
|
43
|
+
//# sourceMappingURL=useFormArrayField.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormArrayField.d.ts","sourceRoot":"","sources":["../../src/hooks/useFormArrayField.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,QAAA,MAAM,iBAAiB,GAAI,CAAC,EAAE,WAAW,MAAM,EAAE,gBAAgB,CAAC,EAAE;;;qBAazC,CAAC;2BAIK,MAAM;4BAIL,MAAM;wBAIV,MAAM;sBAQR,CAAC,SAAS,MAAM;uBAQf,CAAC;;CAoB7B,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { useContext, useEffect, useState } from 'react';
|
|
2
|
+
import { cloneDeep, drop, dropRight, indexOf } from 'lodash';
|
|
3
|
+
import FormContext from '../context';
|
|
4
|
+
/**
|
|
5
|
+
* Hook to manage array fields with specialized array manipulation methods.
|
|
6
|
+
* This hook provides methods to add, remove, and manipulate array items in a form field.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of items in the array
|
|
9
|
+
* @param fieldPath - Dot notation path to the array field
|
|
10
|
+
* @param fallbackValue - Default array value to use if field is undefined
|
|
11
|
+
* @returns Object containing array value and manipulation methods
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* const TagsField = () => {
|
|
16
|
+
* const { value, api } = useFormArrayField<string>('tags', []);
|
|
17
|
+
*
|
|
18
|
+
* return (
|
|
19
|
+
* <div>
|
|
20
|
+
* {value.map((tag, index) => (
|
|
21
|
+
* <div key={index}>
|
|
22
|
+
* {tag}
|
|
23
|
+
* <button onClick={() => api.remove(index)}>Remove</button>
|
|
24
|
+
* </div>
|
|
25
|
+
* ))}
|
|
26
|
+
* <button onClick={() => api.push('New Tag')}>Add Tag</button>
|
|
27
|
+
* </div>
|
|
28
|
+
* );
|
|
29
|
+
* };
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
const useFormArrayField = (fieldPath, fallbackValue) => {
|
|
35
|
+
const { form } = useContext(FormContext);
|
|
36
|
+
const [value, setValue] = useState();
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
setValue(form?.getValue(fieldPath));
|
|
39
|
+
}, [fieldPath, form]);
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
form?.setValue(fieldPath, value);
|
|
42
|
+
}, [fieldPath, form, value]);
|
|
43
|
+
const fieldPush = (item) => {
|
|
44
|
+
setValue((prev) => [...(prev || []), item]);
|
|
45
|
+
};
|
|
46
|
+
const fieldDropLeft = (count) => {
|
|
47
|
+
setValue((prev) => drop(cloneDeep(prev), count));
|
|
48
|
+
};
|
|
49
|
+
const fieldDropRight = (count) => {
|
|
50
|
+
setValue((prev) => dropRight(cloneDeep(prev), count));
|
|
51
|
+
};
|
|
52
|
+
const fieldRemove = (index) => {
|
|
53
|
+
setValue((prev) => {
|
|
54
|
+
const valueClone = cloneDeep(prev);
|
|
55
|
+
valueClone?.splice(index, 1);
|
|
56
|
+
return valueClone;
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
const fieldPaste = (item, index) => {
|
|
60
|
+
setValue((prev) => {
|
|
61
|
+
const valueClone = cloneDeep(prev);
|
|
62
|
+
valueClone?.splice(index, 0, item);
|
|
63
|
+
return valueClone;
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
const fieldToggle = (item) => {
|
|
67
|
+
const itemIndex = indexOf(value, item);
|
|
68
|
+
if (itemIndex < 0) {
|
|
69
|
+
fieldPush(item);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
fieldRemove(itemIndex);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
value: value || fallbackValue,
|
|
77
|
+
api: {
|
|
78
|
+
push: fieldPush,
|
|
79
|
+
dropLeft: fieldDropLeft,
|
|
80
|
+
dropRight: fieldDropRight,
|
|
81
|
+
remove: fieldRemove,
|
|
82
|
+
paste: fieldPaste,
|
|
83
|
+
toggle: fieldToggle,
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
export default useFormArrayField;
|
|
88
|
+
//# sourceMappingURL=useFormArrayField.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormArrayField.js","sourceRoot":"","sources":["../../src/hooks/useFormArrayField.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAExD,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAE7D,OAAO,WAAW,MAAM,YAAY,CAAC;AAErC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,iBAAiB,GAAG,CAAI,SAAiB,EAAE,aAAmB,EAAE,EAAE;IACtE,MAAM,EAAE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pB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,MAAM,UAAU,GAAG,CAAC,IAAO,EAAE,KAAa,EAAE,EAAE;QAC5C,QAAQ,CAAC,CAAC,IAAI,EAAE,EAAE;YAChB,MAAM,UAAU,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;YACnC,UAAU,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;YACnC,OAAO,UAAU,CAAC;QACpB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,MAAM,WAAW,GAAG,CAAC,IAAO,EAAE,EAAE;QAC9B,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QACvC,IAAI,SAAS,GAAG,CAAC,EAAE,CAAC;YAClB,SAAS,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC;aAAM,CAAC;YACN,WAAW,CAAC,SAAS,CAAC,CAAC;QACzB,CAAC;IACH,CAAC,CAAC;IAEF,OAAO;QACL,KAAK,EAAE,KAAK,IAAI,aAAa;QAC7B,GAAG,EAAE;YACH,IAAI,EAAE,SAAS;YACf,QAAQ,EAAE,aAAa;YACvB,SAAS,EAAE,cAAc;YACzB,MAAM,EAAE,WAAW;YACnB,KAAK,EAAE,UAAU;YACjB,MAAM,EAAE,WAAW;SACpB;KACF,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook to access the form context containing form state and methods.
|
|
3
|
+
* This hook provides access to the FormState instance, validation errors,
|
|
4
|
+
* submission state, and form control methods.
|
|
5
|
+
*
|
|
6
|
+
* @returns Form context object with form state and methods
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* const MyComponent = () => {
|
|
11
|
+
* const { form, submitting, submitForm, validationErrors } = useFormContext();
|
|
12
|
+
*
|
|
13
|
+
* return (
|
|
14
|
+
* <div>
|
|
15
|
+
* <button onClick={submitForm} disabled={submitting}>
|
|
16
|
+
* {submitting ? 'Submitting...' : 'Submit'}
|
|
17
|
+
* </button>
|
|
18
|
+
* {validationErrors && <div>Validation errors occurred</div>}
|
|
19
|
+
* </div>
|
|
20
|
+
* );
|
|
21
|
+
* };
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
declare const useFormContext: () => import("..").FormContextType<object>;
|
|
27
|
+
export default useFormContext;
|
|
28
|
+
//# sourceMappingURL=useFormContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormContext.d.ts","sourceRoot":"","sources":["../../src/hooks/useFormContext.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,QAAA,MAAM,cAAc,4CAEnB,CAAC;AAEF,eAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import FormContext from '../context';
|
|
3
|
+
/**
|
|
4
|
+
* Hook to access the form context containing form state and methods.
|
|
5
|
+
* This hook provides access to the FormState instance, validation errors,
|
|
6
|
+
* submission state, and form control methods.
|
|
7
|
+
*
|
|
8
|
+
* @returns Form context object with form state and methods
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* const MyComponent = () => {
|
|
13
|
+
* const { form, submitting, submitForm, validationErrors } = useFormContext();
|
|
14
|
+
*
|
|
15
|
+
* return (
|
|
16
|
+
* <div>
|
|
17
|
+
* <button onClick={submitForm} disabled={submitting}>
|
|
18
|
+
* {submitting ? 'Submitting...' : 'Submit'}
|
|
19
|
+
* </button>
|
|
20
|
+
* {validationErrors && <div>Validation errors occurred</div>}
|
|
21
|
+
* </div>
|
|
22
|
+
* );
|
|
23
|
+
* };
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
const useFormContext = () => {
|
|
29
|
+
return useContext(FormContext);
|
|
30
|
+
};
|
|
31
|
+
export default useFormContext;
|
|
32
|
+
//# sourceMappingURL=useFormContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormContext.js","sourceRoot":"","sources":["../../src/hooks/useFormContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAEnC,OAAO,WAAW,MAAM,YAAY,CAAC;AAErC;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,cAAc,GAAG,GAAG,EAAE;IAC1B,OAAO,UAAU,CAAC,WAAW,CAAC,CAAC;AACjC,CAAC,CAAC;AAEF,eAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook to manage a single form field with value, validation, and touch state.
|
|
3
|
+
* This hook provides reactive access to a form field's value, errors, and touch state,
|
|
4
|
+
* along with methods to update the value and trigger validation.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type of the field value
|
|
7
|
+
* @param fieldPath - Dot notation path to the field (e.g., 'user.name')
|
|
8
|
+
* @param fallbackValue - Default value to use if field is undefined
|
|
9
|
+
* @param subscribe - Whether to subscribe to real-time value changes (default: true)
|
|
10
|
+
* @returns Object containing field value, error state, and control methods
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* const NameField = () => {
|
|
15
|
+
* const { value, error, isTouched, setValue, touch } = useFormField<string>('name', '');
|
|
16
|
+
*
|
|
17
|
+
* return (
|
|
18
|
+
* <div>
|
|
19
|
+
* <input
|
|
20
|
+
* value={value}
|
|
21
|
+
* onChange={(e) => setValue(e.target.value)}
|
|
22
|
+
* onBlur={() => touch('name')}
|
|
23
|
+
* />
|
|
24
|
+
* {isTouched && error && <span className="error">{error}</span>}
|
|
25
|
+
* </div>
|
|
26
|
+
* );
|
|
27
|
+
* };
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
declare const useFormField: <T>(fieldPath: string, fallbackValue?: T, subscribe?: boolean) => {
|
|
33
|
+
value: T | undefined;
|
|
34
|
+
error: string | undefined;
|
|
35
|
+
errors: import("yup").ValidationError[] | undefined;
|
|
36
|
+
isTouched: boolean;
|
|
37
|
+
validate: () => void;
|
|
38
|
+
touch: (fieldName: string) => void;
|
|
39
|
+
setValue: (newValue: T) => void;
|
|
40
|
+
};
|
|
41
|
+
export default useFormField;
|
|
42
|
+
//# sourceMappingURL=useFormField.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormField.d.ts","sourceRoot":"","sources":["../../src/hooks/useFormField.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,QAAA,MAAM,YAAY,GAAI,CAAC,EACrB,WAAW,MAAM,EACjB,gBAAgB,CAAC,EACjB,YAAY,OAAO;;;;;;;yBAwCN,CAAC;CAkBf,CAAC;AAEF,eAAe,YAAY,CAAC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import { first } from 'lodash';
|
|
3
|
+
import FormContext from '../context';
|
|
4
|
+
/**
|
|
5
|
+
* Hook to manage a single form field with value, validation, and touch state.
|
|
6
|
+
* This hook provides reactive access to a form field's value, errors, and touch state,
|
|
7
|
+
* along with methods to update the value and trigger validation.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of the field value
|
|
10
|
+
* @param fieldPath - Dot notation path to the field (e.g., 'user.name')
|
|
11
|
+
* @param fallbackValue - Default value to use if field is undefined
|
|
12
|
+
* @param subscribe - Whether to subscribe to real-time value changes (default: true)
|
|
13
|
+
* @returns Object containing field value, error state, and control methods
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* const NameField = () => {
|
|
18
|
+
* const { value, error, isTouched, setValue, touch } = useFormField<string>('name', '');
|
|
19
|
+
*
|
|
20
|
+
* return (
|
|
21
|
+
* <div>
|
|
22
|
+
* <input
|
|
23
|
+
* value={value}
|
|
24
|
+
* onChange={(e) => setValue(e.target.value)}
|
|
25
|
+
* onBlur={() => touch('name')}
|
|
26
|
+
* />
|
|
27
|
+
* {isTouched && error && <span className="error">{error}</span>}
|
|
28
|
+
* </div>
|
|
29
|
+
* );
|
|
30
|
+
* };
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
const useFormField = (fieldPath, fallbackValue, subscribe) => {
|
|
36
|
+
const { form, touched, validationErrors, validate, touch } = useContext(FormContext);
|
|
37
|
+
const [value, setValue] = useState(fallbackValue);
|
|
38
|
+
const errors = validationErrors?.[fieldPath];
|
|
39
|
+
const error = useMemo(() => {
|
|
40
|
+
return first(errors)?.message;
|
|
41
|
+
}, [errors]);
|
|
42
|
+
const isTouched = useMemo(() => {
|
|
43
|
+
return !!touched[fieldPath];
|
|
44
|
+
}, [fieldPath, touched]);
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
setValue(form?.getValue(fieldPath) || fallbackValue);
|
|
47
|
+
}, [fallbackValue, fieldPath, form]);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (!subscribe) {
|
|
50
|
+
const unsubscribe = form?.subscribeOnHistoryChange(() => {
|
|
51
|
+
setValue(form?.getValue(fieldPath) || fallbackValue);
|
|
52
|
+
});
|
|
53
|
+
return () => {
|
|
54
|
+
unsubscribe?.();
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const unsubscribe = form?.subscribeOnChange(setValue, fieldPath);
|
|
58
|
+
return () => {
|
|
59
|
+
unsubscribe?.();
|
|
60
|
+
};
|
|
61
|
+
}, [fallbackValue, fieldPath, form, subscribe]);
|
|
62
|
+
const handleSetValue = useCallback((newValue) => {
|
|
63
|
+
form?.setValue(fieldPath, newValue);
|
|
64
|
+
if (!subscribe) {
|
|
65
|
+
setValue(newValue);
|
|
66
|
+
}
|
|
67
|
+
}, [fieldPath, form, subscribe]);
|
|
68
|
+
return {
|
|
69
|
+
value,
|
|
70
|
+
error,
|
|
71
|
+
errors,
|
|
72
|
+
isTouched,
|
|
73
|
+
validate,
|
|
74
|
+
touch,
|
|
75
|
+
setValue: handleSetValue,
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
export default useFormField;
|
|
79
|
+
//# sourceMappingURL=useFormField.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormField.js","sourceRoot":"","sources":["../../src/hooks/useFormField.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAE9E,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAE/B,OAAO,WAAW,MAAM,YAAY,CAAC;AAErC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,YAAY,GAAG,CACnB,SAAiB,EACjB,aAAiB,EACjB,SAAmB,EACnB,EAAE;IACF,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,KAAK,EAAE,GACxD,UAAU,CAAC,WAAW,CAAC,CAAC;IAE1B,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAgB,aAAa,CAAC,CAAC;IAEjE,MAAM,MAAM,GAAG,gBAAgB,EAAE,CAAC,SAAS,CAAC,CAAC;IAE7C,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,EAAE;QACzB,OAAO,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAChC,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAEb,MAAM,SAAS,GAAG,OAAO,CAAC,GA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,EAAE,cAAc;KACzB,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,YAAY,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook to manage boolean toggle fields.
|
|
3
|
+
* This hook provides a simple way to handle boolean fields with toggle functionality.
|
|
4
|
+
*
|
|
5
|
+
* @param fieldPath - Dot notation path to the boolean field
|
|
6
|
+
* @returns Object containing current value and toggle function
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* const ToggleField = () => {
|
|
11
|
+
* const { value, toggleValue } = useFormFieldToggle('isActive');
|
|
12
|
+
*
|
|
13
|
+
* return (
|
|
14
|
+
* <label>
|
|
15
|
+
* <input
|
|
16
|
+
* type="checkbox"
|
|
17
|
+
* checked={value}
|
|
18
|
+
* onChange={toggleValue}
|
|
19
|
+
* />
|
|
20
|
+
* Active
|
|
21
|
+
* </label>
|
|
22
|
+
* );
|
|
23
|
+
* };
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
declare const useFormFieldToggle: (fieldPath: string) => {
|
|
29
|
+
value: boolean;
|
|
30
|
+
toggleValue: () => void;
|
|
31
|
+
};
|
|
32
|
+
export default useFormFieldToggle;
|
|
33
|
+
//# sourceMappingURL=useFormFieldToggle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormFieldToggle.d.ts","sourceRoot":"","sources":["../../src/hooks/useFormFieldToggle.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,QAAA,MAAM,kBAAkB,GAAI,WAAW,MAAM;;;CAqB5C,CAAC;AAEF,eAAe,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { useContext, useEffect, useState } from 'react';
|
|
2
|
+
import FormContext from '../context';
|
|
3
|
+
/**
|
|
4
|
+
* Hook to manage boolean toggle fields.
|
|
5
|
+
* This hook provides a simple way to handle boolean fields with toggle functionality.
|
|
6
|
+
*
|
|
7
|
+
* @param fieldPath - Dot notation path to the boolean field
|
|
8
|
+
* @returns Object containing current value and toggle function
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* const ToggleField = () => {
|
|
13
|
+
* const { value, toggleValue } = useFormFieldToggle('isActive');
|
|
14
|
+
*
|
|
15
|
+
* return (
|
|
16
|
+
* <label>
|
|
17
|
+
* <input
|
|
18
|
+
* type="checkbox"
|
|
19
|
+
* checked={value}
|
|
20
|
+
* onChange={toggleValue}
|
|
21
|
+
* />
|
|
22
|
+
* Active
|
|
23
|
+
* </label>
|
|
24
|
+
* );
|
|
25
|
+
* };
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
const useFormFieldToggle = (fieldPath) => {
|
|
31
|
+
const { form } = useContext(FormContext);
|
|
32
|
+
const [value, setValue] = useState(false);
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
setValue(form?.getValue(fieldPath) || false);
|
|
35
|
+
}, [fieldPath, form]);
|
|
36
|
+
const handleToggleValue = () => {
|
|
37
|
+
setValue((prev) => {
|
|
38
|
+
const nextValue = !prev;
|
|
39
|
+
form?.setValue(fieldPath, nextValue);
|
|
40
|
+
return nextValue;
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
value,
|
|
45
|
+
toggleValue: handleToggleValue,
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
export default useFormFieldToggle;
|
|
49
|
+
//# sourceMappingURL=useFormFieldToggle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormFieldToggle.js","sourceRoot":"","sources":["../../src/hooks/useFormFieldToggle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAExD,OAAO,WAAW,MAAM,YAAY,CAAC;AAErC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,kBAAkB,GAAG,CAAC,SAAiB,EAAE,EAAE;IAC/C,MAAM,EAAE,IAAI,EAAE,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;IAEzC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAE1C,SAAS,CAAC,GAAG,EAAE;QACb,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAU,SAAS,CAAC,IAAI,KAAK,CAAC,CAAC;IACxD,CAAC,EAAE,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC;IAEtB,MAAM,iBAAiB,GAAG,GAAG,EAAE;QAC7B,QAAQ,CAAC,CAAC,IAAI,EAAE,EAAE;YAChB,MAAM,SAAS,GAAG,CAAC,IAAI,CAAC;YACxB,IAAI,EAAE,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;YACrC,OAAO,SAAS,CAAC;QACnB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,OAAO;QACL,KAAK;QACL,WAAW,EAAE,iBAAiB;KAC/B,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook to manage form history for undo/redo functionality.
|
|
3
|
+
* This hook provides methods to navigate through form history and check availability of undo/redo operations.
|
|
4
|
+
*
|
|
5
|
+
* @returns Object containing history navigation methods and state
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* const HistoryControls = () => {
|
|
10
|
+
* const { prev, next, canPrev, canNext } = useFormHistory();
|
|
11
|
+
*
|
|
12
|
+
* return (
|
|
13
|
+
* <div>
|
|
14
|
+
* <button onClick={prev} disabled={!canPrev}>Undo</button>
|
|
15
|
+
* <button onClick={next} disabled={!canNext}>Redo</button>
|
|
16
|
+
* </div>
|
|
17
|
+
* );
|
|
18
|
+
* };
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
declare const useFormHistory: () => {
|
|
24
|
+
prev: () => boolean;
|
|
25
|
+
next: () => boolean;
|
|
26
|
+
canPrev: boolean;
|
|
27
|
+
canNext: boolean;
|
|
28
|
+
};
|
|
29
|
+
export default useFormHistory;
|
|
30
|
+
//# sourceMappingURL=useFormHistory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormHistory.d.ts","sourceRoot":"","sources":["../../src/hooks/useFormHistory.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,QAAA,MAAM,cAAc;;;;;CAiCnB,CAAC;AAEF,eAAe,cAAc,CAAC"}
|