@bombillazo/rhf-plus 7.56.2-plus.0 → 7.56.3-plus.1

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.
@@ -415,7 +415,12 @@ function useController(props) {
415
415
  const ref = React__default.useCallback((elm) => {
416
416
  const field = get(control._fields, name);
417
417
  if (field && elm) {
418
- field._f.ref = elm;
418
+ field._f.ref = {
419
+ focus: () => elm.focus(),
420
+ select: () => elm.select(),
421
+ setCustomValidity: (message) => elm.setCustomValidity(message),
422
+ reportValidity: () => elm.reportValidity(),
423
+ };
419
424
  }
420
425
  }, [control._fields, name]);
421
426
  const field = React__default.useMemo(() => ({
@@ -714,6 +719,25 @@ function deepEqual(object1, object2) {
714
719
  return true;
715
720
  }
716
721
 
722
+ function deepMerge(target, source) {
723
+ if (isPrimitive(target) || isPrimitive(source)) {
724
+ return source;
725
+ }
726
+ for (const key in source) {
727
+ const targetValue = target[key];
728
+ const sourceValue = source[key];
729
+ try {
730
+ target[key] =
731
+ (isObject(targetValue) && isObject(sourceValue)) ||
732
+ (Array.isArray(targetValue) && Array.isArray(sourceValue))
733
+ ? deepMerge(targetValue, sourceValue)
734
+ : sourceValue;
735
+ }
736
+ catch (_a) { }
737
+ }
738
+ return target;
739
+ }
740
+
717
741
  var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
718
742
 
719
743
  var isFileInput = (element) => element.type === 'file';
@@ -1272,11 +1296,12 @@ function createFormControl(props = {}) {
1272
1296
  ...defaultOptions,
1273
1297
  ...props,
1274
1298
  };
1299
+ let _internalLoading = _options.isLoading || isFunction(_options.defaultValues);
1275
1300
  let _formState = {
1276
1301
  submitCount: 0,
1277
1302
  isDirty: false,
1278
1303
  isReady: false,
1279
- isLoading: isFunction(_options.defaultValues),
1304
+ isLoading: _internalLoading,
1280
1305
  isValidating: false,
1281
1306
  isSubmitted: false,
1282
1307
  isSubmitting: false,
@@ -1287,6 +1312,7 @@ function createFormControl(props = {}) {
1287
1312
  validatingFields: {},
1288
1313
  errors: _options.errors || {},
1289
1314
  disabled: _options.disabled || false,
1315
+ metadata: _options.defaultMetadata || {},
1290
1316
  };
1291
1317
  const _fields = {};
1292
1318
  let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
@@ -1328,6 +1354,7 @@ function createFormControl(props = {}) {
1328
1354
  const validationModeBeforeSubmit = getValidationModes(_options.mode);
1329
1355
  const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
1330
1356
  const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
1357
+ const id = createId(props.id);
1331
1358
  const debounce = (callback) => (wait) => {
1332
1359
  clearTimeout(timer);
1333
1360
  timer = setTimeout(callback, wait);
@@ -2217,11 +2244,50 @@ function createFormControl(props = {}) {
2217
2244
  _subjects.state.next({
2218
2245
  isLoading: false,
2219
2246
  });
2247
+ _internalLoading = false;
2220
2248
  });
2221
- const id = createId(props.id);
2222
2249
  const submit = () => {
2223
2250
  submitForm(id);
2224
2251
  };
2252
+ const _updateIsLoading = (isLoading) => {
2253
+ if (!isUndefined(isLoading)) {
2254
+ const _loading = isLoading || _internalLoading;
2255
+ if (_formState.isLoading !== _loading) {
2256
+ _formState.isLoading = _loading;
2257
+ _subjects.state.next({
2258
+ isLoading: _loading,
2259
+ });
2260
+ }
2261
+ }
2262
+ else if (!!isLoading !== _formState.isLoading) {
2263
+ _formState.isLoading = _internalLoading;
2264
+ _subjects.state.next({
2265
+ isLoading: _internalLoading,
2266
+ });
2267
+ }
2268
+ };
2269
+ const setMetadata = (metadata) => {
2270
+ let _metadata;
2271
+ if (!metadata) {
2272
+ _metadata = _options.defaultMetadata
2273
+ ? cloneObject(_options.defaultMetadata)
2274
+ : {};
2275
+ }
2276
+ else {
2277
+ _metadata = metadata;
2278
+ }
2279
+ _formState.metadata = _metadata;
2280
+ _subjects.state.next({
2281
+ metadata: _metadata,
2282
+ });
2283
+ };
2284
+ const updateMetadata = (metadata) => {
2285
+ const _metadata = deepMerge(_formState.metadata, metadata);
2286
+ _formState.metadata = _metadata;
2287
+ _subjects.state.next({
2288
+ metadata: _metadata,
2289
+ });
2290
+ };
2225
2291
  const methods = {
2226
2292
  control: {
2227
2293
  register,
@@ -2242,6 +2308,7 @@ function createFormControl(props = {}) {
2242
2308
  _resetDefaultValues,
2243
2309
  _removeUnmounted,
2244
2310
  _disableForm,
2311
+ _updateIsLoading,
2245
2312
  _subjects,
2246
2313
  _proxyFormState,
2247
2314
  get _fields() {
@@ -2294,6 +2361,8 @@ function createFormControl(props = {}) {
2294
2361
  getFieldState,
2295
2362
  id,
2296
2363
  submit,
2364
+ setMetadata,
2365
+ updateMetadata,
2297
2366
  };
2298
2367
  return {
2299
2368
  ...methods,
@@ -2637,12 +2706,13 @@ function useFieldArray(props) {
2637
2706
  * ```
2638
2707
  */
2639
2708
  function useForm(props = {}) {
2709
+ var _a;
2640
2710
  const _formControl = React__default.useRef(undefined);
2641
2711
  const _values = React__default.useRef(undefined);
2642
2712
  const [formState, updateFormState] = React__default.useState({
2643
2713
  isDirty: false,
2644
2714
  isValidating: false,
2645
- isLoading: isFunction(props.defaultValues),
2715
+ isLoading: props.isLoading || isFunction(props.defaultValues),
2646
2716
  isSubmitted: false,
2647
2717
  isSubmitting: false,
2648
2718
  isSubmitSuccessful: false,
@@ -2657,6 +2727,9 @@ function useForm(props = {}) {
2657
2727
  defaultValues: isFunction(props.defaultValues)
2658
2728
  ? undefined
2659
2729
  : props.defaultValues,
2730
+ metadata: props.defaultMetadata ||
2731
+ ((_a = props.formControl) === null || _a === void 0 ? void 0 : _a.control._options.defaultMetadata) ||
2732
+ {},
2660
2733
  });
2661
2734
  if (!_formControl.current) {
2662
2735
  _formControl.current = {
@@ -2685,6 +2758,9 @@ function useForm(props = {}) {
2685
2758
  return sub;
2686
2759
  }, [control]);
2687
2760
  React__default.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
2761
+ React__default.useEffect(() => {
2762
+ control._updateIsLoading(props.isLoading);
2763
+ }, [control, props.isLoading]);
2688
2764
  React__default.useEffect(() => {
2689
2765
  if (props.mode) {
2690
2766
  control._options.mode = props.mode;