Skip to content

Commit c8511b2

Browse files
authored
Add optional onChange callback to TextInput component (#906)
Signed-off-by: Florent MILLOT <[email protected]>
1 parent cdb388f commit c8511b2

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/components/inputs/reactHookForm/text/TextInput.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface TextInputProps {
3030
outputTransform?: (value: string) => Input | null;
3131
inputTransform?: (value: Input) => string;
3232
acceptValue?: (value: string) => boolean;
33+
onChange?: (value: string) => void;
3334
previousValue?: Input;
3435
clearable?: boolean;
3536
formProps?: Omit<
@@ -50,6 +51,7 @@ export function TextInput({
5051
outputTransform = identity, // transform materialUi input value before sending it to react hook form, mostly used to deal with number fields
5152
inputTransform = identity, // transform react hook form value before sending it to materialUi input, mostly used to deal with number fields
5253
acceptValue = () => true, // used to check user entry before committing the input change, used mostly to prevent user from typing a character in number field
54+
onChange, // method called when input value changed, if you want to manually trigger validation for example (do not update the form here unless you know what you do, it's already done by RHF)
5355
previousValue,
5456
clearable,
5557
formProps,
@@ -58,7 +60,7 @@ export function TextInput({
5860
}: TextInputProps) {
5961
const { validationSchema, getValues, removeOptional, isNodeBuilt, isUpdate } = useCustomFormContext();
6062
const {
61-
field: { onChange, value, ref },
63+
field: { onChange: onChangeRhf, value, ref },
6264
fieldState: { error },
6365
} = useController({ name });
6466

@@ -69,12 +71,13 @@ export function TextInput({
6971
};
7072

7173
const handleClearValue = () => {
72-
onChange(outputTransform(''));
74+
onChangeRhf(outputTransform(''));
7375
};
7476

7577
const handleValueChanged = (e: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) => {
7678
if (acceptValue(e.target.value)) {
77-
onChange(outputTransform(e.target.value));
79+
onChangeRhf(outputTransform(e.target.value));
80+
onChange?.(e.target.value);
7881
}
7982
};
8083

0 commit comments

Comments
 (0)