MCPcopy
hub / github.com/react-hook-form/react-hook-form / onChange

Function onChange

src/logic/createFormControl.ts:987–1147  ·  view source on GitHub ↗
(event)

Source from the content-addressed store, hash-verified

985 };
986
987 const onChange: ChangeHandler = async (event) => {
988 _state.mount = true;
989 const target = event.target;
990 let name: string = target.name;
991 let isFieldValueUpdated = true;
992 const field: Field = get(_fields, name);
993 const _updateIsFieldValueUpdated = (fieldValue: unknown) => {
994 isFieldValueUpdated =
995 Number.isNaN(fieldValue) ||
996 (isDateObject(fieldValue) && isNaN(fieldValue.getTime())) ||
997 deepEqual(fieldValue, get(_formValues, name, fieldValue));
998 };
999
1000 if (field) {
1001 let error;
1002 let isValid;
1003 const fieldValue = target.type
1004 ? getFieldValue(field._f)
1005 : getEventValue(event);
1006 const isBlurEvent =
1007 event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
1008 const hasNoValidationEffect =
1009 !hasValidation(field._f) &&
1010 !props.validate &&
1011 !_options.resolver &&
1012 !get(_formState.errors, name) &&
1013 !field._f.deps;
1014 const shouldSkipValidation =
1015 hasNoValidationEffect ||
1016 skipValidation(
1017 isBlurEvent,
1018 get(_formState.touchedFields, name),
1019 _formState.isSubmitted,
1020 _validationModeAfterSubmit,
1021 _validationModeBeforeSubmit,
1022 );
1023 const watched = isWatched(name, _names, isBlurEvent);
1024
1025 set(_formValues, name, fieldValue);
1026
1027 if (isBlurEvent) {
1028 if (!target || !target.readOnly) {
1029 field._f.onBlur && field._f.onBlur(event);
1030 delayErrorCallback && delayErrorCallback(0);
1031 }
1032 } else if (field._f.onChange) {
1033 field._f.onChange(event);
1034 }
1035
1036 const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent);
1037
1038 const shouldRender = !isEmptyObject(fieldState) || watched;
1039
1040 !isBlurEvent &&
1041 _subjects.state.next({
1042 name,
1043 type: event.type,
1044 ...(_valuesSubscriberCount

Callers 13

ComponentFunction · 0.85
InputFunction · 0.85
ComponentFunction · 0.85
CheckboxesFunction · 0.85
bench.tsFile · 0.85
MuiCurrencyFormatFunction · 0.85
CustomMaskedInputFunction · 0.85
handleChangeFunction · 0.85
AppFunction · 0.85
MuiCurrencyFormatFunction · 0.85
CustomMaskedInputFunction · 0.85
handleChangeFunction · 0.85

Calls 12

getFieldValueFunction · 0.85
updateTouchAndDirtyFunction · 0.85
cloneObjectFunction · 0.85
_setValidFunction · 0.85
validateFormFunction · 0.85
_runSchemaFunction · 0.85
_updateIsValidatingFunction · 0.85
schemaErrorLookupFunction · 0.85
executeBuiltInValidationFunction · 0.85
triggerFunction · 0.85
shouldRenderByErrorFunction · 0.85

Tested by 4

ComponentFunction · 0.68
InputFunction · 0.68
ComponentFunction · 0.68
CheckboxesFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…