Skip to content

Commit 0f37c1b

Browse files
Enable/Disable selection onClick the row (#71)
Once "enableRowSelection" option is enabled, a toggle can be used in order to allow the selection clicking anywhere on the row.
1 parent 1c420fc commit 0f37c1b

5 files changed

Lines changed: 43 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- property `fullRowSelectable` in the `reactTableOptions` to manually disable the selection once `enableRowSelection` is enabled and the row is anywhere clicked
13+
1014
## [5.5.0] - 2024-09-25
1115

1216
### Added

src/lib/ReactDataTable/ReactDataTable.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const ReactDataTable = <TData, TFilter extends FilterModel = Record<string, neve
4545

4646
const { pagination } = table.getState();
4747
const {
48-
options: { manualPagination, enableRowSelection, enableExpanding },
48+
options: { manualPagination, enableRowSelection, enableExpanding, fullRowSelectable },
4949
resetPageIndex,
5050
} = table;
5151

@@ -75,6 +75,7 @@ const ReactDataTable = <TData, TFilter extends FilterModel = Record<string, neve
7575
enableRowSelection={enableRowSelection as boolean | ((row: Row<TData>) => boolean)}
7676
enableExpanding={enableExpanding as boolean | ((row: Row<TData>) => boolean)}
7777
rowStyle={rowStyle && rowStyle(row.original)}
78+
fullRowSelectable={fullRowSelectable}
7879
/>
7980
))}
8081
</SortableContext>
@@ -87,6 +88,7 @@ const ReactDataTable = <TData, TFilter extends FilterModel = Record<string, neve
8788
enableRowSelection={enableRowSelection as boolean | ((row: Row<TData>) => boolean)}
8889
enableExpanding={enableExpanding as boolean | ((row: Row<TData>) => boolean)}
8990
rowStyle={rowStyle && rowStyle(row.original)}
91+
fullRowSelectable={fullRowSelectable}
9092
/>
9193
))}
9294
</>

src/lib/ReactDataTable/TableRows.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import { CSS } from "@dnd-kit/utilities";
66
interface TableRowProps<TData> {
77
row: Row<TData>;
88
enableRowSelection?: boolean | ((row: Row<TData>) => boolean);
9+
fullRowSelectable?: boolean;
910
enableExpanding?: boolean | ((row: Row<TData>) => boolean);
1011
rowStyle?: CSSProperties;
1112
setNodeRef?: (node: HTMLElement | null) => void;
1213
}
1314

1415
const InternalTableRow = <TData,>(props: TableRowProps<TData>) => {
15-
const { row, rowStyle, setNodeRef, enableRowSelection = false } = props;
16-
const isRowSelectionEnabled = typeof enableRowSelection === "function" ? enableRowSelection(row) : enableRowSelection;
16+
const { row, rowStyle, setNodeRef, enableRowSelection = false, fullRowSelectable = true } = props;
17+
const isRowSelectionEnabled =
18+
(typeof enableRowSelection === "function" ? enableRowSelection(row) : enableRowSelection) && fullRowSelectable;
1719

1820
return (
1921
<tr
@@ -37,7 +39,7 @@ const InternalTableRow = <TData,>(props: TableRowProps<TData>) => {
3739
};
3840

3941
const DraggableRow = <TData,>(props: TableRowProps<TData>) => {
40-
const { row, rowStyle, enableRowSelection, enableExpanding } = props;
42+
const { row, rowStyle, enableRowSelection, fullRowSelectable, enableExpanding } = props;
4143
const { transform, transition, setNodeRef, isDragging } = useSortable({ id: row.id });
4244
const draggableStyle: CSSProperties = {
4345
transform: CSS.Transform.toString(transform),
@@ -51,6 +53,7 @@ const DraggableRow = <TData,>(props: TableRowProps<TData>) => {
5153
row={row}
5254
enableRowSelection={enableRowSelection}
5355
enableExpanding={enableExpanding}
56+
fullRowSelectable={fullRowSelectable}
5457
setNodeRef={setNodeRef}
5558
rowStyle={rowStyle ? { ...rowStyle, ...draggableStyle } : draggableStyle}
5659
/>

src/react-table.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,8 @@ declare module "@tanstack/table-core" {
6363
*/
6464
isHidden?: boolean;
6565
}
66+
67+
interface RowSelectionOptions<TData extends RowData> {
68+
fullRowSelectable?: boolean;
69+
}
6670
}

test/DataTable/DataTable.test.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable max-lines */
2-
import { render } from "@testing-library/react";
2+
import { render, fireEvent } from "@testing-library/react";
33
import { renderHook } from "@testing-library/react-hooks/server";
44
import "@testing-library/jest-dom";
55
import React from "react";
@@ -218,4 +218,29 @@ describe("DataTable", () => {
218218

219219
expect(container).toMatchSnapshot();
220220
});
221+
222+
test("fullRowSelectable is working: clicking on the rows it does not select them", () => {
223+
const {
224+
result: {
225+
current: { table },
226+
},
227+
} = renderHook(() =>
228+
useReactDataTable({
229+
data: dataDynamic,
230+
isLoading: false,
231+
columns,
232+
reactTableOptions: {
233+
fullRowSelectable: false,
234+
enableRowSelection: true,
235+
enableMultiRowSelection: true,
236+
getRowId: (row) => row.id,
237+
},
238+
}),
239+
);
240+
render(<ReactDataTable table={table} showPaging totalRecords={dataDynamic?.length} isFetching={false} isLoading={false} />);
241+
242+
const rows = document.querySelectorAll("tr");
243+
rows.forEach((row) => fireEvent.click(row));
244+
expect(table.getSelectedRowModel().rows.length).toBe(0);
245+
});
221246
});

0 commit comments

Comments
 (0)