@calcifer-design/ui 0.5.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/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/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 +5 -0
- package/dist/index.js +2 -0
- package/package.json +3 -2
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { flexRender } from "@tanstack/react-table";
|
|
3
|
+
import { ChevronRightIcon } from "../../icons/ChevronRightIcon.js";
|
|
4
|
+
import { textValueOf } from "./columns.js";
|
|
5
|
+
import DataGrid_module from "./DataGrid.module.js";
|
|
6
|
+
import { GridCheckbox } from "./GridCheckbox.js";
|
|
7
|
+
import { cellControlSelector } from "./useGridKeyboard.js";
|
|
8
|
+
function rowCellOf(event) {
|
|
9
|
+
let element = event.target instanceof Element ? event.target : null;
|
|
10
|
+
while(null !== element && element.parentElement !== event.currentTarget)element = element.parentElement;
|
|
11
|
+
return element instanceof HTMLTableCellElement ? element : null;
|
|
12
|
+
}
|
|
13
|
+
function isRowEvent(event) {
|
|
14
|
+
const cell = rowCellOf(event);
|
|
15
|
+
if (null === cell) return false;
|
|
16
|
+
const control = event.target.closest(cellControlSelector);
|
|
17
|
+
return null === control || control === cell || !cell.contains(control);
|
|
18
|
+
}
|
|
19
|
+
function isRowClick(event) {
|
|
20
|
+
return isRowEvent(event) && !rowCellOf(event)?.hasAttribute('data-toggle-cell');
|
|
21
|
+
}
|
|
22
|
+
function onlyRowClicks(handler) {
|
|
23
|
+
return (event)=>{
|
|
24
|
+
if (isRowClick(event)) handler(event);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function onlyRowEvents(handler) {
|
|
28
|
+
return (event)=>{
|
|
29
|
+
if (isRowEvent(event)) handler(event);
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function compactTitle(column, row) {
|
|
33
|
+
const text = textValueOf(column, row);
|
|
34
|
+
return void 0 === text || '' === text.trim() ? void 0 : text;
|
|
35
|
+
}
|
|
36
|
+
function GridRow({ row, entryIndex, measureRef, density, columnOffset, highlighted, clickable, checkbox, expander, renderRowActions, getCellProps, onClick, onDoubleClick, onContextMenu }) {
|
|
37
|
+
const rowIndex = entryIndex + 1;
|
|
38
|
+
const selecting = void 0 !== checkbox;
|
|
39
|
+
const expandColumn = selecting ? 1 : 0;
|
|
40
|
+
const cells = row.getAllCells();
|
|
41
|
+
let ariaSelected;
|
|
42
|
+
ariaSelected = selecting ? checkbox.selectable ? checkbox.selected : void 0 : highlighted || void 0;
|
|
43
|
+
return /*#__PURE__*/ jsxs("tr", {
|
|
44
|
+
ref: measureRef,
|
|
45
|
+
"data-index": entryIndex,
|
|
46
|
+
className: DataGrid_module.row,
|
|
47
|
+
"aria-rowindex": rowIndex + 1,
|
|
48
|
+
"aria-selected": ariaSelected,
|
|
49
|
+
"aria-current": selecting && highlighted ? 'true' : void 0,
|
|
50
|
+
"data-highlighted": highlighted ? '' : void 0,
|
|
51
|
+
"data-clickable": clickable ? '' : void 0,
|
|
52
|
+
onClick: onlyRowClicks(onClick),
|
|
53
|
+
onDoubleClick: onDoubleClick ? onlyRowClicks(onDoubleClick) : void 0,
|
|
54
|
+
onContextMenu: onContextMenu ? onlyRowEvents(onContextMenu) : void 0,
|
|
55
|
+
children: [
|
|
56
|
+
checkbox ? /*#__PURE__*/ jsx("td", {
|
|
57
|
+
role: "gridcell",
|
|
58
|
+
className: `${DataGrid_module.cell} ${DataGrid_module.pinnedCell} ${DataGrid_module.selectCell}`,
|
|
59
|
+
"aria-colindex": 1,
|
|
60
|
+
"data-toggle-cell": "",
|
|
61
|
+
onClickCapture: checkbox.onClickCapture,
|
|
62
|
+
onClick: (event)=>{
|
|
63
|
+
if (event.target === event.currentTarget) checkbox.onCellClick(event);
|
|
64
|
+
},
|
|
65
|
+
...getCellProps(rowIndex, 0),
|
|
66
|
+
children: /*#__PURE__*/ jsx(GridCheckbox, {
|
|
67
|
+
label: checkbox.label,
|
|
68
|
+
checked: checkbox.selected,
|
|
69
|
+
disabled: !checkbox.selectable,
|
|
70
|
+
onCheckedChange: checkbox.onCheckedChange,
|
|
71
|
+
tabIndex: -1
|
|
72
|
+
})
|
|
73
|
+
}) : null,
|
|
74
|
+
expander ? /*#__PURE__*/ jsx("td", {
|
|
75
|
+
role: "gridcell",
|
|
76
|
+
className: `${DataGrid_module.cell} ${DataGrid_module.pinnedCell} ${DataGrid_module.expandCell}`,
|
|
77
|
+
"aria-colindex": expandColumn + 1,
|
|
78
|
+
"data-toggle-cell": expander.expandable ? '' : void 0,
|
|
79
|
+
onClick: expander.expandable ? expander.onToggle : void 0,
|
|
80
|
+
...getCellProps(rowIndex, expandColumn),
|
|
81
|
+
children: expander.expandable ? /*#__PURE__*/ jsx("button", {
|
|
82
|
+
type: "button",
|
|
83
|
+
className: DataGrid_module.expandButton,
|
|
84
|
+
"aria-label": expander.label,
|
|
85
|
+
"aria-expanded": expander.expanded,
|
|
86
|
+
"aria-controls": expander.expanded ? expander.panelId : void 0,
|
|
87
|
+
tabIndex: -1,
|
|
88
|
+
children: /*#__PURE__*/ jsx(ChevronRightIcon, {})
|
|
89
|
+
}) : null
|
|
90
|
+
}) : null,
|
|
91
|
+
cells.map((cell, columnIndex)=>/*#__PURE__*/ jsx("td", {
|
|
92
|
+
role: "gridcell",
|
|
93
|
+
className: DataGrid_module.cell,
|
|
94
|
+
"aria-colindex": columnIndex + 1 + columnOffset,
|
|
95
|
+
title: 'compact' === density ? compactTitle(cell.column.columnDef, row.original) : void 0,
|
|
96
|
+
...getCellProps(rowIndex, columnIndex + columnOffset),
|
|
97
|
+
children: flexRender(cell.column.columnDef.cell, cell.getContext())
|
|
98
|
+
}, cell.id)),
|
|
99
|
+
renderRowActions ? /*#__PURE__*/ jsx("td", {
|
|
100
|
+
role: "gridcell",
|
|
101
|
+
className: `${DataGrid_module.cell} ${DataGrid_module.pinnedCell} ${DataGrid_module.actionsCell}`,
|
|
102
|
+
"aria-colindex": columnOffset + cells.length + 1,
|
|
103
|
+
...getCellProps(rowIndex, columnOffset + cells.length),
|
|
104
|
+
children: renderRowActions(row.original)
|
|
105
|
+
}) : null
|
|
106
|
+
]
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
function GridDetailRow({ entryIndex, measureRef, columnCount, panelId, getCellProps, children }) {
|
|
110
|
+
const rowIndex = entryIndex + 1;
|
|
111
|
+
return /*#__PURE__*/ jsx("tr", {
|
|
112
|
+
ref: measureRef,
|
|
113
|
+
"data-index": entryIndex,
|
|
114
|
+
className: DataGrid_module.detailRow,
|
|
115
|
+
"aria-rowindex": rowIndex + 1,
|
|
116
|
+
children: /*#__PURE__*/ jsx("td", {
|
|
117
|
+
id: panelId,
|
|
118
|
+
role: "gridcell",
|
|
119
|
+
className: DataGrid_module.detailCell,
|
|
120
|
+
colSpan: columnCount,
|
|
121
|
+
"aria-colindex": 1,
|
|
122
|
+
...getCellProps(rowIndex, 0, columnCount),
|
|
123
|
+
children: children
|
|
124
|
+
})
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function GridSpacerRow({ height, columnCount }) {
|
|
128
|
+
return /*#__PURE__*/ jsx("tr", {
|
|
129
|
+
"aria-hidden": "true",
|
|
130
|
+
children: /*#__PURE__*/ jsx("td", {
|
|
131
|
+
className: DataGrid_module.spacerCell,
|
|
132
|
+
colSpan: columnCount,
|
|
133
|
+
style: {
|
|
134
|
+
height
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
export { GridDetailRow, GridRow, GridSpacerRow };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { GetCellProps } from './useGridKeyboard.js';
|
|
3
|
+
export type GridStateKind = 'loading' | 'empty';
|
|
4
|
+
export interface GridStateRowProps {
|
|
5
|
+
kind: GridStateKind;
|
|
6
|
+
/** The message for `kind`. */
|
|
7
|
+
label: string;
|
|
8
|
+
/** Shown in place of the empty message. */
|
|
9
|
+
emptyState?: ReactNode;
|
|
10
|
+
columnCount: number;
|
|
11
|
+
}
|
|
12
|
+
/** The body's only row while it loads or has no rows: one cell across every column. */
|
|
13
|
+
export declare function GridStateRow({ kind, label, emptyState, columnCount }: GridStateRowProps): import("react").JSX.Element;
|
|
14
|
+
export interface GridErrorRowProps {
|
|
15
|
+
/** The row's place after the grid's entries; its keyboard row is one more. */
|
|
16
|
+
entryIndex: number;
|
|
17
|
+
columnCount: number;
|
|
18
|
+
message: string;
|
|
19
|
+
retryLabel: string;
|
|
20
|
+
onRetry: () => void;
|
|
21
|
+
getCellProps: GetCellProps;
|
|
22
|
+
}
|
|
23
|
+
/** A failed load in server mode: the error and a retry button, after the last entry. */
|
|
24
|
+
export declare function GridErrorRow({ entryIndex, columnCount, message, retryLabel, onRetry, getCellProps, }: GridErrorRowProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Button } from "../Button/Button.js";
|
|
3
|
+
import { Spinner } from "../Spinner/Spinner.js";
|
|
4
|
+
import DataGrid_module from "./DataGrid.module.js";
|
|
5
|
+
function GridStateRow({ kind, label, emptyState, columnCount }) {
|
|
6
|
+
return /*#__PURE__*/ jsx("tr", {
|
|
7
|
+
"aria-rowindex": 2,
|
|
8
|
+
children: /*#__PURE__*/ jsx("td", {
|
|
9
|
+
role: "gridcell",
|
|
10
|
+
className: DataGrid_module.stateCell,
|
|
11
|
+
colSpan: columnCount,
|
|
12
|
+
"aria-colindex": 1,
|
|
13
|
+
children: 'loading' === kind ? /*#__PURE__*/ jsxs("div", {
|
|
14
|
+
className: DataGrid_module.state,
|
|
15
|
+
"aria-hidden": "true",
|
|
16
|
+
children: [
|
|
17
|
+
/*#__PURE__*/ jsx(Spinner, {
|
|
18
|
+
size: "md"
|
|
19
|
+
}),
|
|
20
|
+
label
|
|
21
|
+
]
|
|
22
|
+
}) : emptyState ?? /*#__PURE__*/ jsx("div", {
|
|
23
|
+
className: DataGrid_module.state,
|
|
24
|
+
"aria-hidden": "true",
|
|
25
|
+
children: label
|
|
26
|
+
})
|
|
27
|
+
})
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function GridErrorRow({ entryIndex, columnCount, message, retryLabel, onRetry, getCellProps }) {
|
|
31
|
+
const rowIndex = entryIndex + 1;
|
|
32
|
+
return /*#__PURE__*/ jsx("tr", {
|
|
33
|
+
"data-index": entryIndex,
|
|
34
|
+
"aria-rowindex": rowIndex + 1,
|
|
35
|
+
children: /*#__PURE__*/ jsx("td", {
|
|
36
|
+
role: "gridcell",
|
|
37
|
+
className: DataGrid_module.errorCell,
|
|
38
|
+
colSpan: columnCount,
|
|
39
|
+
"aria-colindex": 1,
|
|
40
|
+
...getCellProps(rowIndex, 0, columnCount),
|
|
41
|
+
children: /*#__PURE__*/ jsxs("div", {
|
|
42
|
+
className: DataGrid_module.errorContent,
|
|
43
|
+
children: [
|
|
44
|
+
/*#__PURE__*/ jsx("span", {
|
|
45
|
+
role: "alert",
|
|
46
|
+
children: message
|
|
47
|
+
}),
|
|
48
|
+
/*#__PURE__*/ jsx(Button, {
|
|
49
|
+
variant: "secondary",
|
|
50
|
+
size: "sm",
|
|
51
|
+
onClick: onRetry,
|
|
52
|
+
children: retryLabel
|
|
53
|
+
})
|
|
54
|
+
]
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
export { GridErrorRow, GridStateRow };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type AccessorFn, type AccessorFnColumnDef, type AccessorKeyColumnDef, type ColumnDef as TanStackColumnDef, type DeepKeys, type DeepValue, type DisplayColumnDef, type IdentifiedColumnDef, type RowData } from '@tanstack/react-table';
|
|
2
|
+
export declare const dataGridFeatures: {
|
|
3
|
+
rowSortingFeature: import("@tanstack/react-table").TableFeature;
|
|
4
|
+
rowSelectionFeature: import("@tanstack/react-table").TableFeature;
|
|
5
|
+
rowExpandingFeature: import("@tanstack/react-table").TableFeature;
|
|
6
|
+
sortedRowModel: (table: import("@tanstack/react-table").Table<any, any>) => () => import("@tanstack/react-table").RowModel<any, any>;
|
|
7
|
+
sortFns: {
|
|
8
|
+
alphanumeric: import("@tanstack/react-table").CreatedSortFn<any, any>;
|
|
9
|
+
text: import("@tanstack/react-table").CreatedSortFn<any, any>;
|
|
10
|
+
datetime: import("@tanstack/react-table").CreatedSortFn<any, any>;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export interface TextValueOption<Row extends RowData> {
|
|
14
|
+
/** Plain text for a cell: the row label, compact-density titles, and later search and export. */
|
|
15
|
+
textValue?: (row: Row) => string;
|
|
16
|
+
}
|
|
17
|
+
export type DataGridColumnDef<Row extends RowData, Value = unknown> = TanStackColumnDef<typeof dataGridFeatures, Row, Value> & TextValueOption<Row>;
|
|
18
|
+
export type DataGridFeatures = typeof dataGridFeatures;
|
|
19
|
+
type Features = DataGridFeatures;
|
|
20
|
+
export interface DataGridColumnHelper<Row extends RowData> {
|
|
21
|
+
accessor: <Accessor extends AccessorFn<Row> | DeepKeys<Row>, Value extends (Accessor extends AccessorFn<Row, infer Returned> ? Returned : Accessor extends DeepKeys<Row> ? DeepValue<Row, Accessor> : never)>(accessor: Accessor, column: (Accessor extends AccessorFn<Row> ? DisplayColumnDef<Features, Row, Value> : IdentifiedColumnDef<Features, Row, Value>) & TextValueOption<Row>) => (Accessor extends AccessorFn<Row> ? AccessorFnColumnDef<Features, Row, Value> : AccessorKeyColumnDef<Features, Row, Value>) & TextValueOption<Row>;
|
|
22
|
+
/**
|
|
23
|
+
* Wrap the array in this, not a plain literal: it keeps each column's own value type. The
|
|
24
|
+
* `any` is TanStack's own signature for the same helper.
|
|
25
|
+
*/
|
|
26
|
+
columns: <Columns extends ReadonlyArray<DataGridColumnDef<Row, any>>>(columns: [...Columns]) => Array<DataGridColumnDef<Row, any>> & [...Columns];
|
|
27
|
+
display: (column: DisplayColumnDef<Features, Row> & TextValueOption<Row>) => DisplayColumnDef<Features, Row, unknown> & TextValueOption<Row>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The TanStack v9 column helper, typed to the grid's feature set, with one extra column option:
|
|
31
|
+
* `textValue`. At runtime TanStack's helper only returns the definition objects it is given, so
|
|
32
|
+
* the extra option passes straight through.
|
|
33
|
+
*/
|
|
34
|
+
export declare function createDataGridColumnHelper<Row extends RowData>(): DataGridColumnHelper<Row>;
|
|
35
|
+
/**
|
|
36
|
+
* The plain text a column gives for a row: its `textValue` when it has one, else the accessor
|
|
37
|
+
* value when that is a string, number or boolean. Anything else has no text.
|
|
38
|
+
*/
|
|
39
|
+
export declare function textValueOf<Row extends RowData, Value = unknown>(column: DataGridColumnDef<Row, Value>, row: Row): string | undefined;
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createColumnHelper, createSortedRowModel, rowExpandingFeature, rowSelectionFeature, rowSortingFeature, sortFn_alphanumeric, sortFn_datetime, sortFn_text, tableFeatures } from "@tanstack/react-table";
|
|
2
|
+
const dataGridFeatures = tableFeatures({
|
|
3
|
+
rowSortingFeature: rowSortingFeature,
|
|
4
|
+
rowSelectionFeature: rowSelectionFeature,
|
|
5
|
+
rowExpandingFeature: rowExpandingFeature,
|
|
6
|
+
sortedRowModel: createSortedRowModel(),
|
|
7
|
+
sortFns: {
|
|
8
|
+
alphanumeric: sortFn_alphanumeric,
|
|
9
|
+
text: sortFn_text,
|
|
10
|
+
datetime: sortFn_datetime
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
function createDataGridColumnHelper() {
|
|
14
|
+
return createColumnHelper();
|
|
15
|
+
}
|
|
16
|
+
function isText(value) {
|
|
17
|
+
return 'string' == typeof value || 'number' == typeof value || 'boolean' == typeof value;
|
|
18
|
+
}
|
|
19
|
+
function valueAtPath(source, path) {
|
|
20
|
+
let current = source;
|
|
21
|
+
for (const segment of path.split('.')){
|
|
22
|
+
if (null === current || 'object' != typeof current) return;
|
|
23
|
+
current = current[segment];
|
|
24
|
+
}
|
|
25
|
+
return current;
|
|
26
|
+
}
|
|
27
|
+
function textValueOf(column, row) {
|
|
28
|
+
if (column.textValue) return column.textValue(row);
|
|
29
|
+
let value;
|
|
30
|
+
if ('accessorFn' in column && 'function' == typeof column.accessorFn) value = column.accessorFn(row, 0);
|
|
31
|
+
else {
|
|
32
|
+
if (!('accessorKey' in column) || 'string' != typeof column.accessorKey) return;
|
|
33
|
+
value = row[column.accessorKey] ?? valueAtPath(row, column.accessorKey);
|
|
34
|
+
}
|
|
35
|
+
return isText(value) ? String(value) : void 0;
|
|
36
|
+
}
|
|
37
|
+
export { createDataGridColumnHelper, dataGridFeatures, textValueOf };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface DataGridLabels {
|
|
2
|
+
toolbar: string;
|
|
3
|
+
refresh: string;
|
|
4
|
+
rowCount: (count: number) => string;
|
|
5
|
+
loadedRowCount: (loaded: number, total: number) => string;
|
|
6
|
+
selectedCount: (count: number) => string;
|
|
7
|
+
loading: string;
|
|
8
|
+
loadingMore: string;
|
|
9
|
+
retry: string;
|
|
10
|
+
empty: string;
|
|
11
|
+
selectAll: string;
|
|
12
|
+
selectRow: (rowLabel: string) => string;
|
|
13
|
+
/** The expand column's header, which shows nothing but needs a name. */
|
|
14
|
+
expandColumn: string;
|
|
15
|
+
expandRow: (rowLabel: string) => string;
|
|
16
|
+
rowActions: string;
|
|
17
|
+
}
|
|
18
|
+
export declare const defaultDataGridLabels: DataGridLabels;
|
|
19
|
+
/** The defaults with the given labels over them; a label given as undefined keeps its default. */
|
|
20
|
+
export declare function resolveDataGridLabels(labels: Partial<DataGridLabels> | undefined): DataGridLabels;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const numberFormat = new Intl.NumberFormat('en-US');
|
|
2
|
+
const defaultDataGridLabels = {
|
|
3
|
+
toolbar: 'Grid tools',
|
|
4
|
+
refresh: 'Refresh',
|
|
5
|
+
rowCount: (count)=>`${numberFormat.format(count)} ${1 === count ? 'row' : 'rows'}`,
|
|
6
|
+
loadedRowCount: (loaded, total)=>`Showing ${numberFormat.format(loaded)} of ${numberFormat.format(total)} rows`,
|
|
7
|
+
selectedCount: (count)=>`${numberFormat.format(count)} selected`,
|
|
8
|
+
loading: 'Loading rows…',
|
|
9
|
+
loadingMore: 'Loading more rows…',
|
|
10
|
+
retry: 'Try again',
|
|
11
|
+
empty: 'No rows to show.',
|
|
12
|
+
selectAll: 'Select all rows',
|
|
13
|
+
selectRow: (rowLabel)=>`Select ${rowLabel}`,
|
|
14
|
+
expandColumn: 'Details',
|
|
15
|
+
expandRow: (rowLabel)=>`Show details for ${rowLabel}`,
|
|
16
|
+
rowActions: 'Actions'
|
|
17
|
+
};
|
|
18
|
+
function resolveDataGridLabels(labels) {
|
|
19
|
+
const givenLabels = Object.entries(labels ?? {}).filter(([, label])=>void 0 !== label);
|
|
20
|
+
return {
|
|
21
|
+
...defaultDataGridLabels,
|
|
22
|
+
...Object.fromEntries(givenLabels)
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export { defaultDataGridLabels, resolveDataGridLabels };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { RowData, SortingState } from '@tanstack/react-table';
|
|
2
|
+
import type { KeyboardEvent, MouseEvent, ReactNode } from 'react';
|
|
3
|
+
import type { DataGridColumnDef } from './columns.js';
|
|
4
|
+
import type { DataGridLabels } from './labels.js';
|
|
5
|
+
export type DataGridMode = 'client' | 'server';
|
|
6
|
+
export type DataGridDensity = 'comfortable' | 'compact';
|
|
7
|
+
export type DataGridFilterValue = string | string[];
|
|
8
|
+
export interface DataGridQuery {
|
|
9
|
+
sorting: SortingState;
|
|
10
|
+
filters: Record<string, DataGridFilterValue>;
|
|
11
|
+
matchMode: 'all' | 'any';
|
|
12
|
+
search: string;
|
|
13
|
+
}
|
|
14
|
+
export type DataGridRowEvent = MouseEvent<HTMLElement> | KeyboardEvent<HTMLElement>;
|
|
15
|
+
export interface DataGridProps<Row extends RowData> {
|
|
16
|
+
gridId: string;
|
|
17
|
+
heading: string;
|
|
18
|
+
headingLevel?: 2 | 3 | 4 | 5 | 6;
|
|
19
|
+
/**
|
|
20
|
+
* In server mode, pass a new array only when your data changes: after a query change the grid
|
|
21
|
+
* waits for a new `rows` array before it asks for more, so a copy made on every render would
|
|
22
|
+
* end that wait early.
|
|
23
|
+
*/
|
|
24
|
+
rows: Row[];
|
|
25
|
+
/** Build with `createDataGridColumnHelper<Row>().columns([...])`, not a plain array literal. */
|
|
26
|
+
columns: DataGridColumnDef<Row, unknown>[];
|
|
27
|
+
getRowId: (row: Row) => string;
|
|
28
|
+
/** Defaults to the text value of the first column. */
|
|
29
|
+
getRowLabel?: (row: Row) => string;
|
|
30
|
+
actions?: ReactNode;
|
|
31
|
+
/**
|
|
32
|
+
* `'client'` (the default) sorts the given rows itself. `'server'` leaves sorting to the app and
|
|
33
|
+
* asks for rows page by page; there, headers sort only when `onQueryChange` is given.
|
|
34
|
+
*/
|
|
35
|
+
mode?: DataGridMode;
|
|
36
|
+
totalRowCount?: number;
|
|
37
|
+
hasMore?: boolean;
|
|
38
|
+
loadingMore?: boolean;
|
|
39
|
+
loadError?: string;
|
|
40
|
+
onLoadMore?: () => void;
|
|
41
|
+
/**
|
|
42
|
+
* Server mode: the user changed the sort, so load the first page of rows for this query. Without
|
|
43
|
+
* it, server mode's headers are not sortable and carry no `aria-sort`.
|
|
44
|
+
*/
|
|
45
|
+
onQueryChange?: (query: DataGridQuery) => void;
|
|
46
|
+
/**
|
|
47
|
+
* The sort the grid starts with. The planned `initialQuery` prop will win over it when both are
|
|
48
|
+
* given.
|
|
49
|
+
*/
|
|
50
|
+
initialSorting?: SortingState;
|
|
51
|
+
enableSorting?: boolean;
|
|
52
|
+
multiSort?: boolean;
|
|
53
|
+
loading?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Replaces the default empty message. The grid announces `labels.empty` when it is empty, not
|
|
56
|
+
* this node, so set `labels.empty` to match your message.
|
|
57
|
+
*/
|
|
58
|
+
emptyState?: ReactNode;
|
|
59
|
+
density?: DataGridDensity;
|
|
60
|
+
/**
|
|
61
|
+
* The tallest the scrolling body grows before it scrolls, as a CSS length. Defaults to the
|
|
62
|
+
* viewport height minus 16rem, which leaves room for a page header. Ignored when `fill` is set.
|
|
63
|
+
*/
|
|
64
|
+
maxHeight?: string;
|
|
65
|
+
fill?: boolean;
|
|
66
|
+
hideFooter?: boolean;
|
|
67
|
+
onRefresh?: () => void;
|
|
68
|
+
refreshing?: boolean;
|
|
69
|
+
highlightOnClick?: boolean;
|
|
70
|
+
highlightedRowId?: string | null;
|
|
71
|
+
onHighlightChange?: (rowId: string | null) => void;
|
|
72
|
+
selectionMode?: 'none' | 'multiple';
|
|
73
|
+
selectedRowIds?: string[];
|
|
74
|
+
defaultSelectedRowIds?: string[];
|
|
75
|
+
onSelectedRowIdsChange?: (rowIds: string[]) => void;
|
|
76
|
+
isRowSelectable?: (row: Row) => boolean;
|
|
77
|
+
onRowClick?: (row: Row, event: DataGridRowEvent) => void;
|
|
78
|
+
onRowDoubleClick?: (row: Row, event: DataGridRowEvent) => void;
|
|
79
|
+
onRowContextMenu?: (row: Row, event: DataGridRowEvent) => void;
|
|
80
|
+
renderRowActions?: (row: Row) => ReactNode;
|
|
81
|
+
rowActionsHeader?: ReactNode;
|
|
82
|
+
renderExpandedRow?: (row: Row) => ReactNode;
|
|
83
|
+
isRowExpandable?: (row: Row) => boolean;
|
|
84
|
+
expandedRowIds?: string[];
|
|
85
|
+
defaultExpandedRowIds?: string[];
|
|
86
|
+
onExpandedRowIdsChange?: (rowIds: string[]) => void;
|
|
87
|
+
labels?: Partial<DataGridLabels>;
|
|
88
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type GridEntry<Row> = {
|
|
2
|
+
kind: 'row';
|
|
3
|
+
row: Row;
|
|
4
|
+
rowIndex: number;
|
|
5
|
+
} | {
|
|
6
|
+
kind: 'detail';
|
|
7
|
+
row: Row;
|
|
8
|
+
rowIndex: number;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Flattens rows and the detail panels of expanded rows into the one list the virtualiser walks.
|
|
12
|
+
* `aria-rowindex` of an entry is 2 plus its position here (row 1 is the header). A row gets a panel
|
|
13
|
+
* when its id is in `expandedIds` and `canExpand` allows it.
|
|
14
|
+
*/
|
|
15
|
+
export declare function useGridEntries<Row extends {
|
|
16
|
+
id: string;
|
|
17
|
+
}>(rows: readonly Row[], expandedIds: ReadonlySet<string>, canExpand: (row: Row) => boolean): GridEntry<Row>[];
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
function useGridEntries(rows, expandedIds, canExpand) {
|
|
3
|
+
const rowsById = useMemo(()=>new Map(rows.map((row)=>[
|
|
4
|
+
row.id,
|
|
5
|
+
row
|
|
6
|
+
])), [
|
|
7
|
+
rows
|
|
8
|
+
]);
|
|
9
|
+
const openIdsKey = JSON.stringify([
|
|
10
|
+
...expandedIds
|
|
11
|
+
].filter((rowId)=>{
|
|
12
|
+
const row = rowsById.get(rowId);
|
|
13
|
+
return void 0 !== row && canExpand(row);
|
|
14
|
+
}));
|
|
15
|
+
return useMemo(()=>{
|
|
16
|
+
const openIds = new Set(JSON.parse(openIdsKey));
|
|
17
|
+
return rows.flatMap((row, rowIndex)=>openIds.has(row.id) ? [
|
|
18
|
+
{
|
|
19
|
+
kind: 'row',
|
|
20
|
+
row,
|
|
21
|
+
rowIndex
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
kind: 'detail',
|
|
25
|
+
row,
|
|
26
|
+
rowIndex
|
|
27
|
+
}
|
|
28
|
+
] : [
|
|
29
|
+
{
|
|
30
|
+
kind: 'row',
|
|
31
|
+
row,
|
|
32
|
+
rowIndex
|
|
33
|
+
}
|
|
34
|
+
]);
|
|
35
|
+
}, [
|
|
36
|
+
rows,
|
|
37
|
+
openIdsKey
|
|
38
|
+
]);
|
|
39
|
+
}
|
|
40
|
+
export { useGridEntries };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface UseGridHighlightOptions<Row> {
|
|
2
|
+
rows: readonly Row[];
|
|
3
|
+
getRowId: (row: Row) => string;
|
|
4
|
+
/** Rows on their way in are not yet the rows the highlight must match. */
|
|
5
|
+
loading: boolean;
|
|
6
|
+
highlightedRowId?: string | null;
|
|
7
|
+
onHighlightChange?: (rowId: string | null) => void;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The highlighted row: controlled when `highlightedRowId` is given (null included), else the
|
|
11
|
+
* grid's own. A highlight whose row leaves the grid is cleared, and the consumer hears of it.
|
|
12
|
+
*/
|
|
13
|
+
export declare function useGridHighlight<Row>({ rows, getRowId, loading, highlightedRowId, onHighlightChange, }: UseGridHighlightOptions<Row>): {
|
|
14
|
+
highlightedId: string | null;
|
|
15
|
+
setHighlightedId: (rowId: string | null) => void;
|
|
16
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { useLatest } from "./useLatest.js";
|
|
3
|
+
function useGridHighlight({ rows, getRowId, loading, highlightedRowId, onHighlightChange }) {
|
|
4
|
+
const [ownHighlightedId, setOwnHighlightedId] = useState(null);
|
|
5
|
+
const highlightedId = void 0 === highlightedRowId ? ownHighlightedId : highlightedRowId;
|
|
6
|
+
const setHighlightedId = (next)=>{
|
|
7
|
+
onHighlightChange?.(next);
|
|
8
|
+
if (void 0 === highlightedRowId) setOwnHighlightedId(next);
|
|
9
|
+
};
|
|
10
|
+
const clearHighlightRef = useLatest(()=>setHighlightedId(null));
|
|
11
|
+
const isStale = null !== highlightedId && !loading && !rows.some((row)=>getRowId(row) === highlightedId);
|
|
12
|
+
useEffect(()=>{
|
|
13
|
+
if (isStale) clearHighlightRef.current();
|
|
14
|
+
}, [
|
|
15
|
+
isStale,
|
|
16
|
+
rows,
|
|
17
|
+
clearHighlightRef
|
|
18
|
+
]);
|
|
19
|
+
return {
|
|
20
|
+
highlightedId,
|
|
21
|
+
setHighlightedId
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export { useGridHighlight };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type FocusEvent, type KeyboardEvent, type RefObject } from 'react';
|
|
2
|
+
export interface GridCellPosition {
|
|
3
|
+
/** 0 is the header row; body entry `n` is row `n + 1`. */
|
|
4
|
+
rowIndex: number;
|
|
5
|
+
/** Counts every rendered column. */
|
|
6
|
+
columnIndex: number;
|
|
7
|
+
}
|
|
8
|
+
export type GridCellKeyHandler = (rowIndex: number, columnIndex: number, event: KeyboardEvent<HTMLElement>) => void;
|
|
9
|
+
export interface UseGridKeyboardOptions {
|
|
10
|
+
/** The rows focus can reach, the header row included. */
|
|
11
|
+
rowCount: number;
|
|
12
|
+
columnCount: number;
|
|
13
|
+
/** How many rows Page Up and Page Down move; called from key handlers only. */
|
|
14
|
+
getPageSize: () => number;
|
|
15
|
+
/**
|
|
16
|
+
* Enter or Space on a focused cell. Calling `event.preventDefault()` claims the key; an Enter left
|
|
17
|
+
* unclaimed moves focus into the cell's controls, if it has any. Space is claimed afterwards
|
|
18
|
+
* either way, so that it never scrolls the body.
|
|
19
|
+
*/
|
|
20
|
+
onActivate: GridCellKeyHandler;
|
|
21
|
+
/** The context menu key or Shift+F10 on a focused cell. */
|
|
22
|
+
onContextMenu: GridCellKeyHandler;
|
|
23
|
+
/** Scrolls a body entry (`rowIndex - 1`) into view, so that it renders. */
|
|
24
|
+
scrollToEntry: (entryIndex: number) => void;
|
|
25
|
+
}
|
|
26
|
+
export interface GridCellProps {
|
|
27
|
+
tabIndex: number;
|
|
28
|
+
onKeyDown: (event: KeyboardEvent<HTMLElement>) => void;
|
|
29
|
+
onFocus: (event: FocusEvent<HTMLElement>) => void;
|
|
30
|
+
onBlur: (event: FocusEvent<HTMLElement>) => void;
|
|
31
|
+
'data-focused': '' | undefined;
|
|
32
|
+
}
|
|
33
|
+
/** The props of the cell at a position; a cell spanning several columns gives its `columnSpan`. */
|
|
34
|
+
export type GetCellProps = (rowIndex: number, columnIndex: number, columnSpan?: number) => GridCellProps;
|
|
35
|
+
/**
|
|
36
|
+
* What counts as a control inside a cell. Controls are out of the tab order until the user enters
|
|
37
|
+
* their cell, and a click on one is not a click on its row.
|
|
38
|
+
*/
|
|
39
|
+
export declare const cellControlSelector = "button, a[href], input, select, textarea, [role=\"button\"], [tabindex]";
|
|
40
|
+
/**
|
|
41
|
+
* Roving focus for a grid, per the WAI-ARIA data grid pattern: one cell is the tab stop, the arrow
|
|
42
|
+
* keys, Home, End and the page keys move it, and Enter or F2 moves into a cell's controls, from
|
|
43
|
+
* which Escape returns. The focused position is clamped to the grid's bounds on every render, so
|
|
44
|
+
* rows or columns going away leave focus at the same position.
|
|
45
|
+
*/
|
|
46
|
+
export declare function useGridKeyboard({ rowCount, columnCount, getPageSize, onActivate, onContextMenu, scrollToEntry, }: UseGridKeyboardOptions): {
|
|
47
|
+
focusedCell: GridCellPosition;
|
|
48
|
+
getCellProps: GetCellProps;
|
|
49
|
+
gridRef: RefObject<HTMLTableElement | null>;
|
|
50
|
+
};
|