Skip to content

Commit 38f39a0

Browse files
committed
feat(select) shift select on checkbox possiblr
1 parent 5769068 commit 38f39a0

File tree

6 files changed

+20
-11
lines changed

6 files changed

+20
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@table-library/react-table-library",
3-
"version": "2.0.9",
3+
"version": "2.0.10",
44
"description": "react-table-library",
55
"type": "module",
66
"main": "main.js",

src/common/context/Feature.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const useFeatures = () => {
1111
const sort = React.useContext(SortContext);
1212
const pagination = React.useContext(PaginationContext);
1313

14+
// if changed, adjust applyModifiers usages
1415
return {
1516
select,
1617
tree,

src/table/Table/useShiftDown.ts renamed to src/common/hooks/useShiftDown.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as React from 'react';
22

3+
// not all events offer the shiftKey property (e.g. checkbox click)
4+
35
export const useShiftDown = () => {
46
const [isShiftDown, setShiftDown] = React.useState(false);
57

src/select/CellSelect.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import * as React from 'react';
22

33
import { Cell } from '@table-library/react-table-library/table/Cell';
44
import { SelectContext } from '@table-library/react-table-library/common/context/Select';
5+
import { useFeatures } from '@table-library/react-table-library/common/context/Feature';
6+
import { applyModifiers } from '@table-library/react-table-library/common/util/modifiers';
7+
import { useShiftDown } from '@table-library/react-table-library/common/hooks/useShiftDown';
58

69
import { SelectTypes, CellSelectProps } from '@table-library/react-table-library/types/select';
710

@@ -11,6 +14,9 @@ export const CellSelect: React.FC<CellSelectProps> = React.memo(
1114
({ item, ...passThrough }: CellSelectProps) => {
1215
const select = React.useContext(SelectContext);
1316

17+
const features = useFeatures();
18+
const isShiftDown = useShiftDown();
19+
1420
if (!select) {
1521
throw new Error(
1622
'No Select Context. No return value from useRowSelect provided to Table component.',
@@ -22,18 +28,20 @@ export const CellSelect: React.FC<CellSelectProps> = React.memo(
2228
? select.state.id === item.id || select.state.ids.includes(item.id)
2329
: select.state.ids.includes(item.id);
2430

25-
const handleChange = () => {
26-
const isSingleSelect = select.options.buttonSelect === SelectTypes.SingleSelect;
31+
const handleChange = React.useCallback(() => {
32+
const isSingleSelectType = select.options.buttonSelect === SelectTypes.SingleSelect;
2733

28-
if (isSingleSelect) {
34+
if (isShiftDown) {
35+
select.fns.onToggleByIdShift(item.id, select.options, applyModifiers(features));
36+
} else if (isSingleSelectType) {
2937
select.fns.onToggleByIdExclusively(item.id);
30-
} else {
38+
} /* isMtuliSelectType */ else {
3139
select.fns.onToggleByIdRecursively(item.id, {
3240
isCarryForward: select.options.isCarryForward,
3341
isPartialToAll: select.options.isPartialToAll,
3442
});
3543
}
36-
};
44+
}, [isShiftDown, features, item.id, select]);
3745

3846
return (
3947
<Cell stiff {...passThrough}>

src/select/useRowSelect.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ const getRowProps = (props: RowProps, features: Features): FeatureProps => {
5858

5959
const onClick = (node: TableNode, event: React.SyntheticEvent | React.KeyboardEvent) => {
6060
if (!isRowClick(event)) return;
61-
6261
if (select.options.clickType !== SelectClickTypes.RowClick) return;
6362

6463
const isMultiSelectType = select.options.rowSelect === SelectTypes.MultiSelect;
@@ -67,12 +66,10 @@ const getRowProps = (props: RowProps, features: Features): FeatureProps => {
6766
const isCommandSelectType = !!(event as any).metaKey;
6867
const isShiftSelectType = !!(event as any).shiftKey;
6968

70-
const modifier = applyModifiers(features);
71-
7269
if (isCommandSelectType) {
7370
select.fns.onToggleById(node.id);
7471
} else if (isShiftSelectType) {
75-
select.fns.onToggleByIdShift(node.id, select.options, modifier);
72+
select.fns.onToggleByIdShift(node.id, select.options, applyModifiers(features));
7673
} else if (isMultiSelectType) {
7774
select.fns.onToggleById(node.id);
7875
} /* isSingleSelectType */ else {

src/table/Table/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { TreeContext } from '@table-library/react-table-library/common/context/T
1212
import { PaginationContext } from '@table-library/react-table-library/common/context/Pagination';
1313

1414
import { applyModifiers } from '@table-library/react-table-library/common/util/modifiers';
15+
import { useShiftDown } from '@table-library/react-table-library/common/hooks/useShiftDown';
1516

1617
import { Nullish } from '@table-library/react-table-library/types/common';
1718
import { TableProps } from '@table-library/react-table-library/types/table';
@@ -21,7 +22,6 @@ import {
2122
TableElementRef,
2223
} from '@table-library/react-table-library/types/layout';
2324

24-
import { useShiftDown } from './useShiftDown';
2525
import { useOnInit } from './useOnInit';
2626

2727
import styles from './styles';
@@ -65,6 +65,7 @@ const Table: React.FC<TableProps> = React.forwardRef(
6565
const tableElementRef = useTableElementRef(ref);
6666
const tableMemoryRef = useTableMemoryRef();
6767

68+
// if changed, adjust useFeatures hook
6869
const modifiedNodes = applyModifiers({
6970
sort,
7071
pagination,

0 commit comments

Comments
 (0)