Skip to content

Commit 9e6c0e0

Browse files
authored
feat: add store subscriber prop for dynamic field (#284)
1 parent a141a13 commit 9e6c0e0

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

docs/lib.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This component serves as the primary entry point for drawing dynamic forms.
2020
| withoutInsertFFDebounce | `boolean` | | Flag that disables the delay before inserting data into the final-form store |
2121
| destroyOnUnregister | `boolean` | | If true, the value of a field will be destroyed when that field is unregistered. Defaults to true |
2222
| generateRandomValue | `function` | | Function that is necessary to generate a random value |
23+
| storeSubscriber | `(storeValue: FieldValue) => void` | | Subscriber function will be called when internal store of dynamic field is changed |
2324

2425
### Controller
2526

src/lib/core/components/Form/DynamicField.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface DynamicFieldProps {
3434
destroyOnUnregister?: boolean;
3535
mutators?: DynamicFormMutators;
3636
shared?: Record<string, any>;
37+
storeSubscriber?: (store: FieldValue) => void;
3738
__mirror?: WonderMirror;
3839
}
3940

@@ -48,12 +49,18 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({
4849
destroyOnUnregister = true,
4950
mutators: externalMutators,
5051
shared: externalShared,
52+
storeSubscriber,
5153
__mirror,
5254
}) => {
5355
const DynamicFormsCtx = useCreateContext();
5456
const SearchContext = useCreateSearchContext();
5557
const {tools, store} = useStore(name);
56-
const watcher = useIntegrationFF(store, withoutInsertFFDebounce, destroyOnUnregister);
58+
const watcher = useIntegrationFF({
59+
store,
60+
withoutDebounce: withoutInsertFFDebounce,
61+
destroyOnUnregister,
62+
storeSubscriber,
63+
});
5764
const {mutatorsStore, mutateDFState} = useMutators(externalMutators);
5865
const {store: searchStore, setField, removeField, isHiddenField} = useSearchStore();
5966
const shared = useFormSharedStore(externalShared);

src/lib/core/components/Form/hooks/useIntegrationFF.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@ import {Field as FinalFormField, useForm} from 'react-final-form';
1010
import type {AsyncValidateError, BaseValidateError, DynamicFieldStore, FieldValue} from '../types';
1111
import {transformArrOut} from '../utils';
1212

13-
export const useIntegrationFF = (
14-
store: DynamicFieldStore,
15-
withoutDebounce?: boolean,
16-
destroyOnUnregister?: boolean,
17-
) => {
13+
interface UseIntegrationFFParams {
14+
store: DynamicFieldStore;
15+
withoutDebounce?: boolean;
16+
destroyOnUnregister?: boolean;
17+
storeSubscriber?: (store: FieldValue) => void;
18+
}
19+
20+
export const useIntegrationFF = ({
21+
store,
22+
withoutDebounce,
23+
destroyOnUnregister,
24+
storeSubscriber,
25+
}: UseIntegrationFFParams) => {
1826
const form = useForm();
1927

2028
const watcher = React.useMemo(() => {
@@ -52,6 +60,7 @@ export const useIntegrationFF = (
5260
const cb = (value: FieldValue) => {
5361
if (store.name) {
5462
form.change(store.name, get(transformArrOut(value), store.name));
63+
storeSubscriber?.(get(value, store.name));
5564
}
5665
};
5766

@@ -60,7 +69,7 @@ export const useIntegrationFF = (
6069
}
6170

6271
return debounce(cb, 100);
63-
}, [form.change, store.name, withoutDebounce]);
72+
}, [form.change, store.name, withoutDebounce, storeSubscriber]);
6473

6574
React.useEffect(() => {
6675
change(store.values);

0 commit comments

Comments
 (0)