pcm-shared-components 0.0.85 → 0.0.86
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.
|
@@ -27,6 +27,7 @@ export const GenericDialogWindow = props => {
|
|
|
27
27
|
const [is_loading, setIsLoading] = useState(false);
|
|
28
28
|
const [managedAppBarMenu, setManagedAppBarMenu] = useState(undefined);
|
|
29
29
|
const [save_button_label, setSaveButtonLabel] = useState(undefined);
|
|
30
|
+
const [onClickOverride, setOnClickOverride] = useState([]);
|
|
30
31
|
const childRef = useRef();
|
|
31
32
|
|
|
32
33
|
const onSave = () => {
|
|
@@ -47,7 +48,7 @@ export const GenericDialogWindow = props => {
|
|
|
47
48
|
};
|
|
48
49
|
|
|
49
50
|
const getSaveButtonLabel = () => {
|
|
50
|
-
let return_value = 'Save
|
|
51
|
+
let return_value = 'Save'; //returns the save label for the save button in the bottom right of the dialog.
|
|
51
52
|
// The save label is taken from the mainMenu name item having an id='save'
|
|
52
53
|
|
|
53
54
|
try {
|
|
@@ -61,6 +62,48 @@ export const GenericDialogWindow = props => {
|
|
|
61
62
|
return return_value;
|
|
62
63
|
};
|
|
63
64
|
|
|
65
|
+
const setMenuOnClickCallback = useCallback((id, onClick) => {
|
|
66
|
+
//Used to set the onClick callback on a specific menu matching the id provided.
|
|
67
|
+
//Example: Used by the children component to set the onClick of a menu.
|
|
68
|
+
// A menu must have a unique id object matching the id on which we are searching for and a callback function must be provided
|
|
69
|
+
if (!onClickOverride.find(oco => oco.id.toLowerCase() === id.toLowerCase())) {
|
|
70
|
+
//Nice this override doesn't exist. Add it
|
|
71
|
+
setOnClickOverride(previousState => {
|
|
72
|
+
return [...previousState, {
|
|
73
|
+
id: id,
|
|
74
|
+
onClick: onClick
|
|
75
|
+
}];
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}, []);
|
|
79
|
+
|
|
80
|
+
const setOverrides = () => {
|
|
81
|
+
try {
|
|
82
|
+
//Loop over all our on click overrides and apply the onClick callback for each id found in the onClickOverride state
|
|
83
|
+
onClickOverride.forEach(item => {
|
|
84
|
+
setManagedAppBarMenu(previousState => {
|
|
85
|
+
return { ...previousState,
|
|
86
|
+
mainMenu: [...previousState.mainMenu.map(mm => {
|
|
87
|
+
if (mm.id.toLowerCase() === item.id.toLowerCase()) {
|
|
88
|
+
return { ...mm,
|
|
89
|
+
onClick: item.onClick
|
|
90
|
+
};
|
|
91
|
+
} else {
|
|
92
|
+
return mm;
|
|
93
|
+
}
|
|
94
|
+
})]
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
} catch (error) {
|
|
99
|
+
console.error('Error in setMenuOnClickCallback: ', error);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
//Wait for the state to be updated then loop over the overrides to ensure they are properly applied
|
|
105
|
+
setOverrides();
|
|
106
|
+
}, [onClickOverride]);
|
|
64
107
|
useEffect(() => {
|
|
65
108
|
if (appBarMenu && appBarMenu.mainMenu) {
|
|
66
109
|
// If we have a mainMenu item with the id='save' then override the onClick event to use the onSave function which reaches into the child ref
|
|
@@ -76,7 +119,9 @@ export const GenericDialogWindow = props => {
|
|
|
76
119
|
return mm;
|
|
77
120
|
}
|
|
78
121
|
})]
|
|
79
|
-
});
|
|
122
|
+
}); //Need to re-apply any onClick override to ensure they are still properly set by what was overridden from the child component.
|
|
123
|
+
|
|
124
|
+
setOverrides();
|
|
80
125
|
}
|
|
81
126
|
}, [appBarMenu, can_save, showSaveButton, save_button_label]); // -----------------------------------------
|
|
82
127
|
// Note make sure to include disableEnforceFocus or else I'll have infinite error/warnings on Uncaught RangeError.
|
|
@@ -109,7 +154,8 @@ export const GenericDialogWindow = props => {
|
|
|
109
154
|
setIsLoading,
|
|
110
155
|
setSaveButtonLabel,
|
|
111
156
|
closeDialog,
|
|
112
|
-
data: data
|
|
157
|
+
data: data,
|
|
158
|
+
setMenuOnClickCallback
|
|
113
159
|
},
|
|
114
160
|
ref: childRef
|
|
115
161
|
}) : /*#__PURE__*/React.createElement(React.Fragment, null)))), /*#__PURE__*/React.createElement(DialogActions, {
|