@pagamio/frontend-commons-lib 0.8.343 → 0.8.344

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,6 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
2
  import { useForm } from 'react-hook-form';
3
- import { useEffect, useRef, useState } from 'react';
3
+ import { useEffect, useMemo, useRef, useState } from 'react';
4
4
  import { Button, NotificationModal } from '../../components';
5
5
  import { useToast } from '../../context';
6
6
  import { FieldWrapper } from '../../form-engine';
@@ -8,21 +8,32 @@ import { useFormPersistence } from '../../form-engine/hooks/useFormPersistence';
8
8
  const DrawerContent = ({ fields, onSubmit, isOpen, initialValues, submitButtonText = 'Submit', cancelButtonText = 'Cancel', showForm = true, showDrawerButtons = true, handleCloseDrawer, onFieldUpdate, onFieldChange, children, updateFieldOptions, updateFieldLabel, setFieldHidden, addField, updateFieldValidation, processInitialFieldUpdates, processingDependencyRef, pendingUpdatesRef, persistenceKey, clearOnClose = false, isDirtyRef, cancelRef, }) => {
9
9
  const { saveFormData, restoreFormData, clearPersistedData, hasPersistedData } = useFormPersistence(persistenceKey);
10
10
  const { addToast } = useToast();
11
- // Determine initial values: initialValues take precedence over persisted data
12
- // This ensures that when editing different items, the form shows the correct item's data
11
+ // Build a `{ fieldName: '' }` baseline so every registered input starts
12
+ // controlled (with a defined string value). Without this, RHF defaults
13
+ // unspecified fields to `undefined`, which React flags as a "controlled
14
+ // → uncontrolled" transition the moment we reset, and Radix Select keeps
15
+ // showing its previously-selected display value.
16
+ const emptyFieldValues = useMemo(() => {
17
+ const acc = {};
18
+ for (const f of fields) {
19
+ acc[f.name] = '';
20
+ }
21
+ return acc;
22
+ }, [fields]);
23
+ // Determine initial values: initialValues take precedence over persisted data.
24
+ // For clearOnClose drawers we always start from the empty baseline so a
25
+ // stale persistence entry from a previous lifetime can't leak in.
13
26
  const getEffectiveInitialValues = () => {
14
- // If initialValues are provided, use them
15
27
  if (initialValues && Object.keys(initialValues).length > 0) {
16
- return initialValues;
28
+ return { ...emptyFieldValues, ...initialValues };
17
29
  }
18
- // Otherwise, check for persisted data
19
- if (persistenceKey && hasPersistedData()) {
30
+ if (!clearOnClose && persistenceKey && hasPersistedData()) {
20
31
  const persistedData = restoreFormData();
21
32
  if (persistedData) {
22
- return persistedData;
33
+ return { ...emptyFieldValues, ...persistedData };
23
34
  }
24
35
  }
25
- return initialValues || {};
36
+ return emptyFieldValues;
26
37
  };
27
38
  const { control, handleSubmit, watch, setValue, clearErrors, formState: { errors, isSubmitting }, reset, getValues, } = useForm({ mode: 'onBlur', defaultValues: getEffectiveInitialValues() });
28
39
  const [pendingUpdateCount, setPendingUpdateCount] = useState(0);
@@ -39,19 +50,25 @@ const DrawerContent = ({ fields, onSubmit, isOpen, initialValues, submitButtonTe
39
50
  mountInitialValuesRef.current = initialValues || {};
40
51
  }
41
52
  wasOpenRef.current = !!isOpen;
42
- // Clear persisted data on every open → close transition when the consumer
43
- // opts in. Covers cancel, X, ESC, programmatic close, and post-submit close
44
- // all routes the user can take out of the drawer.
53
+ // Clear persisted data AND react-hook-form state on every open → close
54
+ // transition when the consumer opts in. Resetting RHF is critical: the
55
+ // watch()-driven save effect would otherwise re-populate persistence with
56
+ // the old values on the very next render (e.g. during the close animation).
45
57
  const clearOnCloseRef = useRef(clearOnClose);
46
58
  clearOnCloseRef.current = clearOnClose;
47
59
  const clearPersistedDataRef = useRef(clearPersistedData);
48
60
  clearPersistedDataRef.current = clearPersistedData;
61
+ const resetRef = useRef(reset);
62
+ resetRef.current = reset;
63
+ const emptyFieldValuesRef = useRef(emptyFieldValues);
64
+ emptyFieldValuesRef.current = emptyFieldValues;
49
65
  const previousIsOpenRef = useRef(!!isOpen);
50
66
  useEffect(() => {
51
67
  const wasOpen = previousIsOpenRef.current;
52
68
  previousIsOpenRef.current = !!isOpen;
53
69
  if (wasOpen && !isOpen && clearOnCloseRef.current) {
54
70
  clearPersistedDataRef.current();
71
+ resetRef.current(emptyFieldValuesRef.current);
55
72
  }
56
73
  }, [isOpen]);
57
74
  const password = watch('password');
@@ -74,16 +91,20 @@ const DrawerContent = ({ fields, onSubmit, isOpen, initialValues, submitButtonTe
74
91
  return true;
75
92
  });
76
93
  };
77
- // Save form data when fields change (for persistence)
94
+ // Save form data when fields change (for persistence). Guarded by `isOpen`
95
+ // so a closing drawer (whose form state hasn't been reset yet) doesn't
96
+ // re-write the values we just cleared via clearOnClose. Without this guard,
97
+ // the close-time clear races with the watch()-driven save and the save wins.
78
98
  useEffect(() => {
99
+ if (!isOpen)
100
+ return;
79
101
  if (persistenceKey && allFields && Object.keys(allFields).length > 0) {
80
- // Only save if there are actual field values (not just empty object)
81
102
  const hasValues = Object.values(allFields).some((value) => value !== undefined && value !== null && value !== '');
82
103
  if (hasValues) {
83
104
  saveFormData(allFields, true);
84
105
  }
85
106
  }
86
- }, [allFields, persistenceKey, saveFormData]);
107
+ }, [isOpen, allFields, persistenceKey, saveFormData]);
87
108
  // Process initial field updates once when drawer opens
88
109
  useEffect(() => {
89
110
  if (isOpen && !initialLoadPerformedRef.current && processInitialFieldUpdates) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pagamio/frontend-commons-lib",
3
3
  "description": "Pagamio library for Frontend reusable components like the form engine and table container",
4
- "version": "0.8.343",
4
+ "version": "0.8.344",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "provenance": false