pcm-shared-components 0.0.84 → 0.0.87

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--'; //returns the save label for the save button in the bottom right of the dialog.
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 {
@@ -59,11 +60,63 @@ export const GenericDialogWindow = props => {
59
60
  } catch (error) {}
60
61
 
61
62
  return return_value;
63
+ }; //?-------------------------------------------------------
64
+ //? Children on click callback override, updates the onclick for a given menu
65
+ //?-------------------------------------------------------
66
+
67
+
68
+ const updateObjectByID = (obj, id, onClick) => {
69
+ if (!obj) return;
70
+
71
+ if (obj.id && obj.id === id) {
72
+ obj.onClick = onClick;
73
+ } else if (typeof obj === 'object' && Array.isArray(obj)) {
74
+ for (var i = 0; i < obj.length; i++) {
75
+ updateObjectByID(obj[i], id, onClick);
76
+ }
77
+ } else if (obj.subMenu) {
78
+ updateObjectByID(obj.subMenu, id, onClick);
79
+ }
80
+
81
+ return obj;
82
+ };
83
+
84
+ const setMenuOnClickCallback = useCallback((id, onClick) => {
85
+ //Used to set the onClick callback on a specific menu matching the id provided.
86
+ //Example: Used by the children component to set the onClick of a menu.
87
+ // A menu must have a unique id object matching the id on which we are searching for and a callback function must be provided
88
+ if (!onClickOverride.find(oco => oco.id.toLowerCase() === id.toLowerCase())) {
89
+ //Nice this override doesn't exist. Add it
90
+ setOnClickOverride(previousState => {
91
+ return [...previousState, {
92
+ id: id,
93
+ onClick: onClick
94
+ }];
95
+ });
96
+ }
97
+ }, [appBarMenu]);
98
+
99
+ const setOverrides = () => {
100
+ try {
101
+ //Loop over all our on click overrides and apply the onClick callback for each id found in the onClickOverride state
102
+ onClickOverride.forEach(item => {
103
+ setManagedAppBarMenu(previousState => {
104
+ return { ...previousState,
105
+ mainMenu: updateObjectByID(previousState.mainMenu, item.id, item.onClick)
106
+ };
107
+ });
108
+ });
109
+ } catch (error) {
110
+ console.error('Error in setMenuOnClickCallback: ', error);
111
+ }
62
112
  };
63
113
 
114
+ useEffect(() => {
115
+ //Wait for the state to be updated then loop over the overrides to ensure they are properly applied
116
+ setOverrides();
117
+ }, [onClickOverride]);
64
118
  useEffect(() => {
65
119
  if (appBarMenu && appBarMenu.mainMenu) {
66
- // 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
67
120
  setManagedAppBarMenu({ ...appBarMenu,
68
121
  mainMenu: [...appBarMenu.mainMenu.map(mm => {
69
122
  if (mm.id === 'save') {
@@ -76,7 +129,10 @@ export const GenericDialogWindow = props => {
76
129
  return mm;
77
130
  }
78
131
  })]
79
- });
132
+ }); // 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
133
+ //Need to re-apply any onClick override to ensure they are still properly set by what was overridden from the child component.
134
+
135
+ setOverrides();
80
136
  }
81
137
  }, [appBarMenu, can_save, showSaveButton, save_button_label]); // -----------------------------------------
82
138
  // Note make sure to include disableEnforceFocus or else I'll have infinite error/warnings on Uncaught RangeError.
@@ -97,7 +153,7 @@ export const GenericDialogWindow = props => {
97
153
  }, /*#__PURE__*/React.createElement(GenericAppBar, {
98
154
  appBarMenu: managedAppBarMenu
99
155
  }), /*#__PURE__*/React.createElement(DialogContent, {
100
- className: "pt-4 pl-0 pr-0 "
156
+ className: "pt-4 pl-0 pr-0 h-full w-full"
101
157
  }, /*#__PURE__*/React.createElement(PerfectScrollbar, {
102
158
  className: "",
103
159
  enable: true
@@ -109,7 +165,8 @@ export const GenericDialogWindow = props => {
109
165
  setIsLoading,
110
166
  setSaveButtonLabel,
111
167
  closeDialog,
112
- data: data
168
+ data: data,
169
+ setMenuOnClickCallback
113
170
  },
114
171
  ref: childRef
115
172
  }) : /*#__PURE__*/React.createElement(React.Fragment, null)))), /*#__PURE__*/React.createElement(DialogActions, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pcm-shared-components",
3
- "version": "0.0.84",
3
+ "version": "0.0.87",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "babel": {