@bombillazo/rhf-plus 7.56.3-plus.0 → 7.56.3-plus.2

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.
@@ -357,7 +357,10 @@ function useWatch(props) {
357
357
  */
358
358
  function useController(props) {
359
359
  const methods = useFormContext();
360
- const { name, disabled, control = methods.control, shouldUnregister } = props;
360
+ const { name, disabled, control = methods === null || methods === void 0 ? void 0 : methods.control, shouldUnregister, } = props;
361
+ if (!control) {
362
+ throw new Error('useController: missing `control`. Pass `control` as a prop or provide it via FormProvider.');
363
+ }
361
364
  const isArrayField = isNameInFieldArray(control._names.array, name);
362
365
  const value = useWatch({
363
366
  control,
@@ -719,6 +722,25 @@ function deepEqual(object1, object2) {
719
722
  return true;
720
723
  }
721
724
 
725
+ function deepMerge(target, source) {
726
+ if (isPrimitive(target) || isPrimitive(source)) {
727
+ return source;
728
+ }
729
+ for (const key in source) {
730
+ const targetValue = target[key];
731
+ const sourceValue = source[key];
732
+ try {
733
+ target[key] =
734
+ (isObject(targetValue) && isObject(sourceValue)) ||
735
+ (Array.isArray(targetValue) && Array.isArray(sourceValue))
736
+ ? deepMerge(targetValue, sourceValue)
737
+ : sourceValue;
738
+ }
739
+ catch (_a) { }
740
+ }
741
+ return target;
742
+ }
743
+
722
744
  var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
723
745
 
724
746
  var isFileInput = (element) => element.type === 'file';
@@ -1277,11 +1299,12 @@ function createFormControl(props = {}) {
1277
1299
  ...defaultOptions,
1278
1300
  ...props,
1279
1301
  };
1302
+ let _internalLoading = _options.isLoading || isFunction(_options.defaultValues);
1280
1303
  let _formState = {
1281
1304
  submitCount: 0,
1282
1305
  isDirty: false,
1283
1306
  isReady: false,
1284
- isLoading: isFunction(_options.defaultValues),
1307
+ isLoading: _internalLoading,
1285
1308
  isValidating: false,
1286
1309
  isSubmitted: false,
1287
1310
  isSubmitting: false,
@@ -1292,6 +1315,7 @@ function createFormControl(props = {}) {
1292
1315
  validatingFields: {},
1293
1316
  errors: _options.errors || {},
1294
1317
  disabled: _options.disabled || false,
1318
+ metadata: _options.defaultMetadata || {},
1295
1319
  };
1296
1320
  const _fields = {};
1297
1321
  let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
@@ -1333,6 +1357,7 @@ function createFormControl(props = {}) {
1333
1357
  const validationModeBeforeSubmit = getValidationModes(_options.mode);
1334
1358
  const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
1335
1359
  const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
1360
+ const id = createId(props.id);
1336
1361
  const debounce = (callback) => (wait) => {
1337
1362
  clearTimeout(timer);
1338
1363
  timer = setTimeout(callback, wait);
@@ -2222,11 +2247,50 @@ function createFormControl(props = {}) {
2222
2247
  _subjects.state.next({
2223
2248
  isLoading: false,
2224
2249
  });
2250
+ _internalLoading = false;
2225
2251
  });
2226
- const id = createId(props.id);
2227
2252
  const submit = () => {
2228
2253
  submitForm(id);
2229
2254
  };
2255
+ const _updateIsLoading = (isLoading) => {
2256
+ if (!isUndefined(isLoading)) {
2257
+ const _loading = isLoading || _internalLoading;
2258
+ if (_formState.isLoading !== _loading) {
2259
+ _formState.isLoading = _loading;
2260
+ _subjects.state.next({
2261
+ isLoading: _loading,
2262
+ });
2263
+ }
2264
+ }
2265
+ else if (!!isLoading !== _formState.isLoading) {
2266
+ _formState.isLoading = _internalLoading;
2267
+ _subjects.state.next({
2268
+ isLoading: _internalLoading,
2269
+ });
2270
+ }
2271
+ };
2272
+ const setMetadata = (metadata) => {
2273
+ let _metadata;
2274
+ if (!metadata) {
2275
+ _metadata = _options.defaultMetadata
2276
+ ? cloneObject(_options.defaultMetadata)
2277
+ : {};
2278
+ }
2279
+ else {
2280
+ _metadata = metadata;
2281
+ }
2282
+ _formState.metadata = _metadata;
2283
+ _subjects.state.next({
2284
+ metadata: _metadata,
2285
+ });
2286
+ };
2287
+ const updateMetadata = (metadata) => {
2288
+ const _metadata = deepMerge(_formState.metadata, metadata);
2289
+ _formState.metadata = _metadata;
2290
+ _subjects.state.next({
2291
+ metadata: _metadata,
2292
+ });
2293
+ };
2230
2294
  const methods = {
2231
2295
  control: {
2232
2296
  register,
@@ -2247,6 +2311,7 @@ function createFormControl(props = {}) {
2247
2311
  _resetDefaultValues,
2248
2312
  _removeUnmounted,
2249
2313
  _disableForm,
2314
+ _updateIsLoading,
2250
2315
  _subjects,
2251
2316
  _proxyFormState,
2252
2317
  get _fields() {
@@ -2299,6 +2364,8 @@ function createFormControl(props = {}) {
2299
2364
  getFieldState,
2300
2365
  id,
2301
2366
  submit,
2367
+ setMetadata,
2368
+ updateMetadata,
2302
2369
  };
2303
2370
  return {
2304
2371
  ...methods,
@@ -2642,12 +2709,13 @@ function useFieldArray(props) {
2642
2709
  * ```
2643
2710
  */
2644
2711
  function useForm(props = {}) {
2712
+ var _a;
2645
2713
  const _formControl = React__default.useRef(undefined);
2646
2714
  const _values = React__default.useRef(undefined);
2647
2715
  const [formState, updateFormState] = React__default.useState({
2648
2716
  isDirty: false,
2649
2717
  isValidating: false,
2650
- isLoading: isFunction(props.defaultValues),
2718
+ isLoading: props.isLoading || isFunction(props.defaultValues),
2651
2719
  isSubmitted: false,
2652
2720
  isSubmitting: false,
2653
2721
  isSubmitSuccessful: false,
@@ -2662,6 +2730,9 @@ function useForm(props = {}) {
2662
2730
  defaultValues: isFunction(props.defaultValues)
2663
2731
  ? undefined
2664
2732
  : props.defaultValues,
2733
+ metadata: props.defaultMetadata ||
2734
+ ((_a = props.formControl) === null || _a === void 0 ? void 0 : _a.control._options.defaultMetadata) ||
2735
+ {},
2665
2736
  });
2666
2737
  if (!_formControl.current) {
2667
2738
  _formControl.current = {
@@ -2690,6 +2761,9 @@ function useForm(props = {}) {
2690
2761
  return sub;
2691
2762
  }, [control]);
2692
2763
  React__default.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
2764
+ React__default.useEffect(() => {
2765
+ control._updateIsLoading(props.isLoading);
2766
+ }, [control, props.isLoading]);
2693
2767
  React__default.useEffect(() => {
2694
2768
  if (props.mode) {
2695
2769
  control._options.mode = props.mode;