@olenbetong/appframe-ds 1.6.1 → 1.7.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/CHANGELOG.md +21 -0
- package/package.json +2 -2
- package/src/AfLookup/Lookup.css +147 -147
- package/src/AfLookup/Lookup.tsx +186 -186
- package/src/AfLookup/LookupCombobox.css +69 -69
- package/src/AfLookup/LookupCombobox.tsx +273 -273
- package/src/AfLookup/LookupEdit.tsx +130 -130
- package/src/AfLookup/LookupGrid.css +145 -145
- package/src/AfLookup/LookupGrid.test.tsx +142 -142
- package/src/AfLookup/LookupGrid.tsx +324 -324
- package/src/AfLookup/index.ts +5 -5
- package/src/grid/index.css +8 -2
- package/src/grid/index.tsx +73 -3
- package/src/grid/useAfGridApi.ts +3 -0
- package/src/layout/AppMenuItem.css +16 -2
- package/src/mdi/Mdi.css +138 -138
|
@@ -1,324 +1,324 @@
|
|
|
1
|
-
import "./LookupGrid.css";
|
|
2
|
-
|
|
3
|
-
import { ArrowDownIcon, ArrowUpIcon } from "@navikt/aksel-icons";
|
|
4
|
-
import type { LookupColumn } from "@olenbetong/appframe-react";
|
|
5
|
-
|
|
6
|
-
import clsx from "clsx";
|
|
7
|
-
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
8
|
-
import { List, type RowComponentProps, useListRef } from "react-window";
|
|
9
|
-
import { getLocalizedString } from "../i18n.js";
|
|
10
|
-
|
|
11
|
-
const ROW_HEIGHT = 40;
|
|
12
|
-
const HEADER_HEIGHT = 40;
|
|
13
|
-
/** Narrowest a column can be made, and what a flexible column counts for in the minimum width */
|
|
14
|
-
const MIN_COLUMN_WIDTH = 80;
|
|
15
|
-
|
|
16
|
-
export type LookupGridSort = {
|
|
17
|
-
field: string;
|
|
18
|
-
direction: "asc" | "desc";
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export interface LookupGridProps<T extends Record<string, unknown>> {
|
|
22
|
-
columns: LookupColumn<T>[];
|
|
23
|
-
data: T[];
|
|
24
|
-
height: number;
|
|
25
|
-
currentIndex: number;
|
|
26
|
-
currentColumn: number;
|
|
27
|
-
getItemId: (item: T | undefined) => string | undefined;
|
|
28
|
-
isItemSelected?: (item: T) => boolean;
|
|
29
|
-
onSelectItem: (item: T) => void;
|
|
30
|
-
/**
|
|
31
|
-
* The column the rows are sorted by, shown with an arrow in its header
|
|
32
|
-
*/
|
|
33
|
-
sort?: LookupGridSort;
|
|
34
|
-
/**
|
|
35
|
-
* Called with the field of a header the user clicked. When left out the headers are plain text.
|
|
36
|
-
*/
|
|
37
|
-
onSort?: (field: string) => void;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** The track for one column in `grid-template-columns`; a width the user dragged wins over the configured one */
|
|
41
|
-
function getColumnTrack<T extends Record<string, unknown>>(column: LookupColumn<T>, resizedWidth: number | undefined) {
|
|
42
|
-
if (resizedWidth !== undefined) {
|
|
43
|
-
return `${resizedWidth}px`;
|
|
44
|
-
}
|
|
45
|
-
if (typeof column.width === "number") {
|
|
46
|
-
return `${column.width}px`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return column.width ?? `minmax(${MIN_COLUMN_WIDTH}px, 1fr)`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** What a column contributes to the narrowest width the grid can be laid out in */
|
|
53
|
-
function getColumnMinWidth<T extends Record<string, unknown>>(
|
|
54
|
-
column: LookupColumn<T>,
|
|
55
|
-
resizedWidth: number | undefined,
|
|
56
|
-
) {
|
|
57
|
-
if (resizedWidth !== undefined) {
|
|
58
|
-
return resizedWidth;
|
|
59
|
-
}
|
|
60
|
-
if (typeof column.width === "number") {
|
|
61
|
-
return column.width;
|
|
62
|
-
}
|
|
63
|
-
if (typeof column.width === "string" && /^\d+(\.\d+)?px$/.test(column.width)) {
|
|
64
|
-
return Number.parseFloat(column.width);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return MIN_COLUMN_WIDTH;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
type LookupGridRowProps<T extends Record<string, unknown>> = Omit<LookupGridProps<T>, "height" | "sort" | "onSort"> & {
|
|
71
|
-
gridTemplateColumns: string;
|
|
72
|
-
minWidth: number;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
function LookupGridRow<T extends Record<string, unknown>>({
|
|
76
|
-
index,
|
|
77
|
-
style,
|
|
78
|
-
columns,
|
|
79
|
-
data,
|
|
80
|
-
currentIndex,
|
|
81
|
-
currentColumn,
|
|
82
|
-
gridTemplateColumns,
|
|
83
|
-
minWidth,
|
|
84
|
-
getItemId,
|
|
85
|
-
isItemSelected,
|
|
86
|
-
onSelectItem,
|
|
87
|
-
}: RowComponentProps<LookupGridRowProps<T>>) {
|
|
88
|
-
let item = data[index];
|
|
89
|
-
let itemId = getItemId(item);
|
|
90
|
-
let active = index === currentIndex;
|
|
91
|
-
|
|
92
|
-
return (
|
|
93
|
-
<div
|
|
94
|
-
role="row"
|
|
95
|
-
aria-selected={active}
|
|
96
|
-
className={clsx("ObLookupGrid-row", active && "active", isItemSelected?.(item) && "selected")}
|
|
97
|
-
style={{ ...style, gridTemplateColumns, minWidth }}
|
|
98
|
-
onClick={() => onSelectItem(item)}
|
|
99
|
-
id={itemId}
|
|
100
|
-
>
|
|
101
|
-
{columns.map((column, columnIndex) => (
|
|
102
|
-
<div
|
|
103
|
-
role="gridcell"
|
|
104
|
-
className={clsx("ObLookupGrid-cell", active && columnIndex === currentColumn && "active")}
|
|
105
|
-
key={column.field}
|
|
106
|
-
title={String(item[column.field] ?? "")}
|
|
107
|
-
>
|
|
108
|
-
{/* The cell is a flex container, so the ellipsis has to be on an element inside it */}
|
|
109
|
-
<span className="ObLookupGrid-cellText">
|
|
110
|
-
{column.renderCell ? column.renderCell(item[column.field], item) : String(item[column.field] ?? "")}
|
|
111
|
-
</span>
|
|
112
|
-
</div>
|
|
113
|
-
))}
|
|
114
|
-
</div>
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
type ColumnResizerProps = {
|
|
119
|
-
onResize: (delta: number) => void;
|
|
120
|
-
/** Called with the handle, whose parent is the header cell being resized */
|
|
121
|
-
onResizeStart: (handle: HTMLElement) => void;
|
|
122
|
-
onResizeEnd: () => void;
|
|
123
|
-
onReset: () => void;
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* The drag handle at the end of a header cell. Pointer capture keeps the drag alive when the
|
|
128
|
-
* pointer leaves the handle; a double click puts the column back to its configured width.
|
|
129
|
-
*/
|
|
130
|
-
function ColumnResizer({ onResize, onResizeStart, onResizeEnd, onReset }: ColumnResizerProps) {
|
|
131
|
-
let startX = useRef(0);
|
|
132
|
-
|
|
133
|
-
return (
|
|
134
|
-
// oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- a drag handle; the grid is keyboard navigable by cell
|
|
135
|
-
<div
|
|
136
|
-
role="separator"
|
|
137
|
-
aria-orientation="vertical"
|
|
138
|
-
aria-label={getLocalizedString("Resize column")}
|
|
139
|
-
className="ObLookupGrid-resizer"
|
|
140
|
-
onClick={(event) => event.stopPropagation()}
|
|
141
|
-
onDoubleClick={(event) => {
|
|
142
|
-
event.stopPropagation();
|
|
143
|
-
onReset();
|
|
144
|
-
}}
|
|
145
|
-
onPointerDown={(event) => {
|
|
146
|
-
if (event.button !== 0) return;
|
|
147
|
-
event.preventDefault();
|
|
148
|
-
event.stopPropagation();
|
|
149
|
-
event.currentTarget.setPointerCapture(event.pointerId);
|
|
150
|
-
startX.current = event.clientX;
|
|
151
|
-
onResizeStart(event.currentTarget);
|
|
152
|
-
}}
|
|
153
|
-
onPointerMove={(event) => {
|
|
154
|
-
if (!event.currentTarget.hasPointerCapture(event.pointerId)) return;
|
|
155
|
-
onResize(event.clientX - startX.current);
|
|
156
|
-
}}
|
|
157
|
-
onPointerUp={(event) => {
|
|
158
|
-
if (!event.currentTarget.hasPointerCapture(event.pointerId)) return;
|
|
159
|
-
event.currentTarget.releasePointerCapture(event.pointerId);
|
|
160
|
-
onResizeEnd();
|
|
161
|
-
}}
|
|
162
|
-
onPointerCancel={onResizeEnd}
|
|
163
|
-
/>
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* The grid variant of the lookup popup: a header row and a virtualised list of rows, laid out
|
|
169
|
-
* with the same `grid-template-columns`.
|
|
170
|
-
*
|
|
171
|
-
* Only the list scrolls. It scrolls both ways when the columns are wider than the popup, and
|
|
172
|
-
* the header follows it sideways with a transform, so the header never needs a scrollbar of
|
|
173
|
-
* its own. The list reserves its vertical scrollbar gutter, and the header row is given the
|
|
174
|
-
* list's inner width, so flexible (`1fr`) columns line up between the two.
|
|
175
|
-
*
|
|
176
|
-
* Columns can be resized by dragging the handle at the end of a header cell. The widths live
|
|
177
|
-
* in the grid, so they last for as long as the popup is open.
|
|
178
|
-
*/
|
|
179
|
-
export function LookupGrid<T extends Record<string, unknown>>({
|
|
180
|
-
columns,
|
|
181
|
-
data,
|
|
182
|
-
height,
|
|
183
|
-
currentIndex,
|
|
184
|
-
currentColumn,
|
|
185
|
-
getItemId,
|
|
186
|
-
isItemSelected,
|
|
187
|
-
onSelectItem,
|
|
188
|
-
sort,
|
|
189
|
-
onSort,
|
|
190
|
-
}: LookupGridProps<T>) {
|
|
191
|
-
let listRef = useListRef(null);
|
|
192
|
-
let headerRowRef = useRef<HTMLDivElement>(null);
|
|
193
|
-
let [resizedWidths, setResizedWidths] = useState<Partial<Record<string, number>>>({});
|
|
194
|
-
let [resizing, setResizing] = useState<{ field: string; startWidth: number } | null>(null);
|
|
195
|
-
let [listInnerWidth, setListInnerWidth] = useState<number>();
|
|
196
|
-
|
|
197
|
-
let gridTemplateColumns = useMemo(
|
|
198
|
-
() => columns.map((column) => getColumnTrack(column, resizedWidths[column.field])).join(" "),
|
|
199
|
-
[columns, resizedWidths],
|
|
200
|
-
);
|
|
201
|
-
let minWidth = useMemo(
|
|
202
|
-
() => columns.reduce((sum, column) => sum + getColumnMinWidth(column, resizedWidths[column.field]), 0),
|
|
203
|
-
[columns, resizedWidths],
|
|
204
|
-
);
|
|
205
|
-
|
|
206
|
-
useEffect(() => {
|
|
207
|
-
if (currentIndex >= 0 && currentIndex < data.length) {
|
|
208
|
-
listRef.current?.scrollToRow({ index: currentIndex, align: "smart" });
|
|
209
|
-
}
|
|
210
|
-
}, [currentIndex, data.length, listRef]);
|
|
211
|
-
|
|
212
|
-
// The header row is as wide as the list's inner (client) width, so 1fr columns match the rows
|
|
213
|
-
useEffect(() => {
|
|
214
|
-
let element = listRef.current?.element;
|
|
215
|
-
if (!element) return;
|
|
216
|
-
|
|
217
|
-
let measure = () => setListInnerWidth(element.clientWidth);
|
|
218
|
-
measure();
|
|
219
|
-
|
|
220
|
-
let observer = new ResizeObserver(measure);
|
|
221
|
-
observer.observe(element);
|
|
222
|
-
|
|
223
|
-
return () => observer.disconnect();
|
|
224
|
-
}, [listRef, height]);
|
|
225
|
-
|
|
226
|
-
let handleScroll = useCallback((event: React.UIEvent<HTMLDivElement>) => {
|
|
227
|
-
if (headerRowRef.current) {
|
|
228
|
-
headerRowRef.current.style.transform = `translateX(${-event.currentTarget.scrollLeft}px)`;
|
|
229
|
-
}
|
|
230
|
-
}, []);
|
|
231
|
-
|
|
232
|
-
function startResize(field: string, cell: HTMLElement) {
|
|
233
|
-
setResizing({ field, startWidth: cell.getBoundingClientRect().width });
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
function resizeBy(delta: number) {
|
|
237
|
-
if (!resizing) return;
|
|
238
|
-
let { field, startWidth } = resizing;
|
|
239
|
-
let width = Math.max(MIN_COLUMN_WIDTH, Math.round(startWidth + delta));
|
|
240
|
-
setResizedWidths((widths) => (widths[field] === width ? widths : { ...widths, [field]: width }));
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function resetWidth(field: string) {
|
|
244
|
-
setResizedWidths(({ [field]: _removed, ...rest }) => rest);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
return (
|
|
248
|
-
<div
|
|
249
|
-
role="grid"
|
|
250
|
-
aria-label={getLocalizedString("Search results")}
|
|
251
|
-
className="ObLookupGrid-root"
|
|
252
|
-
data-resizing={resizing ? "" : undefined}
|
|
253
|
-
>
|
|
254
|
-
<div className="ObLookupGrid-header">
|
|
255
|
-
<div
|
|
256
|
-
role="row"
|
|
257
|
-
ref={headerRowRef}
|
|
258
|
-
className="ObLookupGrid-headerRow"
|
|
259
|
-
style={{ gridTemplateColumns, minWidth, width: listInnerWidth }}
|
|
260
|
-
>
|
|
261
|
-
{columns.map((column) => {
|
|
262
|
-
let sortable = !!onSort && column.sortable !== false;
|
|
263
|
-
let sorted = sort?.field === column.field ? sort.direction : undefined;
|
|
264
|
-
let label = column.headerName ?? column.field;
|
|
265
|
-
let SortIcon = sorted === "desc" ? ArrowDownIcon : ArrowUpIcon;
|
|
266
|
-
|
|
267
|
-
return (
|
|
268
|
-
<div
|
|
269
|
-
role="columnheader"
|
|
270
|
-
className="ObLookupGrid-headerCell"
|
|
271
|
-
key={column.field}
|
|
272
|
-
aria-sort={sorted === "asc" ? "ascending" : sorted === "desc" ? "descending" : undefined}
|
|
273
|
-
>
|
|
274
|
-
{sortable ? (
|
|
275
|
-
<button
|
|
276
|
-
type="button"
|
|
277
|
-
className={clsx("ObLookupGrid-sortButton", sorted && "sorted")}
|
|
278
|
-
// Keeps the focus in the search field, so typing carries on after a click
|
|
279
|
-
onMouseDown={(event) => event.preventDefault()}
|
|
280
|
-
onClick={() => onSort?.(column.field)}
|
|
281
|
-
tabIndex={-1}
|
|
282
|
-
>
|
|
283
|
-
<span className="ObLookupGrid-headerText">{label}</span>
|
|
284
|
-
<SortIcon className="ObLookupGrid-sortIcon" aria-hidden />
|
|
285
|
-
</button>
|
|
286
|
-
) : (
|
|
287
|
-
<span className="ObLookupGrid-headerText">{label}</span>
|
|
288
|
-
)}
|
|
289
|
-
<ColumnResizer
|
|
290
|
-
onResizeStart={(handle) => startResize(column.field, handle.parentElement as HTMLElement)}
|
|
291
|
-
onResize={resizeBy}
|
|
292
|
-
onResizeEnd={() => setResizing(null)}
|
|
293
|
-
onReset={() => resetWidth(column.field)}
|
|
294
|
-
/>
|
|
295
|
-
</div>
|
|
296
|
-
);
|
|
297
|
-
})}
|
|
298
|
-
</div>
|
|
299
|
-
</div>
|
|
300
|
-
<List<LookupGridRowProps<T>>
|
|
301
|
-
role="rowgroup"
|
|
302
|
-
listRef={listRef}
|
|
303
|
-
className="ObLookupGrid-list"
|
|
304
|
-
style={{ height: Math.max(0, height - HEADER_HEIGHT) }}
|
|
305
|
-
rowCount={data.length}
|
|
306
|
-
rowHeight={ROW_HEIGHT}
|
|
307
|
-
overscanCount={5}
|
|
308
|
-
onScroll={handleScroll}
|
|
309
|
-
rowComponent={LookupGridRow}
|
|
310
|
-
rowProps={{
|
|
311
|
-
columns,
|
|
312
|
-
data,
|
|
313
|
-
currentIndex,
|
|
314
|
-
currentColumn,
|
|
315
|
-
gridTemplateColumns,
|
|
316
|
-
minWidth,
|
|
317
|
-
getItemId,
|
|
318
|
-
isItemSelected,
|
|
319
|
-
onSelectItem,
|
|
320
|
-
}}
|
|
321
|
-
/>
|
|
322
|
-
</div>
|
|
323
|
-
);
|
|
324
|
-
}
|
|
1
|
+
import "./LookupGrid.css";
|
|
2
|
+
|
|
3
|
+
import { ArrowDownIcon, ArrowUpIcon } from "@navikt/aksel-icons";
|
|
4
|
+
import type { LookupColumn } from "@olenbetong/appframe-react";
|
|
5
|
+
|
|
6
|
+
import clsx from "clsx";
|
|
7
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
8
|
+
import { List, type RowComponentProps, useListRef } from "react-window";
|
|
9
|
+
import { getLocalizedString } from "../i18n.js";
|
|
10
|
+
|
|
11
|
+
const ROW_HEIGHT = 40;
|
|
12
|
+
const HEADER_HEIGHT = 40;
|
|
13
|
+
/** Narrowest a column can be made, and what a flexible column counts for in the minimum width */
|
|
14
|
+
const MIN_COLUMN_WIDTH = 80;
|
|
15
|
+
|
|
16
|
+
export type LookupGridSort = {
|
|
17
|
+
field: string;
|
|
18
|
+
direction: "asc" | "desc";
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export interface LookupGridProps<T extends Record<string, unknown>> {
|
|
22
|
+
columns: LookupColumn<T>[];
|
|
23
|
+
data: T[];
|
|
24
|
+
height: number;
|
|
25
|
+
currentIndex: number;
|
|
26
|
+
currentColumn: number;
|
|
27
|
+
getItemId: (item: T | undefined) => string | undefined;
|
|
28
|
+
isItemSelected?: (item: T) => boolean;
|
|
29
|
+
onSelectItem: (item: T) => void;
|
|
30
|
+
/**
|
|
31
|
+
* The column the rows are sorted by, shown with an arrow in its header
|
|
32
|
+
*/
|
|
33
|
+
sort?: LookupGridSort;
|
|
34
|
+
/**
|
|
35
|
+
* Called with the field of a header the user clicked. When left out the headers are plain text.
|
|
36
|
+
*/
|
|
37
|
+
onSort?: (field: string) => void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The track for one column in `grid-template-columns`; a width the user dragged wins over the configured one */
|
|
41
|
+
function getColumnTrack<T extends Record<string, unknown>>(column: LookupColumn<T>, resizedWidth: number | undefined) {
|
|
42
|
+
if (resizedWidth !== undefined) {
|
|
43
|
+
return `${resizedWidth}px`;
|
|
44
|
+
}
|
|
45
|
+
if (typeof column.width === "number") {
|
|
46
|
+
return `${column.width}px`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return column.width ?? `minmax(${MIN_COLUMN_WIDTH}px, 1fr)`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** What a column contributes to the narrowest width the grid can be laid out in */
|
|
53
|
+
function getColumnMinWidth<T extends Record<string, unknown>>(
|
|
54
|
+
column: LookupColumn<T>,
|
|
55
|
+
resizedWidth: number | undefined,
|
|
56
|
+
) {
|
|
57
|
+
if (resizedWidth !== undefined) {
|
|
58
|
+
return resizedWidth;
|
|
59
|
+
}
|
|
60
|
+
if (typeof column.width === "number") {
|
|
61
|
+
return column.width;
|
|
62
|
+
}
|
|
63
|
+
if (typeof column.width === "string" && /^\d+(\.\d+)?px$/.test(column.width)) {
|
|
64
|
+
return Number.parseFloat(column.width);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return MIN_COLUMN_WIDTH;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
type LookupGridRowProps<T extends Record<string, unknown>> = Omit<LookupGridProps<T>, "height" | "sort" | "onSort"> & {
|
|
71
|
+
gridTemplateColumns: string;
|
|
72
|
+
minWidth: number;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
function LookupGridRow<T extends Record<string, unknown>>({
|
|
76
|
+
index,
|
|
77
|
+
style,
|
|
78
|
+
columns,
|
|
79
|
+
data,
|
|
80
|
+
currentIndex,
|
|
81
|
+
currentColumn,
|
|
82
|
+
gridTemplateColumns,
|
|
83
|
+
minWidth,
|
|
84
|
+
getItemId,
|
|
85
|
+
isItemSelected,
|
|
86
|
+
onSelectItem,
|
|
87
|
+
}: RowComponentProps<LookupGridRowProps<T>>) {
|
|
88
|
+
let item = data[index];
|
|
89
|
+
let itemId = getItemId(item);
|
|
90
|
+
let active = index === currentIndex;
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<div
|
|
94
|
+
role="row"
|
|
95
|
+
aria-selected={active}
|
|
96
|
+
className={clsx("ObLookupGrid-row", active && "active", isItemSelected?.(item) && "selected")}
|
|
97
|
+
style={{ ...style, gridTemplateColumns, minWidth }}
|
|
98
|
+
onClick={() => onSelectItem(item)}
|
|
99
|
+
id={itemId}
|
|
100
|
+
>
|
|
101
|
+
{columns.map((column, columnIndex) => (
|
|
102
|
+
<div
|
|
103
|
+
role="gridcell"
|
|
104
|
+
className={clsx("ObLookupGrid-cell", active && columnIndex === currentColumn && "active")}
|
|
105
|
+
key={column.field}
|
|
106
|
+
title={String(item[column.field] ?? "")}
|
|
107
|
+
>
|
|
108
|
+
{/* The cell is a flex container, so the ellipsis has to be on an element inside it */}
|
|
109
|
+
<span className="ObLookupGrid-cellText">
|
|
110
|
+
{column.renderCell ? column.renderCell(item[column.field], item) : String(item[column.field] ?? "")}
|
|
111
|
+
</span>
|
|
112
|
+
</div>
|
|
113
|
+
))}
|
|
114
|
+
</div>
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
type ColumnResizerProps = {
|
|
119
|
+
onResize: (delta: number) => void;
|
|
120
|
+
/** Called with the handle, whose parent is the header cell being resized */
|
|
121
|
+
onResizeStart: (handle: HTMLElement) => void;
|
|
122
|
+
onResizeEnd: () => void;
|
|
123
|
+
onReset: () => void;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The drag handle at the end of a header cell. Pointer capture keeps the drag alive when the
|
|
128
|
+
* pointer leaves the handle; a double click puts the column back to its configured width.
|
|
129
|
+
*/
|
|
130
|
+
function ColumnResizer({ onResize, onResizeStart, onResizeEnd, onReset }: ColumnResizerProps) {
|
|
131
|
+
let startX = useRef(0);
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
// oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- a drag handle; the grid is keyboard navigable by cell
|
|
135
|
+
<div
|
|
136
|
+
role="separator"
|
|
137
|
+
aria-orientation="vertical"
|
|
138
|
+
aria-label={getLocalizedString("Resize column")}
|
|
139
|
+
className="ObLookupGrid-resizer"
|
|
140
|
+
onClick={(event) => event.stopPropagation()}
|
|
141
|
+
onDoubleClick={(event) => {
|
|
142
|
+
event.stopPropagation();
|
|
143
|
+
onReset();
|
|
144
|
+
}}
|
|
145
|
+
onPointerDown={(event) => {
|
|
146
|
+
if (event.button !== 0) return;
|
|
147
|
+
event.preventDefault();
|
|
148
|
+
event.stopPropagation();
|
|
149
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
150
|
+
startX.current = event.clientX;
|
|
151
|
+
onResizeStart(event.currentTarget);
|
|
152
|
+
}}
|
|
153
|
+
onPointerMove={(event) => {
|
|
154
|
+
if (!event.currentTarget.hasPointerCapture(event.pointerId)) return;
|
|
155
|
+
onResize(event.clientX - startX.current);
|
|
156
|
+
}}
|
|
157
|
+
onPointerUp={(event) => {
|
|
158
|
+
if (!event.currentTarget.hasPointerCapture(event.pointerId)) return;
|
|
159
|
+
event.currentTarget.releasePointerCapture(event.pointerId);
|
|
160
|
+
onResizeEnd();
|
|
161
|
+
}}
|
|
162
|
+
onPointerCancel={onResizeEnd}
|
|
163
|
+
/>
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The grid variant of the lookup popup: a header row and a virtualised list of rows, laid out
|
|
169
|
+
* with the same `grid-template-columns`.
|
|
170
|
+
*
|
|
171
|
+
* Only the list scrolls. It scrolls both ways when the columns are wider than the popup, and
|
|
172
|
+
* the header follows it sideways with a transform, so the header never needs a scrollbar of
|
|
173
|
+
* its own. The list reserves its vertical scrollbar gutter, and the header row is given the
|
|
174
|
+
* list's inner width, so flexible (`1fr`) columns line up between the two.
|
|
175
|
+
*
|
|
176
|
+
* Columns can be resized by dragging the handle at the end of a header cell. The widths live
|
|
177
|
+
* in the grid, so they last for as long as the popup is open.
|
|
178
|
+
*/
|
|
179
|
+
export function LookupGrid<T extends Record<string, unknown>>({
|
|
180
|
+
columns,
|
|
181
|
+
data,
|
|
182
|
+
height,
|
|
183
|
+
currentIndex,
|
|
184
|
+
currentColumn,
|
|
185
|
+
getItemId,
|
|
186
|
+
isItemSelected,
|
|
187
|
+
onSelectItem,
|
|
188
|
+
sort,
|
|
189
|
+
onSort,
|
|
190
|
+
}: LookupGridProps<T>) {
|
|
191
|
+
let listRef = useListRef(null);
|
|
192
|
+
let headerRowRef = useRef<HTMLDivElement>(null);
|
|
193
|
+
let [resizedWidths, setResizedWidths] = useState<Partial<Record<string, number>>>({});
|
|
194
|
+
let [resizing, setResizing] = useState<{ field: string; startWidth: number } | null>(null);
|
|
195
|
+
let [listInnerWidth, setListInnerWidth] = useState<number>();
|
|
196
|
+
|
|
197
|
+
let gridTemplateColumns = useMemo(
|
|
198
|
+
() => columns.map((column) => getColumnTrack(column, resizedWidths[column.field])).join(" "),
|
|
199
|
+
[columns, resizedWidths],
|
|
200
|
+
);
|
|
201
|
+
let minWidth = useMemo(
|
|
202
|
+
() => columns.reduce((sum, column) => sum + getColumnMinWidth(column, resizedWidths[column.field]), 0),
|
|
203
|
+
[columns, resizedWidths],
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
useEffect(() => {
|
|
207
|
+
if (currentIndex >= 0 && currentIndex < data.length) {
|
|
208
|
+
listRef.current?.scrollToRow({ index: currentIndex, align: "smart" });
|
|
209
|
+
}
|
|
210
|
+
}, [currentIndex, data.length, listRef]);
|
|
211
|
+
|
|
212
|
+
// The header row is as wide as the list's inner (client) width, so 1fr columns match the rows
|
|
213
|
+
useEffect(() => {
|
|
214
|
+
let element = listRef.current?.element;
|
|
215
|
+
if (!element) return;
|
|
216
|
+
|
|
217
|
+
let measure = () => setListInnerWidth(element.clientWidth);
|
|
218
|
+
measure();
|
|
219
|
+
|
|
220
|
+
let observer = new ResizeObserver(measure);
|
|
221
|
+
observer.observe(element);
|
|
222
|
+
|
|
223
|
+
return () => observer.disconnect();
|
|
224
|
+
}, [listRef, height]);
|
|
225
|
+
|
|
226
|
+
let handleScroll = useCallback((event: React.UIEvent<HTMLDivElement>) => {
|
|
227
|
+
if (headerRowRef.current) {
|
|
228
|
+
headerRowRef.current.style.transform = `translateX(${-event.currentTarget.scrollLeft}px)`;
|
|
229
|
+
}
|
|
230
|
+
}, []);
|
|
231
|
+
|
|
232
|
+
function startResize(field: string, cell: HTMLElement) {
|
|
233
|
+
setResizing({ field, startWidth: cell.getBoundingClientRect().width });
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function resizeBy(delta: number) {
|
|
237
|
+
if (!resizing) return;
|
|
238
|
+
let { field, startWidth } = resizing;
|
|
239
|
+
let width = Math.max(MIN_COLUMN_WIDTH, Math.round(startWidth + delta));
|
|
240
|
+
setResizedWidths((widths) => (widths[field] === width ? widths : { ...widths, [field]: width }));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function resetWidth(field: string) {
|
|
244
|
+
setResizedWidths(({ [field]: _removed, ...rest }) => rest);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return (
|
|
248
|
+
<div
|
|
249
|
+
role="grid"
|
|
250
|
+
aria-label={getLocalizedString("Search results")}
|
|
251
|
+
className="ObLookupGrid-root"
|
|
252
|
+
data-resizing={resizing ? "" : undefined}
|
|
253
|
+
>
|
|
254
|
+
<div className="ObLookupGrid-header">
|
|
255
|
+
<div
|
|
256
|
+
role="row"
|
|
257
|
+
ref={headerRowRef}
|
|
258
|
+
className="ObLookupGrid-headerRow"
|
|
259
|
+
style={{ gridTemplateColumns, minWidth, width: listInnerWidth }}
|
|
260
|
+
>
|
|
261
|
+
{columns.map((column) => {
|
|
262
|
+
let sortable = !!onSort && column.sortable !== false;
|
|
263
|
+
let sorted = sort?.field === column.field ? sort.direction : undefined;
|
|
264
|
+
let label = column.headerName ?? column.field;
|
|
265
|
+
let SortIcon = sorted === "desc" ? ArrowDownIcon : ArrowUpIcon;
|
|
266
|
+
|
|
267
|
+
return (
|
|
268
|
+
<div
|
|
269
|
+
role="columnheader"
|
|
270
|
+
className="ObLookupGrid-headerCell"
|
|
271
|
+
key={column.field}
|
|
272
|
+
aria-sort={sorted === "asc" ? "ascending" : sorted === "desc" ? "descending" : undefined}
|
|
273
|
+
>
|
|
274
|
+
{sortable ? (
|
|
275
|
+
<button
|
|
276
|
+
type="button"
|
|
277
|
+
className={clsx("ObLookupGrid-sortButton", sorted && "sorted")}
|
|
278
|
+
// Keeps the focus in the search field, so typing carries on after a click
|
|
279
|
+
onMouseDown={(event) => event.preventDefault()}
|
|
280
|
+
onClick={() => onSort?.(column.field)}
|
|
281
|
+
tabIndex={-1}
|
|
282
|
+
>
|
|
283
|
+
<span className="ObLookupGrid-headerText">{label}</span>
|
|
284
|
+
<SortIcon className="ObLookupGrid-sortIcon" aria-hidden />
|
|
285
|
+
</button>
|
|
286
|
+
) : (
|
|
287
|
+
<span className="ObLookupGrid-headerText">{label}</span>
|
|
288
|
+
)}
|
|
289
|
+
<ColumnResizer
|
|
290
|
+
onResizeStart={(handle) => startResize(column.field, handle.parentElement as HTMLElement)}
|
|
291
|
+
onResize={resizeBy}
|
|
292
|
+
onResizeEnd={() => setResizing(null)}
|
|
293
|
+
onReset={() => resetWidth(column.field)}
|
|
294
|
+
/>
|
|
295
|
+
</div>
|
|
296
|
+
);
|
|
297
|
+
})}
|
|
298
|
+
</div>
|
|
299
|
+
</div>
|
|
300
|
+
<List<LookupGridRowProps<T>>
|
|
301
|
+
role="rowgroup"
|
|
302
|
+
listRef={listRef}
|
|
303
|
+
className="ObLookupGrid-list"
|
|
304
|
+
style={{ height: Math.max(0, height - HEADER_HEIGHT) }}
|
|
305
|
+
rowCount={data.length}
|
|
306
|
+
rowHeight={ROW_HEIGHT}
|
|
307
|
+
overscanCount={5}
|
|
308
|
+
onScroll={handleScroll}
|
|
309
|
+
rowComponent={LookupGridRow}
|
|
310
|
+
rowProps={{
|
|
311
|
+
columns,
|
|
312
|
+
data,
|
|
313
|
+
currentIndex,
|
|
314
|
+
currentColumn,
|
|
315
|
+
gridTemplateColumns,
|
|
316
|
+
minWidth,
|
|
317
|
+
getItemId,
|
|
318
|
+
isItemSelected,
|
|
319
|
+
onSelectItem,
|
|
320
|
+
}}
|
|
321
|
+
/>
|
|
322
|
+
</div>
|
|
323
|
+
);
|
|
324
|
+
}
|
package/src/AfLookup/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from "./BoundLookup.js";
|
|
2
|
-
export * from "./LookupCombobox.js";
|
|
3
|
-
export * from "./Lookup.js";
|
|
4
|
-
export * from "./LookupEdit.js";
|
|
5
|
-
export * from "./LookupGrid.js";
|
|
1
|
+
export * from "./BoundLookup.js";
|
|
2
|
+
export * from "./LookupCombobox.js";
|
|
3
|
+
export * from "./Lookup.js";
|
|
4
|
+
export * from "./LookupEdit.js";
|
|
5
|
+
export * from "./LookupGrid.js";
|