Skip to content

Commit a10c752

Browse files
committed
feat: add the registerEffect method to the FormStore and InternalFieldHooks interfaces, and implement the useEffectField hook to support the effect registration functionality.
1 parent 6fae8d9 commit a10c752

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

primitives/filed-form /src/form-core/createStore.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,7 @@ class FormStore {
13371337
getArrayFields: this.getArrayFields,
13381338
getInitialValue: this.getInitialValue,
13391339
registerComputed: this.registerComputed,
1340+
registerEffect: this.registerEffect,
13401341
registerField: this.registerField,
13411342
setCallbacks: this.setCallbacks,
13421343
setFieldRules: this.setFieldRules,

primitives/filed-form /src/react/hooks/FieldContext.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ export interface InternalFieldHooks<Values = any> {
130130
deps: AllPathsKeys<Values>[],
131131
compute: (get: (n: AllPathsKeys<Values>) => any, all: Values) => PathToDeepType<Values, T>
132132
) => () => void;
133+
registerEffect: (
134+
deps: AllPathsKeys<Values>[],
135+
effect: (get: (n: AllPathsKeys<Values>) => any, all: Values) => void
136+
) => () => void;
133137
registerField: (entity: FieldEntity) => () => void;
134138
setFieldRules: (name: AllPathsKeys<Values>, rules?: Rule[]) => void;
135139
setRules: (name: AllPathsKeys<Values>, rules?: Rule[]) => void;

primitives/filed-form /src/react/hooks/useFieldArray.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ export type ArrayFieldItem = {
1111
};
1212

1313
export function useArrayField<Values = any>(name: ArrayKeys<Values>, form?: FormInstance<Values>) {
14-
const context = useFieldContext();
14+
const contextForm = useFieldContext();
1515

16-
const fieldContext = form ?? context;
16+
const formInstance = form ?? contextForm;
1717

18-
if (!fieldContext) {
18+
if (!formInstance) {
1919
throw new Error('Can not find FormContext. Please make sure you wrap Field under Form or provide a form instance.');
2020
}
2121

22-
const { arrayOp } = fieldContext as unknown as InternalFormInstance<Values>;
22+
const { arrayOp } = formInstance as unknown as InternalFormInstance<Values>;
2323

2424
return arrayOp(name);
2525
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useEffect } from 'react';
2+
import type { AllPathsKeys } from 'skyroc-type-utils';
3+
4+
import type { FormInstance, InternalFormInstance } from './FieldContext';
5+
import { useFieldContext } from './FieldContext';
6+
7+
export function useEffectField<Values = any>(
8+
deps: AllPathsKeys<Values>[],
9+
effect: (get: (n: AllPathsKeys<Values>) => any, all: Values) => void,
10+
form?: FormInstance<Values>
11+
) {
12+
const contextForm = useFieldContext<Values>();
13+
14+
const formInstance = form ?? contextForm;
15+
16+
if (!formInstance) {
17+
throw new Error('Can not find FormContext. Please make sure you wrap Field under Form or provide a form instance.');
18+
}
19+
20+
const { getInternalHooks } = formInstance as unknown as InternalFormInstance<Values>;
21+
22+
const { registerEffect } = getInternalHooks();
23+
24+
useEffect(() => {
25+
const unregister = registerEffect(deps, effect);
26+
return unregister;
27+
}, [deps]);
28+
}

0 commit comments

Comments
 (0)