virtualized-ui 0.0.1 → 0.1.1
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/README.md +192 -0
- package/dist/index.cjs +477 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +120 -0
- package/dist/index.d.ts +120 -0
- package/dist/index.js +471 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -4
- package/CLAUDE.MD +0 -54
- package/LICENSE +0 -21
package/README.md
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# virtualized-ui
|
|
2
|
+
|
|
3
|
+
Headless virtualized table primitives for React. Built on [TanStack Table](https://tanstack.com/table) and [TanStack Virtual](https://tanstack.com/virtual).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install virtualized-ui
|
|
9
|
+
# or
|
|
10
|
+
pnpm add virtualized-ui
|
|
11
|
+
# or
|
|
12
|
+
yarn add virtualized-ui
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
**Peer dependencies:** React 18+
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- **Virtualization** - Efficiently render thousands of rows
|
|
20
|
+
- **Sorting** - Single and multi-column sorting
|
|
21
|
+
- **Row Selection** - Checkbox or click-based selection
|
|
22
|
+
- **Row Expansion** - Expandable rows with variable heights
|
|
23
|
+
- **Column Resizing** - Drag to resize columns
|
|
24
|
+
- **Column Reordering** - Drag and drop columns
|
|
25
|
+
- **Keyboard Navigation** - Arrow keys, Home/End, Space/Enter
|
|
26
|
+
- **Infinite Scroll** - Load more data on scroll
|
|
27
|
+
- **Controlled & Uncontrolled** - Flexible state management
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
```tsx
|
|
32
|
+
import { useVirtualTable } from 'virtualized-ui';
|
|
33
|
+
import { createColumnHelper } from '@tanstack/react-table';
|
|
34
|
+
|
|
35
|
+
interface Person {
|
|
36
|
+
id: number;
|
|
37
|
+
name: string;
|
|
38
|
+
age: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const columnHelper = createColumnHelper<Person>();
|
|
42
|
+
|
|
43
|
+
const columns = [
|
|
44
|
+
columnHelper.accessor('name', { header: 'Name' }),
|
|
45
|
+
columnHelper.accessor('age', { header: 'Age' }),
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
function MyTable({ data }: { data: Person[] }) {
|
|
49
|
+
const {
|
|
50
|
+
table,
|
|
51
|
+
rows,
|
|
52
|
+
virtualItems,
|
|
53
|
+
totalSize,
|
|
54
|
+
containerRef,
|
|
55
|
+
} = useVirtualTable({
|
|
56
|
+
data,
|
|
57
|
+
columns,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<div ref={containerRef} style={{ height: 400, overflow: 'auto' }}>
|
|
62
|
+
<div style={{ height: totalSize, position: 'relative' }}>
|
|
63
|
+
{virtualItems.map((virtualRow) => {
|
|
64
|
+
const row = rows[virtualRow.index];
|
|
65
|
+
return (
|
|
66
|
+
<div
|
|
67
|
+
key={row.id}
|
|
68
|
+
style={{
|
|
69
|
+
position: 'absolute',
|
|
70
|
+
top: virtualRow.start,
|
|
71
|
+
height: virtualRow.size,
|
|
72
|
+
}}
|
|
73
|
+
>
|
|
74
|
+
{row.getVisibleCells().map((cell) => (
|
|
75
|
+
<span key={cell.id}>
|
|
76
|
+
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
|
77
|
+
</span>
|
|
78
|
+
))}
|
|
79
|
+
</div>
|
|
80
|
+
);
|
|
81
|
+
})}
|
|
82
|
+
</div>
|
|
83
|
+
</div>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## API
|
|
89
|
+
|
|
90
|
+
### `useVirtualTable<TData>(options)`
|
|
91
|
+
|
|
92
|
+
The main hook that combines TanStack Table with TanStack Virtual.
|
|
93
|
+
|
|
94
|
+
#### Options
|
|
95
|
+
|
|
96
|
+
| Option | Type | Default | Description |
|
|
97
|
+
|--------|------|---------|-------------|
|
|
98
|
+
| `data` | `TData[]` | required | The data array |
|
|
99
|
+
| `columns` | `ColumnDef<TData>[]` | required | Column definitions |
|
|
100
|
+
| `rowHeight` | `number` | `40` | Height of each row in pixels |
|
|
101
|
+
| `overscan` | `number` | `5` | Number of rows to render outside viewport |
|
|
102
|
+
| `enableRowSelection` | `boolean` | `false` | Enable row selection |
|
|
103
|
+
| `rowSelection` | `RowSelectionState` | - | Controlled selection state |
|
|
104
|
+
| `onRowSelectionChange` | `(state) => void` | - | Selection change callback |
|
|
105
|
+
| `enableSorting` | `boolean` | `false` | Enable column sorting |
|
|
106
|
+
| `enableMultiSort` | `boolean` | `false` | Enable multi-column sorting |
|
|
107
|
+
| `sorting` | `SortingState` | - | Controlled sorting state |
|
|
108
|
+
| `onSortingChange` | `(state) => void` | - | Sorting change callback |
|
|
109
|
+
| `enableRowExpansion` | `boolean` | `false` | Enable expandable rows |
|
|
110
|
+
| `expandedRowHeight` | `number` | `200` | Additional height for expanded rows |
|
|
111
|
+
| `expanded` | `ExpandedState` | - | Controlled expansion state |
|
|
112
|
+
| `onExpandedChange` | `(state) => void` | - | Expansion change callback |
|
|
113
|
+
| `enableColumnResizing` | `boolean` | `false` | Enable column resizing |
|
|
114
|
+
| `columnResizeMode` | `'onChange' \| 'onEnd'` | `'onChange'` | When to update sizes |
|
|
115
|
+
| `enableColumnReordering` | `boolean` | `false` | Enable column reordering |
|
|
116
|
+
| `enableKeyboardNavigation` | `boolean` | `false` | Enable keyboard navigation |
|
|
117
|
+
| `onScrollToBottom` | `() => void` | - | Called when scrolled near bottom |
|
|
118
|
+
| `scrollBottomThreshold` | `number` | `100` | Pixels from bottom to trigger callback |
|
|
119
|
+
| `getRowId` | `(row) => string` | - | Custom row ID function |
|
|
120
|
+
|
|
121
|
+
#### Returns
|
|
122
|
+
|
|
123
|
+
| Property | Type | Description |
|
|
124
|
+
|----------|------|-------------|
|
|
125
|
+
| `table` | `Table<TData>` | TanStack Table instance |
|
|
126
|
+
| `rows` | `Row<TData>[]` | Processed rows from table |
|
|
127
|
+
| `virtualizer` | `Virtualizer` | TanStack Virtual instance |
|
|
128
|
+
| `virtualItems` | `VirtualItem[]` | Currently visible virtual items |
|
|
129
|
+
| `totalSize` | `number` | Total scrollable height |
|
|
130
|
+
| `containerRef` | `RefObject<HTMLDivElement>` | Ref for scroll container |
|
|
131
|
+
| `handleScroll` | `() => void` | Scroll handler for infinite scroll |
|
|
132
|
+
| `handleKeyDown` | `(e) => void` | Keyboard event handler |
|
|
133
|
+
| `reorderColumn` | `(from, to) => void` | Reorder columns helper |
|
|
134
|
+
| `setFocusedRow` | `(index) => void` | Set focused row index |
|
|
135
|
+
| `rowSelection` | `RowSelectionState` | Current selection state |
|
|
136
|
+
| `sorting` | `SortingState` | Current sorting state |
|
|
137
|
+
| `expanded` | `ExpandedState` | Current expansion state |
|
|
138
|
+
| `columnSizing` | `ColumnSizingState` | Current column sizes |
|
|
139
|
+
| `columnOrder` | `ColumnOrderState` | Current column order |
|
|
140
|
+
| `focusedRowIndex` | `number` | Currently focused row |
|
|
141
|
+
|
|
142
|
+
## Examples
|
|
143
|
+
|
|
144
|
+
### With Row Expansion
|
|
145
|
+
|
|
146
|
+
```tsx
|
|
147
|
+
const { table, rows, virtualItems, totalSize, containerRef } = useVirtualTable({
|
|
148
|
+
data,
|
|
149
|
+
columns,
|
|
150
|
+
enableRowExpansion: true,
|
|
151
|
+
rowHeight: 52,
|
|
152
|
+
expandedRowHeight: 200,
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### With Infinite Scroll
|
|
157
|
+
|
|
158
|
+
```tsx
|
|
159
|
+
const { containerRef, handleScroll } = useVirtualTable({
|
|
160
|
+
data,
|
|
161
|
+
columns,
|
|
162
|
+
onScrollToBottom: () => fetchNextPage(),
|
|
163
|
+
scrollBottomThreshold: 500,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
<div ref={containerRef} onScroll={handleScroll} style={{ height: 400, overflow: 'auto' }}>
|
|
168
|
+
{/* ... */}
|
|
169
|
+
</div>
|
|
170
|
+
);
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### With Sorting
|
|
174
|
+
|
|
175
|
+
```tsx
|
|
176
|
+
const { table, sorting } = useVirtualTable({
|
|
177
|
+
data,
|
|
178
|
+
columns,
|
|
179
|
+
enableSorting: true,
|
|
180
|
+
enableMultiSort: true,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// In header:
|
|
184
|
+
<th onClick={header.column.getToggleSortingHandler()}>
|
|
185
|
+
{header.column.columnDef.header}
|
|
186
|
+
{header.column.getIsSorted() === 'asc' ? ' ↑' : header.column.getIsSorted() === 'desc' ? ' ↓' : ''}
|
|
187
|
+
</th>
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## License
|
|
191
|
+
|
|
192
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
var reactTable = require('@tanstack/react-table');
|
|
5
|
+
var reactVirtual = require('@tanstack/react-virtual');
|
|
6
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
7
|
+
|
|
8
|
+
// src/VirtualTable/VirtualTable.tsx
|
|
9
|
+
var DEFAULT_ROW_HEIGHT = 40;
|
|
10
|
+
var DEFAULT_EXPANDED_ROW_HEIGHT = 200;
|
|
11
|
+
var DEFAULT_OVERSCAN = 5;
|
|
12
|
+
var DEFAULT_SCROLL_THRESHOLD = 100;
|
|
13
|
+
function getColumnId(column) {
|
|
14
|
+
if ("id" in column && typeof column.id === "string") {
|
|
15
|
+
return column.id;
|
|
16
|
+
}
|
|
17
|
+
if ("accessorKey" in column && typeof column.accessorKey === "string") {
|
|
18
|
+
return column.accessorKey;
|
|
19
|
+
}
|
|
20
|
+
return "";
|
|
21
|
+
}
|
|
22
|
+
function useVirtualTable(options) {
|
|
23
|
+
const {
|
|
24
|
+
data,
|
|
25
|
+
columns,
|
|
26
|
+
rowHeight = DEFAULT_ROW_HEIGHT,
|
|
27
|
+
overscan = DEFAULT_OVERSCAN,
|
|
28
|
+
enableRowSelection = false,
|
|
29
|
+
rowSelection: controlledRowSelection,
|
|
30
|
+
onRowSelectionChange,
|
|
31
|
+
enableSorting = false,
|
|
32
|
+
sorting: controlledSorting,
|
|
33
|
+
onSortingChange,
|
|
34
|
+
enableMultiSort = false,
|
|
35
|
+
maxMultiSortColCount,
|
|
36
|
+
enableColumnResizing = false,
|
|
37
|
+
columnResizeMode = "onChange",
|
|
38
|
+
columnSizing: controlledColumnSizing,
|
|
39
|
+
onColumnSizingChange,
|
|
40
|
+
enableColumnReordering = false,
|
|
41
|
+
columnOrder: controlledColumnOrder,
|
|
42
|
+
onColumnOrderChange,
|
|
43
|
+
enableRowExpansion = false,
|
|
44
|
+
expanded: controlledExpanded,
|
|
45
|
+
onExpandedChange,
|
|
46
|
+
expandedRowHeight = DEFAULT_EXPANDED_ROW_HEIGHT,
|
|
47
|
+
getRowCanExpand,
|
|
48
|
+
getRowId,
|
|
49
|
+
enableKeyboardNavigation = false,
|
|
50
|
+
focusedRowIndex: controlledFocusedRowIndex,
|
|
51
|
+
onFocusedRowChange,
|
|
52
|
+
onScrollToBottom,
|
|
53
|
+
scrollBottomThreshold = DEFAULT_SCROLL_THRESHOLD
|
|
54
|
+
} = options;
|
|
55
|
+
const containerRef = react.useRef(null);
|
|
56
|
+
const defaultColumnOrder = react.useMemo(() => columns.map(getColumnId), [columns]);
|
|
57
|
+
const [internalRowSelection, setInternalRowSelection] = react.useState({});
|
|
58
|
+
const [internalSorting, setInternalSorting] = react.useState([]);
|
|
59
|
+
const [internalExpanded, setInternalExpanded] = react.useState({});
|
|
60
|
+
const [internalColumnSizing, setInternalColumnSizing] = react.useState({});
|
|
61
|
+
const [internalColumnOrder, setInternalColumnOrder] = react.useState([]);
|
|
62
|
+
const [internalFocusedRowIndex, setInternalFocusedRowIndex] = react.useState(-1);
|
|
63
|
+
const rowSelectionState = controlledRowSelection ?? internalRowSelection;
|
|
64
|
+
const sortingState = controlledSorting ?? internalSorting;
|
|
65
|
+
const expandedState = controlledExpanded ?? internalExpanded;
|
|
66
|
+
const columnSizingState = controlledColumnSizing ?? internalColumnSizing;
|
|
67
|
+
const columnOrderState = controlledColumnOrder ?? (internalColumnOrder.length ? internalColumnOrder : defaultColumnOrder);
|
|
68
|
+
const focusedRowIndex = controlledFocusedRowIndex ?? internalFocusedRowIndex;
|
|
69
|
+
const handleRowSelectionChange = react.useCallback(
|
|
70
|
+
(updater) => {
|
|
71
|
+
const newValue = typeof updater === "function" ? updater(rowSelectionState) : updater;
|
|
72
|
+
if (onRowSelectionChange) {
|
|
73
|
+
onRowSelectionChange(newValue);
|
|
74
|
+
} else {
|
|
75
|
+
setInternalRowSelection(newValue);
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
[rowSelectionState, onRowSelectionChange]
|
|
79
|
+
);
|
|
80
|
+
const handleSortingChange = react.useCallback(
|
|
81
|
+
(updater) => {
|
|
82
|
+
const newValue = typeof updater === "function" ? updater(sortingState) : updater;
|
|
83
|
+
if (onSortingChange) {
|
|
84
|
+
onSortingChange(newValue);
|
|
85
|
+
} else {
|
|
86
|
+
setInternalSorting(newValue);
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
[sortingState, onSortingChange]
|
|
90
|
+
);
|
|
91
|
+
const handleExpandedChange = react.useCallback(
|
|
92
|
+
(updater) => {
|
|
93
|
+
const newValue = typeof updater === "function" ? updater(expandedState) : updater;
|
|
94
|
+
if (onExpandedChange) {
|
|
95
|
+
onExpandedChange(newValue);
|
|
96
|
+
} else {
|
|
97
|
+
setInternalExpanded(newValue);
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
[expandedState, onExpandedChange]
|
|
101
|
+
);
|
|
102
|
+
const handleColumnSizingChange = react.useCallback(
|
|
103
|
+
(updater) => {
|
|
104
|
+
const newValue = typeof updater === "function" ? updater(columnSizingState) : updater;
|
|
105
|
+
if (onColumnSizingChange) {
|
|
106
|
+
onColumnSizingChange(newValue);
|
|
107
|
+
} else {
|
|
108
|
+
setInternalColumnSizing(newValue);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
[columnSizingState, onColumnSizingChange]
|
|
112
|
+
);
|
|
113
|
+
const handleColumnOrderChange = react.useCallback(
|
|
114
|
+
(updater) => {
|
|
115
|
+
const newValue = typeof updater === "function" ? updater(columnOrderState) : updater;
|
|
116
|
+
if (onColumnOrderChange) {
|
|
117
|
+
onColumnOrderChange(newValue);
|
|
118
|
+
} else {
|
|
119
|
+
setInternalColumnOrder(newValue);
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
[columnOrderState, onColumnOrderChange]
|
|
123
|
+
);
|
|
124
|
+
const table = reactTable.useReactTable({
|
|
125
|
+
data,
|
|
126
|
+
columns,
|
|
127
|
+
getCoreRowModel: reactTable.getCoreRowModel(),
|
|
128
|
+
getSortedRowModel: enableSorting ? reactTable.getSortedRowModel() : void 0,
|
|
129
|
+
getExpandedRowModel: enableRowExpansion ? reactTable.getExpandedRowModel() : void 0,
|
|
130
|
+
enableRowSelection,
|
|
131
|
+
enableSorting,
|
|
132
|
+
enableMultiSort,
|
|
133
|
+
maxMultiSortColCount,
|
|
134
|
+
enableColumnResizing,
|
|
135
|
+
columnResizeMode,
|
|
136
|
+
getRowCanExpand: enableRowExpansion ? getRowCanExpand ?? (() => true) : void 0,
|
|
137
|
+
state: {
|
|
138
|
+
rowSelection: rowSelectionState,
|
|
139
|
+
sorting: sortingState,
|
|
140
|
+
expanded: expandedState,
|
|
141
|
+
columnSizing: columnSizingState,
|
|
142
|
+
columnOrder: enableColumnReordering ? columnOrderState : void 0
|
|
143
|
+
},
|
|
144
|
+
onRowSelectionChange: handleRowSelectionChange,
|
|
145
|
+
onSortingChange: handleSortingChange,
|
|
146
|
+
onExpandedChange: handleExpandedChange,
|
|
147
|
+
onColumnSizingChange: handleColumnSizingChange,
|
|
148
|
+
onColumnOrderChange: enableColumnReordering ? handleColumnOrderChange : void 0,
|
|
149
|
+
getRowId
|
|
150
|
+
});
|
|
151
|
+
const { rows } = table.getRowModel();
|
|
152
|
+
const estimateSize = react.useCallback(
|
|
153
|
+
(index) => {
|
|
154
|
+
if (!enableRowExpansion) return rowHeight;
|
|
155
|
+
const row = rows[index];
|
|
156
|
+
return row?.getIsExpanded() ? rowHeight + expandedRowHeight : rowHeight;
|
|
157
|
+
},
|
|
158
|
+
[rows, rowHeight, expandedRowHeight, enableRowExpansion]
|
|
159
|
+
);
|
|
160
|
+
const virtualizer = reactVirtual.useVirtualizer({
|
|
161
|
+
count: rows.length,
|
|
162
|
+
getScrollElement: () => containerRef.current,
|
|
163
|
+
estimateSize,
|
|
164
|
+
overscan
|
|
165
|
+
});
|
|
166
|
+
const virtualItems = virtualizer.getVirtualItems();
|
|
167
|
+
const totalSize = virtualizer.getTotalSize();
|
|
168
|
+
const handleScroll = react.useCallback(() => {
|
|
169
|
+
if (!onScrollToBottom || !containerRef.current) return;
|
|
170
|
+
const { scrollTop, scrollHeight, clientHeight } = containerRef.current;
|
|
171
|
+
const distanceFromBottom = scrollHeight - scrollTop - clientHeight;
|
|
172
|
+
if (distanceFromBottom < scrollBottomThreshold) {
|
|
173
|
+
onScrollToBottom();
|
|
174
|
+
}
|
|
175
|
+
}, [onScrollToBottom, scrollBottomThreshold]);
|
|
176
|
+
const reorderColumn = react.useCallback(
|
|
177
|
+
(draggedColumnId, targetColumnId) => {
|
|
178
|
+
const newOrder = [...columnOrderState];
|
|
179
|
+
const draggedIndex = newOrder.indexOf(draggedColumnId);
|
|
180
|
+
const targetIndex = newOrder.indexOf(targetColumnId);
|
|
181
|
+
if (draggedIndex === -1 || targetIndex === -1) return;
|
|
182
|
+
newOrder.splice(draggedIndex, 1);
|
|
183
|
+
newOrder.splice(targetIndex, 0, draggedColumnId);
|
|
184
|
+
handleColumnOrderChange(newOrder);
|
|
185
|
+
},
|
|
186
|
+
[columnOrderState, handleColumnOrderChange]
|
|
187
|
+
);
|
|
188
|
+
const setFocusedRow = react.useCallback(
|
|
189
|
+
(index) => {
|
|
190
|
+
const clampedIndex = Math.max(-1, Math.min(index, rows.length - 1));
|
|
191
|
+
if (onFocusedRowChange) {
|
|
192
|
+
onFocusedRowChange(clampedIndex);
|
|
193
|
+
} else {
|
|
194
|
+
setInternalFocusedRowIndex(clampedIndex);
|
|
195
|
+
}
|
|
196
|
+
if (clampedIndex >= 0) {
|
|
197
|
+
virtualizer.scrollToIndex(clampedIndex, { align: "auto" });
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
[rows.length, onFocusedRowChange, virtualizer]
|
|
201
|
+
);
|
|
202
|
+
const handleKeyDown = react.useCallback(
|
|
203
|
+
(e) => {
|
|
204
|
+
if (!enableKeyboardNavigation) return;
|
|
205
|
+
const currentIndex = focusedRowIndex;
|
|
206
|
+
switch (e.key) {
|
|
207
|
+
case "ArrowDown":
|
|
208
|
+
e.preventDefault();
|
|
209
|
+
setFocusedRow(currentIndex + 1);
|
|
210
|
+
break;
|
|
211
|
+
case "ArrowUp":
|
|
212
|
+
e.preventDefault();
|
|
213
|
+
setFocusedRow(currentIndex - 1);
|
|
214
|
+
break;
|
|
215
|
+
case "Home":
|
|
216
|
+
e.preventDefault();
|
|
217
|
+
setFocusedRow(0);
|
|
218
|
+
break;
|
|
219
|
+
case "End":
|
|
220
|
+
e.preventDefault();
|
|
221
|
+
setFocusedRow(rows.length - 1);
|
|
222
|
+
break;
|
|
223
|
+
case "Enter":
|
|
224
|
+
if (currentIndex >= 0 && enableRowExpansion) {
|
|
225
|
+
e.preventDefault();
|
|
226
|
+
const row = rows[currentIndex];
|
|
227
|
+
if (row?.getCanExpand()) {
|
|
228
|
+
row.toggleExpanded();
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
break;
|
|
232
|
+
case " ":
|
|
233
|
+
if (currentIndex >= 0 && enableRowSelection) {
|
|
234
|
+
e.preventDefault();
|
|
235
|
+
const row = rows[currentIndex];
|
|
236
|
+
row?.toggleSelected();
|
|
237
|
+
}
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
},
|
|
241
|
+
[
|
|
242
|
+
enableKeyboardNavigation,
|
|
243
|
+
focusedRowIndex,
|
|
244
|
+
rows,
|
|
245
|
+
enableRowExpansion,
|
|
246
|
+
enableRowSelection,
|
|
247
|
+
setFocusedRow
|
|
248
|
+
]
|
|
249
|
+
);
|
|
250
|
+
return {
|
|
251
|
+
table,
|
|
252
|
+
rows,
|
|
253
|
+
virtualizer,
|
|
254
|
+
virtualItems,
|
|
255
|
+
totalSize,
|
|
256
|
+
containerRef,
|
|
257
|
+
handleScroll,
|
|
258
|
+
handleKeyDown,
|
|
259
|
+
reorderColumn,
|
|
260
|
+
setFocusedRow,
|
|
261
|
+
// Expose state for consumers
|
|
262
|
+
rowSelection: rowSelectionState,
|
|
263
|
+
sorting: sortingState,
|
|
264
|
+
expanded: expandedState,
|
|
265
|
+
columnSizing: columnSizingState,
|
|
266
|
+
columnOrder: columnOrderState,
|
|
267
|
+
focusedRowIndex
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
var DEFAULT_HEIGHT = 400;
|
|
271
|
+
function VirtualTable(props) {
|
|
272
|
+
const {
|
|
273
|
+
height = DEFAULT_HEIGHT,
|
|
274
|
+
stickyHeader = true,
|
|
275
|
+
renderExpandedRow,
|
|
276
|
+
className,
|
|
277
|
+
style,
|
|
278
|
+
...tableOptions
|
|
279
|
+
} = props;
|
|
280
|
+
const {
|
|
281
|
+
rows,
|
|
282
|
+
virtualizer,
|
|
283
|
+
virtualItems,
|
|
284
|
+
totalSize,
|
|
285
|
+
containerRef,
|
|
286
|
+
handleScroll,
|
|
287
|
+
handleKeyDown,
|
|
288
|
+
table,
|
|
289
|
+
reorderColumn,
|
|
290
|
+
setFocusedRow,
|
|
291
|
+
focusedRowIndex
|
|
292
|
+
} = useVirtualTable(tableOptions);
|
|
293
|
+
const headerGroups = table.getHeaderGroups();
|
|
294
|
+
const enableRowExpansion = tableOptions.enableRowExpansion;
|
|
295
|
+
const enableColumnResizing = tableOptions.enableColumnResizing;
|
|
296
|
+
const enableColumnReordering = tableOptions.enableColumnReordering;
|
|
297
|
+
const enableKeyboardNavigation = tableOptions.enableKeyboardNavigation;
|
|
298
|
+
const [draggedColumnId, setDraggedColumnId] = react.useState(null);
|
|
299
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
300
|
+
"div",
|
|
301
|
+
{
|
|
302
|
+
ref: containerRef,
|
|
303
|
+
className,
|
|
304
|
+
tabIndex: enableKeyboardNavigation ? 0 : void 0,
|
|
305
|
+
style: {
|
|
306
|
+
height,
|
|
307
|
+
overflow: "auto",
|
|
308
|
+
position: "relative",
|
|
309
|
+
outline: "none",
|
|
310
|
+
...style
|
|
311
|
+
},
|
|
312
|
+
onScroll: handleScroll,
|
|
313
|
+
onKeyDown: enableKeyboardNavigation ? handleKeyDown : void 0,
|
|
314
|
+
children: /* @__PURE__ */ jsxRuntime.jsxs(
|
|
315
|
+
"table",
|
|
316
|
+
{
|
|
317
|
+
style: {
|
|
318
|
+
display: "grid",
|
|
319
|
+
width: "100%"
|
|
320
|
+
},
|
|
321
|
+
children: [
|
|
322
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
323
|
+
"thead",
|
|
324
|
+
{
|
|
325
|
+
style: {
|
|
326
|
+
display: "grid",
|
|
327
|
+
position: stickyHeader ? "sticky" : "relative",
|
|
328
|
+
top: 0,
|
|
329
|
+
zIndex: 1
|
|
330
|
+
},
|
|
331
|
+
children: headerGroups.map((headerGroup) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
332
|
+
"tr",
|
|
333
|
+
{
|
|
334
|
+
style: {
|
|
335
|
+
display: "flex",
|
|
336
|
+
width: "100%"
|
|
337
|
+
},
|
|
338
|
+
children: headerGroup.headers.map((header) => /* @__PURE__ */ jsxRuntime.jsxs(
|
|
339
|
+
"th",
|
|
340
|
+
{
|
|
341
|
+
draggable: enableColumnReordering && !header.isPlaceholder,
|
|
342
|
+
"data-dragging": draggedColumnId === header.column.id || void 0,
|
|
343
|
+
onDragStart: enableColumnReordering ? (e) => {
|
|
344
|
+
setDraggedColumnId(header.column.id);
|
|
345
|
+
e.dataTransfer.effectAllowed = "move";
|
|
346
|
+
} : void 0,
|
|
347
|
+
onDragOver: enableColumnReordering ? (e) => {
|
|
348
|
+
e.preventDefault();
|
|
349
|
+
e.dataTransfer.dropEffect = "move";
|
|
350
|
+
} : void 0,
|
|
351
|
+
onDrop: enableColumnReordering ? (e) => {
|
|
352
|
+
e.preventDefault();
|
|
353
|
+
if (draggedColumnId && draggedColumnId !== header.column.id) {
|
|
354
|
+
reorderColumn(draggedColumnId, header.column.id);
|
|
355
|
+
}
|
|
356
|
+
} : void 0,
|
|
357
|
+
onDragEnd: enableColumnReordering ? () => setDraggedColumnId(null) : void 0,
|
|
358
|
+
style: {
|
|
359
|
+
flex: `0 0 ${header.getSize()}px`,
|
|
360
|
+
cursor: enableColumnReordering ? "grab" : header.column.getCanSort() ? "pointer" : "default",
|
|
361
|
+
position: "relative",
|
|
362
|
+
opacity: draggedColumnId === header.column.id ? 0.5 : 1
|
|
363
|
+
},
|
|
364
|
+
onClick: !enableColumnReordering ? header.column.getToggleSortingHandler() : void 0,
|
|
365
|
+
children: [
|
|
366
|
+
header.isPlaceholder ? null : reactTable.flexRender(header.column.columnDef.header, header.getContext()),
|
|
367
|
+
header.column.getIsSorted() === "asc" && " \u2191",
|
|
368
|
+
header.column.getIsSorted() === "desc" && " \u2193",
|
|
369
|
+
enableColumnResizing && header.column.getCanResize() && /* @__PURE__ */ jsxRuntime.jsx(
|
|
370
|
+
"div",
|
|
371
|
+
{
|
|
372
|
+
onMouseDown: header.getResizeHandler(),
|
|
373
|
+
onTouchStart: header.getResizeHandler(),
|
|
374
|
+
onClick: (e) => e.stopPropagation(),
|
|
375
|
+
className: "virtual-table-resizer",
|
|
376
|
+
"data-resizing": header.column.getIsResizing() || void 0,
|
|
377
|
+
style: {
|
|
378
|
+
position: "absolute",
|
|
379
|
+
right: 0,
|
|
380
|
+
top: 0,
|
|
381
|
+
height: "100%",
|
|
382
|
+
width: "5px",
|
|
383
|
+
cursor: "col-resize",
|
|
384
|
+
userSelect: "none",
|
|
385
|
+
touchAction: "none",
|
|
386
|
+
background: header.column.getIsResizing() ? "rgba(0, 0, 0, 0.5)" : "transparent"
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
)
|
|
390
|
+
]
|
|
391
|
+
},
|
|
392
|
+
header.id
|
|
393
|
+
))
|
|
394
|
+
},
|
|
395
|
+
headerGroup.id
|
|
396
|
+
))
|
|
397
|
+
}
|
|
398
|
+
),
|
|
399
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
400
|
+
"tbody",
|
|
401
|
+
{
|
|
402
|
+
style: {
|
|
403
|
+
display: "grid",
|
|
404
|
+
height: `${totalSize}px`,
|
|
405
|
+
position: "relative"
|
|
406
|
+
},
|
|
407
|
+
children: virtualItems.map((virtualRow) => {
|
|
408
|
+
const row = rows[virtualRow.index];
|
|
409
|
+
const isExpanded = row.getIsExpanded();
|
|
410
|
+
const canExpand = enableRowExpansion && row.getCanExpand();
|
|
411
|
+
const isFocused = enableKeyboardNavigation && virtualRow.index === focusedRowIndex;
|
|
412
|
+
const handleRowClick = () => {
|
|
413
|
+
if (enableKeyboardNavigation) {
|
|
414
|
+
setFocusedRow(virtualRow.index);
|
|
415
|
+
}
|
|
416
|
+
if (canExpand) {
|
|
417
|
+
row.toggleExpanded();
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
421
|
+
"tr",
|
|
422
|
+
{
|
|
423
|
+
"data-index": virtualRow.index,
|
|
424
|
+
"data-expanded": isExpanded || void 0,
|
|
425
|
+
"data-focused": isFocused || void 0,
|
|
426
|
+
ref: (node) => virtualizer.measureElement(node),
|
|
427
|
+
style: {
|
|
428
|
+
display: "block",
|
|
429
|
+
position: "absolute",
|
|
430
|
+
transform: `translateY(${virtualRow.start}px)`,
|
|
431
|
+
width: "100%",
|
|
432
|
+
cursor: canExpand || enableKeyboardNavigation ? "pointer" : "default"
|
|
433
|
+
},
|
|
434
|
+
onClick: handleRowClick,
|
|
435
|
+
children: [
|
|
436
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
437
|
+
"div",
|
|
438
|
+
{
|
|
439
|
+
style: {
|
|
440
|
+
display: "flex",
|
|
441
|
+
width: "100%"
|
|
442
|
+
},
|
|
443
|
+
children: row.getVisibleCells().map((cell) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
444
|
+
"td",
|
|
445
|
+
{
|
|
446
|
+
style: {
|
|
447
|
+
flex: `0 0 ${cell.column.getSize()}px`
|
|
448
|
+
},
|
|
449
|
+
children: reactTable.flexRender(cell.column.columnDef.cell, cell.getContext())
|
|
450
|
+
},
|
|
451
|
+
cell.id
|
|
452
|
+
))
|
|
453
|
+
}
|
|
454
|
+
),
|
|
455
|
+
isExpanded && renderExpandedRow && /* @__PURE__ */ jsxRuntime.jsx("div", { onClick: (e) => e.stopPropagation(), style: { width: "100%" }, children: renderExpandedRow(row) })
|
|
456
|
+
]
|
|
457
|
+
},
|
|
458
|
+
row.id
|
|
459
|
+
);
|
|
460
|
+
})
|
|
461
|
+
}
|
|
462
|
+
)
|
|
463
|
+
]
|
|
464
|
+
}
|
|
465
|
+
)
|
|
466
|
+
}
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
Object.defineProperty(exports, "createColumnHelper", {
|
|
471
|
+
enumerable: true,
|
|
472
|
+
get: function () { return reactTable.createColumnHelper; }
|
|
473
|
+
});
|
|
474
|
+
exports.VirtualTable = VirtualTable;
|
|
475
|
+
exports.useVirtualTable = useVirtualTable;
|
|
476
|
+
//# sourceMappingURL=index.cjs.map
|
|
477
|
+
//# sourceMappingURL=index.cjs.map
|