@underverse-ui/underverse 0.2.19 → 0.2.20

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.
package/dist/index.cjs CHANGED
@@ -10467,25 +10467,23 @@ var isPlainObject = (tempObject) => {
10467
10467
  };
10468
10468
  var isWeb = typeof window !== "undefined" && typeof window.HTMLElement !== "undefined" && typeof document !== "undefined";
10469
10469
  function cloneObject(data) {
10470
- let copy;
10471
- const isArray = Array.isArray(data);
10472
- const isFileListInstance = typeof FileList !== "undefined" ? data instanceof FileList : false;
10473
10470
  if (data instanceof Date) {
10474
- copy = new Date(data);
10475
- } else if (!(isWeb && (data instanceof Blob || isFileListInstance)) && (isArray || isObject(data))) {
10476
- copy = isArray ? [] : Object.create(Object.getPrototypeOf(data));
10477
- if (!isArray && !isPlainObject(data)) {
10478
- copy = data;
10479
- } else {
10480
- for (const key in data) {
10481
- if (data.hasOwnProperty(key)) {
10482
- copy[key] = cloneObject(data[key]);
10483
- }
10484
- }
10485
- }
10486
- } else {
10471
+ return new Date(data);
10472
+ }
10473
+ const isFileListInstance = typeof FileList !== "undefined" && data instanceof FileList;
10474
+ if (isWeb && (data instanceof Blob || isFileListInstance)) {
10475
+ return data;
10476
+ }
10477
+ const isArray = Array.isArray(data);
10478
+ if (!isArray && !(isObject(data) && isPlainObject(data))) {
10487
10479
  return data;
10488
10480
  }
10481
+ const copy = isArray ? [] : Object.create(Object.getPrototypeOf(data));
10482
+ for (const key in data) {
10483
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
10484
+ copy[key] = cloneObject(data[key]);
10485
+ }
10486
+ }
10489
10487
  return copy;
10490
10488
  }
10491
10489
  var isKey = (value) => /^\w*$/.test(value);
@@ -10500,6 +10498,7 @@ var get = (object, path, defaultValue) => {
10500
10498
  return isUndefined(result) || result === object ? isUndefined(object[path]) ? defaultValue : object[path] : result;
10501
10499
  };
10502
10500
  var isBoolean = (value) => typeof value === "boolean";
10501
+ var isFunction = (value) => typeof value === "function";
10503
10502
  var set = (object, path, value) => {
10504
10503
  let index = -1;
10505
10504
  const tempPath = isKey(path) ? [path] : stringToPath(path);
@@ -10611,7 +10610,7 @@ var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) =>
10611
10610
  var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
