@dashforge/tw 0.5.0-beta → 0.6.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.
Files changed (64) hide show
  1. package/CHANGELOG.md +268 -0
  2. package/dist/index.esm.js +1553 -4
  3. package/dist/src/components/Table/Table.d.ts +24 -0
  4. package/dist/src/components/Table/Table.d.ts.map +1 -0
  5. package/dist/src/components/Table/_internal/getNestedValue.d.ts +20 -0
  6. package/dist/src/components/Table/_internal/getNestedValue.d.ts.map +1 -0
  7. package/dist/src/components/Table/_internal/useColumnAutoDetect.d.ts +45 -0
  8. package/dist/src/components/Table/_internal/useColumnAutoDetect.d.ts.map +1 -0
  9. package/dist/src/components/Table/_internal/useControllableState.d.ts +23 -0
  10. package/dist/src/components/Table/_internal/useControllableState.d.ts.map +1 -0
  11. package/dist/src/components/Table/_internal/useDebouncedValue.d.ts +10 -0
  12. package/dist/src/components/Table/_internal/useDebouncedValue.d.ts.map +1 -0
  13. package/dist/src/components/Table/_internal/useTableFilter.d.ts +13 -0
  14. package/dist/src/components/Table/_internal/useTableFilter.d.ts.map +1 -0
  15. package/dist/src/components/Table/_internal/useTableSearch.d.ts +23 -0
  16. package/dist/src/components/Table/_internal/useTableSearch.d.ts.map +1 -0
  17. package/dist/src/components/Table/_internal/useTableSelection.d.ts +25 -0
  18. package/dist/src/components/Table/_internal/useTableSelection.d.ts.map +1 -0
  19. package/dist/src/components/Table/_internal/useTableSort.d.ts +20 -0
  20. package/dist/src/components/Table/_internal/useTableSort.d.ts.map +1 -0
  21. package/dist/src/components/Table/cells/RenderButton.d.ts +13 -0
  22. package/dist/src/components/Table/cells/RenderButton.d.ts.map +1 -0
  23. package/dist/src/components/Table/cells/RenderChip.d.ts +80 -0
  24. package/dist/src/components/Table/cells/RenderChip.d.ts.map +1 -0
  25. package/dist/src/components/Table/cells/RenderText.d.ts +21 -0
  26. package/dist/src/components/Table/cells/RenderText.d.ts.map +1 -0
  27. package/dist/src/components/Table/cells/RenderTwoLine.d.ts +20 -0
  28. package/dist/src/components/Table/cells/RenderTwoLine.d.ts.map +1 -0
  29. package/dist/src/components/Table/cells/RowActionsMenu.d.ts +36 -0
  30. package/dist/src/components/Table/cells/RowActionsMenu.d.ts.map +1 -0
  31. package/dist/src/components/Table/cells/index.d.ts +11 -0
  32. package/dist/src/components/Table/cells/index.d.ts.map +1 -0
  33. package/dist/src/components/Table/table.types.d.ts +304 -0
  34. package/dist/src/components/Table/table.types.d.ts.map +1 -0
  35. package/dist/src/components/Table/table.variants.d.ts +285 -0
  36. package/dist/src/components/Table/table.variants.d.ts.map +1 -0
  37. package/dist/src/index.d.ts +7 -1
  38. package/dist/src/index.d.ts.map +1 -1
  39. package/package.json +1 -1
  40. package/src/components/Table/Table.test.tsx +603 -0
  41. package/src/components/Table/Table.tsx +816 -0
  42. package/src/components/Table/_internal/getNestedValue.test.ts +50 -0
  43. package/src/components/Table/_internal/getNestedValue.ts +32 -0
  44. package/src/components/Table/_internal/themeIdentity.test.ts +84 -0
  45. package/src/components/Table/_internal/useColumnAutoDetect.test.ts +123 -0
  46. package/src/components/Table/_internal/useColumnAutoDetect.ts +100 -0
  47. package/src/components/Table/_internal/useControllableState.ts +48 -0
  48. package/src/components/Table/_internal/useDebouncedValue.ts +24 -0
  49. package/src/components/Table/_internal/useTableFilter.ts +37 -0
  50. package/src/components/Table/_internal/useTableSearch.test.ts +115 -0
  51. package/src/components/Table/_internal/useTableSearch.ts +66 -0
  52. package/src/components/Table/_internal/useTableSelection.test.ts +100 -0
  53. package/src/components/Table/_internal/useTableSelection.ts +100 -0
  54. package/src/components/Table/_internal/useTableSort.test.ts +150 -0
  55. package/src/components/Table/_internal/useTableSort.ts +141 -0
  56. package/src/components/Table/cells/RenderButton.tsx +23 -0
  57. package/src/components/Table/cells/RenderChip.tsx +103 -0
  58. package/src/components/Table/cells/RenderText.tsx +37 -0
  59. package/src/components/Table/cells/RenderTwoLine.tsx +34 -0
  60. package/src/components/Table/cells/RowActionsMenu.tsx +147 -0
  61. package/src/components/Table/cells/index.ts +14 -0
  62. package/src/components/Table/table.types.ts +337 -0
  63. package/src/components/Table/table.variants.ts +207 -0
  64. package/src/index.ts +43 -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.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.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.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,23 @@
