react-hook-form 7.45.3 → 7.46.0-next.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.
@@ -435,6 +435,7 @@ function useController(props) {
435
435
  field: {
436
436
  name,
437
437
  value,
438
+ disabled: formState.disabled,
438
439
  onChange: React.useCallback((event) => _registerProps.current.onChange({
439
440
  target: {
440
441
  value: getEventValue(event),
@@ -568,7 +569,7 @@ function Form(props) {
568
569
  formData.append(name, get(data, name));
569
570
  }
570
571
  if (onSubmit) {
571
- onSubmit({
572
+ await onSubmit({
572
573
  data,
573
574
  event,
574
575
  method,
@@ -635,28 +636,6 @@ var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => va
635
636
  }
636
637
  : {};
637
638
 
638
- const focusFieldBy = (fields, callback, fieldsNames) => {
639
- for (const key of fieldsNames || Object.keys(fields)) {
640
- const field = get(fields, key);
641
- if (field) {
642
- const { _f, ...currentField } = field;
643
- if (_f && callback(_f.name)) {
644
- if (_f.ref.focus) {
645
- _f.ref.focus();
646
- break;
647
- }
648
- else if (_f.refs && _f.refs[0].focus) {
649
- _f.refs[0].focus();
650
- break;
651
- }
652
- }
653
- else if (isObject(currentField)) {
654
- focusFieldBy(currentField, callback);
655
- }
656
- }
657
- }
658
- };
659
-
660
639
  var generateId = () => {
661
640
  const d = typeof performance === 'undefined' ? Date.now() : performance.now() * 1000;
662
641
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
@@ -684,6 +663,26 @@ var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
684
663
  [..._names.watch].some((watchName) => name.startsWith(watchName) &&
685
664
  /^\.\w+/.test(name.slice(watchName.length))));
686
665
 
666
+ const iterateFieldsByAction = (fields, action, fieldsNames, abortEarly) => {
667
+ for (const key of fieldsNames || Object.keys(fields)) {
668
+ const field = get(fields, key);
669
+ if (field) {
670
+ const { _f, ...currentField } = field;
671
+ if (_f) {
672
+ if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
673
+ break;
674
+ }
675
+ else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
676
+ break;
677
+ }
678
+ }
679
+ else if (isObject(currentField)) {
680
+ iterateFieldsByAction(currentField, action);
681
+ }
682
+ }
683
+ }
684
+ };
685
+
687
686
  var updateFieldArrayRootError = (errors, error, name) => {
688
687
  const fieldArrayErrors = compact(get(errors, name));
689
688
  set(fieldArrayErrors, 'root', error[name]);
@@ -1227,7 +1226,15 @@ function useFieldArray(props) {
1227
1226
  values: { ...control._formValues },
1228
1227
  });
1229
1228
  control._names.focus &&
1230
- focusFieldBy(control._fields, (key) => !!key && key.startsWith(control._names.focus || ''));
1229
+ iterateFieldsByAction(control._fields, (ref, key) => {
1230
+ if (control._names.focus &&
1231
+ key.startsWith(control._names.focus) &&
1232
+ ref.focus) {
1233
+ ref.focus();
1234
+ return 1;
1235
+ }
1236
+ return;
1237
+ });
1231
1238
  control._names.focus = '';
1232
1239
  control._updateValid();
1233
1240
  _actioned.current = false;
@@ -1508,6 +1515,7 @@ function createFormControl(props = {}, flushRootRender) {
1508
1515
  touchedFields: {},
1509
1516
  dirtyFields: {},
1510
1517
  errors: {},
1518
+ disabled: false,
1511
1519
  };
1512
1520
  let _fields = {};
1513
1521
  let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
@@ -1916,6 +1924,13 @@ function createFormControl(props = {}, flushRootRender) {
1916
1924
  }
1917
1925
  }
1918
1926
  };
1927
+ const _focusInput = (ref, key) => {
1928
+ if (get(_formState.errors, key) && ref.focus) {
1929
+ ref.focus();
1930
+ return 1;
1931
+ }
1932
+ return;
1933
+ };
1919
1934
  const trigger = async (name, options = {}) => {
1920
1935
  let isValid;
1921
1936
  let validationResult;
@@ -1949,7 +1964,7 @@ function createFormControl(props = {}, flushRootRender) {
1949
1964
  });
1950
1965
  options.shouldFocus &&
1951
1966
  !validationResult &&
1952
- focusFieldBy(_fields, (key) => key && get(_formState.errors, key), name ? fieldNames : _names.mount);
1967
+ iterateFieldsByAction(_fields, _focusInput, name ? fieldNames : _names.mount);
1953
1968
  return validationResult;
1954
1969
  };
1955
1970
  const getValues = (fieldNames) => {
@@ -2098,7 +2113,15 @@ function createFormControl(props = {}, flushRootRender) {
2098
2113
  };
2099
2114
  };
2100
2115
  const _focusError = () => _options.shouldFocusError &&
2101
- focusFieldBy(_fields, (key) => key && get(_formState.errors, key), _names.mount);
2116
+ iterateFieldsByAction(_fields, _focusInput, _names.mount);
2117
+ const _disableForm = (disabled) => {
2118
+ if (isBoolean(props.disabled)) {
2119
+ _subjects.state.next({ disabled: props.disabled });
2120
+ iterateFieldsByAction(_fields, (ref) => {
2121
+ ref.disabled = disabled;
2122
+ }, 0, false);
2123
+ }
2124
+ };
2102
2125
  const handleSubmit = (onValid, onInvalid) => async (e) => {
2103
2126
  if (e) {
2104
2127
  e.preventDefault && e.preventDefault();
@@ -2293,6 +2316,7 @@ function createFormControl(props = {}, flushRootRender) {
2293
2316
  _reset,
2294
2317
  _resetDefaultValues,
2295
2318
  _updateFormState,
2319
+ _disableForm,
2296
2320
  _subjects,
2297
2321
  _proxyFormState,
2298
2322
  get _fields() {
@@ -2392,6 +2416,7 @@ function useForm(props = {}) {
2392
2416
  dirtyFields: {},
2393
2417
  touchedFields: {},
2394
2418
  errors: {},
2419
+ disabled: false,
2395
2420
  defaultValues: isFunction(props.defaultValues)
2396
2421
  ? undefined
2397
2422
  : props.defaultValues,
@@ -2432,6 +2457,7 @@ function useForm(props = {}) {
2432
2457
  }
2433
2458
  control._removeUnmounted();
2434
2459
  });
2460
+ React.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
2435
2461
  _formControl.current.formState = getProxyFormState(formState, control);
2436
2462
  return _formControl.current;
2437
2463
  }