Skip to content

Commit 92ed671

Browse files
committed
feat: add onFieldChange
1 parent 5f2182a commit 92ed671

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "react-any-shape-form",
33
"author": "Eugene Trofimov",
44
"license": "ISC",
5-
"version": "2.1.1",
5+
"version": "2.1.3",
66
"description": "Small, type-friendly and flexible react form component.",
77
"keywords": [
88
"react",

src/Form.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type FormProps<
2121
initialState?: State
2222
children: React.ReactNode
2323
onFinish?: (state: State) => void
24+
onFieldChange?: (field: keyof State, value: State[keyof State]) => void;
2425
className?: string
2526
style?: CSSProperties
2627
id?: string
@@ -37,6 +38,7 @@ export const Form = <
3738
const {
3839
children,
3940
onFinish,
41+
onFieldChange,
4042
className,
4143
initialState,
4244
style,
@@ -79,6 +81,7 @@ export const Form = <
7981
formApi,
8082
formId: id,
8183
CSSPrefix,
84+
onFieldChange,
8285
}}
8386
>
8487
<form

src/FormContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type FormContextState<F extends FormApi<any> = FormApi<any>> = {
66
formApi: F
77
formId?: string
88
CSSPrefix?: string
9+
onFieldChange?: (field: string, value: any) => void
910
}
1011

1112
const formDefaultContext: FormContextState = {

src/FormItem.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const FormItem = <Value, FieldName extends string = string, Children extends For
4343
renderLabel,
4444
validationDebounceDelay = 300,
4545
} = props;
46-
const { formApi } = useFormContext()
46+
const { formApi, onFieldChange } = useFormContext()
4747
const { value, setValue, id, CSSPrefix } = useFieldData<Value>(name, rules);
4848
const { errors, status } = useFieldValidation(formApi, name);
4949
const formItemId = idFromProps || id;
@@ -64,7 +64,8 @@ const FormItem = <Value, FieldName extends string = string, Children extends For
6464
stateRef.current.valueChanged = true;
6565
setValue(value);
6666
onChange?.(value);
67-
}, [setValue, onChange]);
67+
onFieldChange?.(name, value)
68+
}, [setValue, onChange, name]);
6869

6970
return (
7071
<div className={`${className || ''} ${CSSPrefix}__form-item`} style={style}>

0 commit comments

Comments
 (0)