I'm looking to simplify the below code for validating an onChange event. The allowedInput is a regex that is passed (optional) from all the form components.
const validatedOnChange = (event: ChangeEvent<HTMLInputElement>): void => {
if (isDisabled) {
return;
}
if (allowedInput) {
if (allowedInput.test(event.target.value)) {
onChange({ name, value: event.target.value });
}
return;
}
onChange({ name, value: event.target.value });
};
This is a common utility method used for all my React form components. Hence looking to write a readable and consistent code.