@pagamio/frontend-commons-lib 0.8.276 → 0.8.277
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.
|
@@ -24,6 +24,8 @@ interface DrawerContentProps {
|
|
|
24
24
|
persistenceKey?: string;
|
|
25
25
|
/** Ref updated every render with current dirty state — read by FormEngineDrawer for X-button check. */
|
|
26
26
|
isDirtyRef?: React.MutableRefObject<boolean>;
|
|
27
|
+
/** Ref updated every render with handleCancel — call from outside (e.g. X button) to show the same unsaved-changes modal. */
|
|
28
|
+
cancelRef?: React.MutableRefObject<() => void>;
|
|
27
29
|
}
|
|
28
30
|
declare const DrawerContent: React.FC<DrawerContentProps>;
|
|
29
31
|
export default DrawerContent;
|
|
@@ -5,7 +5,7 @@ import { Button, NotificationModal } from '../../components';
|
|
|
5
5
|
import { useToast } from '../../context';
|
|
6
6
|
import { FieldWrapper } from '../../form-engine';
|
|
7
7
|
import { useFormPersistence } from '../../form-engine/hooks/useFormPersistence';
|
|
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, isDirtyRef, }) => {
|
|
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, isDirtyRef, cancelRef, }) => {
|
|
9
9
|
const { saveFormData, restoreFormData, clearPersistedData, hasPersistedData } = useFormPersistence(persistenceKey);
|
|
10
10
|
const { addToast } = useToast();
|
|
11
11
|
// Determine initial values: initialValues take precedence over persisted data
|
|
@@ -144,10 +144,13 @@ const DrawerContent = ({ fields, onSubmit, isOpen, initialValues, submitButtonTe
|
|
|
144
144
|
const handleContinueEditing = () => {
|
|
145
145
|
setShowCancelConfirmModal(false);
|
|
146
146
|
};
|
|
147
|
-
// Keep isDirtyRef in sync
|
|
147
|
+
// Keep isDirtyRef and cancelRef in sync every render.
|
|
148
148
|
if (isDirtyRef) {
|
|
149
149
|
isDirtyRef.current = checkUnsavedChanges();
|
|
150
150
|
}
|
|
151
|
+
if (cancelRef) {
|
|
152
|
+
cancelRef.current = handleCancel;
|
|
153
|
+
}
|
|
151
154
|
// Helper function to process dependency validations
|
|
152
155
|
const processDependencyValidation = (field) => {
|
|
153
156
|
if (!field.validation?.dependency) {
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import { jsx as _jsx
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { useEffect, useRef, useState } from 'react';
|
|
3
|
-
import { Button, NotificationModal } from '../components';
|
|
4
3
|
import BaseDrawer from './components/BaseDrawer';
|
|
5
4
|
import DrawerContent from './components/DrawerContent';
|
|
6
5
|
const FormEngineDrawer = ({ title, cancelButtonText = 'Cancel', submitButtonText = 'Submit', showForm = true, showDrawerButtons = true, isOpen, fields: initialFields, initialValues, children, marginTop = '0px', onClose, onSubmit, onFieldUpdate, onFieldChange, persistenceKey, }) => {
|
|
7
6
|
const [fields, setFields] = useState(initialFields);
|
|
8
|
-
const [showDiscardConfirm, setShowDiscardConfirm] = useState(false);
|
|
9
7
|
const initializedRef = useRef(false);
|
|
10
8
|
const processingDependencyRef = useRef(false);
|
|
11
9
|
const pendingUpdatesRef = useRef(new Map());
|
|
12
|
-
//
|
|
13
|
-
|
|
10
|
+
// cancelRef is updated every render by DrawerContent with its handleCancel function.
|
|
11
|
+
// Calling cancelRef.current() from the X button shows the same modal as the Cancel button.
|
|
12
|
+
const cancelRef = useRef(() => { });
|
|
14
13
|
// Update fields when initialFields changes while preserving values
|
|
15
14
|
useEffect(() => {
|
|
16
15
|
setFields((prevFields) => {
|
|
@@ -134,18 +133,6 @@ const FormEngineDrawer = ({ title, cancelButtonText = 'Cancel', submitButtonText
|
|
|
134
133
|
processingDependencyRef.current = false;
|
|
135
134
|
}
|
|
136
135
|
};
|
|
137
|
-
|
|
138
|
-
if (isDirtyRef.current) {
|
|
139
|
-
setShowDiscardConfirm(true);
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
onClose();
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
const handleConfirmDiscard = () => {
|
|
146
|
-
setShowDiscardConfirm(false);
|
|
147
|
-
onClose();
|
|
148
|
-
};
|
|
149
|
-
return (_jsxs(_Fragment, { children: [_jsx(BaseDrawer, { title: title, isOpen: isOpen, marginTop: marginTop, onClose: onClose, onCloseAttempt: handleCloseAttempt, children: _jsx(DrawerContent, { isOpen: isOpen, fields: fields, onSubmit: onSubmit, handleCloseDrawer: onClose, initialValues: initialValues, showForm: showForm, showDrawerButtons: showDrawerButtons, cancelButtonText: cancelButtonText, submitButtonText: submitButtonText, onFieldUpdate: onFieldUpdate, onFieldChange: onFieldChange, updateFieldOptions: updateFieldOptions, updateFieldLabel: updateFieldLabel, setFieldHidden: setFieldHidden, addField: addField, updateFieldValidation: updateFieldValidation, processInitialFieldUpdates: processInitialFieldUpdates, processingDependencyRef: processingDependencyRef, pendingUpdatesRef: pendingUpdatesRef, persistenceKey: persistenceKey, isDirtyRef: isDirtyRef, children: children }, persistenceKey || 'drawer-content') }), _jsx(NotificationModal, { show: showDiscardConfirm, onClose: () => setShowDiscardConfirm(false), title: "Unsaved Changes", message: "You have unsaved changes. Are you sure you want to close?", variant: "warning", showConfirmButton: false, showCancelButton: false, children: _jsxs("div", { className: "flex justify-center gap-3", children: [_jsx(Button, { variant: "primary", onClick: () => setShowDiscardConfirm(false), children: "Keep Editing" }), _jsx(Button, { variant: "outline", onClick: handleConfirmDiscard, children: "Discard Changes" })] }) })] }));
|
|
136
|
+
return (_jsx(BaseDrawer, { title: title, isOpen: isOpen, marginTop: marginTop, onClose: onClose, onCloseAttempt: () => cancelRef.current(), children: _jsx(DrawerContent, { isOpen: isOpen, fields: fields, onSubmit: onSubmit, handleCloseDrawer: onClose, initialValues: initialValues, showForm: showForm, showDrawerButtons: showDrawerButtons, cancelButtonText: cancelButtonText, submitButtonText: submitButtonText, onFieldUpdate: onFieldUpdate, onFieldChange: onFieldChange, updateFieldOptions: updateFieldOptions, updateFieldLabel: updateFieldLabel, setFieldHidden: setFieldHidden, addField: addField, updateFieldValidation: updateFieldValidation, processInitialFieldUpdates: processInitialFieldUpdates, processingDependencyRef: processingDependencyRef, pendingUpdatesRef: pendingUpdatesRef, persistenceKey: persistenceKey, cancelRef: cancelRef, children: children }, persistenceKey || 'drawer-content') }));
|
|
150
137
|
};
|
|
151
138
|
export default FormEngineDrawer;
|
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.
|
|
4
|
+
"version": "0.8.277",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
7
7
|
"provenance": false
|