Skip to content

Commit f445246

Browse files
committed
fix(manager): allow to use onChange in formSpy
1 parent c850f3c commit f445246

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

packages/form-state-manager/src/files/form-spy.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import FormManagerContext from './form-manager-context';
33
import generateId from '../utils/generate-id';
44
import FormSpyProps from '../types/form-spy';
55

6-
const FormSpy: React.FunctionComponent<FormSpyProps> = ({ children, subscription = { all: true } }) => {
6+
const FormSpy: React.FunctionComponent<FormSpyProps> = ({ children, onChange, subscription = { all: true } }) => {
77
const { subscribe, unsubscribe, getState, formOptions } = useContext(FormManagerContext);
88
const [, rerender] = useReducer((prev) => prev + 1, 0);
99

@@ -23,7 +23,14 @@ const FormSpy: React.FunctionComponent<FormSpyProps> = ({ children, subscription
2323
[]
2424
);
2525

26-
return children({ ...getState(), form: formOptions });
26+
const newState = { ...getState(), form: formOptions };
27+
28+
if (onChange) {
29+
onChange(newState);
30+
return null;
31+
}
32+
33+
return children(newState);
2734
};
2835

2936
export default FormSpy;

0 commit comments

Comments
 (0)