@gaddario98/react-core 2.1.2 → 2.1.4

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.
@@ -1,32 +1,11 @@
1
1
  import * as _gaddario98_react_state from '../state/index';
2
2
  import * as React$1 from 'react';
3
- import React__default, { PropsWithChildren, JSX } from 'react';
4
- import * as react_jsx_runtime from 'react/jsx-runtime';
3
+ import React__default, { JSX, PropsWithChildren } from 'react';
5
4
  import * as _tanstack_react_form from '@tanstack/react-form';
6
5
  import { FormOptions, DeepKeys, DeepValue, FieldApi, FieldState, ReactFormExtendedApi } from '@tanstack/react-form';
6
+ import * as react_jsx_runtime from 'react/jsx-runtime';
7
7
  import * as jotai from 'jotai';
8
8
 
9
- interface FormNotificationMessage {
10
- message: string;
11
- type: 'success' | 'error' | 'info' | 'warning';
12
- autoHideDuration?: number;
13
- textTransOption?: Record<string, unknown>;
14
- ns?: string;
15
- }
16
- interface FormTranslationOptions {
17
- [key: string]: string | number | boolean | undefined;
18
- ns?: string;
19
- }
20
- interface FormConfigProps {
21
- formFieldContainer: React.FC<PropsWithChildren>;
22
- showNotification?: (notification: FormNotificationMessage) => void;
23
- translateText?: (key: string, options?: FormTranslationOptions) => string;
24
- }
25
- declare const formConfigAtom: _gaddario98_react_state.PrimitiveAtom<FormConfigProps>;
26
- declare const useFormConfigValue: () => FormConfigProps;
27
- declare const useFormConfigState: () => [FormConfigProps, (value: FormConfigProps) => void];
28
- declare const useFormConfigReset: () => () => void;
29
-
30
9
  interface UseCustomFormOptions<TData extends FieldValues> extends FormOptions<TData, any, any, any, any, any, any, any, any, any, any, any> {
31
10
  formId?: string;
32
11
  }
@@ -82,13 +61,13 @@ interface Submit<T extends FieldValues = FieldValues, K extends SubmitKeysArg<T>
82
61
  isInDraggableView?: boolean;
83
62
  isLoading?: boolean;
84
63
  usedBoxes?: number;
85
- type?: 'submit' | 'button';
64
+ type?: "submit" | "button";
86
65
  key?: string;
87
66
  component?: (props: {
88
67
  onClick: () => void;
89
68
  index: number;
90
69
  key: string;
91
- type: 'submit' | 'button';
70
+ type: "submit" | "button";
92
71
  }) => React__default.JSX.Element;
93
72
  }