10612
10611
  function deepEqual(object1, object2, _internal_visited = /* @__PURE__ */ new WeakSet()) {
10613
10612
  if (isPrimitive(object1) || isPrimitive(object2)) {
10614
- return object1 === object2;
10613
+ return Object.is(object1, object2);
10615
10614
  }
10616
10615
  if (isDateObject(object1) && isDateObject(object2)) {
10617
10616
  return object1.getTime() === object2.getTime();
@@ -10633,7 +10632,7 @@ function deepEqual(object1, object2, _internal_visited = /* @__PURE__ */ new Wea
10633
10632
  }
10634
10633
  if (key !== "ref") {
10635
10634
  const val2 = object2[key];
10636
- if (isDateObject(val1) && isDateObject(val2) || isObject(val1) && isObject(val2) || Array.isArray(val1) && Array.isArray(val2) ? !deepEqual(val1, val2, _internal_visited) : val1 !== val2) {
10635
+ if (isDateObject(val1) && isDateObject(val2) || isObject(val1) && isObject(val2) || Array.isArray(val1) && Array.isArray(val2) ? !deepEqual(val1, val2, _internal_visited) : !Object.is(val1, val2)) {
10637
10636
  return false;
10638
10637
  }
10639
10638
  }
@@ -10646,48 +10645,76 @@ function useWatch(props) {
10646
10645
  const _defaultValue = import_react24.default.useRef(defaultValue);
10647
10646
  const _compute = import_react24.default.useRef(compute);
10648
10647
  const _computeFormValues = import_react24.default.useRef(void 0);
10648
+ const _prevControl = import_react24.default.useRef(control);
10649
+ const _prevName = import_react24.default.useRef(name);
10649
10650
  _compute.current = compute;
10650
- const defaultValueMemo = import_react24.default.useMemo(() => control._getWatch(name, _defaultValue.current), [control, name]);
10651
- const [value, updateValue] = import_react24.default.useState(_compute.current ? _compute.current(defaultValueMemo) : defaultValueMemo);
10652
- useIsomorphicLayoutEffect(() => control._subscribe({
10653
- name,
10654
- formState: {
10655
- values: true
10656
- },
10657
- exact,
10658
- callback: (formState) => {
10659
- if (!disabled) {
10660
- const formValues = generateWatchOutput(name, control._names, formState.values || control._formValues, false, _defaultValue.current);
10661
- if (_compute.current) {
10662
- const computedFormValues = _compute.current(formValues);
10663
- if (!deepEqual(computedFormValues, _computeFormValues.current)) {
10664
- updateValue(computedFormValues);
10665
- _computeFormValues.current = computedFormValues;
10666
- }
10667
- } else {
10668
- updateValue(formValues);
10651
+ const [value, updateValue] = import_react24.default.useState(() => {
10652
+ const defaultValue2 = control._getWatch(name, _defaultValue.current);
10653
+ return _compute.current ? _compute.current(defaultValue2) : defaultValue2;
10654
+ });
10655
+ const getCurrentOutput = import_react24.default.useCallback((values) => {
10656
+ const formValues = generateWatchOutput(name, control._names, values || control._formValues, false, _defaultValue.current);
10657
+ return _compute.current ? _compute.current(formValues) : formValues;
10658
+ }, [control._formValues, control._names, name]);
10659
+ const refreshValue = import_react24.default.useCallback((values) => {
10660
+ if (!disabled) {
10661
+ const formValues = generateWatchOutput(name, control._names, values || control._formValues, false, _defaultValue.current);
10662
+ if (_compute.current) {
10663
+ const computedFormValues = _compute.current(formValues);
10664
+ if (!deepEqual(computedFormValues, _computeFormValues.current)) {
10665
+ updateValue(computedFormValues);
10666
+ _computeFormValues.current = computedFormValues;
10669
10667
  }
10668
+ } else {
10669
+ updateValue(formValues);
10670
10670
  }
10671
10671
  }
10672
- }), [control, disabled, name, exact]);
10672
+ }, [control._formValues, control._names, disabled, name]);
10673
+ useIsomorphicLayoutEffect(() => {
10674
+ if (_prevControl.current !== control || !deepEqual(_prevName.current, name)) {
10675
+ _prevControl.current = control;
10676
+ _prevName.current = name;
10677
+ refreshValue();
10678
+ }
10679
+ return control._subscribe({
10680
+ name,
10681
+ formState: {
10682
+ values: true
10683
+ },
10684
+ exact,
10685
+ callback: (formState) => {
10686
+ refreshValue(formState.values);
10687
+ }
10688
+ });
10689
+ }, [control, exact, name, refreshValue]);
10673
10690
  import_react24.default.useEffect(() => control._removeUnmounted());
10674
- return value;
10691
+ const controlChanged = _prevControl.current !== control;
10692
+ const prevName = _prevName.current;
10693
+ const computedOutput = import_react24.default.useMemo(() => {
10694
+ if (disabled) {
10695
+ return null;
10696
+ }
10697
+ const nameChanged = !controlChanged && !deepEqual(prevName, name);
10698
+ const shouldReturnImmediate = controlChanged || nameChanged;
10699
+ return shouldReturnImmediate ? getCurrentOutput() : null;
10700
+ }, [disabled, controlChanged, name, prevName, getCurrentOutput]);
10701
+ return computedOutput !== null ? computedOutput : value;
10675
10702
  }
10676
10703
  function useController(props) {
10677
10704
  const methods = useFormContext();
10678
- const { name, disabled, control = methods.control, shouldUnregister, defaultValue } = props;
10705
+ const { name, disabled, control = methods.control, shouldUnregister, defaultValue, exact = true } = props;
10679
10706
  const isArrayField = isNameInFieldArray(control._names.array, name);
10680
10707
  const defaultValueMemo = import_react24.default.useMemo(() => get(control._formValues, name, get(control._defaultValues, name, defaultValue)), [control, name, defaultValue]);
10681
10708
  const value = useWatch({
10682
10709
  control,
10683
10710
  name,
10684
10711
  defaultValue: defaultValueMemo,
10685
- exact: true
10712
+ exact
10686
10713
  });
10687
10714
  const formState = useFormState({
10688
10715
  control,
10689
10716
  name,
10690
- exact: true
10717
+ exact
10691
10718
  });
10692
10719
  const _props = import_react24.default.useRef(props);
10693
10720
  const _previousNameRef = import_react24.default.useRef(void 0);
@@ -10735,12 +10762,12 @@ function useController(props) {
10735
10762
  }), [name, control._formValues]);
10736
10763
  const ref = import_react24.default.useCallback((elm) => {
10737
10764
  const field2 = get(control._fields, name);
10738
- if (field2 && elm) {
10765
+ if (field2 && field2._f && elm) {
10739
10766
  field2._f.ref = {
10740
- focus: () => elm.focus && elm.focus(),
10741
- select: () => elm.select && elm.select(),
10742
- setCustomValidity: (message) => elm.setCustomValidity(message),
10743
- reportValidity: () => elm.reportValidity()
10767
+ focus: () => isFunction(elm.focus) && elm.focus(),
10768
+ select: () => isFunction(elm.select) && elm.select(),
10769
+ setCustomValidity: (message) => isFunction(elm.setCustomValidity) && elm.setCustomValidity(message),
10770
+ reportValidity: () => isFunction(elm.reportValidity) && elm.reportValidity()
10744
10771
  };
10745
10772
  }
10746
10773
  }, [control._fields, name]);
@@ -10850,7 +10877,6 @@ function extractFormValues(fieldsState, formValues) {
10850
10877
  }
10851
10878
  var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
10852
10879
  var isFileInput = (element) => element.type === "file";
10853
- var isFunction = (value) => typeof value === "function";
10854
10880
  var isHTMLElement = (value) => {
10855
10881
  if (!isWeb) {
10856
10882
  return false;
@@ -10904,10 +10930,11 @@ function isTraversable(value) {
10904
10930
  }
10905
10931
  function markFieldsDirty(data, fields = {}) {
10906
10932
  for (const key in data) {
10907
- if (isTraversable(data[key])) {
10908
- fields[key] = Array.isArray(data[key]) ? [] : {};
10909
- markFieldsDirty(data[key], fields[key]);
10910
- } else if (!isUndefined(data[key])) {
10933
+ const value = data[key];
10934
+ if (isTraversable(value)) {
10935
+ fields[key] = Array.isArray(value) ? [] : {};
10936
+ markFieldsDirty(value, fields[key]);
10937
+ } else if (!isUndefined(value)) {
10911
10938
  fields[key] = true;
10912
10939
  }
10913
10940
  }
@@ -10918,14 +10945,16 @@ function getDirtyFields(data, formValues, dirtyFieldsFromValues) {
10918
10945
  dirtyFieldsFromValues = markFieldsDirty(formValues);
10919
10946
  }
10920
10947
  for (const key in data) {
10921
- if (isTraversable(data[key])) {
10948
+ const value = data[key];
10949
+ if (isTraversable(value)) {
10922
10950
  if (isUndefined(formValues) || isPrimitive(dirtyFieldsFromValues[key])) {
10923
- dirtyFieldsFromValues[key] = markFieldsDirty(data[key], Array.isArray(data[key]) ? [] : {});
10951
+ dirtyFieldsFromValues[key] = markFieldsDirty(value, Array.isArray(value) ? [] : {});
10924
10952
  } else {
10925
- getDirtyFields(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
10953
+ getDirtyFields(value, isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
10926
10954
  }
10927
10955
  } else {
10928
- dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
10956
+ const formValue = formValues[key];
10957
+ dirtyFieldsFromValues[key] = !deepEqual(value, formValue);
10929
10958
  }
10930
10959
  }
10931
10960
  return dirtyFieldsFromValues;
@@ -11277,7 +11306,8 @@ function createFormControl(props = {}) {
11277
11306
  let _state = {
11278
11307
  action: false,
11279
11308
  mount: false,
11280
- watch: false
11309
+ watch: false,
11310
+ keepIsValid: false
11281
11311
  };
11282
11312
  let _names = {
11283
11313
  mount: /* @__PURE__ */ new Set(),
@@ -11288,7 +11318,7 @@ function createFormControl(props = {}) {
11288
11318
  };
11289
11319
  let delayErrorCallback;
11290
11320
  let timer = 0;
11291
- const _proxyFormState = {
11321
+ const defaultProxyFormState = {
11292
11322
  isDirty: false,
11293
11323
  dirtyFields: false,
11294
11324
  validatingFields: false,
@@ -11297,6 +11327,9 @@ function createFormControl(props = {}) {
11297
11327
  isValid: false,
11298
11328
  errors: false
11299
11329
  };
11330
+ const _proxyFormState = {
11331
+ ...defaultProxyFormState
11332
+ };
11300
11333
  let _proxySubscribeFormState = {
11301
11334
  ..._proxyFormState
11302
11335
  };
@@ -11310,8 +11343,17 @@ function createFormControl(props = {}) {
11310
11343
  timer = setTimeout(callback, wait);
11311
11344
  };
11312
11345
  const _setValid = async (shouldUpdateValid) => {
11346
+ if (_state.keepIsValid) {
11347
+ return;
11348
+ }
11313
11349
  if (!_options.disabled && (_proxyFormState.isValid || _proxySubscribeFormState.isValid || shouldUpdateValid)) {
11314
- const isValid = _options.resolver ? isEmptyObject((await _runSchema()).errors) : await executeBuiltInValidation(_fields, true);
11350
+ let isValid;
11351
+ if (_options.resolver) {
11352
+ isValid = isEmptyObject((await _runSchema()).errors);
11353
+ _updateIsValidating();
11354
+ } else {
11355
+ isValid = await executeBuiltInValidation(_fields, true);
11356
+ }
11315
11357
  if (isValid !== _formState.isValid) {
11316
11358
  _subjects.state.next({
11317
11359
  isValid
@@ -11380,7 +11422,7 @@ function createFormControl(props = {}) {
11380
11422
  if (field) {
11381
11423
  const defaultValue = get(_formValues, name, isUndefined(value) ? get(_defaultValues, name) : value);
11382
11424
  isUndefined(defaultValue) || ref && ref.defaultChecked || shouldSkipSetValueAs ? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f)) : setFieldValue(name, defaultValue);
11383
- _state.mount && _setValid();
11425
+ _state.mount && !_state.action && _setValid();
11384
11426
  }
11385
11427
  };
11386
11428
  const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
@@ -11442,11 +11484,11 @@ function createFormControl(props = {}) {
11442
11484
  const _runSchema = async (name) => {
11443
11485
  _updateIsValidating(name, true);
11444
11486
  const result = await _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
11445
- _updateIsValidating(name);
11446
11487
  return result;
11447
11488
  };
11448
11489
  const executeSchemaAndUpdateState = async (names) => {
11449
11490
  const { errors } = await _runSchema(names);
11491
+ _updateIsValidating(names);
11450
11492
  if (names) {
11451
11493
  for (const name of names) {
11452
11494
  const error = get(errors, name);
@@ -11625,6 +11667,7 @@ function createFormControl(props = {}) {
11625
11667
  !isBlurEvent && watched && _subjects.state.next({ ..._formState });
11626
11668
  if (_options.resolver) {
11627
11669
  const { errors } = await _runSchema([name]);
11670
+ _updateIsValidating([name]);
11628
11671
  _updateIsFieldValueUpdated(fieldValue);
11629
11672
  if (isFieldValueUpdated) {
11630
11673
  const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
@@ -11745,7 +11788,10 @@ function createFormControl(props = {}) {
11745
11788
  };
11746
11789
  return _subscribe({
11747
11790
  ...props2,
11748
- formState: _proxySubscribeFormState
11791
+ formState: {
11792
+ ...defaultProxyFormState,
11793
+ ...props2.formState
11794
+ }
11749
11795
  });
11750
11796
  };
11751
11797
  const unregister = (name, options = {}) => {
@@ -11873,6 +11919,7 @@ function createFormControl(props = {}) {
11873
11919
  });
11874
11920
  if (_options.resolver) {
11875
11921
  const { errors, values } = await _runSchema();
11922
+ _updateIsValidating();
11876
11923
  _formState.errors = errors;
11877
11924
  fieldValues = cloneObject(values);
11878
11925
  } else {
@@ -11991,8 +12038,13 @@ function createFormControl(props = {}) {
11991
12038
  watchAll: false,
11992
12039
  focus: ""
11993
12040
  };
11994
- _state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid || !!keepStateOptions.keepDirtyValues;
12041
+ _state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid || !!keepStateOptions.keepDirtyValues || !_options.shouldUnregister && !isEmptyObject(values);
11995
12042
  _state.watch = !!_options.shouldUnregister;
12043
+ _state.keepIsValid = !!keepStateOptions.keepIsValid;
12044
+ _state.action = false;
12045
+ if (!keepStateOptions.keepErrors) {
12046
+ _formState.errors = {};
12047
+ }
11996
12048
  _subjects.state.next({
11997
12049
  submitCount: keepStateOptions.keepSubmitCount ? _formState.submitCount : 0,
11998
12050
  isDirty: isEmptyResetValues ? false : keepStateOptions.keepDirty ? _formState.isDirty : !!(keepStateOptions.keepDefaultValues && !deepEqual(formValues, _defaultValues)),
@@ -12005,15 +12057,17 @@ function createFormControl(props = {}) {
12005
12057
  defaultValues: _defaultValues
12006
12058
  });
12007
12059
  };
12008
- const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues) ? formValues(_formValues) : formValues, keepStateOptions);
12060
+ const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues) ? formValues(_formValues) : formValues, { ..._options.resetOptions, ...keepStateOptions });
12009
12061
  const setFocus = (name, options = {}) => {
12010
12062
  const field = get(_fields, name);
12011
12063
  const fieldReference = field && field._f;
12012
12064
  if (fieldReference) {
12013
12065
  const fieldRef = fieldReference.refs ? fieldReference.refs[0] : fieldReference.ref;
12014
12066
  if (fieldRef.focus) {
12015
- fieldRef.focus();
12016
- options.shouldSelect && isFunction(fieldRef.select) && fieldRef.select();
12067
+ setTimeout(() => {
12068
+ fieldRef.focus();
12069
+ options.shouldSelect && isFunction(fieldRef.select) && fieldRef.select();
12070
+ });
12017
12071
  }
12018
12072
  }
12019
12073
  };
@@ -12038,6 +12092,7 @@ function createFormControl(props = {}) {
12038
12092
  setError,
12039
12093
  _subscribe,
12040
12094
  _runSchema,
12095
+ _updateIsValidating,
12041
12096
  _focusError,
12042
12097
  _getWatch,
12043
12098
  _getDirty,
@@ -12189,11 +12244,15 @@ function useForm(props = {}) {
12189
12244
  }
12190
12245
  }, [control, formState.isDirty]);
12191
12246
  import_react24.default.useEffect(() => {
12247
+ var _a;
12192
12248
  if (props.values && !deepEqual(props.values, _values.current)) {
12193
12249
  control._reset(props.values, {
12194
12250
  keepFieldsRef: true,
12195
12251
  ...control._options.resetOptions
12196
12252
  });
12253
+ if (!((_a = control._options.resetOptions) === null || _a === void 0 ? void 0 : _a.keepIsValid)) {
12254
+ control._setValid();
12255
+ }
12197
12256
  _values.current = props.values;
12198
12257
  updateFormState((state) => ({ ...state }));
12199
12258
  } else {