@ehfuse/forma 1.0.4 → 1.2.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.
- package/README.md +107 -59
- package/dist/esm/hooks/useForm.d.ts +2 -4
- package/dist/esm/hooks/useForm.d.ts.map +1 -1
- package/dist/esm/hooks/useForm.js +36 -71
- package/dist/esm/hooks/useForm.js.map +1 -1
- package/dist/esm/hooks/useFormaState.d.ts +79 -0
- package/dist/esm/hooks/useFormaState.d.ts.map +1 -0
- package/dist/esm/hooks/useFormaState.js +154 -0
- package/dist/esm/hooks/useFormaState.js.map +1 -0
- package/dist/esm/index.d.ts +4 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/hooks/useForm.d.ts +2 -4
- package/dist/hooks/useForm.d.ts.map +1 -1
- package/dist/hooks/useForm.js +35 -70
- package/dist/hooks/useForm.js.map +1 -1
- package/dist/hooks/useFormaState.d.ts +79 -0
- package/dist/hooks/useFormaState.d.ts.map +1 -0
- package/dist/hooks/useFormaState.js +157 -0
- package/dist/hooks/useFormaState.js.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useFormaState.ts
|
|
3
|
+
*
|
|
4
|
+
* Advanced state management hook with individual field subscriptions
|
|
5
|
+
* Optimized for arrays, objects, and complex nested data structures
|
|
6
|
+
*
|
|
7
|
+
* @author KIM YOUNG JIN (ehfuse@gmail.com)
|
|
8
|
+
* @license MIT License
|
|
9
|
+
*/
|
|
10
|
+
import { useRef, useCallback, useMemo, useState, useEffect } from "react";
|
|
11
|
+
import { FieldStore } from "../core/FieldStore";
|
|
12
|
+
import { setNestedValue } from "../utils/dotNotation";
|
|
13
|
+
/**
|
|
14
|
+
* Individual field subscription for useFormaState
|
|
15
|
+
* 개별 필드 구독을 위한 내부 함수
|
|
16
|
+
*/
|
|
17
|
+
function useFieldValue(store, fieldName) {
|
|
18
|
+
const [value, setValue] = useState(() => store.getValue(fieldName));
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
// 구독 설정 / Setup subscription
|
|
21
|
+
const unsubscribe = store.subscribe(fieldName, () => {
|
|
22
|
+
const newValue = store.getValue(fieldName);
|
|
23
|
+
setValue(newValue);
|
|
24
|
+
});
|
|
25
|
+
return unsubscribe;
|
|
26
|
+
}, [fieldName]); // store 의존성 제거로 불필요한 재구독 방지 / Remove store dependency to prevent unnecessary re-subscriptions
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Advanced state management hook with individual field subscriptions
|
|
31
|
+
* 개별 필드 구독을 통한 고급 상태 관리 훅
|
|
32
|
+
*
|
|
33
|
+
* Optimized for managing complex arrays, objects, and nested data structures
|
|
34
|
+
* where you want to avoid unnecessary re-renders when only specific fields change.
|
|
35
|
+
*
|
|
36
|
+
* 복잡한 배열, 객체, 중첩된 데이터 구조를 관리하는 데 최적화되어 있으며,
|
|
37
|
+
* 특정 필드만 변경될 때 불필요한 재렌더링을 방지하고자 할 때 사용합니다.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* // Managing an array of users
|
|
42
|
+
* const state = useFormaState({
|
|
43
|
+
* initialValues: {
|
|
44
|
+
* users: [
|
|
45
|
+
* { name: 'John', email: 'john@example.com', age: 30 },
|
|
46
|
+
* { name: 'Jane', email: 'jane@example.com', age: 25 }
|
|
47
|
+
* ],
|
|
48
|
+
* settings: { theme: 'dark', notifications: true }
|
|
49
|
+
* }
|
|
50
|
+
* });
|
|
51
|
+
*
|
|
52
|
+
* // Subscribe to individual fields - only these components re-render when changed
|
|
53
|
+
* const firstName = state.useValue('users.0.name'); // Only re-renders when John's name changes
|
|
54
|
+
* const userAge = state.useValue('users.1.age'); // Only re-renders when Jane's age changes
|
|
55
|
+
* const theme = state.useValue('settings.theme'); // Only re-renders when theme changes
|
|
56
|
+
*
|
|
57
|
+
* // Update specific fields
|
|
58
|
+
* state.setValue('users.0.name', 'Johnny');
|
|
59
|
+
* state.setValue('settings.theme', 'light');
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function useFormaState(initialValues, options = {}) {
|
|
63
|
+
const { onChange, deepEquals = false, _externalStore } = options;
|
|
64
|
+
// Create or use external field store instance (persists across renders)
|
|
65
|
+
// 필드 스토어 인스턴스 생성 또는 외부 스토어 사용 (렌더링 간 유지)
|
|
66
|
+
const storeRef = useRef(null);
|
|
67
|
+
if (_externalStore) {
|
|
68
|
+
storeRef.current = _externalStore;
|
|
69
|
+
}
|
|
70
|
+
else if (!storeRef.current) {
|
|
71
|
+
storeRef.current = new FieldStore(initialValues);
|
|
72
|
+
// Set up global change listener if provided
|
|
73
|
+
// 글로벌 변경 리스너 설정 (제공된 경우)
|
|
74
|
+
if (onChange) {
|
|
75
|
+
storeRef.current.subscribeGlobal(() => {
|
|
76
|
+
onChange(storeRef.current.getValues());
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const store = storeRef.current;
|
|
81
|
+
// Update initial values when they change (only for non-external stores)
|
|
82
|
+
// 초기값이 변경되면 업데이트 (외부 스토어가 아닌 경우에만)
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
if (!_externalStore && store) {
|
|
85
|
+
store.setInitialValues(initialValues);
|
|
86
|
+
}
|
|
87
|
+
}, [initialValues, store, _externalStore]);
|
|
88
|
+
// Subscribe to a specific field value with dot notation
|
|
89
|
+
// dot notation으로 특정 필드 값 구독
|
|
90
|
+
const useValue = useCallback((path) => {
|
|
91
|
+
return useFieldValue(store, path);
|
|
92
|
+
}, [store]);
|
|
93
|
+
// Set a specific field value with dot notation
|
|
94
|
+
// dot notation으로 특정 필드 값 설정
|
|
95
|
+
const setValue = useCallback((path, value) => {
|
|
96
|
+
const currentValues = store.getValues();
|
|
97
|
+
const newValues = setNestedValue(currentValues, path, value);
|
|
98
|
+
store.setValues(newValues);
|
|
99
|
+
}, [store]);
|
|
100
|
+
// Get all current values (non-reactive)
|
|
101
|
+
// 모든 현재 값 가져오기 (반응형 아님)
|
|
102
|
+
const getValues = useCallback(() => {
|
|
103
|
+
return store.getValues();
|
|
104
|
+
}, [store]);
|
|
105
|
+
// Set all values at once
|
|
106
|
+
// 모든 값을 한 번에 설정
|
|
107
|
+
const setValues = useCallback((values) => {
|
|
108
|
+
const currentValues = store.getValues();
|
|
109
|
+
const newValues = { ...currentValues, ...values };
|
|
110
|
+
store.setValues(newValues);
|
|
111
|
+
}, [store]);
|
|
112
|
+
// Reset to initial values
|
|
113
|
+
// 초기값으로 재설정
|
|
114
|
+
const reset = useCallback(() => {
|
|
115
|
+
store.setValues(initialValues);
|
|
116
|
+
}, [store, initialValues]);
|
|
117
|
+
// Handle standard input change events
|
|
118
|
+
// 표준 입력 변경 이벤트 처리
|
|
119
|
+
const handleChange = useCallback((event) => {
|
|
120
|
+
const { name, value, type } = event.target;
|
|
121
|
+
if (!name) {
|
|
122
|
+
console.warn('useFormaState.handleChange: input element must have a "name" attribute');
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
let processedValue = value;
|
|
126
|
+
// Handle different input types
|
|
127
|
+
// 다양한 입력 타입 처리
|
|
128
|
+
if (type === "checkbox") {
|
|
129
|
+
processedValue = event.target.checked;
|
|
130
|
+
}
|
|
131
|
+
else if (type === "number") {
|
|
132
|
+
processedValue = value === "" ? "" : Number(value);
|
|
133
|
+
}
|
|
134
|
+
setValue(name, processedValue);
|
|
135
|
+
}, [setValue]);
|
|
136
|
+
// Get reactive values using the store's reactive mechanism
|
|
137
|
+
// 스토어의 반응형 메커니즘을 사용하여 반응형 값 가져오기
|
|
138
|
+
const values = useMemo(() => {
|
|
139
|
+
// This creates a reactive subscription to all values
|
|
140
|
+
// 모든 값에 대한 반응형 구독 생성
|
|
141
|
+
return store.getValues();
|
|
142
|
+
}, [store]);
|
|
143
|
+
return {
|
|
144
|
+
useValue,
|
|
145
|
+
setValue,
|
|
146
|
+
getValues,
|
|
147
|
+
setValues,
|
|
148
|
+
reset,
|
|
149
|
+
handleChange,
|
|
150
|
+
_store: store,
|
|
151
|
+
values,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=useFormaState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormaState.js","sourceRoot":"","sources":["../../../hooks/useFormaState.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAC1E,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAkB,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAmDtE;;;GAGG;AACH,SAAS,aAAa,CAAI,KAAsB,EAAE,SAAiB;IAC/D,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC;IAEpE,SAAS,CAAC,GAAG,EAAE;QACX,6BAA6B;QAC7B,MAAM,WAAW,GAAG,KAAK,CAAC,SAAS,CAAC,SAAS,EAAE,GAAG,EAAE;YAChD,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;YAC3C,QAAQ,CAAC,QAAQ,CAAC,CAAC;QACvB,CAAC,CAAC,CAAC;QAEH,OAAO,WAAW,CAAC;IACvB,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,8FAA8F;IAE/G,OAAO,KAAK,CAAC;AACjB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,UAAU,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+B;QAC/B,eAAe;QACf,IAAI,IAAI,KAAK,UAAU,EAAE,CAAC;YACtB,cAAc,GAAI,KAAK,CAAC,MAA2B,CAAC,OAAO,CAAC;QAChE,CAAC;aAAM,IAAI,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC3B,cAAc,GAAG,KAAK,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACvD,CAAC;QAED,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC,CAAC;IACnC,CAAC,EACD,CAAC,QAAQ,CAAC,CACb,CAAC;IAEF,2DAA2D;IAC3D,iCAAiC;IACjC,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,EAAE;QACxB,qDAAqD;QACrD,qBAAqB;QACrB,OAAO,KAAK,CAAC,SAAS,EAAE,CAAC;IAC7B,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,OAAO;QACH,QAAQ;QACR,QAAQ;QACR,SAAS;QACT,SAAS;QACT,KAAK;QACL,YAAY;QACZ,MAAM,EAAE,KAAK;QACb,MAAM;KACT,CAAC;AACN,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* index.ts
|
|
3
3
|
*
|
|
4
|
-
* Forma -
|
|
4
|
+
* Forma - Advexport { useFormaState } from './hooks/useFormaState';nced React form state management library
|
|
5
5
|
* Main entry point and API exports
|
|
6
6
|
*
|
|
7
7
|
* @license MIT License
|
|
@@ -29,10 +29,12 @@
|
|
|
29
29
|
export { useForm } from "./hooks/useForm";
|
|
30
30
|
export { useGlobalForm } from "./hooks/useGlobalForm";
|
|
31
31
|
export { useRegisterGlobalForm } from "./hooks/useRegisterGlobalForm";
|
|
32
|
+
export { useFormaState } from "./hooks/useFormaState";
|
|
32
33
|
export { GlobalFormContext, GlobalFormProvider, } from "./contexts/GlobalFormContext";
|
|
33
34
|
export { FieldStore } from "./core/FieldStore";
|
|
34
35
|
export { getNestedValue, setNestedValue } from "./utils/dotNotation";
|
|
35
|
-
export type { UseFormProps, UseFormReturn,
|
|
36
|
+
export type { UseFormProps, UseFormReturn, FormValidationResult, FormChangeEvent, } from "./types/form";
|
|
37
|
+
export type { UseFormaStateOptions, UseFormaStateReturn, } from "./hooks/useFormaState";
|
|
36
38
|
export type { UseGlobalFormProps, UseGlobalFormReturn, GlobalFormProviderProps, GlobalFormContextType, GlobalFormStoreMap, GlobalFormMetadata, GlobalFormEvent, GlobalFormEventListener, GlobalFormConfig, GlobalFormMiddleware, ExtendedGlobalFormProviderProps, } from "./types/globalForm";
|
|
37
39
|
/**
|
|
38
40
|
* Forma library version
|
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAGtD,OAAO,EACH,iBAAiB,EACjB,kBAAkB,GACrB,MAAM,8BAA8B,CAAC;AAGtC,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAG/C,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAIrE,YAAY,EACR,YAAY,EACZ,aAAa,EACb,oBAAoB,EACpB,eAAe,GAClB,MAAM,cAAc,CAAC;AAGtB,YAAY,EACR,oBAAoB,EACpB,mBAAmB,GACtB,MAAM,uBAAuB,CAAC;AAG/B,YAAY,EACR,kBAAkB,EAClB,mBAAmB,EACnB,uBAAuB,EACvB,qBAAqB,EACrB,kBAAkB,EAClB,kBAAkB,EAClB,eAAe,EACf,uBAAuB,EACvB,gBAAgB,EAChB,oBAAoB,EACpB,+BAA+B,GAClC,MAAM,oBAAoB,CAAC;AAE5B;;GAEG;AACH,eAAO,MAAM,aAAa,UAAU,CAAC;AAErC;;GAEG;AACH,eAAO,MAAM,cAAc;;;;;;;;CAejB,CAAC"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* index.ts
|
|
3
3
|
*
|
|
4
|
-
* Forma -
|
|
4
|
+
* Forma - Advexport { useFormaState } from './hooks/useFormaState';nced React form state management library
|
|
5
5
|
* Main entry point and API exports
|
|
6
6
|
*
|
|
7
7
|
* @license MIT License
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
export { useForm } from "./hooks/useForm";
|
|
31
31
|
export { useGlobalForm } from "./hooks/useGlobalForm";
|
|
32
32
|
export { useRegisterGlobalForm } from "./hooks/useRegisterGlobalForm";
|
|
33
|
+
export { useFormaState } from "./hooks/useFormaState";
|
|
33
34
|
// ===== Context & Providers =====
|
|
34
35
|
export { GlobalFormContext, GlobalFormProvider, } from "./contexts/GlobalFormContext";
|
|
35
36
|
// ===== Core Classes =====
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,yBAAyB;AACzB,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,yBAAyB;AACzB,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,kCAAkC;AAClC,OAAO,EACH,iBAAiB,EACjB,kBAAkB,GACrB,MAAM,8BAA8B,CAAC;AAEtC,2BAA2B;AAC3B,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE/C,gCAAgC;AAChC,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAgCrE;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,OAAO,CAAC;AAErC;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG;IAC1B,IAAI,EAAE,OAAO;IACb,OAAO,EAAE,aAAa;IACtB,WAAW,EAAE,8CAA8C;IAC3D,MAAM,EAAE,+BAA+B;IACvC,KAAK,EAAE,kBAAkB;IACzB,OAAO,EAAE,KAAK;IACd,QAAQ,EAAE;QACN,4DAA4D;QAC5D,kDAAkD;QAClD,wCAAwC;QACxC,8BAA8B;QAC9B,6BAA6B;QAC7B,qCAAqC;KACxC;CACK,CAAC"}
|
package/dist/hooks/useForm.d.ts
CHANGED
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
28
|
* SOFTWARE.
|
|
29
29
|
*/
|
|
30
|
-
import { FieldStore } from "../core/FieldStore";
|
|
31
30
|
import { FormChangeEvent, DatePickerChangeHandler, UseFormProps } from "../types/form";
|
|
32
31
|
import React from "react";
|
|
33
32
|
/**
|
|
@@ -50,7 +49,7 @@ export declare function useForm<T extends Record<string, any>>({ initialValues,
|
|
|
50
49
|
isSubmitting: boolean;
|
|
51
50
|
isValidating: boolean;
|
|
52
51
|
isModified: boolean;
|
|
53
|
-
useFormValue: (fieldName: string) =>
|
|
52
|
+
useFormValue: (fieldName: string) => any;
|
|
54
53
|
getFormValue: (fieldName: string) => any;
|
|
55
54
|
getFormValues: () => T;
|
|
56
55
|
setFormValue: (name: string, value: any) => void;
|
|
@@ -61,7 +60,6 @@ export declare function useForm<T extends Record<string, any>>({ initialValues,
|
|
|
61
60
|
submit: (e?: React.FormEvent) => Promise<boolean>;
|
|
62
61
|
resetForm: () => void;
|
|
63
62
|
validateForm: (valuesToValidate?: T) => Promise<boolean>;
|
|
64
|
-
|
|
65
|
-
_store: FieldStore<T>;
|
|
63
|
+
_store: import("..").FieldStore<T>;
|
|
66
64
|
};
|
|
67
65
|
//# sourceMappingURL=useForm.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useForm.d.ts","sourceRoot":"","sources":["../../hooks/useForm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"useForm.d.ts","sourceRoot":"","sources":["../../hooks/useForm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,EACH,eAAe,EACf,uBAAuB,EACvB,YAAY,EACf,MAAM,eAAe,CAAC;AAGvB,OAAO,KAMN,MAAM,OAAO,CAAC;AAKf;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,EACnD,aAAa,EACb,QAAQ,EACR,UAAU,EACV,UAAU,EACV,cAAc,GACjB,EAAE,YAAY,CAAC,CAAC,CAAC;;;;8BA8IE,MAAM;8BAnBN,MAAM,KAAG,GAAG;yBASU,CAAC;yBAhD5B,MAAM,SAAS,GAAG;+BAmBb,OAAO,CAAC,CAAC,CAAC;6CAUH,CAAC;0BApFhB,eAAe;;iBAyJR,KAAK,CAAC,SAAS,KAAG,OAAO,CAAC,OAAO,CAAC;;sCA9BnB,CAAC;;EA2GlC"}
|
package/dist/hooks/useForm.js
CHANGED
|
@@ -30,25 +30,8 @@
|
|
|
30
30
|
*/
|
|
31
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
32
|
exports.useForm = useForm;
|
|
33
|
-
const
|
|
33
|
+
const useFormaState_1 = require("./useFormaState");
|
|
34
34
|
const react_1 = require("react");
|
|
35
|
-
/**
|
|
36
|
-
* 개별 필드 구독 / Individual field subscription
|
|
37
|
-
* 특정 필드만 구독하여 해당 필드가 변경될 때만 리렌더링
|
|
38
|
-
* Subscribe to specific field only, re-render only when that field changes
|
|
39
|
-
*/
|
|
40
|
-
function useFieldValue(store, fieldName) {
|
|
41
|
-
const [value, setValue] = (0, react_1.useState)(() => store.getValue(fieldName));
|
|
42
|
-
(0, react_1.useEffect)(() => {
|
|
43
|
-
// 구독 설정 / Setup subscription
|
|
44
|
-
const unsubscribe = store.subscribe(fieldName, () => {
|
|
45
|
-
const newValue = store.getValue(fieldName);
|
|
46
|
-
setValue(newValue);
|
|
47
|
-
});
|
|
48
|
-
return unsubscribe;
|
|
49
|
-
}, [fieldName]); // store 의존성 제거로 불필요한 재구독 방지 / Remove store dependency to prevent unnecessary re-subscriptions
|
|
50
|
-
return value;
|
|
51
|
-
}
|
|
52
35
|
/**
|
|
53
36
|
* Forma 핵심 폼 관리 훅 / Forma core form management hook
|
|
54
37
|
*
|
|
@@ -66,38 +49,22 @@ function useFieldValue(store, fieldName) {
|
|
|
66
49
|
* @returns 폼 관리 API 객체 / Form management API object
|
|
67
50
|
*/
|
|
68
51
|
function useForm({ initialValues, onSubmit, onValidate, onComplete, _externalStore, }) {
|
|
69
|
-
//
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (_externalStore) {
|
|
73
|
-
storeRef.current = _externalStore;
|
|
74
|
-
}
|
|
75
|
-
else if (!storeRef.current) {
|
|
76
|
-
storeRef.current = new FieldStore_1.FieldStore(initialValues);
|
|
77
|
-
}
|
|
78
|
-
const store = storeRef.current;
|
|
52
|
+
// useFormaState를 기반으로 사용 / Use useFormaState as foundation
|
|
53
|
+
const fieldState = (0, useFormaState_1.useFormaState)(initialValues, { _externalStore });
|
|
54
|
+
// 폼 특정 상태 관리 / Form-specific state management
|
|
79
55
|
const [isSubmitting, setIsSubmitting] = (0, react_1.useState)(false);
|
|
80
56
|
const [isValidating, setIsValidating] = (0, react_1.useState)(false);
|
|
57
|
+
// 폼이 수정되었는지 확인 / Check if form is modified
|
|
58
|
+
// Store의 변경 사항을 추적하여 효율적으로 감지
|
|
81
59
|
const [isModified, setIsModified] = (0, react_1.useState)(false);
|
|
82
|
-
// 전역 상태 구독 (isModified 추적) / Global state subscription (isModified tracking)
|
|
83
|
-
(0, react_1.useEffect)(() => {
|
|
84
|
-
const checkModified = () => {
|
|
85
|
-
const modified = store.isModified();
|
|
86
|
-
setIsModified(modified);
|
|
87
|
-
};
|
|
88
|
-
checkModified();
|
|
89
|
-
const unsubscribe = store.subscribeGlobal(checkModified);
|
|
90
|
-
return unsubscribe;
|
|
91
|
-
}, [store]);
|
|
92
|
-
// 호환성을 위한 values 객체 (비권장) / Values object for compatibility (not recommended)
|
|
93
|
-
const [valuesSnapshot, setValuesSnapshot] = (0, react_1.useState)(() => store.getValues());
|
|
94
60
|
(0, react_1.useEffect)(() => {
|
|
95
|
-
const unsubscribe =
|
|
96
|
-
|
|
61
|
+
const unsubscribe = fieldState._store.subscribeGlobal(() => {
|
|
62
|
+
setIsModified(fieldState._store.isModified());
|
|
97
63
|
});
|
|
64
|
+
// 초기 상태 설정
|
|
65
|
+
setIsModified(fieldState._store.isModified());
|
|
98
66
|
return unsubscribe;
|
|
99
|
-
}, [
|
|
100
|
-
const values = valuesSnapshot;
|
|
67
|
+
}, [fieldState._store]);
|
|
101
68
|
/**
|
|
102
69
|
* 통합 폼 변경 핸들러 / Unified form change handler
|
|
103
70
|
* MUI Select, TextField, DatePicker 등 모든 컴포넌트 지원
|
|
@@ -125,8 +92,8 @@ function useForm({ initialValues, onSubmit, onValidate, onComplete, _externalSto
|
|
|
125
92
|
else if (value === null) {
|
|
126
93
|
newValue = undefined;
|
|
127
94
|
}
|
|
128
|
-
|
|
129
|
-
}, [
|
|
95
|
+
fieldState.setValue(name, newValue);
|
|
96
|
+
}, [fieldState.setValue]);
|
|
130
97
|
/**
|
|
131
98
|
* DatePicker 전용 변경 핸들러 / DatePicker-specific change handler
|
|
132
99
|
* 간편한 사용을 위한 커링 함수 / Curried function for convenient usage
|
|
@@ -141,9 +108,9 @@ function useForm({ initialValues, onSubmit, onValidate, onComplete, _externalSto
|
|
|
141
108
|
else if (value === null) {
|
|
142
109
|
newValue = undefined;
|
|
143
110
|
}
|
|
144
|
-
|
|
111
|
+
fieldState.setValue(fieldName, newValue);
|
|
145
112
|
};
|
|
146
|
-
}, [
|
|
113
|
+
}, [fieldState.setValue]);
|
|
147
114
|
/**
|
|
148
115
|
* 개별 필드 값 설정 / Set individual field value
|
|
149
116
|
*/
|
|
@@ -156,40 +123,40 @@ function useForm({ initialValues, onSubmit, onValidate, onComplete, _externalSto
|
|
|
156
123
|
else if (value === null) {
|
|
157
124
|
processedValue = undefined;
|
|
158
125
|
}
|
|
159
|
-
|
|
160
|
-
}, [
|
|
126
|
+
fieldState.setValue(name, processedValue);
|
|
127
|
+
}, [fieldState.setValue]);
|
|
161
128
|
/**
|
|
162
129
|
* 전체 폼 값 설정 / Set all form values
|
|
163
130
|
*/
|
|
164
131
|
const setFormValues = (0, react_1.useCallback)((newValues) => {
|
|
165
|
-
|
|
166
|
-
}, [
|
|
132
|
+
fieldState.setValues(newValues);
|
|
133
|
+
}, [fieldState.setValues]);
|
|
167
134
|
/**
|
|
168
135
|
* 초기값 재설정 / Reset initial values
|
|
169
136
|
*/
|
|
170
137
|
const setInitialFormValues = (0, react_1.useCallback)((newInitialValues) => {
|
|
171
|
-
|
|
172
|
-
}, [
|
|
138
|
+
fieldState._store.setInitialValues(newInitialValues);
|
|
139
|
+
}, [fieldState._store]);
|
|
173
140
|
/**
|
|
174
141
|
* 구독 없이 현재 값만 가져오기 / Get current value without subscription
|
|
175
142
|
*/
|
|
176
143
|
const getFormValue = (0, react_1.useCallback)((fieldName) => {
|
|
177
|
-
return
|
|
178
|
-
}, [
|
|
144
|
+
return fieldState._store.getValue(fieldName);
|
|
145
|
+
}, [fieldState._store]);
|
|
179
146
|
/**
|
|
180
147
|
* 모든 폼 값 가져오기 / Get all form values
|
|
181
148
|
*/
|
|
182
149
|
const getFormValues = (0, react_1.useCallback)(() => {
|
|
183
|
-
return
|
|
184
|
-
}, [
|
|
150
|
+
return fieldState.getValues();
|
|
151
|
+
}, [fieldState.getValues]);
|
|
185
152
|
/**
|
|
186
153
|
* 개별 필드 구독 Hook / Individual field subscription hook
|
|
187
154
|
* 해당 필드가 변경될 때만 컴포넌트가 리렌더링됩니다
|
|
188
155
|
* Component re-renders only when the specific field changes
|
|
189
156
|
*/
|
|
190
157
|
const useFormValue = (0, react_1.useCallback)((fieldName) => {
|
|
191
|
-
return
|
|
192
|
-
}, [
|
|
158
|
+
return fieldState.useValue(fieldName);
|
|
159
|
+
}, [fieldState.useValue]);
|
|
193
160
|
/**
|
|
194
161
|
* 폼 검증 / Form validation
|
|
195
162
|
*/
|
|
@@ -197,7 +164,7 @@ function useForm({ initialValues, onSubmit, onValidate, onComplete, _externalSto
|
|
|
197
164
|
if (!onValidate)
|
|
198
165
|
return true;
|
|
199
166
|
setIsValidating(true);
|
|
200
|
-
const currentValues = valuesToValidate ||
|
|
167
|
+
const currentValues = valuesToValidate || fieldState.getValues();
|
|
201
168
|
try {
|
|
202
169
|
return await onValidate(currentValues);
|
|
203
170
|
}
|
|
@@ -208,22 +175,22 @@ function useForm({ initialValues, onSubmit, onValidate, onComplete, _externalSto
|
|
|
208
175
|
finally {
|
|
209
176
|
setIsValidating(false);
|
|
210
177
|
}
|
|
211
|
-
}, [onValidate,
|
|
178
|
+
}, [onValidate, fieldState.getValues]);
|
|
212
179
|
/**
|
|
213
180
|
* 폼 초기화 / Reset form
|
|
214
181
|
*/
|
|
215
182
|
const resetForm = (0, react_1.useCallback)(() => {
|
|
216
|
-
|
|
183
|
+
fieldState.reset();
|
|
217
184
|
setIsSubmitting(false);
|
|
218
185
|
setIsValidating(false);
|
|
219
|
-
}, [
|
|
186
|
+
}, [fieldState.reset]);
|
|
220
187
|
/**
|
|
221
188
|
* 폼 제출 / Submit form
|
|
222
189
|
*/
|
|
223
190
|
const submit = (0, react_1.useCallback)(async (e) => {
|
|
224
191
|
if (e)
|
|
225
192
|
e.preventDefault();
|
|
226
|
-
const currentValues =
|
|
193
|
+
const currentValues = fieldState.getValues();
|
|
227
194
|
if (!(await validateForm(currentValues))) {
|
|
228
195
|
return false;
|
|
229
196
|
}
|
|
@@ -244,7 +211,7 @@ function useForm({ initialValues, onSubmit, onValidate, onComplete, _externalSto
|
|
|
244
211
|
finally {
|
|
245
212
|
setIsSubmitting(false);
|
|
246
213
|
}
|
|
247
|
-
}, [onSubmit, onComplete, validateForm,
|
|
214
|
+
}, [onSubmit, onComplete, validateForm, fieldState.getValues]);
|
|
248
215
|
return (0, react_1.useMemo)(() => ({
|
|
249
216
|
// 상태 / State
|
|
250
217
|
isSubmitting,
|
|
@@ -265,10 +232,8 @@ function useForm({ initialValues, onSubmit, onValidate, onComplete, _externalSto
|
|
|
265
232
|
submit, // 폼 제출 / submit form
|
|
266
233
|
resetForm, // 폼 초기화 / reset form
|
|
267
234
|
validateForm, // 폼 검증 / validate form
|
|
268
|
-
// 호환성 / Compatibility
|
|
269
|
-
values, // 비권장: 전체 리렌더링 발생 / not recommended: causes full re-renders
|
|
270
235
|
// 고급 사용 / Advanced usage
|
|
271
|
-
_store:
|
|
236
|
+
_store: fieldState._store, // 직접 store 접근용 / direct store access
|
|
272
237
|
}), [
|
|
273
238
|
isSubmitting,
|
|
274
239
|
isValidating,
|
|
@@ -284,7 +249,7 @@ function useForm({ initialValues, onSubmit, onValidate, onComplete, _externalSto
|
|
|
284
249
|
submit,
|
|
285
250
|
resetForm,
|
|
286
251
|
validateForm,
|
|
287
|
-
|
|
252
|
+
fieldState._store, // Store 의존성으로 대체
|
|
288
253
|
]);
|
|
289
254
|
}
|
|
290
255
|
//# sourceMappingURL=useForm.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useForm.js","sourceRoot":"","sources":["../../hooks/useForm.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;;
|
|
1
|
+
{"version":3,"file":"useForm.js","sourceRoot":"","sources":["../../hooks/useForm.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;;AAoCH,0BAyQC;AAtSD,mDAAgD;AAEhD,iCAMe;AAKf;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,OAAO,CAAgC,EACnD,aAAa,EACb,QAAQ,EACR,UAAU,EACV,UAAU,EACV,cAAc,GACA;IACd,2DAA2D;IAC3D,MAAM,UAAU,GAAG,IAAA,6BAAa,EAAI,aAAa,EAAE,EAAE,cAAc,EAAE,CAAC,CAAC;IAEvE,8CAA8C;IAC9C,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,IAAA,gBAAQ,EAAC,KAAK,CAAC,CAAC;IACxD,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,IAAA,gBAAQ,EAAC,KAAK,CAAC,CAAC;IAExD,2CAA2C;IAC3C,8BAA8B;IAC9B,MAAM,CAAC,UAAU,E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jB,CAAC;gBAAS,CAAC;YACP,eAAe,CAAC,KAAK,CAAC,CAAC;QAC3B,CAAC;IACL,CAAC,EACD,CAAC,QAAQ,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,CAAC,SAAS,CAAC,CAC7D,CAAC;IAEF,OAAO,IAAA,eAAO,EACV,GAAG,EAAE,CAAC,CAAC;QACH,aAAa;QACb,YAAY;QACZ,YAAY;QACZ,UAAU;QAEV,sBAAsB;QACtB,YAAY,EAAE,oEAAoE;QAClF,YAAY,EAAE,uEAAuE;QACrF,aAAa,EAAE,oCAAoC;QAEnD,oBAAoB;QACpB,YAAY,EAAE,kCAAkC;QAChD,aAAa,EAAE,2BAA2B;QAC1C,oBAAoB,EAAE,iCAAiC;QAEvD,2BAA2B;QAC3B,gBAAgB,EAAE,2EAA2E;QAC7F,sBAAsB,EAAE,wDAAwD;QAEhF,sBAAsB;QACtB,MAAM,EAAE,qBAAqB;QAC7B,SAAS,EAAE,qBAAqB;QAChC,YAAY,EAAE,uBAAuB;QAErC,yBAAyB;QACzB,MAAM,EAAE,UAAU,CAAC,MAAM,EAAE,qCAAqC;KACnE,CAAC,EACF;QACI,YAAY;QACZ,YAAY;QACZ,UAAU;QACV,YAAY;QACZ,YAAY;QACZ,aAAa;QACb,YAAY;QACZ,aAAa;QACb,oBAAoB;QACpB,gBAAgB;QAChB,sBAAsB;QACtB,MAAM;QACN,SAAS;QACT,YAAY;QACZ,UAAU,CAAC,MAAM,EAAE,iBAAiB;KACvC,CACJ,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useFormaState.ts
|
|
3
|
+
*
|
|
4
|
+
* Advanced state management hook with individual field subscriptions
|
|
5
|
+
* Optimized for arrays, objects, and complex nested data structures
|
|
6
|
+
*
|
|
7
|
+
* @author KIM YOUNG JIN (ehfuse@gmail.com)
|
|
8
|
+
* @license MIT License
|
|
9
|
+
*/
|
|
10
|
+
import { FieldStore } from "../core/FieldStore";
|
|
11
|
+
/**
|
|
12
|
+
* Options for configuring useFormaState hook
|
|
13
|
+
* useFormaState 훅 설정을 위한 옵션
|
|
14
|
+
*/
|
|
15
|
+
export interface UseFormaStateOptions<T extends Record<string, any>> {
|
|
16
|
+
/** Optional callback when state changes | 상태 변경 시 선택적 콜백 */
|
|
17
|
+
onChange?: (values: T) => void;
|
|
18
|
+
/** Enable deep equality checking for better performance | 성능 향상을 위한 깊은 동등성 검사 활성화 */
|
|
19
|
+
deepEquals?: boolean;
|
|
20
|
+
/** External FieldStore instance for shared state | 공유 상태를 위한 외부 FieldStore 인스턴스 */
|
|
21
|
+
_externalStore?: FieldStore<T>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Return type of useFormaState hook
|
|
25
|
+
* useFormaState 훅의 반환 타입
|
|
26
|
+
*/
|
|
27
|
+
export interface UseFormaStateReturn<T extends Record<string, any>> {
|
|
28
|
+
/** Subscribe to a specific field value with dot notation | dot notation으로 특정 필드 값 구독 */
|
|
29
|
+
useValue: <K extends string>(path: K) => any;
|
|
30
|
+
/** Set a specific field value with dot notation | dot notation으로 특정 필드 값 설정 */
|
|
31
|
+
setValue: <K extends string>(path: K, value: any) => void;
|
|
32
|
+
/** Get all current values (non-reactive) | 모든 현재 값 가져오기 (반응형 아님) */
|
|
33
|
+
getValues: () => T;
|
|
34
|
+
/** Set all values at once | 모든 값을 한 번에 설정 */
|
|
35
|
+
setValues: (values: Partial<T>) => void;
|
|
36
|
+
/** Reset to initial values | 초기값으로 재설정 */
|
|
37
|
+
reset: () => void;
|
|
38
|
+
/** Handle standard input change events | 표준 입력 변경 이벤트 처리 */
|
|
39
|
+
handleChange: (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>) => void;
|
|
40
|
+
/** Direct access to the internal store for advanced usage | 고급 사용을 위한 내부 스토어 직접 접근 */
|
|
41
|
+
_store: FieldStore<T>;
|
|
42
|
+
/** Current values (reactive) | 현재 값들 (반응형) */
|
|
43
|
+
values: T;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Advanced state management hook with individual field subscriptions
|
|
47
|
+
* 개별 필드 구독을 통한 고급 상태 관리 훅
|
|
48
|
+
*
|
|
49
|
+
* Optimized for managing complex arrays, objects, and nested data structures
|
|
50
|
+
* where you want to avoid unnecessary re-renders when only specific fields change.
|
|
51
|
+
*
|
|
52
|
+
* 복잡한 배열, 객체, 중첩된 데이터 구조를 관리하는 데 최적화되어 있으며,
|
|
53
|
+
* 특정 필드만 변경될 때 불필요한 재렌더링을 방지하고자 할 때 사용합니다.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* // Managing an array of users
|
|
58
|
+
* const state = useFormaState({
|
|
59
|
+
* initialValues: {
|
|
60
|
+
* users: [
|
|
61
|
+
* { name: 'John', email: 'john@example.com', age: 30 },
|
|
62
|
+
* { name: 'Jane', email: 'jane@example.com', age: 25 }
|
|
63
|
+
* ],
|
|
64
|
+
* settings: { theme: 'dark', notifications: true }
|
|
65
|
+
* }
|
|
66
|
+
* });
|
|
67
|
+
*
|
|
68
|
+
* // Subscribe to individual fields - only these components re-render when changed
|
|
69
|
+
* const firstName = state.useValue('users.0.name'); // Only re-renders when John's name changes
|
|
70
|
+
* const userAge = state.useValue('users.1.age'); // Only re-renders when Jane's age changes
|
|
71
|
+
* const theme = state.useValue('settings.theme'); // Only re-renders when theme changes
|
|
72
|
+
*
|
|
73
|
+
* // Update specific fields
|
|
74
|
+
* state.setValue('users.0.name', 'Johnny');
|
|
75
|
+
* state.setValue('settings.theme', 'light');
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare function useFormaState<T extends Record<string, any>>(initialValues: T, options?: UseFormaStateOptions<T>): UseFormaStateReturn<T>;
|
|
79
|
+
//# sourceMappingURL=useFormaState.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormaState.d.ts","sourceRo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