react-hook-form 7.23.0 → 7.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -231,9 +231,7 @@ function useController(props) {
231
231
  control,
232
232
  name,
233
233
  });
234
- const _name = React.useRef(name);
235
- _name.current = name;
236
- const registerProps = control.register(name, Object.assign(Object.assign({}, props.rules), { value }));
234
+ const _registerProps = React.useRef(control.register(name, Object.assign(Object.assign({}, props.rules), { value })));
237
235
  React.useEffect(() => {
238
236
  const updateMounted = (name, value) => {
239
237
  const field = get(control._fields, name);
@@ -253,27 +251,27 @@ function useController(props) {
253
251
  }, [name, control, isArrayField, shouldUnregister]);
254
252
  return {
255
253
  field: {
256
- onChange: (event) => {
257
- registerProps.onChange({
254
+ name,
255
+ value,
256
+ onChange: React.useCallback((event) => {
257
+ _registerProps.current.onChange({
258
258
  target: {
259
259
  value: getEventValue(event),
260
260
  name: name,
261
261
  },
262
262
  type: EVENTS.CHANGE,
263
263
  });
264
- },
265
- onBlur: () => {
266
- registerProps.onBlur({
264
+ }, [name]),
265
+ onBlur: React.useCallback(() => {
266
+ _registerProps.current.onBlur({
267
267
  target: {
268
268
  value: get(control._formValues, name),
269
269
  name: name,
270
270
  },
271
271
  type: EVENTS.BLUR,
272
272
  });
273
- },
274
- name,
275
- value,
276
- ref: (elm) => {
273
+ }, [name, control]),
274
+ ref: React.useCallback((elm) => {
277
275
  const field = get(control._fields, name);
278
276
  if (elm && field && elm.focus) {
279
277
  field._f.ref = {
@@ -282,7 +280,7 @@ function useController(props) {
282
280
  reportValidity: () => elm.reportValidity(),
283
281
  };
284
282
  }
285
- },
283
+ }, [name, control]),
286
284
  },
287
285
  formState,
288
286
  fieldState: {
@@ -1189,7 +1187,7 @@ function createFormControl(props = {}) {
1189
1187
  _stateFlags.mount && _updateValid();
1190
1188
  }
1191
1189
  };
1192
- const updateTouchAndDirty = (name, fieldValue, isCurrentTouched, shouldRender = true) => {
1190
+ const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
1193
1191
  let isFieldDirty = false;
1194
1192
  const output = {
1195
1193
  name,
@@ -1200,7 +1198,7 @@ function createFormControl(props = {}) {
1200
1198
  _formState.isDirty = output.isDirty = _getDirty();
1201
1199
  isFieldDirty = isPreviousFormDirty !== output.isDirty;
1202
1200
  }
1203
- if (_proxyFormState.dirtyFields && !isCurrentTouched) {
1201
+ if (_proxyFormState.dirtyFields && (!isBlurEvent || shouldDirty)) {
1204
1202
  const isPreviousFieldDirty = get(_formState.dirtyFields, name);
1205
1203
  const isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
1206
1204
  isCurrentFieldPristine
@@ -1211,13 +1209,13 @@ function createFormControl(props = {}) {
1211
1209
  isFieldDirty ||
1212
1210
  isPreviousFieldDirty !== get(_formState.dirtyFields, name);
1213
1211
  }
1214
- if (isCurrentTouched && !isPreviousFieldTouched) {
1215
- set(_formState.touchedFields, name, isCurrentTouched);
1212
+ if (isBlurEvent && !isPreviousFieldTouched) {
1213
+ set(_formState.touchedFields, name, isBlurEvent);
1216
1214
  output.touchedFields = _formState.touchedFields;
1217
1215
  isFieldDirty =
1218
1216
  isFieldDirty ||
1219
1217
  (_proxyFormState.touchedFields &&
1220
- isPreviousFieldTouched !== isCurrentTouched);
1218
+ isPreviousFieldTouched !== isBlurEvent);
1221
1219
  }
1222
1220
  isFieldDirty && shouldRender && _subjects.state.next(output);
1223
1221
  return isFieldDirty ? output : {};
@@ -1361,7 +1359,7 @@ function createFormControl(props = {}) {
1361
1359
  }
1362
1360
  }
1363
1361
  (options.shouldDirty || options.shouldTouch) &&
1364
- updateTouchAndDirty(name, fieldValue, options.shouldTouch);
1362
+ updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
1365
1363
  options.shouldValidate && trigger(name);
1366
1364
  };
1367
1365
  const setValues = (name, value, options) => {
@@ -1423,13 +1421,13 @@ function createFormControl(props = {}) {
1423
1421
  !field._f.deps) ||
1424
1422
  skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, validationModeAfterSubmit, validationModeBeforeSubmit);
1425
1423
  const watched = isWatched(name, _names, isBlurEvent);
1424
+ set(_formValues, name, fieldValue);
1426
1425
  if (isBlurEvent) {
1427
1426
  field._f.onBlur && field._f.onBlur(event);
1428
1427
  }
1429
1428
  else if (field._f.onChange) {
1430
1429
  field._f.onChange(event);
1431
1430
  }
1432
- set(_formValues, name, fieldValue);
1433
1431
  const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent, false);
1434
1432
  const shouldRender = !isEmptyObject(fieldState) || watched;
1435
1433
  !isBlurEvent &&
@@ -1659,27 +1657,29 @@ function createFormControl(props = {}) {
1659
1657
  }
1660
1658
  };
1661
1659
  const resetField = (name, options = {}) => {
1662
- if (isUndefined(options.defaultValue)) {
1663
- setValue(name, get(_defaultValues, name));
1664
- }
1665
- else {
1666
- setValue(name, options.defaultValue);
1667
- set(_defaultValues, name, options.defaultValue);
1668
- }
1669
- if (!options.keepTouched) {
1670
- unset(_formState.touchedFields, name);
1671
- }
1672
- if (!options.keepDirty) {
1673
- unset(_formState.dirtyFields, name);
1674
- _formState.isDirty = options.defaultValue
1675
- ? _getDirty(name, get(_defaultValues, name))
1676
- : _getDirty();
1677
- }
1678
- if (!options.keepError) {
1679
- unset(_formState.errors, name);
1680
- _proxyFormState.isValid && _updateValid();
1660
+ if (get(_fields, name)) {
1661
+ if (isUndefined(options.defaultValue)) {
1662
+ setValue(name, get(_defaultValues, name));
1663
+ }
1664
+ else {
1665
+ setValue(name, options.defaultValue);
1666
+ set(_defaultValues, name, options.defaultValue);
1667
+ }
1668
+ if (!options.keepTouched) {
1669
+ unset(_formState.touchedFields, name);
1670
+ }
1671
+ if (!options.keepDirty) {
1672
+ unset(_formState.dirtyFields, name);
1673
+ _formState.isDirty = options.defaultValue
1674
+ ? _getDirty(name, get(_defaultValues, name))
1675
+ : _getDirty();
1676
+ }
1677
+ if (!options.keepError) {
1678
+ unset(_formState.errors, name);
1679
+ _proxyFormState.isValid && _updateValid();
1680
+ }
1681
+ _subjects.state.next(Object.assign({}, _formState));
1681
1682
  }
1682
- _subjects.state.next(Object.assign({}, _formState));
1683
1683
  };
1684
1684
  const reset = (formValues, keepStateOptions = {}) => {
1685
1685
  const updatedValues = formValues || _defaultValues;