@alexgorbatchev/shadcn 0.0.78 → 0.0.79
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/dist/{chunk-ef29qj2b.js → chunk-5jtbr6q4.js} +1 -1
- package/dist/{chunk-k8zjjxtp.js → chunk-tmt6rwg1.js} +5 -3
- package/dist/{chunk-xbxg149v.js → chunk-xhq6s4z7.js} +1 -1
- package/dist/{chunk-czg4v4zt.js → chunk-ya14cv0r.js} +1 -1
- package/dist/components/assistant-ui/elements/DataTable.js +2 -2
- package/dist/components/assistant-ui/index.js +2 -2
- package/dist/components/data-grid/index.js +2 -2
- package/dist/components/gantt/index.js +1 -1
- package/dist/components/json-render/index.js +1 -1
- package/dist/components/virtual-data-grid/VirtualDataGrid.d.ts +20 -3
- package/dist/components/virtual-data-grid/getDataGridCursorTarget.d.ts +6 -0
- package/dist/components/virtual-data-grid/getDataGridRowRange.d.ts +2 -0
- package/dist/components/virtual-data-grid/hooks/useDataGridRowCursor.d.ts +10 -0
- package/dist/components/virtual-data-grid/index.d.ts +3 -0
- package/dist/components/virtual-data-grid/index.js +232 -8
- package/dist/components/virtual-data-grid/types.d.ts +70 -0
- package/dist/components/virtual-table/VirtualTable.d.ts +6 -1
- package/dist/components/virtual-table/index.js +2 -2
- package/dist/index.js +1 -1
- package/dist/prototype.js +1 -1
- package/llms/components/data-display/VirtualDataGrid.md +35 -0
- package/package.json +1 -1
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
Table2,
|
|
3
3
|
TableHeader2,
|
|
4
4
|
TableBody2
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-xhq6s4z7.js";
|
|
6
6
|
|
|
7
7
|
// src/components/virtual-table/VirtualTable.tsx
|
|
8
8
|
import {
|
|
@@ -26,7 +26,8 @@ function VirtualTable2({
|
|
|
26
26
|
estimateRowHeight = 80,
|
|
27
27
|
overscan = 5,
|
|
28
28
|
retainedKeys,
|
|
29
|
-
layout = "fixed"
|
|
29
|
+
layout = "fixed",
|
|
30
|
+
isRowGrid = false
|
|
30
31
|
}) {
|
|
31
32
|
const viewport = useRef(null);
|
|
32
33
|
const [focusedKey, setFocusedKey] = useState();
|
|
@@ -49,7 +50,7 @@ function VirtualTable2({
|
|
|
49
50
|
"data-testid": "VirtualTable",
|
|
50
51
|
role: "region",
|
|
51
52
|
"aria-label": label,
|
|
52
|
-
tabIndex: 0,
|
|
53
|
+
tabIndex: isRowGrid ? -1 : 0,
|
|
53
54
|
className: height === "fill" ? "h-full min-h-0 min-w-0 flex-1 overflow-auto overscroll-contain" : "min-w-0 overflow-auto overscroll-contain",
|
|
54
55
|
style: height === "fill" ? undefined : { maxHeight },
|
|
55
56
|
onFocusCapture: (event) => {
|
|
@@ -63,6 +64,7 @@ function VirtualTable2({
|
|
|
63
64
|
layout,
|
|
64
65
|
"aria-label": label,
|
|
65
66
|
"aria-rowcount": items.length + 1,
|
|
67
|
+
...isRowGrid && { role: "grid", "aria-multiselectable": true },
|
|
66
68
|
children: [
|
|
67
69
|
columns,
|
|
68
70
|
/* @__PURE__ */ jsx(TableHeader2, {
|
|
@@ -53,7 +53,7 @@ function TableRow2({ density, className, ...props }) {
|
|
|
53
53
|
return /* @__PURE__ */ jsx("tr", {
|
|
54
54
|
"data-density": density,
|
|
55
55
|
"data-slot": "table-row",
|
|
56
|
-
className: cn2("border-b transition-colors hover:bg-muted/50 has-aria-expanded:bg-muted/50 data-[state=selected]:bg-muted", className),
|
|
56
|
+
className: cn2("border-b transition-colors outline-none hover:bg-muted/50 has-aria-expanded:bg-muted/50 focus:outline-2 focus:-outline-offset-2 focus:outline-ring data-[state=selected]:bg-muted aria-selected:bg-primary/10 aria-selected:shadow-[inset_3px_0_0_var(--primary)] aria-selected:hover:bg-primary/15", className),
|
|
57
57
|
...props
|
|
58
58
|
});
|
|
59
59
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
DataTable2
|
|
3
|
-
} from "../../../chunk-
|
|
3
|
+
} from "../../../chunk-ya14cv0r.js";
|
|
4
4
|
import"../../../chunk-1wz92wgg.js";
|
|
5
|
-
import"../../../chunk-
|
|
5
|
+
import"../../../chunk-xhq6s4z7.js";
|
|
6
6
|
import"../../../chunk-k1f35j55.js";
|
|
7
7
|
import"../../../chunk-m4geb1s5.js";
|
|
8
8
|
import"../../../chunk-9t530p75.js";
|
|
@@ -203,8 +203,8 @@ import"../../chunk-1wz92wgg.js";
|
|
|
203
203
|
import"../../chunk-9t530p75.js";
|
|
204
204
|
import {
|
|
205
205
|
DataTable2
|
|
206
|
-
} from "../../chunk-
|
|
207
|
-
import"../../chunk-
|
|
206
|
+
} from "../../chunk-ya14cv0r.js";
|
|
207
|
+
import"../../chunk-xhq6s4z7.js";
|
|
208
208
|
import {
|
|
209
209
|
DaySeparator2
|
|
210
210
|
} from "../../chunk-3j9enr2q.js";
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
DataGridColumnHeader2,
|
|
8
8
|
DataGridFilter2,
|
|
9
9
|
DataGridPagination2
|
|
10
|
-
} from "../../chunk-
|
|
10
|
+
} from "../../chunk-5jtbr6q4.js";
|
|
11
11
|
import"../../chunk-7ny0vyek.js";
|
|
12
12
|
import"../../chunk-8ssktt4b.js";
|
|
13
13
|
import"../../chunk-en44w1pn.js";
|
|
@@ -17,7 +17,7 @@ import"../../chunk-jpvw9xqy.js";
|
|
|
17
17
|
import {
|
|
18
18
|
Button2
|
|
19
19
|
} from "../../chunk-32t5kc0y.js";
|
|
20
|
-
import"../../chunk-
|
|
20
|
+
import"../../chunk-xhq6s4z7.js";
|
|
21
21
|
import"../../chunk-p6z2m3zp.js";
|
|
22
22
|
import"../../chunk-7jpsybag.js";
|
|
23
23
|
import"../../chunk-7vg045gd.js";
|
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
import type { RowData } from "@tanstack/react-table";
|
|
2
|
-
import { type ReactElement, type ReactNode } from "react";
|
|
2
|
+
import { type FocusEvent, type KeyboardEvent, type MouseEvent, type ReactElement, type ReactNode } from "react";
|
|
3
3
|
import { type DataGridRow, type DataGridTable } from "../data-grid/DataGrid.js";
|
|
4
4
|
import { type IVirtualTableRowProps } from "../virtual-table/VirtualTable.js";
|
|
5
|
+
import type { IDataGridRowCursor } from "./types.js";
|
|
6
|
+
/** A row's props: spread them onto the row element. With a row cursor they also make the row its keyboard target. */
|
|
7
|
+
export interface IVirtualDataGridRowProps extends IVirtualTableRowProps {
|
|
8
|
+
tabIndex?: number;
|
|
9
|
+
"aria-selected"?: boolean;
|
|
10
|
+
"data-cursor"?: "";
|
|
11
|
+
"data-row-id"?: string;
|
|
12
|
+
onFocus?: (event: FocusEvent<HTMLTableRowElement>) => void;
|
|
13
|
+
onClick?: (event: MouseEvent<HTMLTableRowElement>) => void;
|
|
14
|
+
onKeyDown?: (event: KeyboardEvent<HTMLTableRowElement>) => void;
|
|
15
|
+
}
|
|
5
16
|
interface IVirtualDataGridProps<T extends RowData, TSelected> {
|
|
6
17
|
table: DataGridTable<T, TSelected>;
|
|
7
18
|
label: string;
|
|
@@ -23,11 +34,17 @@ interface IVirtualDataGridProps<T extends RowData, TSelected> {
|
|
|
23
34
|
onFitWidthChange?: (width: number) => void;
|
|
24
35
|
/** Row IDs that stay rendered while scrolled out of view, such as rows with an open editor. */
|
|
25
36
|
retainedRowIds?: readonly string[];
|
|
37
|
+
/**
|
|
38
|
+
* A row cursor from `useDataGridRowCursor`: the grid becomes one tab stop whose cursor row holds focus, rows set
|
|
39
|
+
* `aria-selected`, and clicks select with Finder's Shift and Mod gestures. Controls inside rows should leave the tab
|
|
40
|
+
* order, since each row is its own keyboard target.
|
|
41
|
+
*/
|
|
42
|
+
rowCursor?: IDataGridRowCursor;
|
|
26
43
|
/**
|
|
27
44
|
* Renders one row. Spread `props` onto the row element and render `DataGridCells` inside it; the default renders a
|
|
28
45
|
* plain `TableRow` marked selected while the row is selected.
|
|
29
46
|
*/
|
|
30
|
-
renderRow?: (row: DataGridRow<T>, props:
|
|
47
|
+
renderRow?: (row: DataGridRow<T>, props: IVirtualDataGridRowProps) => ReactNode;
|
|
31
48
|
}
|
|
32
49
|
/**
|
|
33
50
|
* A virtualized, fixed-layout data grid over every filtered and sorted row, without pagination. Columns marked
|
|
@@ -35,5 +52,5 @@ interface IVirtualDataGridProps<T extends RowData, TSelected> {
|
|
|
35
52
|
* Column widths change through CSS variables, so a `useTable` selector can leave out sizing state and resizing never
|
|
36
53
|
* renders rows again.
|
|
37
54
|
*/
|
|
38
|
-
export declare function VirtualDataGrid<T extends RowData, TSelected>({ table, label, maxHeight, height, estimateRowHeight, overscan, fitColumns, onFitWidthChange, retainedRowIds, renderRow, }: IVirtualDataGridProps<T, TSelected>): ReactElement;
|
|
55
|
+
export declare function VirtualDataGrid<T extends RowData, TSelected>({ table, label, maxHeight, height, estimateRowHeight, overscan, fitColumns, onFitWidthChange, retainedRowIds, rowCursor, renderRow, }: IVirtualDataGridProps<T, TSelected>): ReactElement;
|
|
39
56
|
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { DataGridCursorMove } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* The row a cursor move lands on, in displayed order. Without a cursor, moving forward starts at the first row and
|
|
4
|
+
* moving back at the last; moves stop at either end.
|
|
5
|
+
*/
|
|
6
|
+
export declare function getDataGridCursorTarget(rowIds: readonly string[], cursorRowId: string | undefined, move: DataGridCursorMove, pageSize: number): string | undefined;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RowData } from "@tanstack/react-table";
|
|
2
|
+
import type { DataGridTable } from "../../data-grid/DataGrid.js";
|
|
3
|
+
import type { IDataGridRowCursor, IDataGridRowCursorOptions } from "../types.js";
|
|
4
|
+
/**
|
|
5
|
+
* A row cursor with Finder-style selection for `VirtualDataGrid` (pass it as `rowCursor`). The grid becomes one tab
|
|
6
|
+
* stop whose cursor row holds focus; a click selects one row and sets the range anchor, Shift+click selects the
|
|
7
|
+
* displayed range from the anchor, Mod+click toggles a row and Mod+Shift+click adds a range. The cursor's methods run
|
|
8
|
+
* the keyboard gestures, which rows also handle themselves unless `keyboard` is off.
|
|
9
|
+
*/
|
|
10
|
+
export declare function useDataGridRowCursor<T extends RowData, TSelected>(table: DataGridTable<T, TSelected>, options?: IDataGridRowCursorOptions): IDataGridRowCursor;
|
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
export { VirtualDataGrid } from "./VirtualDataGrid.js";
|
|
2
|
+
export type { IVirtualDataGridRowProps } from "./VirtualDataGrid.js";
|
|
3
|
+
export { useDataGridRowCursor } from "./hooks/useDataGridRowCursor.js";
|
|
4
|
+
export type { DataGridCursorChangeHandler, DataGridCursorMove, DataGridCursorSelection, DataGridSelectionChangeHandler, DataGridSelectionGesture, IDataGridPointerModifiers, IDataGridRevealOptions, IDataGridRowCursor, IDataGridRowCursorOptions, IDataGridRowNavigation, IDataGridSelectionChange, } from "./types.js";
|
|
2
5
|
export type { IVirtualTableRowProps } from "../virtual-table/VirtualTable.js";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import"../../chunk-en44w1pn.js";
|
|
2
2
|
import {
|
|
3
3
|
TableRow2
|
|
4
|
-
} from "../../chunk-
|
|
4
|
+
} from "../../chunk-xhq6s4z7.js";
|
|
5
5
|
import {
|
|
6
6
|
DataGridColumnFitContext,
|
|
7
7
|
getDataGridColumnFit,
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
DataGridHeaderRows,
|
|
11
11
|
DataGridCells2,
|
|
12
12
|
DataGridColumns
|
|
13
|
-
} from "../../chunk-
|
|
13
|
+
} from "../../chunk-5jtbr6q4.js";
|
|
14
14
|
import"../../chunk-7ny0vyek.js";
|
|
15
15
|
import"../../chunk-8ssktt4b.js";
|
|
16
16
|
import"../../chunk-e6hk39ev.js";
|
|
@@ -22,11 +22,16 @@ import"../../chunk-7jpsybag.js";
|
|
|
22
22
|
import"../../chunk-7vg045gd.js";
|
|
23
23
|
import {
|
|
24
24
|
VirtualTable2
|
|
25
|
-
} from "../../chunk-
|
|
25
|
+
} from "../../chunk-tmt6rwg1.js";
|
|
26
26
|
import"../../chunk-5mydsjp3.js";
|
|
27
27
|
|
|
28
28
|
// src/components/virtual-data-grid/VirtualDataGrid.tsx
|
|
29
|
-
import {
|
|
29
|
+
import {
|
|
30
|
+
useEffect,
|
|
31
|
+
useMemo,
|
|
32
|
+
useRef,
|
|
33
|
+
useState as useState2
|
|
34
|
+
} from "react";
|
|
30
35
|
|
|
31
36
|
// src/components/data-grid/hooks/useDataGridColumnSizes.ts
|
|
32
37
|
import { useLayoutEffect } from "react";
|
|
@@ -81,6 +86,15 @@ function useDataGridColumnFit(table, scope, isEnabled, onWidthChange) {
|
|
|
81
86
|
|
|
82
87
|
// src/components/virtual-data-grid/VirtualDataGrid.tsx
|
|
83
88
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
89
|
+
var MOVES = {
|
|
90
|
+
ArrowDown: "next",
|
|
91
|
+
ArrowUp: "previous",
|
|
92
|
+
Home: "first",
|
|
93
|
+
End: "last",
|
|
94
|
+
PageDown: "next-page",
|
|
95
|
+
PageUp: "previous-page"
|
|
96
|
+
};
|
|
97
|
+
var ROW_CONTROLS = "button, a, input, select, textarea, label, [role='checkbox'], [role='radio'], [role='menuitem']";
|
|
84
98
|
function renderDefaultRow(row, props) {
|
|
85
99
|
return /* @__PURE__ */ jsx(TableRow2, {
|
|
86
100
|
...props,
|
|
@@ -90,6 +104,22 @@ function renderDefaultRow(row, props) {
|
|
|
90
104
|
})
|
|
91
105
|
}, row.id);
|
|
92
106
|
}
|
|
107
|
+
function handleRowKeys(cursor, event) {
|
|
108
|
+
if (event.target !== event.currentTarget || event.altKey)
|
|
109
|
+
return;
|
|
110
|
+
const isMod = event.metaKey || event.ctrlKey;
|
|
111
|
+
const move = MOVES[event.key];
|
|
112
|
+
if (move) {
|
|
113
|
+
event.preventDefault();
|
|
114
|
+
cursor.move(move, event.shiftKey ? "extend" : isMod ? "keep" : "follow");
|
|
115
|
+
} else if (event.key === " " && event.shiftKey && !isMod) {
|
|
116
|
+
event.preventDefault();
|
|
117
|
+
cursor.toggle();
|
|
118
|
+
} else if (event.key.toLowerCase() === "a" && isMod && !event.shiftKey) {
|
|
119
|
+
event.preventDefault();
|
|
120
|
+
cursor.selectAll();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
93
123
|
function VirtualDataGrid({
|
|
94
124
|
table,
|
|
95
125
|
label,
|
|
@@ -100,12 +130,70 @@ function VirtualDataGrid({
|
|
|
100
130
|
fitColumns = false,
|
|
101
131
|
onFitWidthChange,
|
|
102
132
|
retainedRowIds,
|
|
133
|
+
rowCursor,
|
|
103
134
|
renderRow = renderDefaultRow
|
|
104
135
|
}) {
|
|
105
136
|
const scope = useRef(null);
|
|
106
137
|
useDataGridColumnSizes(table, scope);
|
|
107
138
|
const fit = useDataGridColumnFit(table, scope, fitColumns, onFitWidthChange);
|
|
108
139
|
const hiddenLabels = table.getVisibleLeafColumns().filter((column) => fit?.hiddenColumnIds.includes(column.id)).map(getDataGridColumnLabel);
|
|
140
|
+
const rows = table.getPrePaginatedRowModel().rows;
|
|
141
|
+
const cursorRowId = rowCursor?.cursorRowId;
|
|
142
|
+
const retained = useMemo(() => cursorRowId === undefined ? retainedRowIds : [...retainedRowIds ?? [], cursorRowId], [retainedRowIds, cursorRowId]);
|
|
143
|
+
const tabStop = rows.some((row) => row.id === cursorRowId) ? cursorRowId : rows[0]?.id;
|
|
144
|
+
const [reveal, setReveal] = useState2();
|
|
145
|
+
const latestRows = useRef(rows);
|
|
146
|
+
useEffect(() => {
|
|
147
|
+
latestRows.current = rows;
|
|
148
|
+
});
|
|
149
|
+
useEffect(() => {
|
|
150
|
+
if (!rowCursor)
|
|
151
|
+
return;
|
|
152
|
+
rowCursor.attach({
|
|
153
|
+
rowIds: () => latestRows.current.map((row) => row.id),
|
|
154
|
+
pageSize: () => {
|
|
155
|
+
const viewport = scope.current?.querySelector("[data-testid='VirtualTable']");
|
|
156
|
+
const header = viewport?.querySelector("thead")?.getBoundingClientRect().height ?? 0;
|
|
157
|
+
const row = viewport?.querySelector("tr[data-row-id]")?.getBoundingClientRect().height;
|
|
158
|
+
return viewport && row ? Math.max(1, Math.floor((viewport.clientHeight - header) / row) - 1) : 10;
|
|
159
|
+
},
|
|
160
|
+
reveal: (rowId, focus) => setReveal({ rowId, focus })
|
|
161
|
+
});
|
|
162
|
+
return () => rowCursor.attach(null);
|
|
163
|
+
}, [rowCursor]);
|
|
164
|
+
useEffect(() => {
|
|
165
|
+
if (!reveal)
|
|
166
|
+
return;
|
|
167
|
+
const viewport = scope.current?.querySelector("[data-testid='VirtualTable']");
|
|
168
|
+
const row = viewport?.querySelector(`tr[data-row-id="${CSS.escape(reveal.rowId)}"]`);
|
|
169
|
+
if (!viewport || !row)
|
|
170
|
+
return;
|
|
171
|
+
const header = viewport.querySelector("thead")?.getBoundingClientRect().height ?? 0;
|
|
172
|
+
const view = viewport.getBoundingClientRect();
|
|
173
|
+
const bounds = row.getBoundingClientRect();
|
|
174
|
+
if (bounds.top < view.top + header)
|
|
175
|
+
viewport.scrollTop -= view.top + header - bounds.top;
|
|
176
|
+
else if (bounds.bottom > view.bottom)
|
|
177
|
+
viewport.scrollTop += bounds.bottom - view.bottom;
|
|
178
|
+
if (reveal.focus)
|
|
179
|
+
row.focus({ preventScroll: true });
|
|
180
|
+
}, [reveal]);
|
|
181
|
+
const cursorProps = (row) => rowCursor ? {
|
|
182
|
+
tabIndex: row.id === tabStop ? 0 : -1,
|
|
183
|
+
"aria-selected": row.getIsSelected(),
|
|
184
|
+
"data-cursor": row.id === cursorRowId ? "" : undefined,
|
|
185
|
+
"data-row-id": row.id,
|
|
186
|
+
onFocus: () => rowCursor.setCursor(row.id),
|
|
187
|
+
onClick: (event) => {
|
|
188
|
+
if (event.target instanceof Element && event.target.closest(ROW_CONTROLS))
|
|
189
|
+
return;
|
|
190
|
+
rowCursor.pointer(row.id, { shift: event.shiftKey, mod: event.metaKey || event.ctrlKey });
|
|
191
|
+
},
|
|
192
|
+
onKeyDown: (event) => {
|
|
193
|
+
if (rowCursor.keyboard)
|
|
194
|
+
handleRowKeys(rowCursor, event);
|
|
195
|
+
}
|
|
196
|
+
} : {};
|
|
109
197
|
return /* @__PURE__ */ jsx("div", {
|
|
110
198
|
ref: scope,
|
|
111
199
|
"data-testid": "VirtualDataGrid",
|
|
@@ -129,14 +217,15 @@ function VirtualDataGrid({
|
|
|
129
217
|
]
|
|
130
218
|
}),
|
|
131
219
|
/* @__PURE__ */ jsx(VirtualTable2, {
|
|
132
|
-
items:
|
|
220
|
+
items: rows,
|
|
133
221
|
getItemKey: (row) => row.id,
|
|
134
222
|
label,
|
|
135
223
|
maxHeight,
|
|
136
224
|
height,
|
|
137
225
|
estimateRowHeight,
|
|
138
226
|
overscan,
|
|
139
|
-
retainedKeys:
|
|
227
|
+
retainedKeys: retained,
|
|
228
|
+
isRowGrid: rowCursor !== undefined,
|
|
140
229
|
columns: /* @__PURE__ */ jsx(DataGridColumns, {
|
|
141
230
|
table
|
|
142
231
|
}),
|
|
@@ -144,12 +233,147 @@ function VirtualDataGrid({
|
|
|
144
233
|
table,
|
|
145
234
|
isResizable: true
|
|
146
235
|
}),
|
|
147
|
-
renderRow
|
|
236
|
+
renderRow: (row, props) => renderRow(row, { ...props, ...cursorProps(row) })
|
|
148
237
|
})
|
|
149
238
|
]
|
|
150
239
|
})
|
|
151
240
|
});
|
|
152
241
|
}
|
|
242
|
+
// src/components/virtual-data-grid/hooks/useDataGridRowCursor.ts
|
|
243
|
+
import { useLayoutEffect as useLayoutEffect3, useMemo as useMemo2, useRef as useRef2, useState as useState3 } from "react";
|
|
244
|
+
|
|
245
|
+
// src/components/virtual-data-grid/getDataGridCursorTarget.ts
|
|
246
|
+
function getDataGridCursorTarget(rowIds, cursorRowId, move, pageSize) {
|
|
247
|
+
if (rowIds.length === 0)
|
|
248
|
+
return;
|
|
249
|
+
const last = rowIds.length - 1;
|
|
250
|
+
const index = cursorRowId === undefined ? -1 : rowIds.indexOf(cursorRowId);
|
|
251
|
+
const page = Math.max(1, pageSize);
|
|
252
|
+
const target = (() => {
|
|
253
|
+
switch (move) {
|
|
254
|
+
case "first":
|
|
255
|
+
return 0;
|
|
256
|
+
case "last":
|
|
257
|
+
return last;
|
|
258
|
+
case "next":
|
|
259
|
+
return index < 0 ? 0 : index + 1;
|
|
260
|
+
case "previous":
|
|
261
|
+
return index < 0 ? last : index - 1;
|
|
262
|
+
case "next-page":
|
|
263
|
+
return index < 0 ? 0 : index + page;
|
|
264
|
+
case "previous-page":
|
|
265
|
+
return index < 0 ? last : index - page;
|
|
266
|
+
}
|
|
267
|
+
})();
|
|
268
|
+
return rowIds[Math.min(last, Math.max(0, target))];
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// src/components/virtual-data-grid/getDataGridRowRange.ts
|
|
272
|
+
function getDataGridRowRange(rowIds, fromRowId, toRowId) {
|
|
273
|
+
const to = rowIds.indexOf(toRowId);
|
|
274
|
+
if (to < 0)
|
|
275
|
+
return [];
|
|
276
|
+
const from = fromRowId === undefined ? -1 : rowIds.indexOf(fromRowId);
|
|
277
|
+
if (from < 0)
|
|
278
|
+
return [toRowId];
|
|
279
|
+
return rowIds.slice(Math.min(from, to), Math.max(from, to) + 1);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// src/components/virtual-data-grid/hooks/useDataGridRowCursor.ts
|
|
283
|
+
function selectedIds(selection) {
|
|
284
|
+
return Object.keys(selection).filter((id) => selection[id]);
|
|
285
|
+
}
|
|
286
|
+
function useDataGridRowCursor(table, options = {}) {
|
|
287
|
+
const [cursorRowId, setCursorRowId] = useState3();
|
|
288
|
+
const [ownAnchor, setOwnAnchor] = useState3();
|
|
289
|
+
const isOwned = options.onSelectionChange !== undefined;
|
|
290
|
+
const anchorRowId = isOwned ? options.anchorRowId : ownAnchor;
|
|
291
|
+
const navigation = useRef2(null);
|
|
292
|
+
const latest = useRef2({ table, options, cursorRowId, anchorRowId, isOwned });
|
|
293
|
+
useLayoutEffect3(() => {
|
|
294
|
+
latest.current = { table, options, cursorRowId, anchorRowId, isOwned };
|
|
295
|
+
});
|
|
296
|
+
const methods = useMemo2(() => {
|
|
297
|
+
const rowIds = () => navigation.current?.rowIds() ?? latest.current.table.getPrePaginatedRowModel().rows.map((row) => row.id);
|
|
298
|
+
const moveCursor = (rowId, focus) => {
|
|
299
|
+
latest.current.cursorRowId = rowId;
|
|
300
|
+
setCursorRowId(rowId);
|
|
301
|
+
latest.current.options.onCursorChange?.(rowId);
|
|
302
|
+
if (rowId !== undefined)
|
|
303
|
+
navigation.current?.reveal(rowId, focus);
|
|
304
|
+
};
|
|
305
|
+
const select = (ids, anchor, gesture) => {
|
|
306
|
+
const current = latest.current;
|
|
307
|
+
if (current.isOwned) {
|
|
308
|
+
current.options.onSelectionChange?.({ rowIds: ids, anchorRowId: anchor, gesture });
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
current.anchorRowId = anchor;
|
|
312
|
+
setOwnAnchor(anchor);
|
|
313
|
+
current.table.setRowSelection(Object.fromEntries(ids.map((id) => [id, true])));
|
|
314
|
+
};
|
|
315
|
+
const selection = () => selectedIds(latest.current.table.atoms.rowSelection.get());
|
|
316
|
+
return {
|
|
317
|
+
move: (move, mode = "follow") => {
|
|
318
|
+
const { cursorRowId: from, anchorRowId: anchor } = latest.current;
|
|
319
|
+
const target = getDataGridCursorTarget(rowIds(), from, move, navigation.current?.pageSize() ?? 10);
|
|
320
|
+
if (target === undefined)
|
|
321
|
+
return;
|
|
322
|
+
moveCursor(target, true);
|
|
323
|
+
if (mode === "follow" && selection().length <= 1)
|
|
324
|
+
select([target], target, "follow");
|
|
325
|
+
else if (mode === "extend") {
|
|
326
|
+
const start = anchor ?? from ?? target;
|
|
327
|
+
select(getDataGridRowRange(rowIds(), start, target), start, "extend");
|
|
328
|
+
}
|
|
329
|
+
},
|
|
330
|
+
toggle: () => {
|
|
331
|
+
const rowId = latest.current.cursorRowId;
|
|
332
|
+
if (rowId === undefined)
|
|
333
|
+
return;
|
|
334
|
+
const current = selection();
|
|
335
|
+
select(current.includes(rowId) ? current.filter((id) => id !== rowId) : [...current, rowId], rowId, "toggle");
|
|
336
|
+
},
|
|
337
|
+
selectAll: () => {
|
|
338
|
+
const visible = latest.current.table.getFilteredRowModel().flatRows.filter((row) => row.getCanSelect());
|
|
339
|
+
const ids = [...new Set([...selection(), ...visible.map((row) => row.id)])];
|
|
340
|
+
select(ids, latest.current.anchorRowId, "all");
|
|
341
|
+
},
|
|
342
|
+
clear: () => select([], undefined, "clear"),
|
|
343
|
+
pointer: (rowId, { shift, mod }) => {
|
|
344
|
+
const { cursorRowId: from, anchorRowId: anchor } = latest.current;
|
|
345
|
+
moveCursor(rowId, false);
|
|
346
|
+
if (shift) {
|
|
347
|
+
const start = anchor ?? from ?? rowIds()[0];
|
|
348
|
+
const range = getDataGridRowRange(rowIds(), start, rowId);
|
|
349
|
+
select(mod ? [...new Set([...selection(), ...range])] : range, start, mod ? "add-range" : "range");
|
|
350
|
+
} else if (mod) {
|
|
351
|
+
const current = selection();
|
|
352
|
+
select(current.includes(rowId) ? current.filter((id) => id !== rowId) : [...current, rowId], rowId, "toggle");
|
|
353
|
+
} else
|
|
354
|
+
select([rowId], rowId, "select");
|
|
355
|
+
},
|
|
356
|
+
setCursor: (rowId) => {
|
|
357
|
+
if (rowId === latest.current.cursorRowId)
|
|
358
|
+
return;
|
|
359
|
+
latest.current.cursorRowId = rowId;
|
|
360
|
+
setCursorRowId(rowId);
|
|
361
|
+
latest.current.options.onCursorChange?.(rowId);
|
|
362
|
+
},
|
|
363
|
+
reveal: ({ focus = true } = {}) => {
|
|
364
|
+
const rowId = latest.current.cursorRowId;
|
|
365
|
+
if (rowId !== undefined)
|
|
366
|
+
navigation.current?.reveal(rowId, focus);
|
|
367
|
+
},
|
|
368
|
+
attach: (next) => {
|
|
369
|
+
navigation.current = next;
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
}, []);
|
|
373
|
+
const keyboard = options.keyboard ?? true;
|
|
374
|
+
return useMemo2(() => ({ ...methods, cursorRowId, anchorRowId, keyboard }), [methods, cursorRowId, anchorRowId, keyboard]);
|
|
375
|
+
}
|
|
153
376
|
export {
|
|
154
|
-
VirtualDataGrid
|
|
377
|
+
VirtualDataGrid,
|
|
378
|
+
useDataGridRowCursor
|
|
155
379
|
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/** Where a cursor move goes, in displayed order. */
|
|
2
|
+
export type DataGridCursorMove = "previous" | "next" | "first" | "last" | "previous-page" | "next-page";
|
|
3
|
+
/**
|
|
4
|
+
* What a cursor move does to the selection: `follow` carries a selection of at most one row along with the cursor, as
|
|
5
|
+
* Finder's arrows do, and leaves a larger selection alone; `keep` moves the cursor only; `extend` selects from the
|
|
6
|
+
* anchor to the cursor.
|
|
7
|
+
*/
|
|
8
|
+
export type DataGridCursorSelection = "follow" | "keep" | "extend";
|
|
9
|
+
/** How a selection change was made, so consumers can tell explicit picks from one that follows the cursor. */
|
|
10
|
+
export type DataGridSelectionGesture = "select" | "toggle" | "range" | "add-range" | "follow" | "extend" | "all" | "clear";
|
|
11
|
+
/** One gesture's whole new selection and range anchor. */
|
|
12
|
+
export interface IDataGridSelectionChange {
|
|
13
|
+
rowIds: string[];
|
|
14
|
+
anchorRowId?: string;
|
|
15
|
+
gesture: DataGridSelectionGesture;
|
|
16
|
+
}
|
|
17
|
+
export type DataGridSelectionChangeHandler = (change: IDataGridSelectionChange) => void;
|
|
18
|
+
export type DataGridCursorChangeHandler = (rowId: string | undefined) => void;
|
|
19
|
+
/** Pointer modifiers: Shift selects a range; Mod (⌘ or Ctrl) toggles a row, or adds a range with Shift. */
|
|
20
|
+
export interface IDataGridPointerModifiers {
|
|
21
|
+
shift: boolean;
|
|
22
|
+
mod: boolean;
|
|
23
|
+
}
|
|
24
|
+
export interface IDataGridRowCursorOptions {
|
|
25
|
+
/**
|
|
26
|
+
* Receives every gesture's new selection and anchor instead of the table's own row selection, for consumers that
|
|
27
|
+
* keep the selection elsewhere. Pair it with `anchorRowId`.
|
|
28
|
+
*/
|
|
29
|
+
onSelectionChange?: DataGridSelectionChangeHandler;
|
|
30
|
+
/** The range anchor of a selection the consumer owns. */
|
|
31
|
+
anchorRowId?: string;
|
|
32
|
+
/** Reports the row under the cursor whenever it changes. */
|
|
33
|
+
onCursorChange?: DataGridCursorChangeHandler;
|
|
34
|
+
/**
|
|
35
|
+
* Whether rows handle the built-in keys: ↑/↓, Home/End and PageUp/PageDown move the cursor (Mod keeps the selection,
|
|
36
|
+
* Shift extends it), Shift+Space toggles the cursor row and Mod+A selects every matching row. Turn it off when an
|
|
37
|
+
* app keymap runs the same actions through the cursor's methods.
|
|
38
|
+
*/
|
|
39
|
+
keyboard?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/** What a virtualized grid gives its cursor: displayed rows, page size and a way to show and focus a row. */
|
|
42
|
+
export interface IDataGridRowNavigation {
|
|
43
|
+
rowIds: () => string[];
|
|
44
|
+
pageSize: () => number;
|
|
45
|
+
reveal: (rowId: string, focus: boolean) => void;
|
|
46
|
+
}
|
|
47
|
+
export interface IDataGridRevealOptions {
|
|
48
|
+
focus?: boolean;
|
|
49
|
+
}
|
|
50
|
+
/** A grid's row cursor and selection gestures (`useDataGridRowCursor`), passed to `VirtualDataGrid`'s `rowCursor`. */
|
|
51
|
+
export interface IDataGridRowCursor {
|
|
52
|
+
/** The row under the cursor, which takes the grid's one tab stop. */
|
|
53
|
+
cursorRowId: string | undefined;
|
|
54
|
+
anchorRowId: string | undefined;
|
|
55
|
+
keyboard: boolean;
|
|
56
|
+
move: (move: DataGridCursorMove, selection?: DataGridCursorSelection) => void;
|
|
57
|
+
/** Toggles the cursor row and moves the anchor to it. */
|
|
58
|
+
toggle: () => void;
|
|
59
|
+
/** Selects every row the filters leave, keeping picks they hide. */
|
|
60
|
+
selectAll: () => void;
|
|
61
|
+
clear: () => void;
|
|
62
|
+
/** A click on a row, with its modifiers. */
|
|
63
|
+
pointer: (rowId: string, modifiers: IDataGridPointerModifiers) => void;
|
|
64
|
+
/** Puts the cursor on a row without moving focus or scrolling, as focusing the row does. */
|
|
65
|
+
setCursor: (rowId: string | undefined) => void;
|
|
66
|
+
/** Scrolls the cursor row into view, and focuses it unless told not to, such as after a sort moves it. */
|
|
67
|
+
reveal: (options?: IDataGridRevealOptions) => void;
|
|
68
|
+
/** Connects the grid that renders the rows; `VirtualDataGrid` calls it. */
|
|
69
|
+
attach: (navigation: IDataGridRowNavigation | null) => void;
|
|
70
|
+
}
|
|
@@ -19,6 +19,11 @@ interface IVirtualTableProps<T> {
|
|
|
19
19
|
overscan?: number;
|
|
20
20
|
retainedKeys?: readonly Key[];
|
|
21
21
|
layout?: ComponentProps<typeof Table>["layout"];
|
|
22
|
+
/**
|
|
23
|
+
* Rows are the keyboard targets of a multi-select grid, as `VirtualDataGrid`'s row cursor makes them: the table
|
|
24
|
+
* becomes a `grid` and the scroll region leaves the tab order, so the grid is one tab stop.
|
|
25
|
+
*/
|
|
26
|
+
isRowGrid?: boolean;
|
|
22
27
|
}
|
|
23
|
-
export declare function VirtualTable<T>({ items, getItemKey, renderRow, header, columns, label, maxHeight, height, estimateRowHeight, overscan, retainedKeys, layout, }: IVirtualTableProps<T>): ReactElement;
|
|
28
|
+
export declare function VirtualTable<T>({ items, getItemKey, renderRow, header, columns, label, maxHeight, height, estimateRowHeight, overscan, retainedKeys, layout, isRowGrid, }: IVirtualTableProps<T>): ReactElement;
|
|
24
29
|
export {};
|
package/dist/index.js
CHANGED
package/dist/prototype.js
CHANGED
|
@@ -2051,7 +2051,7 @@ var program = new Command;
|
|
|
2051
2051
|
// package.json
|
|
2052
2052
|
var package_default = {
|
|
2053
2053
|
name: "@alexgorbatchev/shadcn",
|
|
2054
|
-
version: "0.0.
|
|
2054
|
+
version: "0.0.79",
|
|
2055
2055
|
description: "Sharp, monospace React components with compact and comfortable densities.",
|
|
2056
2056
|
license: "MIT",
|
|
2057
2057
|
repository: {
|
|
@@ -109,3 +109,38 @@ Storybook CSF excerpts. Default args and render apply unless overridden. Local h
|
|
|
109
109
|
</div>)
|
|
110
110
|
}
|
|
111
111
|
```
|
|
112
|
+
|
|
113
|
+
### RowCursorPointer
|
|
114
|
+
|
|
115
|
+
```tsx
|
|
116
|
+
{
|
|
117
|
+
render: () => <VirtualDataGridExample files={fixture_gridFiles} cursor={{}}/>
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### RowCursorKeyboard
|
|
122
|
+
|
|
123
|
+
```tsx
|
|
124
|
+
{
|
|
125
|
+
render: () => <VirtualDataGridExample files={fixture_gridFiles} cursor={{}}/>
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### RowCursorOwnedSelection
|
|
130
|
+
|
|
131
|
+
```tsx
|
|
132
|
+
{
|
|
133
|
+
render: function Example() {
|
|
134
|
+
const [gestures, setGestures] = useState<string[]>([]);
|
|
135
|
+
return (<div className="grid gap-3">
|
|
136
|
+
<p role="status" aria-label="Gestures">
|
|
137
|
+
{gestures.join(" ")}
|
|
138
|
+
</p>
|
|
139
|
+
<VirtualDataGridExample files={fixture_gridFiles} cursor={{
|
|
140
|
+
keyboard: false,
|
|
141
|
+
onSelectionChange: (change) => setGestures((current) => [...current, `${change.gesture}:${change.rowIds.join(",")}`]),
|
|
142
|
+
}}/>
|
|
143
|
+
</div>);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
```
|