@dashforge/tw 0.5.0-beta → 0.7.0-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +526 -0
- package/THEME-AUDIT.md +175 -0
- package/dist/index.esm.js +2504 -51
- package/dist/src/components/Accordion/accordion.variants.d.ts.map +1 -1
- package/dist/src/components/Box/box.variants.d.ts.map +1 -1
- package/dist/src/components/DataGrid/DataGrid.d.ts +29 -0
- package/dist/src/components/DataGrid/DataGrid.d.ts.map +1 -0
- package/dist/src/components/DataGrid/dataGrid.types.d.ts +171 -0
- package/dist/src/components/DataGrid/dataGrid.types.d.ts.map +1 -0
- package/dist/src/components/DataGrid/dataGrid.variants.d.ts +216 -0
- package/dist/src/components/DataGrid/dataGrid.variants.d.ts.map +1 -0
- package/dist/src/components/Dialog/dialog.variants.d.ts.map +1 -1
- package/dist/src/components/Divider/divider.variants.d.ts.map +1 -1
- package/dist/src/components/Pagination/Pagination.d.ts.map +1 -1
- package/dist/src/components/Pagination/pagination.variants.d.ts.map +1 -1
- package/dist/src/components/Popover/popover.variants.d.ts.map +1 -1
- package/dist/src/components/Skeleton/skeleton.variants.d.ts.map +1 -1
- package/dist/src/components/Table/Table.d.ts +24 -0
- package/dist/src/components/Table/Table.d.ts.map +1 -0
- package/dist/src/components/Table/cells/RenderButton.d.ts +13 -0
- package/dist/src/components/Table/cells/RenderButton.d.ts.map +1 -0
- package/dist/src/components/Table/cells/RenderChip.d.ts +80 -0
- package/dist/src/components/Table/cells/RenderChip.d.ts.map +1 -0
- package/dist/src/components/Table/cells/RenderText.d.ts +21 -0
- package/dist/src/components/Table/cells/RenderText.d.ts.map +1 -0
- package/dist/src/components/Table/cells/RenderTwoLine.d.ts +20 -0
- package/dist/src/components/Table/cells/RenderTwoLine.d.ts.map +1 -0
- package/dist/src/components/Table/cells/RowActionsMenu.d.ts +36 -0
- package/dist/src/components/Table/cells/RowActionsMenu.d.ts.map +1 -0
- package/dist/src/components/Table/cells/index.d.ts +11 -0
- package/dist/src/components/Table/cells/index.d.ts.map +1 -0
- package/dist/src/components/Table/table.types.d.ts +312 -0
- package/dist/src/components/Table/table.types.d.ts.map +1 -0
- package/dist/src/components/Table/table.variants.d.ts +285 -0
- package/dist/src/components/Table/table.variants.d.ts.map +1 -0
- package/dist/src/components/Tabs/tabs.variants.d.ts.map +1 -1
- package/dist/src/components/Tooltip/tooltip.variants.d.ts.map +1 -1
- package/dist/src/components/Typography/typography.variants.d.ts.map +1 -1
- package/dist/src/components/_shared/data/getNestedValue.d.ts +20 -0
- package/dist/src/components/_shared/data/getNestedValue.d.ts.map +1 -0
- package/dist/src/components/_shared/data/useColumnAutoDetect.d.ts +45 -0
- package/dist/src/components/_shared/data/useColumnAutoDetect.d.ts.map +1 -0
- package/dist/src/components/_shared/data/useControllableState.d.ts +23 -0
- package/dist/src/components/_shared/data/useControllableState.d.ts.map +1 -0
- package/dist/src/components/_shared/data/useDebouncedValue.d.ts +10 -0
- package/dist/src/components/_shared/data/useDebouncedValue.d.ts.map +1 -0
- package/dist/src/components/_shared/data/useTableFilter.d.ts +13 -0
- package/dist/src/components/_shared/data/useTableFilter.d.ts.map +1 -0
- package/dist/src/components/_shared/data/useTableSearch.d.ts +23 -0
- package/dist/src/components/_shared/data/useTableSearch.d.ts.map +1 -0
- package/dist/src/components/_shared/data/useTableSelection.d.ts +25 -0
- package/dist/src/components/_shared/data/useTableSelection.d.ts.map +1 -0
- package/dist/src/components/_shared/data/useTableSort.d.ts +20 -0
- package/dist/src/components/_shared/data/useTableSort.d.ts.map +1 -0
- package/dist/src/components/_shared/data/useVirtualizer.d.ts +107 -0
- package/dist/src/components/_shared/data/useVirtualizer.d.ts.map +1 -0
- package/dist/src/index.d.ts +10 -1
- package/dist/src/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/components/Accordion/accordion.variants.ts +5 -3
- package/src/components/Box/Box.test.tsx +32 -11
- package/src/components/Box/box.variants.ts +23 -4
- package/src/components/DataGrid/DataGrid.test.tsx +496 -0
- package/src/components/DataGrid/DataGrid.tsx +831 -0
- package/src/components/DataGrid/dataGrid.types.ts +189 -0
- package/src/components/DataGrid/dataGrid.variants.ts +144 -0
- package/src/components/Dialog/dialog.variants.ts +10 -6
- package/src/components/Divider/Divider.test.tsx +3 -1
- package/src/components/Divider/Divider.tsx +1 -1
- package/src/components/Divider/divider.variants.ts +2 -1
- package/src/components/Pagination/Pagination.tsx +3 -1
- package/src/components/Pagination/pagination.variants.ts +19 -13
- package/src/components/Popover/popover.variants.ts +10 -4
- package/src/components/Skeleton/skeleton.variants.ts +2 -1
- package/src/components/Table/Table.test.tsx +603 -0
- package/src/components/Table/Table.tsx +816 -0
- package/src/components/Table/cells/RenderButton.tsx +23 -0
- package/src/components/Table/cells/RenderChip.tsx +103 -0
- package/src/components/Table/cells/RenderText.tsx +37 -0
- package/src/components/Table/cells/RenderTwoLine.tsx +34 -0
- package/src/components/Table/cells/RowActionsMenu.tsx +147 -0
- package/src/components/Table/cells/index.ts +14 -0
- package/src/components/Table/table.types.ts +346 -0
- package/src/components/Table/table.variants.ts +207 -0
- package/src/components/Tabs/tabs.variants.ts +19 -8
- package/src/components/Tooltip/tooltip.variants.ts +12 -2
- package/src/components/Typography/Typography.test.tsx +3 -1
- package/src/components/Typography/typography.variants.ts +6 -2
- package/src/components/_shared/data/getNestedValue.test.ts +50 -0
- package/src/components/_shared/data/getNestedValue.ts +32 -0
- package/src/components/_shared/data/useColumnAutoDetect.test.ts +123 -0
- package/src/components/_shared/data/useColumnAutoDetect.ts +100 -0
- package/src/components/_shared/data/useControllableState.ts +48 -0
- package/src/components/_shared/data/useDebouncedValue.ts +24 -0
- package/src/components/_shared/data/useTableFilter.ts +37 -0
- package/src/components/_shared/data/useTableSearch.test.ts +115 -0
- package/src/components/_shared/data/useTableSearch.ts +66 -0
- package/src/components/_shared/data/useTableSelection.test.ts +100 -0
- package/src/components/_shared/data/useTableSelection.ts +100 -0
- package/src/components/_shared/data/useTableSort.test.ts +150 -0
- package/src/components/_shared/data/useTableSort.ts +141 -0
- package/src/components/_shared/data/useVirtualizer.test.ts +158 -0
- package/src/components/_shared/data/useVirtualizer.ts +199 -0
- package/src/components/_shared/themeIdentity.test.ts +113 -0
- package/src/index.ts +59 -1
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { describe, it, expect } from 'vitest';
|
|
3
|
+
import { renderHook, act } from '@testing-library/react';
|
|
4
|
+
import { useState } from 'react';
|
|
5
|
+
import { useTableSelection } from './useTableSelection.js';
|
|
6
|
+
|
|
7
|
+
function setup(mode: 'none' | 'single' | 'multiple', initial: string[] = []) {
|
|
8
|
+
return renderHook(() => {
|
|
9
|
+
const [ids, setIds] = useState<string[]>(initial);
|
|
10
|
+
const setIdsWithFn = (next: string[] | ((prev: string[]) => string[])) =>
|
|
11
|
+
setIds((prev) => (typeof next === 'function' ? next(prev) : next));
|
|
12
|
+
const selection = useTableSelection(mode, ids, setIdsWithFn);
|
|
13
|
+
return { ids, selection };
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe('useTableSelection — single mode', () => {
|
|
18
|
+
it('toggleRow selects when not selected', () => {
|
|
19
|
+
const { result } = setup('single');
|
|
20
|
+
act(() => result.current.selection.toggleRow('a'));
|
|
21
|
+
expect(result.current.ids).toEqual(['a']);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('toggleRow on a different row REPLACES', () => {
|
|
25
|
+
const { result } = setup('single', ['a']);
|
|
26
|
+
act(() => result.current.selection.toggleRow('b'));
|
|
27
|
+
expect(result.current.ids).toEqual(['b']);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('toggleRow on same row DESELECTS', () => {
|
|
31
|
+
const { result } = setup('single', ['a']);
|
|
32
|
+
act(() => result.current.selection.toggleRow('a'));
|
|
33
|
+
expect(result.current.ids).toEqual([]);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('useTableSelection — multiple mode', () => {
|
|
38
|
+
it('toggleRow ADDS to selection', () => {
|
|
39
|
+
const { result } = setup('multiple');
|
|
40
|
+
act(() => result.current.selection.toggleRow('a'));
|
|
41
|
+
act(() => result.current.selection.toggleRow('b'));
|
|
42
|
+
expect(result.current.ids.sort()).toEqual(['a', 'b']);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('toggleRow on selected row REMOVES', () => {
|
|
46
|
+
const { result } = setup('multiple', ['a', 'b']);
|
|
47
|
+
act(() => result.current.selection.toggleRow('a'));
|
|
48
|
+
expect(result.current.ids).toEqual(['b']);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('toggleAll selects all when none selected', () => {
|
|
52
|
+
const { result } = setup('multiple');
|
|
53
|
+
act(() => result.current.selection.toggleAll(['a', 'b', 'c']));
|
|
54
|
+
expect(result.current.ids).toEqual(['a', 'b', 'c']);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('toggleAll deselects all when all already selected', () => {
|
|
58
|
+
const { result } = setup('multiple', ['a', 'b', 'c']);
|
|
59
|
+
act(() => result.current.selection.toggleAll(['a', 'b', 'c']));
|
|
60
|
+
expect(result.current.ids).toEqual([]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('toggleAll selects all when SOME already selected', () => {
|
|
64
|
+
const { result } = setup('multiple', ['a']);
|
|
65
|
+
act(() => result.current.selection.toggleAll(['a', 'b', 'c']));
|
|
66
|
+
expect(result.current.ids).toEqual(['a', 'b', 'c']);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('isAllSelected reflects state', () => {
|
|
70
|
+
const { result } = setup('multiple', ['a', 'b']);
|
|
71
|
+
expect(result.current.selection.isAllSelected(['a', 'b'])).toBe(true);
|
|
72
|
+
expect(result.current.selection.isAllSelected(['a', 'b', 'c'])).toBe(false);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('isIndeterminate is true when some but not all selected', () => {
|
|
76
|
+
const { result } = setup('multiple', ['a']);
|
|
77
|
+
expect(result.current.selection.isIndeterminate(['a', 'b', 'c'])).toBe(true);
|
|
78
|
+
expect(result.current.selection.isIndeterminate(['a'])).toBe(false); // all selected → false
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('clearSelection resets to empty', () => {
|
|
82
|
+
const { result } = setup('multiple', ['a', 'b', 'c']);
|
|
83
|
+
act(() => result.current.selection.clearSelection());
|
|
84
|
+
expect(result.current.ids).toEqual([]);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe('useTableSelection — none mode', () => {
|
|
89
|
+
it('toggleRow is no-op', () => {
|
|
90
|
+
const { result } = setup('none');
|
|
91
|
+
act(() => result.current.selection.toggleRow('a'));
|
|
92
|
+
expect(result.current.ids).toEqual([]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('toggleAll is no-op', () => {
|
|
96
|
+
const { result } = setup('none');
|
|
97
|
+
act(() => result.current.selection.toggleAll(['a', 'b']));
|
|
98
|
+
expect(result.current.ids).toEqual([]);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { useCallback, useMemo } from 'react';
|
|
2
|
+
import type { TableRowSelectionMode } from '../../Table/table.types.js';
|
|
3
|
+
|
|
4
|
+
export interface UseTableSelectionResult {
|
|
5
|
+
selectedRowIds: string[];
|
|
6
|
+
isSelected: (rowId: string) => boolean;
|
|
7
|
+
toggleRow: (rowId: string) => void;
|
|
8
|
+
toggleAll: (allRowIds: string[]) => void;
|
|
9
|
+
clearSelection: () => void;
|
|
10
|
+
/** True when EVERY id in `allRowIds` is selected. */
|
|
11
|
+
isAllSelected: (allRowIds: string[]) => boolean;
|
|
12
|
+
/** True when at least one (but not all) of `allRowIds` is selected. */
|
|
13
|
+
isIndeterminate: (allRowIds: string[]) => boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Selection logic helper. Given the controllable selection state +
|
|
18
|
+
* the setter (from `useControllableState`), exposes ergonomic
|
|
19
|
+
* helpers the Table calls from its render path.
|
|
20
|
+
*
|
|
21
|
+
* - `single` mode: at most one row selected. Toggling another
|
|
22
|
+
* replaces the previous.
|
|
23
|
+
* - `multiple` mode: any subset selectable; toggling adds/removes.
|
|
24
|
+
* - `none` mode: every toggle is a no-op (the Table shouldn't
|
|
25
|
+
* render the checkbox cells anyway, but this is defensive).
|
|
26
|
+
*/
|
|
27
|
+
export function useTableSelection(
|
|
28
|
+
mode: TableRowSelectionMode,
|
|
29
|
+
selectedRowIds: string[],
|
|
30
|
+
setSelectedRowIds: (next: string[] | ((prev: string[]) => string[])) => void,
|
|
31
|
+
): UseTableSelectionResult {
|
|
32
|
+
const selectedSet = useMemo(
|
|
33
|
+
() => new Set(selectedRowIds),
|
|
34
|
+
[selectedRowIds],
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const isSelected = useCallback(
|
|
38
|
+
(rowId: string) => selectedSet.has(rowId),
|
|
39
|
+
[selectedSet],
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const toggleRow = useCallback(
|
|
43
|
+
(rowId: string) => {
|
|
44
|
+
if (mode === 'none') return;
|
|
45
|
+
if (mode === 'single') {
|
|
46
|
+
setSelectedRowIds(selectedSet.has(rowId) ? [] : [rowId]);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
// multiple
|
|
50
|
+
setSelectedRowIds((prev) => {
|
|
51
|
+
const next = new Set(prev);
|
|
52
|
+
if (next.has(rowId)) next.delete(rowId);
|
|
53
|
+
else next.add(rowId);
|
|
54
|
+
return [...next];
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
[mode, selectedSet, setSelectedRowIds],
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const toggleAll = useCallback(
|
|
61
|
+
(allRowIds: string[]) => {
|
|
62
|
+
if (mode !== 'multiple') return;
|
|
63
|
+
const allSelected = allRowIds.every((id) => selectedSet.has(id));
|
|
64
|
+
setSelectedRowIds(allSelected ? [] : [...allRowIds]);
|
|
65
|
+
},
|
|
66
|
+
[mode, selectedSet, setSelectedRowIds],
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const clearSelection = useCallback(() => {
|
|
70
|
+
setSelectedRowIds([]);
|
|
71
|
+
}, [setSelectedRowIds]);
|
|
72
|
+
|
|
73
|
+
const isAllSelected = useCallback(
|
|
74
|
+
(allRowIds: string[]) => {
|
|
75
|
+
if (allRowIds.length === 0) return false;
|
|
76
|
+
return allRowIds.every((id) => selectedSet.has(id));
|
|
77
|
+
},
|
|
78
|
+
[selectedSet],
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const isIndeterminate = useCallback(
|
|
82
|
+
(allRowIds: string[]) => {
|
|
83
|
+
if (allRowIds.length === 0) return false;
|
|
84
|
+
const some = allRowIds.some((id) => selectedSet.has(id));
|
|
85
|
+
const all = allRowIds.every((id) => selectedSet.has(id));
|
|
86
|
+
return some && !all;
|
|
87
|
+
},
|
|
88
|
+
[selectedSet],
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
selectedRowIds,
|
|
93
|
+
isSelected,
|
|
94
|
+
toggleRow,
|
|
95
|
+
toggleAll,
|
|
96
|
+
clearSelection,
|
|
97
|
+
isAllSelected,
|
|
98
|
+
isIndeterminate,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { describe, it, expect } from 'vitest';
|
|
3
|
+
import { renderHook } from '@testing-library/react';
|
|
4
|
+
import { useTableSort, cycleSortFor } from './useTableSort.js';
|
|
5
|
+
import type { TableColumn, TableSortModel } from '../../Table/table.types.js';
|
|
6
|
+
|
|
7
|
+
interface Row {
|
|
8
|
+
id: string;
|
|
9
|
+
name: string;
|
|
10
|
+
age: number | null;
|
|
11
|
+
joinedAt: Date | null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const cols: TableColumn<Row>[] = [
|
|
15
|
+
{ field: 'name', header: 'Name', sortable: true },
|
|
16
|
+
{ field: 'age', header: 'Age', sortable: true },
|
|
17
|
+
{ field: 'joinedAt' as never, header: 'Joined', sortable: true },
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
const rows: Row[] = [
|
|
21
|
+
{ id: '1', name: 'Charlie', age: 30, joinedAt: new Date('2024-01-15') },
|
|
22
|
+
{ id: '2', name: 'Alice', age: 25, joinedAt: new Date('2024-03-01') },
|
|
23
|
+
{ id: '3', name: 'Bob', age: null, joinedAt: null },
|
|
24
|
+
{ id: '4', name: 'alice', age: 40, joinedAt: new Date('2024-02-10') },
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
describe('useTableSort', () => {
|
|
28
|
+
it('returns rows untouched when sortModel is empty', () => {
|
|
29
|
+
const { result } = renderHook(() => useTableSort(rows, cols, []));
|
|
30
|
+
expect(result.current.map((r) => r.id)).toEqual(['1', '2', '3', '4']);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('sorts strings asc (case-insensitive)', () => {
|
|
34
|
+
const sortModel: TableSortModel = [{ field: 'name', direction: 'asc' }];
|
|
35
|
+
const { result } = renderHook(() => useTableSort(rows, cols, sortModel));
|
|
36
|
+
// alice < Alice < Bob < Charlie (case-insensitive)
|
|
37
|
+
expect(result.current.map((r) => r.name.toLowerCase())).toEqual([
|
|
38
|
+
'alice', 'alice', 'bob', 'charlie',
|
|
39
|
+
]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('sorts numbers desc', () => {
|
|
43
|
+
const sortModel: TableSortModel = [{ field: 'age', direction: 'desc' }];
|
|
44
|
+
const { result } = renderHook(() => useTableSort(rows, cols, sortModel));
|
|
45
|
+
// 40, 30, 25, null-last
|
|
46
|
+
expect(result.current.map((r) => r.age)).toEqual([40, 30, 25, null]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('places null values LAST regardless of direction (asc)', () => {
|
|
50
|
+
const sortModel: TableSortModel = [{ field: 'age', direction: 'asc' }];
|
|
51
|
+
const { result } = renderHook(() => useTableSort(rows, cols, sortModel));
|
|
52
|
+
// 25, 30, 40, null-last
|
|
53
|
+
expect(result.current.map((r) => r.age)).toEqual([25, 30, 40, null]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('sorts Date asc (epoch ms order)', () => {
|
|
57
|
+
const sortModel: TableSortModel = [{ field: 'joinedAt', direction: 'asc' }];
|
|
58
|
+
const { result } = renderHook(() => useTableSort(rows, cols, sortModel));
|
|
59
|
+
// 2024-01-15 → 2024-02-10 → 2024-03-01 → null-last
|
|
60
|
+
expect(result.current.map((r) => r.id)).toEqual(['1', '4', '2', '3']);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('supports custom comparator', () => {
|
|
64
|
+
const colsCustom: TableColumn<Row>[] = [
|
|
65
|
+
// Sort by length of name ascending
|
|
66
|
+
{ field: 'name', header: 'Name', sortable: (a, b) => a.name.length - b.name.length },
|
|
67
|
+
];
|
|
68
|
+
const sortModel: TableSortModel = [{ field: 'name', direction: 'asc' }];
|
|
69
|
+
const { result } = renderHook(() => useTableSort(rows, colsCustom, sortModel));
|
|
70
|
+
expect(result.current.map((r) => r.name)).toEqual(['Bob', 'Alice', 'alice', 'Charlie']);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('falls back to next sort item on tie (multi-column sort)', () => {
|
|
74
|
+
const sortModel: TableSortModel = [
|
|
75
|
+
{ field: 'name', direction: 'asc' }, // 'alice', 'Alice' tie
|
|
76
|
+
{ field: 'age', direction: 'asc' }, // then 25 before 40
|
|
77
|
+
];
|
|
78
|
+
const { result } = renderHook(() => useTableSort(rows, cols, sortModel));
|
|
79
|
+
// 'alice' and 'Alice' tie on name → 25 before 40 → so id=2 before id=4
|
|
80
|
+
expect(result.current.slice(0, 2).map((r) => r.id)).toEqual(['2', '4']);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('ignores sort items for non-sortable columns', () => {
|
|
84
|
+
const colsNotSortable: TableColumn<Row>[] = cols.map((c) => ({ ...c, sortable: false }));
|
|
85
|
+
const sortModel: TableSortModel = [{ field: 'name', direction: 'asc' }];
|
|
86
|
+
const { result } = renderHook(() => useTableSort(rows, colsNotSortable, sortModel));
|
|
87
|
+
// Original order preserved
|
|
88
|
+
expect(result.current.map((r) => r.id)).toEqual(['1', '2', '3', '4']);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('cycleSortFor', () => {
|
|
93
|
+
it('none → asc on first click', () => {
|
|
94
|
+
expect(cycleSortFor([], 'name', false)).toEqual([{ field: 'name', direction: 'asc' }]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('asc → desc on second click', () => {
|
|
98
|
+
expect(cycleSortFor([{ field: 'name', direction: 'asc' }], 'name', false)).toEqual([
|
|
99
|
+
{ field: 'name', direction: 'desc' },
|
|
100
|
+
]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('desc → none (empty) on third click', () => {
|
|
104
|
+
expect(cycleSortFor([{ field: 'name', direction: 'desc' }], 'name', false)).toEqual([]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('single-click on different column REPLACES sort', () => {
|
|
108
|
+
expect(
|
|
109
|
+
cycleSortFor([{ field: 'name', direction: 'asc' }], 'age', false),
|
|
110
|
+
).toEqual([{ field: 'age', direction: 'asc' }]);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('shift-click on different column APPENDS (multi-sort)', () => {
|
|
114
|
+
expect(
|
|
115
|
+
cycleSortFor([{ field: 'name', direction: 'asc' }], 'age', true),
|
|
116
|
+
).toEqual([
|
|
117
|
+
{ field: 'name', direction: 'asc' },
|
|
118
|
+
{ field: 'age', direction: 'asc' },
|
|
119
|
+
]);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('shift-click on existing column cycles its direction', () => {
|
|
123
|
+
expect(
|
|
124
|
+
cycleSortFor(
|
|
125
|
+
[
|
|
126
|
+
{ field: 'name', direction: 'asc' },
|
|
127
|
+
{ field: 'age', direction: 'asc' },
|
|
128
|
+
],
|
|
129
|
+
'age',
|
|
130
|
+
true,
|
|
131
|
+
),
|
|
132
|
+
).toEqual([
|
|
133
|
+
{ field: 'name', direction: 'asc' },
|
|
134
|
+
{ field: 'age', direction: 'desc' },
|
|
135
|
+
]);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('shift-click on desc column REMOVES it (preserving rest)', () => {
|
|
139
|
+
expect(
|
|
140
|
+
cycleSortFor(
|
|
141
|
+
[
|
|
142
|
+
{ field: 'name', direction: 'asc' },
|
|
143
|
+
{ field: 'age', direction: 'desc' },
|
|
144
|
+
],
|
|
145
|
+
'age',
|
|
146
|
+
true,
|
|
147
|
+
),
|
|
148
|
+
).toEqual([{ field: 'name', direction: 'asc' }]);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { getNestedValue } from './getNestedValue.js';
|
|
3
|
+
import type {
|
|
4
|
+
TableColumn,
|
|
5
|
+
TableSortModel,
|
|
6
|
+
TableSortDirection,
|
|
7
|
+
} from '../../Table/table.types.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Default comparator. Handles the common primitive types in a
|
|
11
|
+
* sensible way:
|
|
12
|
+
*
|
|
13
|
+
* - `null` / `undefined` always sort LAST (stable across asc/desc)
|
|
14
|
+
* - `number` / `bigint` → numeric comparison
|
|
15
|
+
* - `string` → locale-aware compare via `Intl.Collator`
|
|
16
|
+
* - `Date` / ISO-string-date → numeric compare on epoch ms
|
|
17
|
+
* - `boolean` → `false < true`
|
|
18
|
+
* - mixed types fall back to string compare
|
|
19
|
+
*
|
|
20
|
+
* The locale collator is created once per call (cheap; cached
|
|
21
|
+
* upstream via memoization).
|
|
22
|
+
*/
|
|
23
|
+
function defaultCompare(a: unknown, b: unknown): number {
|
|
24
|
+
// Numbers
|
|
25
|
+
if (typeof a === 'number' && typeof b === 'number') {
|
|
26
|
+
return a - b;
|
|
27
|
+
}
|
|
28
|
+
if (typeof a === 'bigint' && typeof b === 'bigint') {
|
|
29
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Dates
|
|
33
|
+
if (a instanceof Date && b instanceof Date) {
|
|
34
|
+
return a.getTime() - b.getTime();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Booleans
|
|
38
|
+
if (typeof a === 'boolean' && typeof b === 'boolean') {
|
|
39
|
+
return a === b ? 0 : a ? 1 : -1;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Strings (incl. ISO date strings — natural lexical ordering matches chronology)
|
|
43
|
+
if (typeof a === 'string' && typeof b === 'string') {
|
|
44
|
+
return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Fallback: stringify and compare
|
|
48
|
+
return String(a).localeCompare(String(b), undefined, {
|
|
49
|
+
numeric: true,
|
|
50
|
+
sensitivity: 'base',
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Applies the `sortModel` to `rows`. Returns a NEW sorted array
|
|
56
|
+
* (does not mutate input). Stable sort guaranteed by the underlying
|
|
57
|
+
* `Array.prototype.sort` in modern engines.
|
|
58
|
+
*
|
|
59
|
+
* Multi-column sort: items earlier in `sortModel` have higher
|
|
60
|
+
* precedence. Equal values fall through to the next sort item; if
|
|
61
|
+
* all sort items tie, the original order is preserved (stable).
|
|
62
|
+
*/
|
|
63
|
+
export function useTableSort<T extends object>(
|
|
64
|
+
rows: T[],
|
|
65
|
+
cols: TableColumn<T>[],
|
|
66
|
+
sortModel: TableSortModel,
|
|
67
|
+
): T[] {
|
|
68
|
+
return useMemo(() => {
|
|
69
|
+
if (!sortModel || sortModel.length === 0) return rows;
|
|
70
|
+
|
|
71
|
+
const sortConfigs = sortModel
|
|
72
|
+
.map((item) => {
|
|
73
|
+
const col = cols.find((c) => (c.field as string) === item.field);
|
|
74
|
+
if (!col || !col.sortable) return null;
|
|
75
|
+
const customCompare =
|
|
76
|
+
typeof col.sortable === 'function' ? col.sortable : undefined;
|
|
77
|
+
return { item, col, customCompare };
|
|
78
|
+
})
|
|
79
|
+
.filter((x): x is NonNullable<typeof x> => x != null);
|
|
80
|
+
|
|
81
|
+
if (sortConfigs.length === 0) return rows;
|
|
82
|
+
|
|
83
|
+
// Decorate-sort-undecorate for stability across engines.
|
|
84
|
+
//
|
|
85
|
+
// Per sort item: null/undefined values always sort LAST, regardless
|
|
86
|
+
// of direction. This invariant lives outside the comparator so it's
|
|
87
|
+
// not inverted by `-cmp` when direction is `desc`. (Without this
|
|
88
|
+
// separation, `desc` flipped null-handling and placed nulls FIRST,
|
|
89
|
+
// which broke user expectations — fixed in the v1 implementation.)
|
|
90
|
+
return [...rows].sort((a, b) => {
|
|
91
|
+
for (const { item, customCompare } of sortConfigs) {
|
|
92
|
+
const aVal = customCompare ? a : getNestedValue(a, item.field);
|
|
93
|
+
const bVal = customCompare ? b : getNestedValue(b, item.field);
|
|
94
|
+
|
|
95
|
+
// Null-last (direction-independent)
|
|
96
|
+
const aNull = aVal == null;
|
|
97
|
+
const bNull = bVal == null;
|
|
98
|
+
if (aNull && bNull) continue;
|
|
99
|
+
if (aNull) return 1;
|
|
100
|
+
if (bNull) return -1;
|
|
101
|
+
|
|
102
|
+
const cmp = customCompare ? customCompare(a, b) : defaultCompare(aVal, bVal);
|
|
103
|
+
if (cmp !== 0) {
|
|
104
|
+
return item.direction === 'desc' ? -cmp : cmp;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return 0;
|
|
108
|
+
});
|
|
109
|
+
}, [rows, cols, sortModel]);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Three-state sort cycler: `none` → `asc` → `desc` → `none`.
|
|
114
|
+
*
|
|
115
|
+
* Used by the header click handler. Returns the next sort model
|
|
116
|
+
* to apply, given the current model + the clicked field + whether
|
|
117
|
+
* the user is multi-sorting (shift key).
|
|
118
|
+
*/
|
|
119
|
+
export function cycleSortFor(
|
|
120
|
+
current: TableSortModel,
|
|
121
|
+
field: string,
|
|
122
|
+
multi: boolean,
|
|
123
|
+
): TableSortModel {
|
|
124
|
+
const existing = current.find((s) => s.field === field);
|
|
125
|
+
const nextDirection: TableSortDirection | null = !existing
|
|
126
|
+
? 'asc'
|
|
127
|
+
: existing.direction === 'asc'
|
|
128
|
+
? 'desc'
|
|
129
|
+
: null;
|
|
130
|
+
|
|
131
|
+
if (multi) {
|
|
132
|
+
// Multi-sort: keep other entries, update / add / remove this one.
|
|
133
|
+
const rest = current.filter((s) => s.field !== field);
|
|
134
|
+
if (nextDirection == null) return rest;
|
|
135
|
+
return [...rest, { field, direction: nextDirection }];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Single-sort: replace the whole model.
|
|
139
|
+
if (nextDirection == null) return [];
|
|
140
|
+
return [{ field, direction: nextDirection }];
|
|
141
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { describe, it, expect } from 'vitest';
|
|
3
|
+
import { computeWindow } from './useVirtualizer.js';
|
|
4
|
+
|
|
5
|
+
describe('computeWindow', () => {
|
|
6
|
+
it('returns empty window when totalCount is 0', () => {
|
|
7
|
+
const w = computeWindow({
|
|
8
|
+
totalCount: 0,
|
|
9
|
+
rowHeight: 48,
|
|
10
|
+
viewportHeight: 600,
|
|
11
|
+
scrollTop: 0,
|
|
12
|
+
});
|
|
13
|
+
expect(w.endIndex).toBeLessThan(w.startIndex);
|
|
14
|
+
expect(w.paddingTop).toBe(0);
|
|
15
|
+
expect(w.paddingBottom).toBe(0);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('returns first window when scrollTop=0 (with overscan)', () => {
|
|
19
|
+
const w = computeWindow({
|
|
20
|
+
totalCount: 1000,
|
|
21
|
+
rowHeight: 48,
|
|
22
|
+
viewportHeight: 600,
|
|
23
|
+
scrollTop: 0,
|
|
24
|
+
overscan: 5,
|
|
25
|
+
});
|
|
26
|
+
// viewport fits 600 / 48 = 12.5 → 13 rows + 5 overscan top + 5 overscan bottom
|
|
27
|
+
expect(w.startIndex).toBe(0);
|
|
28
|
+
// 13 visible + 5 overscan_bottom = end at 17 ish
|
|
29
|
+
expect(w.endIndex).toBeGreaterThanOrEqual(12);
|
|
30
|
+
expect(w.paddingTop).toBe(0);
|
|
31
|
+
expect(w.paddingBottom).toBeGreaterThan(0);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('scrolls forward — start index advances with scrollTop', () => {
|
|
35
|
+
const w = computeWindow({
|
|
36
|
+
totalCount: 1000,
|
|
37
|
+
rowHeight: 48,
|
|
38
|
+
viewportHeight: 600,
|
|
39
|
+
scrollTop: 480, // 10 rows down
|
|
40
|
+
overscan: 0,
|
|
41
|
+
});
|
|
42
|
+
expect(w.startIndex).toBe(10);
|
|
43
|
+
expect(w.paddingTop).toBe(480);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('overscan extends start index upward', () => {
|
|
47
|
+
const w = computeWindow({
|
|
48
|
+
totalCount: 1000,
|
|
49
|
+
rowHeight: 48,
|
|
50
|
+
viewportHeight: 600,
|
|
51
|
+
scrollTop: 480,
|
|
52
|
+
overscan: 5,
|
|
53
|
+
});
|
|
54
|
+
expect(w.startIndex).toBe(5);
|
|
55
|
+
expect(w.paddingTop).toBe(5 * 48);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('overscan extends end index downward', () => {
|
|
59
|
+
const w = computeWindow({
|
|
60
|
+
totalCount: 1000,
|
|
61
|
+
rowHeight: 48,
|
|
62
|
+
viewportHeight: 600,
|
|
63
|
+
scrollTop: 0,
|
|
64
|
+
overscan: 5,
|
|
65
|
+
});
|
|
66
|
+
// raw end was ceil(600/48) = 13. with overscan: 13 + 5 = 18.
|
|
67
|
+
expect(w.endIndex).toBe(18);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('clamps start at 0 when overscan would go negative', () => {
|
|
71
|
+
const w = computeWindow({
|
|
72
|
+
totalCount: 1000,
|
|
73
|
+
rowHeight: 48,
|
|
74
|
+
viewportHeight: 600,
|
|
75
|
+
scrollTop: 0,
|
|
76
|
+
overscan: 10,
|
|
77
|
+
});
|
|
78
|
+
expect(w.startIndex).toBe(0);
|
|
79
|
+
expect(w.paddingTop).toBe(0);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('clamps end at totalCount-1 when overscan would exceed', () => {
|
|
83
|
+
// scroll near the end
|
|
84
|
+
const w = computeWindow({
|
|
85
|
+
totalCount: 20,
|
|
86
|
+
rowHeight: 48,
|
|
87
|
+
viewportHeight: 600,
|
|
88
|
+
scrollTop: 480, // would try to show rows 10-22
|
|
89
|
+
overscan: 5,
|
|
90
|
+
});
|
|
91
|
+
expect(w.endIndex).toBe(19); // clamped
|
|
92
|
+
expect(w.paddingBottom).toBe(0);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('paddingTop + visible rows + paddingBottom = total scrollable height', () => {
|
|
96
|
+
const totalCount = 1000;
|
|
97
|
+
const rowHeight = 48;
|
|
98
|
+
const w = computeWindow({
|
|
99
|
+
totalCount,
|
|
100
|
+
rowHeight,
|
|
101
|
+
viewportHeight: 600,
|
|
102
|
+
scrollTop: 5000,
|
|
103
|
+
overscan: 3,
|
|
104
|
+
});
|
|
105
|
+
const visibleRowCount = w.endIndex - w.startIndex + 1;
|
|
106
|
+
const totalRendered = w.paddingTop + visibleRowCount * rowHeight + w.paddingBottom;
|
|
107
|
+
const totalExpected = totalCount * rowHeight;
|
|
108
|
+
expect(totalRendered).toBe(totalExpected);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('handles small dataset (everything fits in viewport)', () => {
|
|
112
|
+
const w = computeWindow({
|
|
113
|
+
totalCount: 5,
|
|
114
|
+
rowHeight: 48,
|
|
115
|
+
viewportHeight: 600,
|
|
116
|
+
scrollTop: 0,
|
|
117
|
+
overscan: 5,
|
|
118
|
+
});
|
|
119
|
+
expect(w.startIndex).toBe(0);
|
|
120
|
+
expect(w.endIndex).toBe(4); // clamped to totalCount - 1
|
|
121
|
+
expect(w.paddingTop).toBe(0);
|
|
122
|
+
expect(w.paddingBottom).toBe(0);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('returns empty window when rowHeight = 0 (defensive)', () => {
|
|
126
|
+
const w = computeWindow({
|
|
127
|
+
totalCount: 1000,
|
|
128
|
+
rowHeight: 0,
|
|
129
|
+
viewportHeight: 600,
|
|
130
|
+
scrollTop: 100,
|
|
131
|
+
});
|
|
132
|
+
expect(w.endIndex).toBeLessThan(w.startIndex);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('returns empty window when viewportHeight = 0', () => {
|
|
136
|
+
const w = computeWindow({
|
|
137
|
+
totalCount: 1000,
|
|
138
|
+
rowHeight: 48,
|
|
139
|
+
viewportHeight: 0,
|
|
140
|
+
scrollTop: 100,
|
|
141
|
+
});
|
|
142
|
+
expect(w.endIndex).toBeLessThan(w.startIndex);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('supports very large dataset (1 million rows)', () => {
|
|
146
|
+
const w = computeWindow({
|
|
147
|
+
totalCount: 1_000_000,
|
|
148
|
+
rowHeight: 48,
|
|
149
|
+
viewportHeight: 600,
|
|
150
|
+
scrollTop: 24_000_000, // ~middle
|
|
151
|
+
overscan: 5,
|
|
152
|
+
});
|
|
153
|
+
expect(w.startIndex).toBe(499_995);
|
|
154
|
+
expect(w.endIndex).toBeGreaterThan(w.startIndex);
|
|
155
|
+
expect(w.paddingTop).toBe(499_995 * 48);
|
|
156
|
+
expect(w.paddingBottom).toBeGreaterThan(0);
|
|
157
|
+
});
|
|
158
|
+
});
|