-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathInternalContext.tsx
More file actions
54 lines (45 loc) · 1.9 KB
/
InternalContext.tsx
File metadata and controls
54 lines (45 loc) · 1.9 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
import React, { createContext, PropsWithChildren, useContext, useMemo } from 'react';
import { DataViewState } from '../DataView';
export interface DataViewSelection {
/** Called when the selection of items changes */
onSelect: (isSelecting: boolean, items?: any[] | any) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
/** Checks if a specific item is currently selected */
isSelected: (item: any) => boolean; // eslint-disable-line @typescript-eslint/no-explicit-any
/** Directly sets the selected items */
setSelected?: (items: any[]) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
/** Determines if selection is disabled for a given item */
isSelectDisabled?: (item: any) => boolean; // eslint-disable-line @typescript-eslint/no-explicit-any
}
export interface InternalContextProps {
/** Data selection props */
selection?: DataViewSelection;
/** Currently active state */
activeState?: DataViewState | string;
}
/** extends InternalContextProps */
export interface InternalContextValue extends InternalContextProps {
/** Flag indicating if data view is selectable (auto-calculated) */
isSelectable: boolean;
}
export const InternalContext = createContext<InternalContextValue>({
selection: undefined,
activeState: undefined,
isSelectable: false,
});
export type InternalProviderProps = PropsWithChildren<InternalContextProps>
export const InternalContextProvider: React.FC<InternalProviderProps> = ({
children,
selection,
activeState
}) => {
const isSelectable = useMemo(() => Boolean(selection?.onSelect && selection?.isSelected), [ selection?.onSelect, selection?.isSelected ]);
return (
<InternalContext.Provider
value={{ selection, activeState, isSelectable }}
>
{children}
</InternalContext.Provider>
);
}
export const useInternalContext = () => useContext(InternalContext);
export default InternalContext;