94
73
  interface ViewSettingsContainerProps {
@@ -147,13 +126,23 @@ interface FieldComponentProps<T extends FieldValues> {
147
126
  label?: string;
148
127
  helperMessage?: string;
149
128
  }
150
- type FormManagerConfig<T extends FieldValues> = Omit<FormFieldConfig<T>, 'field' | 'fieldState' | 'fieldValues'>;
129
+ type FormManagerConfig<T extends FieldValues> = Omit<FormFieldConfig<T>, "field" | "fieldState" | "fieldValues">;
151
130
  type MappedFormItemsFunction<F extends FieldValues, ComponentType> = (props: FormFunctionProps<F>) => ComponentType;
152
131
  type Rules = {
153
132
  validate?: (value: any) => string | undefined | Promise<string | undefined>;
154
133
  onChange?: (value: any) => void;
155
134
  onBlur?: () => void;
156
135
  onMount?: (value: any) => void;
136
+ /** @deprecated Use validate instead */
137
+ required?: {
138
+ value: boolean;
139
+ message: string;
140
+ } | boolean | string;
141
+ /** @deprecated Use validate instead */
142
+ pattern?: RegExp | {
143
+ value: RegExp;
144
+ message: string;
145
+ };
157
146
  };
158
147
  type FormFieldConfig<T extends FieldValues> = {
159
148
  name: any;
@@ -162,7 +151,7 @@ type FormFieldConfig<T extends FieldValues> = {
162
151
  field: GenericFieldApi<T>;
163
152
  fieldState: GenericFieldState<T>;
164
153
  containerStyle?: string;
165
- component: (props: Omit<FieldComponentProps<T>, 'formValues'>) => React__default.JSX.Element;
154
+ component: (props: Omit<FieldComponentProps<T>, "formValues">) => React__default.JSX.Element;
166
155
  container?: React__default.FC<PropsWithChildren>;
167
156
  ns?: string;
168
157
  errorNs?: string;
@@ -196,6 +185,31 @@ interface FormFieldProps<T extends FieldValues> {
196
185
  container?: React__default.FC<PropsWithChildren>;
197
186
  }
198
187
 
188
+ interface FormNotificationMessage {
189
+ message: string;
190
+ type: "success" | "error" | "info" | "warning";
191
+ autoHideDuration?: number;
192
+ textTransOption?: Record<string, unknown>;
193
+ ns?: string;
194
+ id?: string;
195
+ }
196
+ interface FormTranslationOptions {
197
+ [key: string]: string | number | boolean | undefined;
198
+ ns?: string;
199
+ }
200
+ interface FormConfigProps {
201
+ container?: React.ComponentType<ViewSettingsContainerProps>;
202
+ submitContainer?: React.ComponentType<ViewSettingsContainerProps>;
203
+ bodyContainer?: React.ComponentType<ViewSettingsContainerProps>;
204
+ formFieldContainer: React.FC<PropsWithChildren>;
205
+ showNotification?: (notification: FormNotificationMessage) => void;
206
+ translateText?: (key: string, options?: FormTranslationOptions) => string;
207
+ }
208
+ declare const formConfigAtom: _gaddario98_react_state.PrimitiveAtom<FormConfigProps>;
209
+ declare const useFormConfigValue: () => FormConfigProps;
210
+ declare const useFormConfigState: () => [FormConfigProps, (value: FormConfigProps) => void];
211
+ declare const useFormConfigReset: () => () => void;
212
+
199
213
  interface DefaultFormContainerProps {
200
214
  children: React.ReactNode;
201
215
  }
@@ -1,4 +1,4 @@
1
- 'use strict';var index_ts=require('../state/index.js'),compilerRuntime=require('react/compiler-runtime'),jsxRuntime=require('react/jsx-runtime'),react=require('react'),jotai=require('jotai'),utils=require('jotai/utils'),equal=require('fast-deep-equal'),reactForm=require('@tanstack/react-form');const DefaultContainer$1 = ({
1
+ 'use strict';var index_ts=require('../state/index.js'),compilerRuntime=require('react/compiler-runtime'),jsxRuntime=require('react/jsx-runtime'),react=require('react'),jotai=require('jotai'),utils=require('jotai/utils'),equal=require('fast-deep-equal'),reactForm=require('@tanstack/react-form'),withSelector_js=require('use-sync-external-store/shim/with-selector.js');const DefaultContainer$1 = ({
2
2
  children
3
3
  }) => {
4
4
  return children;
@@ -13,7 +13,7 @@ const {
13
13
  useState: useFormConfigState,
14
14
  useReset: useFormConfigReset
15
15
  } = index_ts.atomStateGenerator({
16
- key: 'formConfig',
16
+ key: "formConfig",
17
17
  defaultValue: _formConfig,
18
18
  persist: false
19
19
  });const createDefaultContainer = () => {
@@ -263,8 +263,59 @@ const useFormValues = ({
263
263
  get,
264
264
  set
265
265
  };
266
- };const trimObject = obj => Object.entries(obj !== null && obj !== void 0 ? obj : {}).reduce((prev, [key, val]) => Object.assign(Object.assign({}, prev), {
267
- [key]: typeof val === 'string' && !['password', 'pPassword'].includes(key) ? val.trim() : val
266
+ };function useStore(store, selector = (d) => d, options = {}) {
267
+ const equal = options.equal ?? shallow;
268
+ const slice = withSelector_js.useSyncExternalStoreWithSelector(
269
+ store.subscribe,
270
+ () => store.state,
271
+ () => store.state,
272
+ selector,
273
+ equal
274
+ );
275
+ return slice;
276
+ }
277
+ function shallow(objA, objB) {
278
+ if (Object.is(objA, objB)) {
279
+ return true;
280
+ }
281
+ if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) {
282
+ return false;
283
+ }
284
+ if (objA instanceof Map && objB instanceof Map) {
285
+ if (objA.size !== objB.size) return false;
286
+ for (const [k, v] of objA) {
287
+ if (!objB.has(k) || !Object.is(v, objB.get(k))) return false;
288
+ }
289
+ return true;
290
+ }
291
+ if (objA instanceof Set && objB instanceof Set) {
292
+ if (objA.size !== objB.size) return false;
293
+ for (const v of objA) {
294
+ if (!objB.has(v)) return false;
295
+ }
296
+ return true;
297
+ }
298
+ if (objA instanceof Date && objB instanceof Date) {
299
+ if (objA.getTime() !== objB.getTime()) return false;
300
+ return true;
301
+ }
302
+ const keysA = getOwnKeys(objA);
303
+ if (keysA.length !== getOwnKeys(objB).length) {
304
+ return false;
305
+ }
306
+ for (let i = 0; i < keysA.length; i++) {
307
+ if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !Object.is(objA[keysA[i]], objB[keysA[i]])) {
308
+ return false;
309
+ }
310
+ }
311
+ return true;
312
+ }
313
+ function getOwnKeys(obj) {
314
+ return Object.keys(obj).concat(
315
+ Object.getOwnPropertySymbols(obj)
316
+ );
317
+ }const trimObject = obj => Object.entries(obj !== null && obj !== void 0 ? obj : {}).reduce((prev, [key, val]) => Object.assign(Object.assign({}, prev), {
318
+ [key]: typeof val === "string" && !["password", "pPassword"].includes(key) ? val.trim() : val
268
319
  }), {});
269
320
  const RenderField = t0 => {
270
321
  const $ = compilerRuntime.c(9);
@@ -319,7 +370,7 @@ const DynamicFieldItem = ({
319
370
  formId
320
371
  });
321
372
  const fieldProps = react.useMemo(() => {
322
- if (typeof item === 'function') {
373
+ if (typeof item === "function") {
323
374
  return item({
324
375
  get,
325
376
  set
@@ -331,7 +382,7 @@ const DynamicFieldItem = ({
331
382
  const isHidden = react.useMemo(() => {
332
383
  if (!fieldProps.hidden) {
333
384
  return false;
334
- } else if (typeof fieldProps.hidden === 'function') {
385
+ } else if (typeof fieldProps.hidden === "function") {
335
386
  return fieldProps.hidden({
336
387
  get,
337
388
  set
@@ -343,7 +394,7 @@ const DynamicFieldItem = ({
343
394
  const rules = react.useMemo(() => {
344
395
  if (!fieldProps.rules) {
345
396
  return undefined;
346
- } else if (typeof fieldProps.rules === 'function') {
397
+ } else if (typeof fieldProps.rules === "function") {
347
398
  return fieldProps.rules({
348
399
  get,
349
400
  set
@@ -389,7 +440,7 @@ const SubmitItem = ({
389
440
  const keys = item.values;
390
441
  if (keys && keys.length > 0) {
391
442
  // Validate only specific fields
392
- await Promise.all(keys.map(key => formControl.validateField(key, 'change')));
443
+ await Promise.all(keys.map(key => formControl.validateField(key, "change")));
393
444
  const hasError = keys.some(key_0 => {
394
445
  // This is a simplified check. TanStack form errors might be structured differently.
395
446
  // You might need deep checking if errors are nested objects.
@@ -401,14 +452,14 @@ const SubmitItem = ({
401
452
  isValid = !hasError;
402
453
  } else {
403
454
  // Validate all
404
- await formControl.validateAllFields('submit');
455
+ await formControl.validateAllFields("submit");
405
456
  isValid = formControl.state.isValid;
406
457
  }
407
458
  if (!isValid) {
408
- onInvalidHandle(formControl.state.errors, item);
459
+ onInvalidHandle(handlersRef.current.errors, item);
409
460
  return;
410
461
  }
411
- const values = formControl.state.values;
462
+ const values = handlersRef.current.values;
412
463
  // Call handlers
413
464
  await createSubmitHandler(item)(values);
414
465
  }, [item, handlersRef]);
@@ -417,7 +468,7 @@ const SubmitItem = ({
417
468
  return jsxRuntime.jsx(Component, {
418
469
  onClick: handleClick,
419
470
  index: index,
420
- type: item.type || 'button'
471
+ type: item.type || "button"
421
472
  }, `submit-${index}`);
422
473
  };
423
474
  const useFormManager = ({
@@ -428,12 +479,11 @@ const useFormManager = ({
428
479
  formOptions,
429
480
  onValuesChange,
430
481
  globalErrorNs,
431
- id = 'form-manager'
482
+ id = "form-manager"
432
483
  }) => {
433
484
  const formControl = useJotaiForm(formOptions);
434
- const formState = react.useMemo(() => formControl.state, [formControl.state]);
435
- const errors = react.useMemo(() => formState.errors, [formState.errors]);
436
- const values = react.useMemo(() => formState.values, [formState.values]);
485
+ const errors = useStore(formControl.store, state => state.errors);
486
+ const values = useStore(formControl.store, state_0 => state_0.values);
437
487
  const setFormState = useSetFormState(id);
438
488
  const {
439
489
  showNotification
@@ -442,17 +492,9 @@ const useFormManager = ({
442
492
  react.useEffect(() => {
443
493
  setFormState({
444
494
  setValue,
445
- formValues: formState.values
446
- });
447
- const unsubscribe = formControl.store.subscribe(store => {
448
- setFormState({
449
- formValues: store.currentVal.values
450
- });
495
+ formValues: values
451
496
  });
452
- return () => {
453
- unsubscribe();
454
- }; /**/
455
- }, [formControl.store, setValue, formState.values, setFormState]);
497
+ }, [setFormState, setValue, values]);
456
498
  const handleNotification = react.useCallback(props => {
457
499
  if (props.message) {
458
500
  showNotification === null || showNotification === void 0 ? void 0 : showNotification(props);
@@ -476,13 +518,13 @@ const useFormManager = ({
476
518
  if (submitConfig_0.onSuccess) {
477
519
  return await submitConfig_0.onSuccess(filteredData);
478
520
  }
479
- throw new Error('No submit handler provided');
521
+ throw new Error("No submit handler provided");
480
522
  }, [filterFormData]);
481
523
  const handleError = react.useCallback((error, submitConfig_1) => {
482
524
  if (submitConfig_1.onError) {
483
525
  submitConfig_1.onError(error);
484
526
  }
485
- const notificationProps = typeof (notification === null || notification === void 0 ? void 0 : notification.error) === 'function' ? notification.error(error.message) : notification === null || notification === void 0 ? void 0 : notification.error;
527
+ const notificationProps = typeof (notification === null || notification === void 0 ? void 0 : notification.error) === "function" ? notification.error(error.message) : notification === null || notification === void 0 ? void 0 : notification.error;
486
528
  if (notificationProps === null || notificationProps === void 0 ? void 0 : notificationProps.message) {
487
529
  handleNotification(notificationProps);
488
530
  }
@@ -490,7 +532,7 @@ const useFormManager = ({
490
532
  const createSubmitHandler = react.useCallback(submitConfig_2 => async dataSubmit => {
491
533
  try {
492
534
  const res = await processSubmit(trimObject(dataSubmit), submitConfig_2);
493
- const notificationProps_0 = typeof (notification === null || notification === void 0 ? void 0 : notification.success) === 'function' ? notification.success(res) : notification === null || notification === void 0 ? void 0 : notification.success;
535
+ const notificationProps_0 = typeof (notification === null || notification === void 0 ? void 0 : notification.success) === "function" ? notification.success(res) : notification === null || notification === void 0 ? void 0 : notification.success;
494
536
  if (notificationProps_0 === null || notificationProps_0 === void 0 ? void 0 : notificationProps_0.message) {
495
537
  handleNotification(notificationProps_0);
496
538
  }
@@ -502,7 +544,7 @@ const useFormManager = ({
502
544
  }, [processSubmit, notification, handleNotification, handleError]);
503
545
  const onInvalidHandle = react.useCallback((err, submitConfig_3) => {
504
546
  onInvalid === null || onInvalid === void 0 ? void 0 : onInvalid(err);
505
- handleError(new Error('invalidData'), submitConfig_3);
547
+ handleError(new Error("invalidData"), submitConfig_3);
506
548
  }, [handleError, onInvalid]);
507
549
  const handlersRef = react.useRef({
508
550
  formControl,
@@ -510,7 +552,9 @@ const useFormManager = ({
510
552
  createSubmitHandler,
511
553
  setValue: formControl.setFieldValue,
512
554
  trigger: formControl.validateField,
513
- onValuesChange
555
+ onValuesChange,
556
+ values,
557
+ errors
514
558
  });
515
559
  react.useEffect(() => {
516
560
  handlersRef.current.onInvalidHandle = onInvalidHandle;
@@ -519,9 +563,13 @@ const useFormManager = ({
519
563
  handlersRef.current.trigger = formControl.validateField;
520
564
  handlersRef.current.onValuesChange = onValuesChange;
521
565
  }, [onInvalidHandle, createSubmitHandler, formControl, onValuesChange]);
566
+ react.useEffect(() => {
567
+ handlersRef.current.values = values;
568
+ handlersRef.current.errors = errors;
569
+ }, [values, errors]);
522
570
  const fields = react.useMemo(() => data.map((item, index) => {
523
571
  var _a, _b;
524
- const staticItem = typeof item === 'function' ? null : item;
572
+ const staticItem = typeof item === "function" ? null : item;
525
573
  return {
526
574
  index: (_a = staticItem === null || staticItem === void 0 ? void 0 : staticItem.index) !== null && _a !== void 0 ? _a : index,
527
575
  element: jsxRuntime.jsx(DynamicFieldItem, {
@@ -656,9 +704,14 @@ const FormManager = t0 => {
656
704
  t9 = $[21];
657
705
  }
658
706
  const renderSubmit = t9;
659
- const Container = (viewSettings === null || viewSettings === void 0 ? void 0 : viewSettings.container) || DefaultFormContainer;
660
- const BodyContainer = (viewSettings === null || viewSettings === void 0 ? void 0 : viewSettings.bodyContainer) || exports.DefaultContainer;
661
- const SubmitContainer = (viewSettings === null || viewSettings === void 0 ? void 0 : viewSettings.submitContainer) || exports.DefaultContainer;
707
+ const {
708
+ container,
709
+ bodyContainer,
710
+ submitContainer
711
+ } = useFormConfigValue();
712
+ const Container = (viewSettings === null || viewSettings === void 0 ? void 0 : viewSettings.container) || container || DefaultFormContainer;
713
+ const BodyContainer = (viewSettings === null || viewSettings === void 0 ? void 0 : viewSettings.bodyContainer) || bodyContainer || exports.DefaultContainer;
714
+ const SubmitContainer = (viewSettings === null || viewSettings === void 0 ? void 0 : viewSettings.submitContainer) || submitContainer || exports.DefaultContainer;
662
715
  let t10;
663
716
  if ($[22] !== BodyContainer || $[23] !== Container || $[24] !== SubmitContainer || $[25] !== render || $[26] !== renderSubmit || $[27] !== viewSettings) {
664
717
  t10 = jsxRuntime.jsxs(Container, Object.assign({}, (viewSettings === null || viewSettings === void 0 ? void 0 : viewSettings.containerProps) || {}, {