Line data Source code
1 : import * as React from 'react';
2 :
3 : type InitialState = {
4 : [key: string]: boolean;
5 : };
6 :
7 : type Dispatch = React.Dispatch<InitialState>;
8 :
9 1 : const initialState: InitialState = {};
10 1 : const Context = React.createContext<InitialState>(initialState);
11 :
12 1 : const reducer = (state: InitialState, action: InitialState) => ({
13 1 : ...state,
14 1 : ...action,
15 1 : });
16 :
17 1 : export const useNestedExpansionState = () => {
18 1 : return React.useContext(Context);
19 1 : };
20 :
21 1 : const DispatchExpands = React.createContext<Dispatch>(() => { });
22 :
23 1 : export function useNestedExpansionReducer() {
24 1 : return React.useReducer(reducer, initialState);
25 1 : }
26 :
27 1 : export function useNestedExpansionDispatch() {
28 1 : return React.useContext(DispatchExpands);
29 1 : }
30 :
31 : interface ExpandsProps {
32 : initial: InitialState;
33 : dispatch: Dispatch;
34 : }
35 :
36 1 : export const NestedExpansionStateProvider: React.FC<React.PropsWithChildren<ExpandsProps>> = ({ initial, dispatch, children }) => {
37 1 : return (
38 1 : <Context.Provider value={initial}>
39 1 : <DispatchExpands.Provider value={dispatch}>{children}</DispatchExpands.Provider>
40 1 : </Context.Provider>
41 : );
42 1 : };
|