1
+ import type { MouseEvent, ReactNode } from 'react';
2
+ import { Button } from '../../Button/Button.js';
3
+ import type { ButtonProps } from '../../Button/button.types.js';
4
+
5
+ export interface RenderButtonProps
6
+ extends Pick<ButtonProps, 'variant' | 'color' | 'size' | 'disabled' | 'access'> {
7
+ label: ReactNode;
8
+ onClick?: (event: MouseEvent<HTMLButtonElement>) => void;
9
+ }
10
+
11
+ /**
12
+ * Inline button cell renderer. Thin wrapper over `<Button>` with
13
+ * the size narrowed to `sm` by default (table density expects compact
14
+ * controls).
15
+ */
16
+ export function RenderButton(props: RenderButtonProps) {
17
+ const { label, onClick, variant = 'ghost', size = 'sm', ...rest } = props;
18
+ return (
19
+ <Button variant={variant} size={size} onClick={onClick} {...rest}>
20
+ {label}
21
+ </Button>
22
+ );
23
+ }
@@ -0,0 +1,103 @@
1
+ import type { ReactNode } from 'react';
2
+ import { tv, type VariantProps } from 'tailwind-variants';
3
+ import { cn } from '../../../utils/cn.js';
4
+
5
+ /**
6
+ * Internal mini-chip used by the cell renderer library. Not exported
7
+ * from `@dashforge/tw` as a standalone component (per Sprint 4.1
8
+ * scope decision — a standalone `<Chip>` may ship in Sprint 5 if
9
+ * demand emerges).
10
+ *
11
+ * Variants:
12
+ * - `color`: 7 intents (neutral / primary / success / warning /
13
+ * danger / info / secondary)
14
+ * - `variant`: `soft` (default) / `solid` / `outline`
15
+ * - `size`: `sm` / `md`
16
+ */
17
+ const chipVariants = tv({
18
+ base: [
19
+ 'inline-flex items-center gap-1 rounded-full font-medium',
20
+ 'whitespace-nowrap',
21
+ ],
22
+ variants: {
23
+ color: {
24
+ neutral: '',
25
+ primary: '',
26
+ success: '',
27
+ warning: '',
28
+ danger: '',
29
+ info: '',
30
+ secondary: '',
31
+ },
32
+ variant: {
33
+ soft: '',
34
+ solid: '',
35
+ outline: '',
36
+ },
37
+ size: {
38
+ sm: 'h-5 px-2 text-xs',
39
+ md: 'h-6 px-2.5 text-xs',
40
+ },
41
+ },
42
+ compoundVariants: [
43
+ // soft = subtle bg + dark text.
44
+ // Neutral row uses NO `dark:` variants — the dashforgePreset()
45
+ // CSS-var swap auto-inverts the neutral palette already.
46
+ // Color rows below KEEP `dark:` variants because color palettes
47
+ // don't auto-invert: the `dark:` shift is an intentional design
48
+ // choice (darker bg + lighter text in dark mode for visual cohesion).
49
+ { variant: 'soft', color: 'neutral', class: 'bg-neutral-100 text-neutral-700' },
50
+ { variant: 'soft', color: 'primary', class: 'bg-primary-100 dark:bg-primary-950 text-primary-700 dark:text-primary-300' },
51
+ { variant: 'soft', color: 'success', class: 'bg-success-100 dark:bg-success-950 text-success-700 dark:text-success-300' },
52
+ { variant: 'soft', color: 'warning', class: 'bg-warning-100 dark:bg-warning-950 text-warning-700 dark:text-warning-300' },
53
+ { variant: 'soft', color: 'danger', class: 'bg-danger-100 dark:bg-danger-950 text-danger-700 dark:text-danger-300' },
54
+ { variant: 'soft', color: 'info', class: 'bg-info-100 dark:bg-info-950 text-info-700 dark:text-info-300' },
55
+ { variant: 'soft', color: 'secondary', class: 'bg-secondary-100 dark:bg-secondary-950 text-secondary-700 dark:text-secondary-300' },
56
+ // solid = full bg + light text
57
+ // solid neutral: bg + text both anchored to neutral palette
58
+ // (auto-inverts). `text-white` would not invert and would break
59
+ // contrast in light mode against a `bg-neutral-700` dark bg, so
60
+ // we stick to `text-neutral-50` (light text in light mode, dark
61
+ // in dark — wait, that's wrong too). Actually `text-neutral-50`
62
+ // auto-inverts: light text in light mode (against dark bg) is
63
+ // what we want; in dark mode `bg-neutral-700` becomes light AND
64
+ // `text-neutral-50` becomes dark — contrast preserved.
65
+ { variant: 'solid', color: 'neutral', class: 'bg-neutral-700 text-neutral-50' },
66
+ { variant: 'solid', color: 'primary', class: 'bg-primary-500 text-white' },
67
+ { variant: 'solid', color: 'success', class: 'bg-success-500 text-white' },
68
+ { variant: 'solid', color: 'warning', class: 'bg-warning-500 text-white' },
69
+ { variant: 'solid', color: 'danger', class: 'bg-danger-500 text-white' },
70
+ { variant: 'solid', color: 'info', class: 'bg-info-500 text-white' },
71
+ { variant: 'solid', color: 'secondary', class: 'bg-secondary-500 text-white' },
72
+ // outline = border + colored text
73
+ // outline neutral: both border + text auto-invert via CSS-var swap.
74
+ { variant: 'outline', color: 'neutral', class: 'border border-neutral-300 text-neutral-700' },
75
+ { variant: 'outline', color: 'primary', class: 'border border-primary-500 text-primary-700 dark:text-primary-300' },
76
+ { variant: 'outline', color: 'success', class: 'border border-success-500 text-success-700 dark:text-success-300' },
77
+ { variant: 'outline', color: 'warning', class: 'border border-warning-500 text-warning-700 dark:text-warning-300' },
78
+ { variant: 'outline', color: 'danger', class: 'border border-danger-500 text-danger-700 dark:text-danger-300' },
79
+ { variant: 'outline', color: 'info', class: 'border border-info-500 text-info-700 dark:text-info-300' },
80
+ { variant: 'outline', color: 'secondary', class: 'border border-secondary-500 text-secondary-700 dark:text-secondary-300' },
81
+ ],
82
+ defaultVariants: {
83
+ color: 'neutral',
84
+ variant: 'soft',
85
+ size: 'sm',
86
+ },
87
+ });
88
+
89
+ type ChipVariants = VariantProps<typeof chipVariants>;
90
+
91
+ export interface RenderChipProps extends ChipVariants {
92
+ children: ReactNode;
93
+ className?: string;
94
+ }
95
+
96
+ export function RenderChip(props: RenderChipProps) {
97
+ const { children, color, variant, size, className } = props;
98
+ return (
99
+ <span className={cn(chipVariants({ color, variant, size }), className)}>
100
+ {children}
101
+ </span>
102
+ );
103
+ }
@@ -0,0 +1,37 @@
1
+ import type { ReactNode } from 'react';
2
+ import { cn } from '../../../utils/cn.js';
3
+
4
+ export interface RenderTextProps {
5
+ value: ReactNode;
6
+ /** Truncate to one line with ellipsis on overflow. @default false */
7
+ truncate?: boolean;
8
+ /** Secondary text color (less prominent). @default false */
9
+ muted?: boolean;
10
+ /** Extra class merged via `tailwind-merge`. */
11
+ className?: string;
12
+ }
13
+
14
+ /**
15
+ * Plain text cell renderer. Wraps the value in a `<span>` and
16
+ * optionally truncates on overflow. Use this for the most common
17
+ * case: render a single string value.
18
+ *
19
+ * The Table component does NOT use this by default for plain
20
+ * string columns — it just renders `{value}` directly. Use
21
+ * `RenderText` when you need the truncate / muted styling.
22
+ */
23
+ export function RenderText(props: RenderTextProps) {
24
+ const { value, truncate, muted, className } = props;
25
+ return (
26
+ <span
27
+ className={cn(
28
+ truncate && 'block max-w-full truncate',
29
+ // Neutral palette auto-inverts via dashforgePreset() CSS-var swap.
30
+ muted && 'text-neutral-500',
31
+ className,
32
+ )}
33
+ >
34
+ {value}
35
+ </span>
36
+ );
37
+ }