-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
useRowSelect.tsx
137 lines (111 loc) · 3.71 KB
/
useRowSelect.tsx
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import * as React from 'react';
import cs from 'clsx';
import * as COLORS from '@table-library/react-table-library/common/colors';
import { isRowClick } from '@table-library/react-table-library/common/util/isRowClick';
import { useIdReducer } from '@table-library/react-table-library/common/util/useIdReducer';
import { useSyncRefState } from '@table-library/react-table-library/common/util/useSyncRefState';
import { applyModifiers } from '@table-library/react-table-library/common/util/modifiers';
import { State, StateAndChange } from '@table-library/react-table-library/types/common';
import {
Data,
TableNode,
Features,
FeatureProps,
RowProps,
} from '@table-library/react-table-library/types/table';
import {
Select,
SelectOptions,
SelectTypes,
SelectClickTypes,
} from '@table-library/react-table-library/types/select';
import { HeaderCellSelect } from './HeaderCellSelect';
import { CellSelect } from './CellSelect';
const getRowProps = (props: RowProps, features: Features): FeatureProps => {
const { item } = props;
const { select } = features;
if (!select) {
throw new Error("No 'select' in getRowProps. That's odd");
}
const isMultiSelected = select.state.ids.includes(item.id);
const isSingleSelect = select.state.id === item.id;
const theme = `
&.row-select-selected,
&.row-select-single-selected {
font-weight: bold;
background-color: ${COLORS.ROW_SELECTED};
}
&.row-select-clickable {
cursor: pointer;
}
`;
const className = cs('row-select', {
'row-select-clickable': select.options.clickType === SelectClickTypes.RowClick,
'row-select-selected': isMultiSelected,
'row-select-single-selected': isSingleSelect,
});
const onClick = (node: TableNode, event: React.SyntheticEvent | React.KeyboardEvent) => {
if (!isRowClick(event)) return;
if (select.options.clickType !== SelectClickTypes.RowClick) return;
const hasMultiSelectType =
select.options.rowSelect === SelectTypes.MultiSelect ||
select.options.buttonSelect === SelectTypes.MultiSelect;
const isMultiSelectType = select.options.rowSelect === SelectTypes.MultiSelect;
// optional ways to activate multi-select with keyboard
const isCommandSelectType = !!(event as any).metaKey;
const isShiftSelectType = !!(event as any).shiftKey;
if (isCommandSelectType && hasMultiSelectType) {
select.fns.onToggleById(node.id);
} else if (isShiftSelectType && hasMultiSelectType) {
select.fns.onToggleByIdShift(node.id, select.options, applyModifiers(features));
} else if (isMultiSelectType) {
select.fns.onToggleById(node.id);
} /* isSingleSelectType */ else {
select.fns.onToggleByIdExclusively(node.id);
}
};
return {
theme,
className,
onClick,
};
};
const DEFAULT_STATE = {
ids: [],
id: null,
};
const DEFAULT_OPTIONS = {
clickType: SelectClickTypes.RowClick,
rowSelect: SelectTypes.SingleSelect,
buttonSelect: SelectTypes.MultiSelect,
isCarryForward: true,
isPartialToAll: false,
};
const useRowSelect = (
data: Data,
primary?: StateAndChange,
options?: SelectOptions,
context?: any,
): Select => {
const controlledState: State = primary?.state
? { ...DEFAULT_STATE, ...primary.state }
: { ...DEFAULT_STATE };
const onChange = primary?.onChange ? primary.onChange : () => {};
const [state, fns] = useIdReducer(data, controlledState, onChange, context);
useSyncRefState('select', context, state);
const mergedOptions = {
...DEFAULT_OPTIONS,
...(options ? options : {}),
};
return {
state,
fns,
options: mergedOptions,
_getRowProps: getRowProps,
components: {
HeaderCellSelect,
CellSelect,
},
};
};
export { useRowSelect };