-
-
Notifications
You must be signed in to change notification settings - Fork 30
Add table range selection feature #1837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
libs/ui/src/lib/data-table/grid/__tests__/grid-row-utils.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import { | ||
| ColumnDef, | ||
| getCoreRowModel, | ||
| getExpandedRowModel, | ||
| getGroupedRowModel, | ||
| GroupingState, | ||
| Row, | ||
| RowSelectionState, | ||
| Table, | ||
| useReactTable, | ||
| } from '@tanstack/react-table'; | ||
| import { act, renderHook } from '@testing-library/react'; | ||
| import { useState } from 'react'; | ||
| import { describe, expect, test } from 'vitest'; | ||
| import { selectRowRange } from '../grid-row-utils'; | ||
|
|
||
| interface TestRow { | ||
| _key: string; | ||
| Name: string; | ||
| Dept: string; | ||
| } | ||
|
|
||
| const data: TestRow[] = [ | ||
| { _key: '1', Name: 'A', Dept: 'X' }, | ||
| { _key: '2', Name: 'B', Dept: 'X' }, | ||
| { _key: '3', Name: 'C', Dept: 'Y' }, | ||
| { _key: '4', Name: 'D', Dept: 'Y' }, | ||
| { _key: '5', Name: 'E', Dept: 'Y' }, | ||
| ]; | ||
|
|
||
| const columns: ColumnDef<TestRow>[] = [ | ||
| { id: 'Name', accessorKey: 'Name' }, | ||
| { id: 'Dept', accessorKey: 'Dept' }, | ||
| ]; | ||
|
|
||
| function makeTable(options: { enableRowSelection?: boolean | ((row: Row<TestRow>) => boolean); grouping?: GroupingState } = {}) { | ||
| const { enableRowSelection = true, grouping } = options; | ||
| const { result } = renderHook(() => { | ||
| const [rowSelection, setRowSelection] = useState<RowSelectionState>({}); | ||
| return useReactTable<TestRow>({ | ||
| data, | ||
| columns, | ||
| state: { rowSelection, grouping: grouping ?? [], expanded: grouping ? true : {} }, | ||
| getRowId: (row) => row._key, | ||
| enableRowSelection, | ||
| onRowSelectionChange: setRowSelection, | ||
| getCoreRowModel: getCoreRowModel(), | ||
| ...(grouping ? { getGroupedRowModel: getGroupedRowModel(), getExpandedRowModel: getExpandedRowModel() } : {}), | ||
| }); | ||
| }); | ||
| return result; | ||
| } | ||
|
|
||
| /** Toggle a single row's selection (sets the anchor's starting state for a range test). */ | ||
| function toggle(table: Table<TestRow>, rowId: string, selected: boolean) { | ||
| act(() => table.getRow(rowId, true).toggleSelected(selected)); | ||
| } | ||
|
|
||
| function selectedKeys(table: Table<TestRow>) { | ||
| return Object.keys(table.getState().rowSelection) | ||
| .filter((key) => table.getState().rowSelection[key]) | ||
| .sort(); | ||
| } | ||
|
|
||
| describe('selectRowRange', () => { | ||
| test('fills a forward range (anchor above target) with the anchor selected value', () => { | ||
| const result = makeTable(); | ||
| toggle(result.current, '2', true); | ||
|
|
||
| let applied = false; | ||
| act(() => { | ||
| applied = selectRowRange(result.current, '2', '4'); | ||
| }); | ||
|
|
||
| expect(applied).toBe(true); | ||
| expect(selectedKeys(result.current)).toEqual(['2', '3', '4']); | ||
| }); | ||
|
|
||
| test('fills a backward range (target above anchor) inclusively', () => { | ||
| const result = makeTable(); | ||
| toggle(result.current, '4', true); | ||
|
|
||
| act(() => { | ||
| selectRowRange(result.current, '4', '2'); | ||
| }); | ||
|
|
||
| expect(selectedKeys(result.current)).toEqual(['2', '3', '4']); | ||
| }); | ||
|
|
||
| test('clears the range when the anchor is unselected', () => { | ||
| const result = makeTable(); | ||
| // Pre-select 2,3,4 then unselect the anchor (2) so its value is "unchecked". | ||
| toggle(result.current, '2', true); | ||
| toggle(result.current, '3', true); | ||
| toggle(result.current, '4', true); | ||
| toggle(result.current, '2', false); | ||
|
|
||
| act(() => { | ||
| selectRowRange(result.current, '2', '4'); | ||
| }); | ||
|
|
||
| expect(selectedKeys(result.current)).toEqual([]); | ||
| }); | ||
|
|
||
| test('skips non-selectable rows in the range', () => { | ||
| const result = makeTable({ enableRowSelection: (row) => row.id !== '3' }); | ||
| toggle(result.current, '2', true); | ||
|
|
||
| act(() => { | ||
| selectRowRange(result.current, '2', '4'); | ||
| }); | ||
|
|
||
| expect(result.current.getRow('3', true).getIsSelected()).toBe(false); | ||
| expect(selectedKeys(result.current)).toEqual(['2', '4']); | ||
| }); | ||
|
|
||
| test('skips group header rows in the range', () => { | ||
| const result = makeTable({ grouping: ['Dept'] }); | ||
| // Rendered order with groups expanded: [group X, 1, 2, group Y, 3, 4, 5]. A range from leaf 2 to | ||
| // leaf 3 spans the "Y" group header, which must not be added to the selection. | ||
| toggle(result.current, '2', true); | ||
|
|
||
| act(() => { | ||
| selectRowRange(result.current, '2', '3'); | ||
| }); | ||
|
|
||
| expect(selectedKeys(result.current)).toEqual(['2', '3']); | ||
| // No group-row id (e.g. "Dept:Y") leaked into the selection. | ||
| expect(selectedKeys(result.current).every((key) => !key.includes(':'))).toBe(true); | ||
| }); | ||
|
|
||
| test('returns false and leaves selection untouched when the anchor is not in the row model', () => { | ||
| const result = makeTable(); | ||
| toggle(result.current, '2', true); | ||
|
|
||
| let applied = true; | ||
| act(() => { | ||
| applied = selectRowRange(result.current, 'missing-anchor', '4'); | ||
| }); | ||
|
|
||
| expect(applied).toBe(false); | ||
| expect(selectedKeys(result.current)).toEqual(['2']); | ||
| }); | ||
|
|
||
| test('returns false when the target is not in the row model', () => { | ||
| const result = makeTable(); | ||
| toggle(result.current, '2', true); | ||
|
|
||
| let applied = true; | ||
| act(() => { | ||
| applied = selectRowRange(result.current, '2', 'missing-target'); | ||
| }); | ||
|
|
||
| expect(applied).toBe(false); | ||
| expect(selectedKeys(result.current)).toEqual(['2']); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.