Skip to content

Commit d743f1e

Browse files
committed
added callback for listening to field onerror events
1 parent 16500d8 commit d743f1e

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/plugins/es_ui_shared/static/forms/hook_form_lib/components/use_field.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface Props<T, FormType = FormData, I = T> {
2020
componentProps?: Record<string, any>;
2121
readDefaultValueOnForm?: boolean;
2222
onChange?: (value: I) => void;
23+
onError?: (errors: string[] | null) => void;
2324
children?: (field: FieldHook<T, I>) => JSX.Element | null;
2425
[key: string]: any;
2526
}
@@ -33,6 +34,7 @@ function UseFieldComp<T = unknown, FormType = FormData, I = T>(props: Props<T, F
3334
componentProps,
3435
readDefaultValueOnForm = true,
3536
onChange,
37+
onError,
3638
children,
3739
...rest
3840
} = props;
@@ -62,7 +64,7 @@ function UseFieldComp<T = unknown, FormType = FormData, I = T>(props: Props<T, F
6264
}
6365
}
6466

65-
const field = useField<T, FormType, I>(form, path, fieldConfig, onChange);
67+
const field = useField<T, FormType, I>(form, path, fieldConfig, onChange, onError);
6668

6769
// Children prevails over anything else provided.
6870
if (children) {

src/plugins/es_ui_shared/static/forms/hook_form_lib/hooks/use_field.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export const useField = <T, FormType = FormData, I = T>(
2727
form: FormHook<FormType>,
2828
path: string,
2929
config: FieldConfig<T, FormType, I> & InternalFieldConfig<T> = {},
30-
valueChangeListener?: (value: I) => void
30+
valueChangeListener?: (value: I) => void,
31+
errorChangeListener?: (errors: string[] | null) => void
3132
) => {
3233
const {
3334
type = FIELD_TYPES.TEXT,
@@ -596,6 +597,15 @@ export const useField = <T, FormType = FormData, I = T>(
596597
};
597598
}, [onValueChange]);
598599

600+
useEffect(() => {
601+
if (!isMounted.current) {
602+
return;
603+
}
604+
if (errorChangeListener) {
605+
errorChangeListener(errors.length ? errors.map((error) => error.message) : null);
606+
}
607+
}, [errors, errorChangeListener]);
608+
599609
useEffect(() => {
600610
isMounted.current = true;
601611

0 commit comments

Comments
 (0)