-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathConfigContext.jsx
More file actions
80 lines (69 loc) · 2.22 KB
/
Copy pathConfigContext.jsx
File metadata and controls
80 lines (69 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import PropTypes from 'prop-types';
import { createContext, useReducer } from 'react';
// project imports
import * as actionType from 'store/actions';
import { CONFIG } from 'config/constant';
const initialState = {
...CONFIG,
isOpen: [], // for active default menu
isTrigger: [] // for active default menu, set blank for horizontal
};
const ConfigContext = createContext({});
const { Provider } = ConfigContext;
function ConfigProvider({ children }) {
let trigger = [];
let open = [];
const [state, dispatch] = useReducer((stateData, action) => {
switch (action.type) {
case actionType.COLLAPSE_MENU:
return {
...stateData,
collapseMenu: !stateData.collapseMenu
};
case actionType.COLLAPSE_HEADERMENU:
return {
...stateData,
collapseHeaderMenu: !stateData.collapseHeaderMenu
};
case actionType.SIDEBAR_HIDE:
return {
...stateData,
sidebarHide: !stateData.sidebarHide
};
case actionType.COLLAPSE_TOGGLE:
if (action.menu.type === 'sub') {
open = stateData.isOpen;
trigger = stateData.isTrigger;
const triggerIndex = trigger.indexOf(action.menu.id);
if (triggerIndex > -1) {
open = open.filter((item) => item !== action.menu.id);
trigger = trigger.filter((item) => item !== action.menu.id);
}
if (triggerIndex === -1) {
open = [...open, action.menu.id];
trigger = [...trigger, action.menu.id];
trigger = [...trigger, action.menu.id];
}
} else {
open = stateData.isOpen;
const triggerIndex = stateData.isTrigger.indexOf(action.menu.id);
trigger = triggerIndex === -1 ? [action.menu.id] : [];
open = triggerIndex === -1 ? [action.menu.id] : [];
}
return {
...stateData,
isOpen: open,
isTrigger: trigger
};
default:
throw new Error();
}
}, initialState);
return (
<Provider value={{ state, dispatch }}>
<>{children}</>{' '}
</Provider>
);
}
export { ConfigContext, ConfigProvider };
ConfigProvider.propTypes = { children: PropTypes.any };