@calcifer-design/ui 0.4.0 → 0.6.0
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/components/Breadcrumb/Breadcrumb.d.ts +24 -0
- package/dist/components/Breadcrumb/Breadcrumb.js +48 -0
- package/dist/components/Breadcrumb/Breadcrumb.module.js +10 -0
- package/dist/components/Breadcrumb/Breadcrumb_module.css +49 -0
- package/dist/components/CardList/CardList.d.ts +30 -0
- package/dist/components/CardList/CardList.js +35 -0
- package/dist/components/CardList/CardList.module.js +7 -0
- package/dist/components/CardList/CardList_module.css +20 -0
- package/dist/components/DataGrid/DataGrid.d.ts +3 -0
- package/dist/components/DataGrid/DataGrid.js +436 -0
- package/dist/components/DataGrid/DataGrid.module.js +36 -0
- package/dist/components/DataGrid/DataGrid_module.css +367 -0
- package/dist/components/DataGrid/GridCardHeader.d.ts +16 -0
- package/dist/components/DataGrid/GridCardHeader.js +40 -0
- package/dist/components/DataGrid/GridCheckbox.d.ts +15 -0
- package/dist/components/DataGrid/GridCheckbox.js +25 -0
- package/dist/components/DataGrid/GridHeaderCell.d.ts +14 -0
- package/dist/components/DataGrid/GridHeaderCell.js +29 -0
- package/dist/components/DataGrid/GridHeaderRow.d.ts +35 -0
- package/dist/components/DataGrid/GridHeaderRow.js +56 -0
- package/dist/components/DataGrid/GridRow.d.ts +69 -0
- package/dist/components/DataGrid/GridRow.js +139 -0
- package/dist/components/DataGrid/GridStateRows.d.ts +24 -0
- package/dist/components/DataGrid/GridStateRows.js +59 -0
- package/dist/components/DataGrid/columns.d.ts +40 -0
- package/dist/components/DataGrid/columns.js +37 -0
- package/dist/components/DataGrid/labels.d.ts +20 -0
- package/dist/components/DataGrid/labels.js +25 -0
- package/dist/components/DataGrid/types.d.ts +88 -0
- package/dist/components/DataGrid/types.js +0 -0
- package/dist/components/DataGrid/useGridEntries.d.ts +17 -0
- package/dist/components/DataGrid/useGridEntries.js +40 -0
- package/dist/components/DataGrid/useGridHighlight.d.ts +16 -0
- package/dist/components/DataGrid/useGridHighlight.js +24 -0
- package/dist/components/DataGrid/useGridKeyboard.d.ts +50 -0
- package/dist/components/DataGrid/useGridKeyboard.js +242 -0
- package/dist/components/DataGrid/useGridMetrics.d.ts +13 -0
- package/dist/components/DataGrid/useGridMetrics.js +35 -0
- package/dist/components/DataGrid/useLatest.d.ts +7 -0
- package/dist/components/DataGrid/useLatest.js +9 -0
- package/dist/components/DataGrid/useLoadMore.d.ts +30 -0
- package/dist/components/DataGrid/useLoadMore.js +39 -0
- package/dist/components/DataGrid/useRowExpansion.d.ts +14 -0
- package/dist/components/DataGrid/useRowExpansion.js +21 -0
- package/dist/components/DataGrid/useRowSelection.d.ts +43 -0
- package/dist/components/DataGrid/useRowSelection.js +107 -0
- package/dist/components/DatePicker/Calendar.d.ts +19 -0
- package/dist/components/DatePicker/Calendar.js +207 -0
- package/dist/components/DatePicker/DatePicker.d.ts +72 -0
- package/dist/components/DatePicker/DatePicker.js +116 -0
- package/dist/components/DatePicker/DatePicker.module.js +14 -0
- package/dist/components/DatePicker/DatePicker_module.css +112 -0
- package/dist/components/DatePicker/calendarDate.d.ts +46 -0
- package/dist/components/DatePicker/calendarDate.js +117 -0
- package/dist/components/Descriptions/Descriptions.d.ts +25 -0
- package/dist/components/Descriptions/Descriptions.js +44 -0
- package/dist/components/Descriptions/Descriptions.module.js +8 -0
- package/dist/components/Descriptions/Descriptions_module.css +47 -0
- package/dist/components/Field/Field.d.ts +1 -1
- package/dist/components/Field/Field.js +7 -1
- package/dist/components/Field/PickerField.d.ts +41 -0
- package/dist/components/Field/PickerField.js +115 -0
- package/dist/components/Field/PickerField.module.js +8 -0
- package/dist/components/Field/PickerField_module.css +36 -0
- package/dist/components/FilterBar/FilterBar.d.ts +55 -0
- package/dist/components/FilterBar/FilterBar.js +138 -0
- package/dist/components/FilterBar/FilterBar.module.js +13 -0
- package/dist/components/FilterBar/FilterBar_module.css +98 -0
- package/dist/components/Form/Form.d.ts +48 -0
- package/dist/components/Form/Form.js +38 -0
- package/dist/components/Form/Form.module.js +5 -0
- package/dist/components/Form/FormContext.d.ts +15 -0
- package/dist/components/Form/FormContext.js +20 -0
- package/dist/components/Form/Form_module.css +6 -0
- package/dist/components/PageShell/PageShell.d.ts +27 -0
- package/dist/components/PageShell/PageShell.js +35 -0
- package/dist/components/PageShell/PageShell.module.js +9 -0
- package/dist/components/PageShell/PageShell_module.css +56 -0
- package/dist/components/Segmented/Segmented.d.ts +49 -0
- package/dist/components/Segmented/Segmented.js +55 -0
- package/dist/components/Segmented/Segmented.module.js +7 -0
- package/dist/components/Segmented/Segmented_module.css +75 -0
- package/dist/components/Steps/Steps.d.ts +30 -0
- package/dist/components/Steps/Steps.js +63 -0
- package/dist/components/Steps/Steps.module.js +10 -0
- package/dist/components/Steps/Steps_module.css +126 -0
- package/dist/components/TextArea/TextArea.d.ts +16 -0
- package/dist/components/TextArea/TextArea.js +28 -0
- package/dist/components/TextArea/TextArea.module.js +5 -0
- package/dist/components/TextArea/TextArea_module.css +12 -0
- package/dist/components/TimePicker/TimePicker.d.ts +19 -0
- package/dist/components/TimePicker/TimePicker.js +17 -0
- package/dist/components/Timeline/Timeline.d.ts +39 -0
- package/dist/components/Timeline/Timeline.js +109 -0
- package/dist/components/Timeline/Timeline.module.js +12 -0
- package/dist/components/Timeline/Timeline_module.css +99 -0
- package/dist/components/Toolbar/InsideToolbarContext.d.ts +6 -0
- package/dist/components/Toolbar/InsideToolbarContext.js +3 -0
- package/dist/components/Toolbar/Toolbar.d.ts +46 -0
- package/dist/components/Toolbar/Toolbar.js +43 -0
- package/dist/components/Toolbar/Toolbar.module.js +7 -0
- package/dist/components/Toolbar/Toolbar_module.css +26 -0
- package/dist/components/Tree/Tree.d.ts +81 -0
- package/dist/components/Tree/Tree.js +232 -0
- package/dist/components/Tree/Tree.module.js +12 -0
- package/dist/components/Tree/Tree_module.css +124 -0
- package/dist/components/Tree/treeIndex.d.ts +23 -0
- package/dist/components/Tree/treeIndex.js +33 -0
- package/dist/components/TreeSelect/TreeSelect.d.ts +61 -0
- package/dist/components/TreeSelect/TreeSelect.js +117 -0
- package/dist/components/TreeSelect/TreeSelect.module.js +7 -0
- package/dist/components/TreeSelect/TreeSelect_module.css +22 -0
- package/dist/components/Upload/Upload.d.ts +66 -0
- package/dist/components/Upload/Upload.js +287 -0
- package/dist/components/Upload/Upload.module.js +12 -0
- package/dist/components/Upload/Upload_module.css +96 -0
- package/dist/components/ViewField/ViewField.d.ts +26 -0
- package/dist/components/ViewField/ViewField.js +29 -0
- package/dist/components/ViewField/ViewField.module.js +6 -0
- package/dist/components/ViewField/ViewField_module.css +17 -0
- package/dist/hooks/useFormReset.d.ts +10 -0
- package/dist/hooks/useFormReset.js +34 -0
- package/dist/icons/CalendarIcon.d.ts +2 -0
- package/dist/icons/CalendarIcon.js +26 -0
- package/dist/icons/ChevronRightIcon.d.ts +6 -0
- package/dist/icons/ChevronRightIcon.js +17 -0
- package/dist/icons/RefreshIcon.d.ts +6 -0
- package/dist/icons/RefreshIcon.js +22 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +19 -0
- package/dist/styles/pair.module.js +6 -0
- package/dist/styles/pair_module.css +18 -0
- package/package.json +4 -3
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { useLayoutEffect, useRef, useState } from "react";
|
|
2
|
+
const cellControlSelector = 'button, a[href], input, select, textarea, [role="button"], [tabindex]';
|
|
3
|
+
const navigableCellSelector = 'thead th, tbody tr[data-index] > td';
|
|
4
|
+
function isHiddenControl(control) {
|
|
5
|
+
return null !== control.closest('[aria-hidden="true"]');
|
|
6
|
+
}
|
|
7
|
+
function isNavigableCell(grid, element) {
|
|
8
|
+
return element?.matches(navigableCellSelector) === true && element.closest('table') === grid;
|
|
9
|
+
}
|
|
10
|
+
function ownCellOf(grid, element) {
|
|
11
|
+
let cell = element.closest('td, th');
|
|
12
|
+
while(null !== cell && cell.closest('table') !== grid)cell = cell.parentElement?.closest('td, th') ?? null;
|
|
13
|
+
return cell;
|
|
14
|
+
}
|
|
15
|
+
function isUnfocusableControl(control) {
|
|
16
|
+
return isHiddenControl(control) || control.matches(':disabled') || null !== control.closest('[hidden], [inert]');
|
|
17
|
+
}
|
|
18
|
+
function controlsOf(grid, cell) {
|
|
19
|
+
return [
|
|
20
|
+
...cell.querySelectorAll(cellControlSelector)
|
|
21
|
+
].filter((control)=>control.closest('[role="grid"]') === grid ? !isUnfocusableControl(control) : 0 === control.tabIndex);
|
|
22
|
+
}
|
|
23
|
+
function isFromCell(event) {
|
|
24
|
+
return event.target instanceof Node && event.currentTarget.contains(event.target);
|
|
25
|
+
}
|
|
26
|
+
function cellAt(grid, position) {
|
|
27
|
+
const ariaRowIndex = String(position.rowIndex + 1);
|
|
28
|
+
const row = [
|
|
29
|
+
...grid.rows
|
|
30
|
+
].find((candidate)=>candidate.getAttribute('aria-rowindex') === ariaRowIndex);
|
|
31
|
+
if (void 0 === row) return null;
|
|
32
|
+
const column = position.columnIndex + 1;
|
|
33
|
+
for (const cell of row.cells){
|
|
34
|
+
const firstColumn = Number(cell.getAttribute('aria-colindex'));
|
|
35
|
+
if (column >= firstColumn && column < firstColumn + cell.colSpan) return cell;
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
function covers(start, columnSpan, target) {
|
|
40
|
+
return target.rowIndex === start.rowIndex && target.columnIndex >= start.columnIndex && target.columnIndex < start.columnIndex + columnSpan;
|
|
41
|
+
}
|
|
42
|
+
function isSamePosition(first, second) {
|
|
43
|
+
return first.rowIndex === second.rowIndex && first.columnIndex === second.columnIndex;
|
|
44
|
+
}
|
|
45
|
+
function clamp(value, lowest, highest) {
|
|
46
|
+
return Math.min(Math.max(value, lowest), Math.max(highest, lowest));
|
|
47
|
+
}
|
|
48
|
+
function syncControlTabIndexes(grid, enteredCell) {
|
|
49
|
+
for (const control of grid.querySelectorAll(cellControlSelector)){
|
|
50
|
+
if (control.closest('[role="grid"]') !== grid) continue;
|
|
51
|
+
const cell = ownCellOf(grid, control);
|
|
52
|
+
if (null !== cell && cell !== control && isNavigableCell(grid, cell)) {
|
|
53
|
+
if (isHiddenControl(control)) {
|
|
54
|
+
control.tabIndex = -1;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
control.tabIndex = cell === enteredCell ? 0 : -1;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function useGridKeyboard({ rowCount, columnCount, getPageSize, onActivate, onContextMenu, scrollToEntry }) {
|
|
62
|
+
const gridRef = useRef(null);
|
|
63
|
+
const [heldCell, setHeldCell] = useState({
|
|
64
|
+
rowIndex: 0,
|
|
65
|
+
columnIndex: 0
|
|
66
|
+
});
|
|
67
|
+
const [inside, setInside] = useState(false);
|
|
68
|
+
const hasFocusRef = useRef(false);
|
|
69
|
+
const pendingFocusRef = useRef(false);
|
|
70
|
+
const lastRow = rowCount - 1;
|
|
71
|
+
const lastColumn = columnCount - 1;
|
|
72
|
+
const clampToGrid = (position)=>({
|
|
73
|
+
rowIndex: clamp(position.rowIndex, 0, lastRow),
|
|
74
|
+
columnIndex: clamp(position.columnIndex, 0, lastColumn)
|
|
75
|
+
});
|
|
76
|
+
const focusedCell = clampToGrid(heldCell);
|
|
77
|
+
if (!isSamePosition(focusedCell, heldCell)) setHeldCell(focusedCell);
|
|
78
|
+
const enteredCell = inside ? focusedCell : null;
|
|
79
|
+
useLayoutEffect(()=>{
|
|
80
|
+
const grid = gridRef.current;
|
|
81
|
+
if (null === grid) return;
|
|
82
|
+
const cell = cellAt(grid, focusedCell);
|
|
83
|
+
const active = document.activeElement;
|
|
84
|
+
const focusAstray = hasFocusRef.current && active !== cell && (!grid.contains(active) || isNavigableCell(grid, active));
|
|
85
|
+
if (!pendingFocusRef.current && !focusAstray) return;
|
|
86
|
+
if (null !== cell) {
|
|
87
|
+
pendingFocusRef.current = false;
|
|
88
|
+
cell.focus();
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
const enteredRow = enteredCell?.rowIndex;
|
|
92
|
+
const enteredColumn = enteredCell?.columnIndex;
|
|
93
|
+
useLayoutEffect(()=>{
|
|
94
|
+
const grid = gridRef.current;
|
|
95
|
+
if (null === grid) return;
|
|
96
|
+
const findEnteredCell = ()=>void 0 === enteredRow || void 0 === enteredColumn ? null : cellAt(grid, {
|
|
97
|
+
rowIndex: enteredRow,
|
|
98
|
+
columnIndex: enteredColumn
|
|
99
|
+
});
|
|
100
|
+
syncControlTabIndexes(grid, findEnteredCell());
|
|
101
|
+
const observer = new MutationObserver(()=>syncControlTabIndexes(grid, findEnteredCell()));
|
|
102
|
+
observer.observe(grid, {
|
|
103
|
+
childList: true,
|
|
104
|
+
subtree: true
|
|
105
|
+
});
|
|
106
|
+
return ()=>observer.disconnect();
|
|
107
|
+
}, [
|
|
108
|
+
enteredRow,
|
|
109
|
+
enteredColumn
|
|
110
|
+
]);
|
|
111
|
+
const moveTo = (target)=>{
|
|
112
|
+
const next = clampToGrid(target);
|
|
113
|
+
if (isSamePosition(next, focusedCell)) return;
|
|
114
|
+
if (next.rowIndex > 0) scrollToEntry(next.rowIndex - 1);
|
|
115
|
+
else if (focusedCell.rowIndex > 0) scrollToEntry(0);
|
|
116
|
+
pendingFocusRef.current = true;
|
|
117
|
+
setHeldCell(next);
|
|
118
|
+
};
|
|
119
|
+
const enter = (controls)=>{
|
|
120
|
+
for (const control of controls){
|
|
121
|
+
control.focus();
|
|
122
|
+
if (document.activeElement === control) return;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const handleKeyDown = (position, columnSpan, event)=>{
|
|
126
|
+
const cell = event.currentTarget;
|
|
127
|
+
if (!isFromCell(event)) return;
|
|
128
|
+
if (event.target !== cell) {
|
|
129
|
+
if ('Escape' === event.key && !event.defaultPrevented) {
|
|
130
|
+
event.preventDefault();
|
|
131
|
+
setInside(false);
|
|
132
|
+
cell.focus();
|
|
133
|
+
}
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const { key, ctrlKey, shiftKey, altKey, metaKey } = event;
|
|
137
|
+
const { rowIndex } = position;
|
|
138
|
+
if ('ContextMenu' === key || 'F10' === key && shiftKey) return void onContextMenu(rowIndex, position.columnIndex, event);
|
|
139
|
+
if ('Enter' === key || ' ' === key) {
|
|
140
|
+
onActivate(rowIndex, position.columnIndex, event);
|
|
141
|
+
if (' ' === key) return void event.preventDefault();
|
|
142
|
+
if (event.defaultPrevented) return;
|
|
143
|
+
}
|
|
144
|
+
if ('Enter' === key || 'F2' === key) {
|
|
145
|
+
const grid = gridRef.current;
|
|
146
|
+
const controls = null === grid ? [] : controlsOf(grid, cell);
|
|
147
|
+
if (controls.length > 0) {
|
|
148
|
+
event.preventDefault();
|
|
149
|
+
enter(controls);
|
|
150
|
+
}
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (altKey || metaKey || shiftKey || ctrlKey && 'Home' !== key && 'End' !== key) return;
|
|
154
|
+
const inRightToLeft = 'rtl' === getComputedStyle(cell).direction;
|
|
155
|
+
const columnIndex = covers(position, columnSpan, focusedCell) ? focusedCell.columnIndex : position.columnIndex;
|
|
156
|
+
const columnBefore = {
|
|
157
|
+
rowIndex,
|
|
158
|
+
columnIndex: position.columnIndex - 1
|
|
159
|
+
};
|
|
160
|
+
const columnAfter = {
|
|
161
|
+
rowIndex,
|
|
162
|
+
columnIndex: position.columnIndex + columnSpan
|
|
163
|
+
};
|
|
164
|
+
const targets = {
|
|
165
|
+
ArrowUp: ()=>({
|
|
166
|
+
rowIndex: rowIndex - 1,
|
|
167
|
+
columnIndex
|
|
168
|
+
}),
|
|
169
|
+
ArrowDown: ()=>({
|
|
170
|
+
rowIndex: rowIndex + 1,
|
|
171
|
+
columnIndex
|
|
172
|
+
}),
|
|
173
|
+
ArrowLeft: ()=>inRightToLeft ? columnAfter : columnBefore,
|
|
174
|
+
ArrowRight: ()=>inRightToLeft ? columnBefore : columnAfter,
|
|
175
|
+
Home: ()=>ctrlKey ? {
|
|
176
|
+
rowIndex: 0,
|
|
177
|
+
columnIndex: 0
|
|
178
|
+
} : {
|
|
179
|
+
rowIndex,
|
|
180
|
+
columnIndex: 0
|
|
181
|
+
},
|
|
182
|
+
End: ()=>ctrlKey ? {
|
|
183
|
+
rowIndex: lastRow,
|
|
184
|
+
columnIndex: lastColumn
|
|
185
|
+
} : {
|
|
186
|
+
rowIndex,
|
|
187
|
+
columnIndex: lastColumn
|
|
188
|
+
},
|
|
189
|
+
PageUp: ()=>({
|
|
190
|
+
rowIndex: rowIndex - getPageSize(),
|
|
191
|
+
columnIndex
|
|
192
|
+
}),
|
|
193
|
+
PageDown: ()=>({
|
|
194
|
+
rowIndex: rowIndex + getPageSize(),
|
|
195
|
+
columnIndex
|
|
196
|
+
})
|
|
197
|
+
};
|
|
198
|
+
const target = targets[key];
|
|
199
|
+
if (!target) return;
|
|
200
|
+
event.preventDefault();
|
|
201
|
+
const next = target();
|
|
202
|
+
const clampedNext = {
|
|
203
|
+
...next,
|
|
204
|
+
columnIndex: clamp(next.columnIndex, 0, lastColumn)
|
|
205
|
+
};
|
|
206
|
+
if (!covers(position, columnSpan, clampedNext)) moveTo(next);
|
|
207
|
+
};
|
|
208
|
+
const getCellProps = (rowIndex, columnIndex, columnSpan = 1)=>{
|
|
209
|
+
const position = {
|
|
210
|
+
rowIndex,
|
|
211
|
+
columnIndex
|
|
212
|
+
};
|
|
213
|
+
const isFocused = covers(position, columnSpan, focusedCell);
|
|
214
|
+
return {
|
|
215
|
+
tabIndex: isFocused && !inside ? 0 : -1,
|
|
216
|
+
'data-focused': isFocused ? '' : void 0,
|
|
217
|
+
onKeyDown: (event)=>handleKeyDown(position, columnSpan, event),
|
|
218
|
+
onFocus: (event)=>{
|
|
219
|
+
if (!isFromCell(event)) return;
|
|
220
|
+
hasFocusRef.current = true;
|
|
221
|
+
if (!isFocused) setHeldCell(position);
|
|
222
|
+
setInside(event.target !== event.currentTarget);
|
|
223
|
+
},
|
|
224
|
+
onBlur: (event)=>{
|
|
225
|
+
if (!isFromCell(event)) return;
|
|
226
|
+
const next = event.relatedTarget;
|
|
227
|
+
if (next instanceof Node && event.currentTarget.contains(next)) return;
|
|
228
|
+
if (!(next instanceof Node && gridRef.current?.contains(next))) {
|
|
229
|
+
hasFocusRef.current = false;
|
|
230
|
+
pendingFocusRef.current = false;
|
|
231
|
+
setInside(false);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
};
|
|
236
|
+
return {
|
|
237
|
+
focusedCell,
|
|
238
|
+
getCellProps,
|
|
239
|
+
gridRef
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
export { cellControlSelector, useGridKeyboard };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type RefObject } from 'react';
|
|
2
|
+
export interface GridMetrics {
|
|
3
|
+
viewportHeight: number;
|
|
4
|
+
/** The sticky header band, which covers the top of the scroll area. */
|
|
5
|
+
headerHeight: number;
|
|
6
|
+
/**
|
|
7
|
+
* The computed direction is right to left, whether a `dir` attribute or a stylesheet set it. Read
|
|
8
|
+
* when the grid mounts and when it resizes, not on every render.
|
|
9
|
+
*/
|
|
10
|
+
rightToLeft: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** The scroll area's height and the header band's, kept current as either resizes. */
|
|
13
|
+
export declare function useGridMetrics(scrollRef: RefObject<HTMLElement | null>, headRef: RefObject<HTMLElement | null>): GridMetrics;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { useLayoutEffect, useState } from "react";
|
|
2
|
+
const initialMetrics = {
|
|
3
|
+
viewportHeight: 0,
|
|
4
|
+
headerHeight: 0,
|
|
5
|
+
rightToLeft: false
|
|
6
|
+
};
|
|
7
|
+
function useGridMetrics(scrollRef, headRef) {
|
|
8
|
+
const [metrics, setMetrics] = useState(initialMetrics);
|
|
9
|
+
useLayoutEffect(()=>{
|
|
10
|
+
const scrollElement = scrollRef.current;
|
|
11
|
+
const head = headRef.current;
|
|
12
|
+
if (null === scrollElement || null === head) return;
|
|
13
|
+
const measure = ()=>{
|
|
14
|
+
const viewportHeight = scrollElement.clientHeight;
|
|
15
|
+
const headerHeight = head.offsetHeight;
|
|
16
|
+
const rightToLeft = 'rtl' === getComputedStyle(scrollElement).direction;
|
|
17
|
+
setMetrics((current)=>current.viewportHeight === viewportHeight && current.headerHeight === headerHeight && current.rightToLeft === rightToLeft ? current : {
|
|
18
|
+
viewportHeight,
|
|
19
|
+
headerHeight,
|
|
20
|
+
rightToLeft
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
measure();
|
|
24
|
+
if ("u" < typeof ResizeObserver) return;
|
|
25
|
+
const observer = new ResizeObserver(measure);
|
|
26
|
+
observer.observe(scrollElement);
|
|
27
|
+
observer.observe(head);
|
|
28
|
+
return ()=>observer.disconnect();
|
|
29
|
+
}, [
|
|
30
|
+
scrollRef,
|
|
31
|
+
headRef
|
|
32
|
+
]);
|
|
33
|
+
return metrics;
|
|
34
|
+
}
|
|
35
|
+
export { useGridMetrics };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type RefObject } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* The newest value, for an effect or handler that must read it without re-running when it changes.
|
|
4
|
+
* It is set in a layout effect, so it is current by the time any effect of the same commit runs.
|
|
5
|
+
* (`useEffectEvent` does this, but only React 19.2 has it, and the peer range starts at 19.0.)
|
|
6
|
+
*/
|
|
7
|
+
export declare function useLatest<Value>(value: Value): RefObject<Value>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface UseLoadMoreOptions {
|
|
2
|
+
/** False outside server mode, and while a pending query rules. */
|
|
3
|
+
enabled: boolean;
|
|
4
|
+
/** A first load or a reload: the rows start again, so nothing is asked and what was is forgotten. */
|
|
5
|
+
loading: boolean;
|
|
6
|
+
/** The rows loaded so far. */
|
|
7
|
+
loadedCount: number;
|
|
8
|
+
/** The row at the end of what the user can see, as a place in the loaded rows; -1 if none. */
|
|
9
|
+
lastRenderedIndex: number;
|
|
10
|
+
/** How many rows from the end of the loaded rows counts as near it. */
|
|
11
|
+
threshold: number;
|
|
12
|
+
hasMore: boolean;
|
|
13
|
+
loadingMore: boolean;
|
|
14
|
+
loadError: string | undefined;
|
|
15
|
+
onLoadMore: (() => void) | undefined;
|
|
16
|
+
}
|
|
17
|
+
export interface LoadMore {
|
|
18
|
+
/** The retry button: asks again at once, though the guard has already asked at this count. */
|
|
19
|
+
retry: () => void;
|
|
20
|
+
/** Forgets what was asked, for rows that start again from the first page. */
|
|
21
|
+
reset: () => void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Asks for the next page when the user is near the end of the loaded rows, or when the loaded rows
|
|
25
|
+
* do not fill the view. It asks once per loaded count: a load that adds nothing is not asked for
|
|
26
|
+
* again until the count changes or the user retries, so an empty answer cannot make a loop.
|
|
27
|
+
* Auto-loading pauses while there is a load error. A failed load does not count as asked, and
|
|
28
|
+
* neither does anything asked before a reload, so the grid asks again once the error clears.
|
|
29
|
+
*/
|
|
30
|
+
export declare function useLoadMore({ enabled, loading, loadedCount, lastRenderedIndex, threshold, hasMore, loadingMore, loadError, onLoadMore, }: UseLoadMoreOptions): LoadMore;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
import { useLatest } from "./useLatest.js";
|
|
3
|
+
function useLoadMore({ enabled, loading, loadedCount, lastRenderedIndex, threshold, hasMore, loadingMore, loadError, onLoadMore }) {
|
|
4
|
+
const requestedCountRef = useRef(null);
|
|
5
|
+
const request = ()=>{
|
|
6
|
+
requestedCountRef.current = loadedCount;
|
|
7
|
+
onLoadMore?.();
|
|
8
|
+
};
|
|
9
|
+
const requestRef = useLatest(request);
|
|
10
|
+
const failed = void 0 !== loadError;
|
|
11
|
+
useEffect(()=>{
|
|
12
|
+
if (failed || loading) requestedCountRef.current = null;
|
|
13
|
+
}, [
|
|
14
|
+
failed,
|
|
15
|
+
loading
|
|
16
|
+
]);
|
|
17
|
+
const nearEnd = loadedCount - 1 - lastRenderedIndex <= threshold;
|
|
18
|
+
useEffect(()=>{
|
|
19
|
+
if (!enabled || loading || !hasMore || loadingMore || failed || !nearEnd) return;
|
|
20
|
+
if (requestedCountRef.current === loadedCount) return;
|
|
21
|
+
requestRef.current();
|
|
22
|
+
}, [
|
|
23
|
+
enabled,
|
|
24
|
+
loading,
|
|
25
|
+
hasMore,
|
|
26
|
+
loadingMore,
|
|
27
|
+
failed,
|
|
28
|
+
nearEnd,
|
|
29
|
+
loadedCount,
|
|
30
|
+
requestRef
|
|
31
|
+
]);
|
|
32
|
+
return {
|
|
33
|
+
retry: request,
|
|
34
|
+
reset: ()=>{
|
|
35
|
+
requestedCountRef.current = null;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export { useLoadMore };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface UseRowExpansionOptions {
|
|
2
|
+
expandedRowIds?: string[];
|
|
3
|
+
defaultExpandedRowIds?: string[];
|
|
4
|
+
onExpandedRowIdsChange?: (rowIds: string[]) => void;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The ids of the expanded rows: controlled when `expandedRowIds` is given, else the grid's own.
|
|
8
|
+
* Keyed by row id, so a panel follows its row through a sort. A toggle adds the id at the end or
|
|
9
|
+
* takes it out; ids are kept for rows that are not loaded, so such a row opens again on its return.
|
|
10
|
+
*/
|
|
11
|
+
export declare function useRowExpansion({ expandedRowIds, defaultExpandedRowIds, onExpandedRowIdsChange, }: UseRowExpansionOptions): {
|
|
12
|
+
expandedIds: ReadonlySet<string>;
|
|
13
|
+
toggleRow: (rowId: string) => void;
|
|
14
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useMemo, useState } from "react";
|
|
2
|
+
function useRowExpansion({ expandedRowIds, defaultExpandedRowIds = [], onExpandedRowIdsChange }) {
|
|
3
|
+
const [ownIds, setOwnIds] = useState(defaultExpandedRowIds);
|
|
4
|
+
const currentIds = expandedRowIds ?? ownIds;
|
|
5
|
+
const expandedIds = useMemo(()=>new Set(currentIds), [
|
|
6
|
+
currentIds
|
|
7
|
+
]);
|
|
8
|
+
const toggleRow = (rowId)=>{
|
|
9
|
+
const nextIds = expandedIds.has(rowId) ? currentIds.filter((expandedId)=>expandedId !== rowId) : [
|
|
10
|
+
...currentIds,
|
|
11
|
+
rowId
|
|
12
|
+
];
|
|
13
|
+
onExpandedRowIdsChange?.(nextIds);
|
|
14
|
+
if (void 0 === expandedRowIds) setOwnIds(nextIds);
|
|
15
|
+
};
|
|
16
|
+
return {
|
|
17
|
+
expandedIds,
|
|
18
|
+
toggleRow
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export { useRowExpansion };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { RowSelectionState } from '@tanstack/react-table';
|
|
2
|
+
import { type MouseEvent } from 'react';
|
|
3
|
+
export interface UseRowSelectionOptions<Row> {
|
|
4
|
+
/** False for `selectionMode: 'none'`: nothing is selected and nothing is reported. */
|
|
5
|
+
enabled: boolean;
|
|
6
|
+
rows: readonly Row[];
|
|
7
|
+
getRowId: (row: Row) => string;
|
|
8
|
+
isRowSelectable?: (row: Row) => boolean;
|
|
9
|
+
/** Rows on their way in are not yet the rows the selection must match. */
|
|
10
|
+
loading: boolean;
|
|
11
|
+
selectedRowIds?: string[];
|
|
12
|
+
defaultSelectedRowIds?: string[];
|
|
13
|
+
onSelectedRowIdsChange?: (rowIds: string[]) => void;
|
|
14
|
+
/** The ids on screen, in display order. Read when a range is chosen, which is after a sort. */
|
|
15
|
+
getDisplayedRowIds: () => string[];
|
|
16
|
+
}
|
|
17
|
+
export interface RowSelection {
|
|
18
|
+
selectedIds: ReadonlySet<string>;
|
|
19
|
+
/** TanStack's own shape of the selection, for its row and table readers. */
|
|
20
|
+
rowSelection: RowSelectionState;
|
|
21
|
+
/** Every selectable row is selected. */
|
|
22
|
+
allSelected: boolean;
|
|
23
|
+
/** Some rows are selected, but not all. */
|
|
24
|
+
someSelected: boolean;
|
|
25
|
+
/** Whether any row can be selected, so select-all has something to do. */
|
|
26
|
+
canSelectAny: boolean;
|
|
27
|
+
isSelectable: (rowId: string) => boolean;
|
|
28
|
+
/** Toggles one row, or with `range` selects from the last toggled row to this one. */
|
|
29
|
+
toggleRow: (rowId: string, options?: {
|
|
30
|
+
range?: boolean;
|
|
31
|
+
}) => void;
|
|
32
|
+
/** A checkbox click: the Shift key it carried decides between a toggle and a range. */
|
|
33
|
+
toggleRowFromCheckbox: (rowId: string) => void;
|
|
34
|
+
/** Capture-phase click handler for the checkbox cell, which sees the Shift key first. */
|
|
35
|
+
noteCheckboxClick: (event: MouseEvent<HTMLElement>) => void;
|
|
36
|
+
toggleAll: () => void;
|
|
37
|
+
clear: () => void;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Checkbox selection over the loaded rows. The ids it reports are in the order of the `rows` it
|
|
41
|
+
* is given, whatever the sort. A selected row that leaves the grid is dropped, once, and reported.
|
|
42
|
+
*/
|
|
43
|
+
export declare function useRowSelection<Row>({ enabled, rows, getRowId, isRowSelectable, loading, selectedRowIds, defaultSelectedRowIds, onSelectedRowIdsChange, getDisplayedRowIds, }: UseRowSelectionOptions<Row>): RowSelection;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { useLatest } from "./useLatest.js";
|
|
3
|
+
const noIds = [];
|
|
4
|
+
const noSelection = {};
|
|
5
|
+
const noRowIds = new Set();
|
|
6
|
+
function useRowSelection({ enabled, rows, getRowId, isRowSelectable, loading, selectedRowIds, defaultSelectedRowIds = [], onSelectedRowIdsChange, getDisplayedRowIds }) {
|
|
7
|
+
const [ownIds, setOwnIds] = useState(defaultSelectedRowIds);
|
|
8
|
+
const anchorRef = useRef(null);
|
|
9
|
+
const checkboxShiftRef = useRef(false);
|
|
10
|
+
const { orderedIds, selectableIds } = useMemo(()=>{
|
|
11
|
+
const ids = [];
|
|
12
|
+
const selectable = new Set();
|
|
13
|
+
if (enabled) for (const row of rows){
|
|
14
|
+
const rowId = getRowId(row);
|
|
15
|
+
ids.push(rowId);
|
|
16
|
+
if (isRowSelectable?.(row) ?? true) selectable.add(rowId);
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
orderedIds: ids,
|
|
20
|
+
selectableIds: selectable
|
|
21
|
+
};
|
|
22
|
+
}, [
|
|
23
|
+
enabled,
|
|
24
|
+
rows,
|
|
25
|
+
getRowId,
|
|
26
|
+
isRowSelectable
|
|
27
|
+
]);
|
|
28
|
+
const reportedIds = enabled ? selectedRowIds ?? ownIds : noIds;
|
|
29
|
+
const selectedIds = useMemo(()=>{
|
|
30
|
+
if (!enabled) return noRowIds;
|
|
31
|
+
const present = new Set(orderedIds);
|
|
32
|
+
return new Set(reportedIds.filter((rowId)=>present.has(rowId)));
|
|
33
|
+
}, [
|
|
34
|
+
enabled,
|
|
35
|
+
orderedIds,
|
|
36
|
+
reportedIds
|
|
37
|
+
]);
|
|
38
|
+
const rowSelection = useMemo(()=>{
|
|
39
|
+
if (0 === selectedIds.size) return noSelection;
|
|
40
|
+
return Object.fromEntries([
|
|
41
|
+
...selectedIds
|
|
42
|
+
].map((rowId)=>[
|
|
43
|
+
rowId,
|
|
44
|
+
true
|
|
45
|
+
]));
|
|
46
|
+
}, [
|
|
47
|
+
selectedIds
|
|
48
|
+
]);
|
|
49
|
+
const commit = (nextIds)=>{
|
|
50
|
+
const ordered = orderedIds.filter((rowId)=>nextIds.has(rowId));
|
|
51
|
+
onSelectedRowIdsChange?.(ordered);
|
|
52
|
+
if (void 0 === selectedRowIds) setOwnIds(ordered);
|
|
53
|
+
};
|
|
54
|
+
const dropGoneRowsRef = useLatest(()=>commit(selectedIds));
|
|
55
|
+
const hasGoneRows = enabled && !loading && selectedIds.size !== reportedIds.length;
|
|
56
|
+
useEffect(()=>{
|
|
57
|
+
if (hasGoneRows) dropGoneRowsRef.current();
|
|
58
|
+
}, [
|
|
59
|
+
hasGoneRows,
|
|
60
|
+
rows,
|
|
61
|
+
dropGoneRowsRef
|
|
62
|
+
]);
|
|
63
|
+
const toggleRow = (rowId, { range = false } = {})=>{
|
|
64
|
+
if (!selectableIds.has(rowId)) return;
|
|
65
|
+
const next = new Set(selectedIds);
|
|
66
|
+
const displayedIds = getDisplayedRowIds();
|
|
67
|
+
const anchorPosition = null === anchorRef.current ? -1 : displayedIds.indexOf(anchorRef.current);
|
|
68
|
+
const targetPosition = displayedIds.indexOf(rowId);
|
|
69
|
+
if (range && anchorPosition >= 0 && targetPosition >= 0) {
|
|
70
|
+
const first = Math.min(anchorPosition, targetPosition);
|
|
71
|
+
const last = Math.max(anchorPosition, targetPosition);
|
|
72
|
+
for (const inRange of displayedIds.slice(first, last + 1))if (selectableIds.has(inRange)) next.add(inRange);
|
|
73
|
+
} else {
|
|
74
|
+
if (next.has(rowId)) next.delete(rowId);
|
|
75
|
+
else next.add(rowId);
|
|
76
|
+
anchorRef.current = rowId;
|
|
77
|
+
}
|
|
78
|
+
commit(next);
|
|
79
|
+
};
|
|
80
|
+
const allSelected = selectableIds.size > 0 && [
|
|
81
|
+
...selectableIds
|
|
82
|
+
].every((rowId)=>selectedIds.has(rowId));
|
|
83
|
+
return {
|
|
84
|
+
selectedIds,
|
|
85
|
+
rowSelection,
|
|
86
|
+
allSelected,
|
|
87
|
+
someSelected: selectedIds.size > 0 && !allSelected,
|
|
88
|
+
canSelectAny: selectableIds.size > 0,
|
|
89
|
+
isSelectable: (rowId)=>selectableIds.has(rowId),
|
|
90
|
+
toggleRow,
|
|
91
|
+
toggleRowFromCheckbox: (rowId)=>{
|
|
92
|
+
const range = checkboxShiftRef.current;
|
|
93
|
+
checkboxShiftRef.current = false;
|
|
94
|
+
toggleRow(rowId, {
|
|
95
|
+
range
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
noteCheckboxClick: (event)=>{
|
|
99
|
+
checkboxShiftRef.current = event.shiftKey;
|
|
100
|
+
},
|
|
101
|
+
toggleAll: ()=>commit(allSelected ? new Set() : selectableIds),
|
|
102
|
+
clear: ()=>{
|
|
103
|
+
if (selectedIds.size > 0) commit(new Set());
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
export { useRowSelection };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type RefObject } from 'react';
|
|
2
|
+
import { type DateAvailability } from './calendarDate.js';
|
|
3
|
+
export interface CalendarProps extends DateAvailability {
|
|
4
|
+
gridRef: RefObject<HTMLTableElement | null>;
|
|
5
|
+
chosen: string | null;
|
|
6
|
+
locale: string | undefined;
|
|
7
|
+
weekStartsOn: number;
|
|
8
|
+
previousMonthLabel: string;
|
|
9
|
+
nextMonthLabel: string;
|
|
10
|
+
onSelect: (date: string) => void;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The month grid inside `DatePicker`'s dialog, mounted only while it is open. Internal.
|
|
14
|
+
*
|
|
15
|
+
* Roving focus, per the WAI-ARIA date picker dialog pattern: one date is the tab stop, the opening
|
|
16
|
+
* focus and the origin of the arrow keys. Unavailable dates can be focused and read out, as the
|
|
17
|
+
* pattern says, but not chosen; months wholly outside `min` and `max` cannot be reached at all.
|
|
18
|
+
*/
|
|
19
|
+
export declare function Calendar({ gridRef, chosen, min, max, isDateDisabled, locale, weekStartsOn, previousMonthLabel, nextMonthLabel, onSelect, }: CalendarProps): import("react").JSX.Element;
|