|
|
@ -1,58 +1,28 @@
|
|
|
|
import { cloneDeep } from 'lodash';
|
|
|
|
import { cloneDeep } from 'lodash';
|
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
import React, { useCallback, useEffect } from 'react';
|
|
|
|
import areAllSelected from 'Utilities/Table/areAllSelected';
|
|
|
|
import useSelectState, { SelectState } from 'Helpers/Hooks/useSelectState';
|
|
|
|
import selectAll from 'Utilities/Table/selectAll';
|
|
|
|
|
|
|
|
import toggleSelected from 'Utilities/Table/toggleSelected';
|
|
|
|
|
|
|
|
import ModelBase from './ModelBase';
|
|
|
|
import ModelBase from './ModelBase';
|
|
|
|
|
|
|
|
|
|
|
|
export enum SelectActionType {
|
|
|
|
export type SelectContextAction =
|
|
|
|
Reset,
|
|
|
|
| { type: 'reset' }
|
|
|
|
SelectAll,
|
|
|
|
| { type: 'selectAll' }
|
|
|
|
UnselectAll,
|
|
|
|
| { type: 'unselectAll' }
|
|
|
|
ToggleSelected,
|
|
|
|
|
|
|
|
RemoveItem,
|
|
|
|
|
|
|
|
UpdateItems,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type SelectedState = Record<number, boolean>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface SelectState {
|
|
|
|
|
|
|
|
selectedState: SelectedState;
|
|
|
|
|
|
|
|
lastToggled: number | null;
|
|
|
|
|
|
|
|
allSelected: boolean;
|
|
|
|
|
|
|
|
allUnselected: boolean;
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
|
|
|
items: any[];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type SelectAction =
|
|
|
|
|
|
|
|
| { type: SelectActionType.Reset }
|
|
|
|
|
|
|
|
| { type: SelectActionType.SelectAll }
|
|
|
|
|
|
|
|
| { type: SelectActionType.UnselectAll }
|
|
|
|
|
|
|
|
| {
|
|
|
|
| {
|
|
|
|
type: SelectActionType.ToggleSelected;
|
|
|
|
type: 'toggleSelected';
|
|
|
|
id: number;
|
|
|
|
id: number;
|
|
|
|
isSelected: boolean;
|
|
|
|
isSelected: boolean;
|
|
|
|
shiftKey: boolean;
|
|
|
|
shiftKey: boolean;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
| {
|
|
|
|
type: SelectActionType.RemoveItem;
|
|
|
|
type: 'removeItem';
|
|
|
|
id: number;
|
|
|
|
id: number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
| {
|
|
|
|
type: SelectActionType.UpdateItems;
|
|
|
|
type: 'updateItems';
|
|
|
|
items: ModelBase[];
|
|
|
|
items: ModelBase[];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
type Dispatch = (action: SelectAction) => void;
|
|
|
|
export type SelectDispatch = (action: SelectContextAction) => void;
|
|
|
|
|
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
|
|
|
|
selectedState: {},
|
|
|
|
|
|
|
|
lastToggled: null,
|
|
|
|
|
|
|
|
allSelected: false,
|
|
|
|
|
|
|
|
allUnselected: true,
|
|
|
|
|
|
|
|
items: [],
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface SelectProviderOptions<T extends ModelBase> {
|
|
|
|
interface SelectProviderOptions<T extends ModelBase> {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
@ -60,90 +30,40 @@ interface SelectProviderOptions<T extends ModelBase> {
|
|
|
|
items: Array<T>;
|
|
|
|
items: Array<T>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getSelectedState(items: ModelBase[], existingState: SelectedState) {
|
|
|
|
const SelectContext = React.createContext<
|
|
|
|
return items.reduce((acc: SelectedState, item) => {
|
|
|
|
[SelectState, SelectDispatch] | undefined
|
|
|
|
const id = item.id;
|
|
|
|
>(cloneDeep(undefined));
|
|
|
|
|
|
|
|
|
|
|
|
acc[id] = existingState[id] ?? false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Can this be reused?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const SelectContext = React.createContext<[SelectState, Dispatch] | undefined>(
|
|
|
|
|
|
|
|
cloneDeep(undefined)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function selectReducer(state: SelectState, action: SelectAction): SelectState {
|
|
|
|
|
|
|
|
const { items, selectedState } = state;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
|
|
|
case SelectActionType.Reset: {
|
|
|
|
|
|
|
|
return cloneDeep(initialState);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case SelectActionType.SelectAll: {
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
items,
|
|
|
|
|
|
|
|
...selectAll(selectedState, true),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case SelectActionType.UnselectAll: {
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
items,
|
|
|
|
|
|
|
|
...selectAll(selectedState, false),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case SelectActionType.ToggleSelected: {
|
|
|
|
|
|
|
|
const result = {
|
|
|
|
|
|
|
|
items,
|
|
|
|
|
|
|
|
...toggleSelected(
|
|
|
|
|
|
|
|
state,
|
|
|
|
|
|
|
|
items,
|
|
|
|
|
|
|
|
action.id,
|
|
|
|
|
|
|
|
action.isSelected,
|
|
|
|
|
|
|
|
action.shiftKey
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case SelectActionType.UpdateItems: {
|
|
|
|
|
|
|
|
const nextSelectedState = getSelectedState(action.items, selectedState);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
...state,
|
|
|
|
|
|
|
|
...areAllSelected(nextSelectedState),
|
|
|
|
|
|
|
|
selectedState: nextSelectedState,
|
|
|
|
|
|
|
|
items: action.items,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
|
|
|
|
throw new Error(`Unhandled action type: ${action.type}`);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function SelectProvider<T extends ModelBase>(
|
|
|
|
export function SelectProvider<T extends ModelBase>(
|
|
|
|
props: SelectProviderOptions<T>
|
|
|
|
props: SelectProviderOptions<T>
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
const { items } = props;
|
|
|
|
const { items } = props;
|
|
|
|
const selectedState = getSelectedState(items, {});
|
|
|
|
const [state, dispatch] = useSelectState();
|
|
|
|
|
|
|
|
|
|
|
|
const [state, dispatch] = React.useReducer(selectReducer, {
|
|
|
|
const dispatchWrapper = useCallback(
|
|
|
|
selectedState,
|
|
|
|
(action: SelectContextAction) => {
|
|
|
|
lastToggled: null,
|
|
|
|
switch (action.type) {
|
|
|
|
allSelected: false,
|
|
|
|
case 'reset':
|
|
|
|
allUnselected: true,
|
|
|
|
case 'removeItem':
|
|
|
|
items,
|
|
|
|
dispatch(action);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
|
|
|
|
...action,
|
|
|
|
|
|
|
|
items,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
[items, dispatch]
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const value: [SelectState, Dispatch] = [state, dispatch];
|
|
|
|
const value: [SelectState, SelectDispatch] = [state, dispatchWrapper];
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
useEffect(() => {
|
|
|
|
dispatch({ type: SelectActionType.UpdateItems, items });
|
|
|
|
dispatch({ type: 'updateItems', items });
|
|
|
|
}, [items]);
|
|
|
|
}, [items, dispatch]);
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<SelectContext.Provider value={value}>
|
|
|
|
<SelectContext.Provider value={value}>
|
|
|
|