@redvars/peacock 3.14.0 → 3.16.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/custom-elements-jsdocs.json +170 -19
- package/dist/custom-elements.json +1171 -249
- package/dist/modal.js +178 -99
- package/dist/modal.js.map +1 -1
- package/dist/pagination.js +408 -110
- package/dist/pagination.js.map +1 -1
- package/dist/src/modal/modal.d.ts +23 -8
- package/dist/src/pagination/page-items.d.ts +14 -0
- package/dist/src/pagination/pagination.d.ts +46 -7
- package/dist/src/table/table.d.ts +125 -27
- package/dist/table.js +1001 -337
- package/dist/table.js.map +1 -1
- package/dist/test/modal.test.d.ts +1 -0
- package/dist/test/pagination.test.d.ts +1 -0
- package/dist/test/table.test.d.ts +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/readme.md +2 -2
- package/src/modal/modal.scss +45 -11
- package/src/modal/modal.ts +148 -96
- package/src/pagination/page-items.ts +50 -0
- package/src/pagination/pagination.scss +159 -44
- package/src/pagination/pagination.ts +275 -74
- package/src/table/table.scss +331 -109
- package/src/table/table.ts +810 -271
package/src/table/table.ts
CHANGED
|
@@ -1,49 +1,100 @@
|
|
|
1
1
|
import { html, LitElement, nothing } from 'lit';
|
|
2
|
-
import {
|
|
2
|
+
import type { PropertyValues, TemplateResult } from 'lit';
|
|
3
|
+
import { property, query, state } from 'lit/decorators.js';
|
|
3
4
|
import { classMap } from 'lit/directives/class-map.js';
|
|
5
|
+
import { styleMap } from 'lit/directives/style-map.js';
|
|
4
6
|
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
|
|
5
7
|
|
|
6
|
-
import { throttle } from '@/__internal/utils/throttle.js';
|
|
7
|
-
|
|
8
8
|
import styles from './table.scss';
|
|
9
9
|
import IndividualComponent from '@/IndividualComponent.js';
|
|
10
|
+
import type { PaginationVariant } from '../pagination/pagination.js';
|
|
10
11
|
|
|
11
12
|
const DEFAULT_CELL_WIDTH = 16; // in rem
|
|
13
|
+
const CHECKBOX_CELL_WIDTH = 3; // in rem
|
|
14
|
+
const SKELETON_ROWS = 5;
|
|
15
|
+
|
|
16
|
+
/** A record shown in one row. */
|
|
17
|
+
export type TableRow = Record<string, any>;
|
|
12
18
|
|
|
13
19
|
export interface TableColumn {
|
|
20
|
+
/** Field of the row shown in this column. */
|
|
14
21
|
name: string;
|
|
22
|
+
/** Header text. */
|
|
15
23
|
label: string;
|
|
24
|
+
/** Width in rem. Defaults to 16. */
|
|
16
25
|
width?: number;
|
|
17
|
-
|
|
18
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Keeps the column in view while the table scrolls sideways. `true` or `"start"` pins it to
|
|
28
|
+
* the start edge, `"end"` to the end edge (for example a row-actions column).
|
|
29
|
+
*/
|
|
30
|
+
fixed?: boolean | 'start' | 'end';
|
|
31
|
+
/** Horizontal alignment. Number columns default to `"end"`, others to `"start"`. */
|
|
32
|
+
align?: 'start' | 'center' | 'end';
|
|
33
|
+
/** Data type. `"number"` aligns to the end and uses tabular figures. */
|
|
34
|
+
type?: 'text' | 'number';
|
|
35
|
+
/** Set to `false` to turn sorting off for this column only. */
|
|
36
|
+
sortable?: boolean;
|
|
37
|
+
/** Lets long text wrap instead of truncating with an ellipsis. */
|
|
38
|
+
wrap?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Custom cell content. Return a Lit `TemplateResult` (preferred), or a string of trusted
|
|
41
|
+
* HTML. A string is inserted as HTML without escaping, so never build it from user input.
|
|
42
|
+
*/
|
|
43
|
+
template?: (row: TableRow, column: TableColumn) => string | TemplateResult;
|
|
19
44
|
}
|
|
20
45
|
|
|
46
|
+
type ColumnPlacement = 'start' | 'scroll' | 'end';
|
|
47
|
+
|
|
48
|
+
/** A rendered column: a data column, or the selection checkbox column. */
|
|
49
|
+
interface GridColumn {
|
|
50
|
+
column?: TableColumn;
|
|
51
|
+
placement: ColumnPlacement;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const collator = new Intl.Collator(undefined, {
|
|
55
|
+
numeric: true,
|
|
56
|
+
sensitivity: 'base',
|
|
57
|
+
});
|
|
58
|
+
|
|
21
59
|
/**
|
|
22
60
|
* @label Table
|
|
23
61
|
* @tag wc-table
|
|
24
62
|
* @rawTag table
|
|
25
63
|
* @summary A configurable component for displaying tabular data.
|
|
26
64
|
* @overview
|
|
27
|
-
* <p>The table
|
|
65
|
+
* <p>The table displays rows of data with sorting, pagination, row selection, sticky header and sticky start or end columns.</p>
|
|
66
|
+
* <p>It follows the grid keyboard pattern: the table is one tab stop and the arrow keys move between cells. <kbd>Space</kbd> selects the focused row, <kbd>Enter</kbd> activates the focused cell, and <kbd>Ctrl</kbd>+<kbd>C</kbd> copies its text.</p>
|
|
67
|
+
* <p>When rows are selected, a bar over the header shows the count and the actions placed in the <code>bulk-actions</code> slot.</p>
|
|
68
|
+
*
|
|
69
|
+
* @fires {CustomEvent} cell-click - Dispatched when a cell is clicked or activated with Enter. Detail: `{ record, column, altKey, ctrlKey, metaKey, shiftKey }`.
|
|
70
|
+
* @fires {CustomEvent} row-click - Dispatched when a row is clicked or activated with Enter. Detail: `{ record }`.
|
|
71
|
+
* @fires {CustomEvent} selection-change - Dispatched when the row selection changes. Detail: `{ value, isSelectAll, allPages }`.
|
|
72
|
+
* @fires {CustomEvent} sort - Dispatched when the table is sorted. Detail: `{ sortBy, sortOrder }`.
|
|
73
|
+
* @fires {CustomEvent} page - Dispatched when the page or page size changes. Detail: `{ page, pageSize }`.
|
|
74
|
+
*
|
|
75
|
+
* @slot bulk-actions - Actions shown in the selection bar while rows are selected.
|
|
28
76
|
*
|
|
29
|
-
* @
|
|
30
|
-
* @
|
|
31
|
-
* @
|
|
32
|
-
* @fires {CustomEvent} page - Dispatched when the page or page size changes.
|
|
77
|
+
* @cssprop --table-row-height - Minimum height of a row. Set by `density`.
|
|
78
|
+
* @cssprop --table-border-color - Color of the row dividers.
|
|
79
|
+
* @cssprop --table-cell-padding-inline - Horizontal padding of a cell.
|
|
33
80
|
*
|
|
34
81
|
* @example
|
|
35
82
|
* ```html
|
|
36
|
-
* <wc-table columns="[{'name': 'name','label': 'Name','width': 16},{'name': 'age','label': 'Age','width': 7}]" data="[{'name': 'John','age': 30},{'name': 'Jane','age': 25}]"></wc-table>
|
|
83
|
+
* <wc-table columns="[{'name': 'name','label': 'Name','width': 16},{'name': 'age','label': 'Age','width': 7,'type': 'number'}]" data="[{'name': 'John','age': 30},{'name': 'Jane','age': 25}]"></wc-table>
|
|
37
84
|
* ```
|
|
38
85
|
* @tags display, data
|
|
39
86
|
*/
|
|
40
87
|
@IndividualComponent
|
|
41
88
|
export class Table extends LitElement {
|
|
89
|
+
// ── Static ───────────────────────────────────────────────────────────────
|
|
90
|
+
|
|
42
91
|
static styles = [styles];
|
|
43
92
|
|
|
93
|
+
// ── Properties ───────────────────────────────────────────────────────────
|
|
94
|
+
|
|
44
95
|
/**
|
|
45
96
|
* Grid columns configuration.
|
|
46
|
-
* Each column can have: name, label, width (
|
|
97
|
+
* Each column can have: name, label, width (rem), fixed, align, type, sortable, wrap, template.
|
|
47
98
|
*/
|
|
48
99
|
@property({ type: Array })
|
|
49
100
|
columns: TableColumn[] = [];
|
|
@@ -52,7 +103,7 @@ export class Table extends LitElement {
|
|
|
52
103
|
* Grid data to display on table.
|
|
53
104
|
*/
|
|
54
105
|
@property({ type: Array })
|
|
55
|
-
data:
|
|
106
|
+
data: TableRow[] = [];
|
|
56
107
|
|
|
57
108
|
/**
|
|
58
109
|
* Row selection type. Set to `"checkbox"` to enable checkbox selection.
|
|
@@ -97,7 +148,7 @@ export class Table extends LitElement {
|
|
|
97
148
|
sortOrder: 'asc' | 'desc' = 'asc';
|
|
98
149
|
|
|
99
150
|
/**
|
|
100
|
-
* If true, pagination is enabled. Defaults to `
|
|
151
|
+
* If true, pagination is enabled. Defaults to `false`.
|
|
101
152
|
*/
|
|
102
153
|
@property({ type: Boolean })
|
|
103
154
|
paginate: boolean = false;
|
|
@@ -115,11 +166,55 @@ export class Table extends LitElement {
|
|
|
115
166
|
pageSize: number = 10;
|
|
116
167
|
|
|
117
168
|
/**
|
|
118
|
-
*
|
|
169
|
+
* Supported page size options, passed to the pagination.
|
|
170
|
+
*/
|
|
171
|
+
@property({ type: Array, attribute: 'page-sizes' })
|
|
172
|
+
pageSizes: number[] | undefined;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Total number of items (used in managed/controlled mode). Leave it unset when the total is
|
|
176
|
+
* unknown; the pagination then shows previous and next, driven by `has-next`.
|
|
119
177
|
*/
|
|
120
178
|
@property({ type: Number, attribute: 'total-items' })
|
|
121
179
|
totalItems: number | undefined;
|
|
122
180
|
|
|
181
|
+
/**
|
|
182
|
+
* Whether another page exists after this one, in managed mode with an unknown total.
|
|
183
|
+
*/
|
|
184
|
+
@property({ type: Boolean, attribute: 'has-next' })
|
|
185
|
+
hasNext: boolean = false;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Pagination variant: `full` (page numbers), `compact` or `simple`. Defaults to `full`.
|
|
189
|
+
*/
|
|
190
|
+
@property({ type: String, attribute: 'pagination-variant' })
|
|
191
|
+
paginationVariant: PaginationVariant = 'full';
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Row height: `compact`, `default` or `comfortable`. Defaults to `default`.
|
|
195
|
+
*/
|
|
196
|
+
@property({ type: String, reflect: true })
|
|
197
|
+
density: 'compact' | 'default' | 'comfortable' = 'default';
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Shades every other row.
|
|
201
|
+
*/
|
|
202
|
+
@property({ type: Boolean, reflect: true })
|
|
203
|
+
striped: boolean = false;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Shows a loading state: skeleton rows while there is no data, otherwise a progress bar
|
|
207
|
+
* under the header so the current rows stay readable.
|
|
208
|
+
*/
|
|
209
|
+
@property({ type: Boolean, reflect: true })
|
|
210
|
+
loading: boolean = false;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Accessible name of the table.
|
|
214
|
+
*/
|
|
215
|
+
@property({ type: String })
|
|
216
|
+
label: string = '';
|
|
217
|
+
|
|
123
218
|
/**
|
|
124
219
|
* Headline text shown when the table has no data.
|
|
125
220
|
*/
|
|
@@ -132,60 +227,254 @@ export class Table extends LitElement {
|
|
|
132
227
|
@property({ type: String, attribute: 'empty-state-description' })
|
|
133
228
|
emptyStateDescription: string = 'There are no items to display';
|
|
134
229
|
|
|
135
|
-
|
|
230
|
+
// ── States ───────────────────────────────────────────────────────────────
|
|
231
|
+
|
|
232
|
+
/** True when every item across all pages is selected, not only the current page. */
|
|
136
233
|
@state()
|
|
137
|
-
private
|
|
234
|
+
private allPagesSelected: boolean = false;
|
|
138
235
|
|
|
139
|
-
/** True when
|
|
236
|
+
/** True when content is scrolled under the start-pinned columns. */
|
|
140
237
|
@state()
|
|
141
|
-
private
|
|
238
|
+
private scrolledFromStart: boolean = false;
|
|
142
239
|
|
|
143
|
-
/** True when
|
|
240
|
+
/** True when more content waits past the end-pinned columns. */
|
|
144
241
|
@state()
|
|
145
|
-
private
|
|
242
|
+
private scrolledFromEnd: boolean = false;
|
|
146
243
|
|
|
147
|
-
/**
|
|
244
|
+
/** The cell that holds the grid's single tab stop. Row 0 is the header. */
|
|
148
245
|
@state()
|
|
149
|
-
private
|
|
150
|
-
|
|
151
|
-
// ──
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
private
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
246
|
+
private activeCell: { row: number; col: number } = { row: 0, col: 0 };
|
|
247
|
+
|
|
248
|
+
// ── Queries ──────────────────────────────────────────────────────────────
|
|
249
|
+
|
|
250
|
+
@query('.table-scroll-container')
|
|
251
|
+
private scrollContainer?: HTMLElement;
|
|
252
|
+
|
|
253
|
+
// ── Private fields ───────────────────────────────────────────────────────
|
|
254
|
+
|
|
255
|
+
/** Set when a keyboard move must focus the active cell after the next render. */
|
|
256
|
+
private pendingFocus: boolean = false;
|
|
257
|
+
|
|
258
|
+
/** Watches the scroll container so the edge shadows follow resizes. */
|
|
259
|
+
private resizeObserver?: ResizeObserver;
|
|
260
|
+
|
|
261
|
+
// ── Lifecycle ────────────────────────────────────────────────────────────
|
|
262
|
+
|
|
263
|
+
disconnectedCallback() {
|
|
264
|
+
super.disconnectedCallback();
|
|
265
|
+
this.resizeObserver?.disconnect();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
firstUpdated() {
|
|
269
|
+
if (typeof ResizeObserver !== 'undefined' && this.scrollContainer) {
|
|
270
|
+
this.resizeObserver = new ResizeObserver(() => this.updateScrollEdges());
|
|
271
|
+
this.resizeObserver.observe(this.scrollContainer);
|
|
272
|
+
}
|
|
273
|
+
this.updateScrollEdges();
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
protected override willUpdate(changed: PropertyValues<Table>): void {
|
|
277
|
+
if (!Number.isFinite(this.page) || this.page < 1) {
|
|
278
|
+
this.page = 1;
|
|
165
279
|
}
|
|
166
|
-
this.onSelectChange(selectedRowKeys);
|
|
167
|
-
};
|
|
168
280
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (
|
|
172
|
-
this.
|
|
173
|
-
|
|
174
|
-
|
|
281
|
+
// Filtering the data can leave the current page past the end.
|
|
282
|
+
// An empty data set (for example before the first load) keeps the requested page.
|
|
283
|
+
if (
|
|
284
|
+
this.paginate &&
|
|
285
|
+
!this.managed &&
|
|
286
|
+
changed.has('data') &&
|
|
287
|
+
this.data?.length
|
|
288
|
+
) {
|
|
289
|
+
const lastPage = Math.max(
|
|
290
|
+
1,
|
|
291
|
+
Math.ceil((this.data?.length ?? 0) / this.pageSize),
|
|
175
292
|
);
|
|
293
|
+
if (this.page > lastPage) this.page = lastPage;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (changed.has('data') && !this.managed) {
|
|
297
|
+
this.allPagesSelected = false;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const rowCount = this.getPageRows().length;
|
|
301
|
+
const colCount = this.getGridColumns().length;
|
|
302
|
+
const row = Math.min(this.activeCell.row, rowCount);
|
|
303
|
+
const col = Math.min(this.activeCell.col, Math.max(0, colCount - 1));
|
|
304
|
+
if (row !== this.activeCell.row || col !== this.activeCell.col) {
|
|
305
|
+
this.activeCell = { row, col };
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
protected override updated(changed: PropertyValues<Table>): void {
|
|
310
|
+
if (changed.has('columns') || changed.has('data')) {
|
|
311
|
+
this.updateScrollEdges();
|
|
312
|
+
}
|
|
313
|
+
if (this.pendingFocus) {
|
|
314
|
+
this.pendingFocus = false;
|
|
315
|
+
this.getFocusTarget(this.activeCell.row, this.activeCell.col)?.focus();
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// ── Private methods ──────────────────────────────────────────────────────
|
|
320
|
+
|
|
321
|
+
/** Columns in visual order: checkbox, start-pinned, scrolling, end-pinned. */
|
|
322
|
+
private getGridColumns(): GridColumn[] {
|
|
323
|
+
const start: GridColumn[] = [];
|
|
324
|
+
const scroll: GridColumn[] = [];
|
|
325
|
+
const end: GridColumn[] = [];
|
|
326
|
+
|
|
327
|
+
if (this.selectionType === 'checkbox') {
|
|
328
|
+
start.push({ placement: 'start' });
|
|
329
|
+
}
|
|
330
|
+
(this.columns ?? []).forEach(column => {
|
|
331
|
+
const placement = Table.getPlacement(column);
|
|
332
|
+
const entry = { column, placement };
|
|
333
|
+
if (placement === 'start') start.push(entry);
|
|
334
|
+
else if (placement === 'end') end.push(entry);
|
|
335
|
+
else scroll.push(entry);
|
|
336
|
+
});
|
|
337
|
+
return [...start, ...scroll, ...end];
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
private static getPlacement(column: TableColumn): ColumnPlacement {
|
|
341
|
+
if (column.fixed === true || column.fixed === 'start') return 'start';
|
|
342
|
+
if (column.fixed === 'end') return 'end';
|
|
343
|
+
return 'scroll';
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
private static getAlign(column: TableColumn): 'start' | 'center' | 'end' {
|
|
347
|
+
return column.align ?? (column.type === 'number' ? 'end' : 'start');
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private static getWidth(column: TableColumn): number {
|
|
351
|
+
const width = Number.parseFloat(String(column.width ?? ''));
|
|
352
|
+
return Number.isFinite(width) && width > 0 ? width : DEFAULT_CELL_WIDTH;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
private get totalColumnsWidth(): number {
|
|
356
|
+
let total = this.selectionType === 'checkbox' ? CHECKBOX_CELL_WIDTH : 0;
|
|
357
|
+
(this.columns ?? []).forEach(col => {
|
|
358
|
+
total += Table.getWidth(col);
|
|
359
|
+
});
|
|
360
|
+
return total;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
private isColumnSortable(column: TableColumn): boolean {
|
|
364
|
+
return this.sortable && column.sortable !== false;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** Total item count across pages, or `undefined` when it is unknown. */
|
|
368
|
+
private getTotalItems(): number | undefined {
|
|
369
|
+
if (!this.managed) return this.data?.length ?? 0;
|
|
370
|
+
return this.totalItems ?? undefined;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Rows shown on the current page, sorted and sliced unless the table is managed. */
|
|
374
|
+
private getPageRows(): TableRow[] {
|
|
375
|
+
let rows = [...(this.data ?? [])];
|
|
376
|
+
if (this.managed) return rows;
|
|
377
|
+
|
|
378
|
+
if (this.sortable && this.sortBy) {
|
|
379
|
+
const direction = this.sortOrder === 'desc' ? -1 : 1;
|
|
380
|
+
const key = this.sortBy;
|
|
381
|
+
rows = rows.sort((a, b) => Table.compare(a?.[key], b?.[key]) * direction);
|
|
382
|
+
}
|
|
383
|
+
if (this.paginate) {
|
|
384
|
+
rows = rows.slice(
|
|
385
|
+
(this.page - 1) * this.pageSize,
|
|
386
|
+
this.page * this.pageSize,
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
return rows;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** Compares numbers by value and everything else as text with natural number order. */
|
|
393
|
+
private static compare(a: unknown, b: unknown): number {
|
|
394
|
+
if (a === b) return 0;
|
|
395
|
+
if (a === undefined || a === null || a === '') return 1;
|
|
396
|
+
if (b === undefined || b === null || b === '') return -1;
|
|
397
|
+
if (typeof a === 'number' && typeof b === 'number') return a - b;
|
|
398
|
+
return collator.compare(String(a), String(b));
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
private getRowKey(row: TableRow): string {
|
|
402
|
+
return row?.[this.keyField];
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
private onSortClick(col: TableColumn) {
|
|
406
|
+
if (this.sortBy === col.name) {
|
|
407
|
+
if (this.sortOrder === 'asc') {
|
|
408
|
+
this.sortOrder = 'desc';
|
|
409
|
+
} else {
|
|
410
|
+
this.sortBy = '';
|
|
411
|
+
this.sortOrder = 'asc';
|
|
412
|
+
}
|
|
176
413
|
} else {
|
|
177
|
-
|
|
414
|
+
this.sortBy = col.name;
|
|
415
|
+
this.sortOrder = 'asc';
|
|
178
416
|
}
|
|
179
|
-
this.
|
|
180
|
-
|
|
417
|
+
this.dispatchEvent(
|
|
418
|
+
new CustomEvent('sort', {
|
|
419
|
+
detail: { sortBy: this.sortBy, sortOrder: this.sortOrder },
|
|
420
|
+
bubbles: true,
|
|
421
|
+
composed: true,
|
|
422
|
+
}),
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
private onSelectAllClick(pageRows: TableRow[]) {
|
|
427
|
+
const pageKeys = pageRows.map(row => this.getRowKey(row));
|
|
428
|
+
const allSelected =
|
|
429
|
+
pageKeys.length > 0 &&
|
|
430
|
+
pageKeys.every(key => this.selectedRowKeys.includes(key));
|
|
431
|
+
|
|
432
|
+
const next = allSelected
|
|
433
|
+
? this.selectedRowKeys.filter(key => !pageKeys.includes(key))
|
|
434
|
+
: [...new Set([...this.selectedRowKeys, ...pageKeys])];
|
|
435
|
+
this.allPagesSelected = false;
|
|
436
|
+
this.onSelectChange(next, pageRows);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
private onRowSelectClick(row: TableRow, pageRows: TableRow[]) {
|
|
440
|
+
const key = this.getRowKey(row);
|
|
441
|
+
const next = this.selectedRowKeys.includes(key)
|
|
442
|
+
? this.selectedRowKeys.filter(rowKey => rowKey !== key)
|
|
443
|
+
: [...this.selectedRowKeys, key];
|
|
444
|
+
this.allPagesSelected = false;
|
|
445
|
+
this.onSelectChange(next, pageRows);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
private selectAllPages(pageRows: TableRow[]) {
|
|
449
|
+
// Without the full data set (managed mode), the flag tells the owner to act on every item.
|
|
450
|
+
const keys = this.managed
|
|
451
|
+
? [
|
|
452
|
+
...new Set([
|
|
453
|
+
...this.selectedRowKeys,
|
|
454
|
+
...pageRows.map(row => this.getRowKey(row)),
|
|
455
|
+
]),
|
|
456
|
+
]
|
|
457
|
+
: (this.data ?? []).map(row => this.getRowKey(row));
|
|
458
|
+
this.allPagesSelected = true;
|
|
459
|
+
this.onSelectChange(keys, pageRows);
|
|
460
|
+
}
|
|
181
461
|
|
|
182
|
-
private
|
|
462
|
+
private clearSelection() {
|
|
463
|
+
this.allPagesSelected = false;
|
|
464
|
+
this.onSelectChange([], this.getPageRows());
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
private onSelectChange(selectedRowKeys: string[], pageRows: TableRow[]) {
|
|
183
468
|
this.selectedRowKeys = selectedRowKeys;
|
|
469
|
+
const isSelectAll =
|
|
470
|
+
pageRows.length > 0 &&
|
|
471
|
+
pageRows.every(row => selectedRowKeys.includes(this.getRowKey(row)));
|
|
184
472
|
this.dispatchEvent(
|
|
185
473
|
new CustomEvent('selection-change', {
|
|
186
474
|
detail: {
|
|
187
475
|
value: this.selectedRowKeys,
|
|
188
|
-
isSelectAll
|
|
476
|
+
isSelectAll,
|
|
477
|
+
allPages: this.allPagesSelected,
|
|
189
478
|
},
|
|
190
479
|
bubbles: true,
|
|
191
480
|
composed: true,
|
|
@@ -193,7 +482,17 @@ export class Table extends LitElement {
|
|
|
193
482
|
);
|
|
194
483
|
}
|
|
195
484
|
|
|
196
|
-
private onCellClick(row:
|
|
485
|
+
private onCellClick(row: TableRow, col: TableColumn, evt: MouseEvent) {
|
|
486
|
+
const selection = window.getSelection();
|
|
487
|
+
if (selection?.type === 'Range') return;
|
|
488
|
+
this.dispatchCellEvents(row, col, evt);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
private dispatchCellEvents(
|
|
492
|
+
row: TableRow,
|
|
493
|
+
col: TableColumn,
|
|
494
|
+
evt: MouseEvent | KeyboardEvent,
|
|
495
|
+
) {
|
|
197
496
|
this.dispatchEvent(
|
|
198
497
|
new CustomEvent('cell-click', {
|
|
199
498
|
detail: {
|
|
@@ -208,235 +507,410 @@ export class Table extends LitElement {
|
|
|
208
507
|
composed: true,
|
|
209
508
|
}),
|
|
210
509
|
);
|
|
510
|
+
this.dispatchEvent(
|
|
511
|
+
new CustomEvent('row-click', {
|
|
512
|
+
detail: { record: row },
|
|
513
|
+
bubbles: true,
|
|
514
|
+
composed: true,
|
|
515
|
+
}),
|
|
516
|
+
);
|
|
211
517
|
}
|
|
212
518
|
|
|
213
|
-
private
|
|
214
|
-
|
|
215
|
-
this.
|
|
519
|
+
private onPage(event: CustomEvent<{ page: number; pageSize: number }>) {
|
|
520
|
+
event.stopPropagation();
|
|
521
|
+
this.page = event.detail.page;
|
|
522
|
+
this.pageSize = event.detail.pageSize;
|
|
523
|
+
this.dispatchEvent(
|
|
524
|
+
new CustomEvent('page', {
|
|
525
|
+
detail: { page: this.page, pageSize: this.pageSize },
|
|
526
|
+
bubbles: true,
|
|
527
|
+
composed: true,
|
|
528
|
+
}),
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
private onScrollContainer = () => {
|
|
533
|
+
this.updateScrollEdges();
|
|
216
534
|
};
|
|
217
535
|
|
|
218
|
-
private
|
|
219
|
-
|
|
220
|
-
if (
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
this.
|
|
224
|
-
|
|
225
|
-
});
|
|
226
|
-
return total;
|
|
536
|
+
private updateScrollEdges() {
|
|
537
|
+
const el = this.scrollContainer;
|
|
538
|
+
if (!el) return;
|
|
539
|
+
// scrollLeft is negative in right-to-left layouts; the magnitude is what matters.
|
|
540
|
+
const scrolled = Math.abs(el.scrollLeft);
|
|
541
|
+
this.scrolledFromStart = scrolled > 0;
|
|
542
|
+
this.scrolledFromEnd = scrolled + el.clientWidth < el.scrollWidth - 1;
|
|
227
543
|
}
|
|
228
544
|
|
|
229
|
-
private
|
|
230
|
-
|
|
231
|
-
|
|
545
|
+
private getCell(row: number, col: number): HTMLElement | null {
|
|
546
|
+
return (
|
|
547
|
+
this.renderRoot.querySelector<HTMLElement>(
|
|
548
|
+
`[data-cell][data-row="${row}"][data-col="${col}"]`,
|
|
549
|
+
) ?? null
|
|
550
|
+
);
|
|
232
551
|
}
|
|
233
552
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
: 'keyboard_arrow_down';
|
|
239
|
-
}
|
|
240
|
-
return 'unfold_more';
|
|
553
|
+
/** The element that takes focus for a cell: its sort button, or the cell itself. */
|
|
554
|
+
private getFocusTarget(row: number, col: number): HTMLElement | null {
|
|
555
|
+
const cell = this.getCell(row, col);
|
|
556
|
+
return cell?.querySelector<HTMLElement>('[data-focus-target]') ?? cell;
|
|
241
557
|
}
|
|
242
558
|
|
|
243
|
-
private
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
} else {
|
|
248
|
-
this.sortBy = '';
|
|
249
|
-
}
|
|
250
|
-
} else {
|
|
251
|
-
this.sortBy = col.name;
|
|
252
|
-
this.sortOrder = 'asc';
|
|
253
|
-
}
|
|
254
|
-
this.dispatchEvent(
|
|
255
|
-
new CustomEvent('sort', {
|
|
256
|
-
detail: { sortBy: this.sortBy, sortOrder: this.sortOrder },
|
|
257
|
-
bubbles: true,
|
|
258
|
-
composed: true,
|
|
259
|
-
}),
|
|
260
|
-
);
|
|
559
|
+
private tabIndexFor(row: number, col: number): string {
|
|
560
|
+
return this.activeCell.row === row && this.activeCell.col === col
|
|
561
|
+
? '0'
|
|
562
|
+
: '-1';
|
|
261
563
|
}
|
|
262
564
|
|
|
263
|
-
private
|
|
264
|
-
const
|
|
265
|
-
|
|
565
|
+
private onGridFocusIn = (event: FocusEvent) => {
|
|
566
|
+
const cell = (event.target as HTMLElement).closest<HTMLElement>(
|
|
567
|
+
'[data-cell]',
|
|
568
|
+
);
|
|
569
|
+
if (!cell) return;
|
|
570
|
+
const row = Number(cell.dataset.row);
|
|
571
|
+
const col = Number(cell.dataset.col);
|
|
572
|
+
if (row !== this.activeCell.row || col !== this.activeCell.col) {
|
|
573
|
+
this.activeCell = { row, col };
|
|
574
|
+
}
|
|
575
|
+
};
|
|
266
576
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
577
|
+
private onGridKeydown = (event: KeyboardEvent) => {
|
|
578
|
+
const target = event.target as HTMLElement;
|
|
579
|
+
const cell = target.closest<HTMLElement>('[data-cell]');
|
|
580
|
+
// Leave keys alone while focus is on interactive content inside a cell.
|
|
581
|
+
if (!cell || (target !== cell && !target.hasAttribute('data-focus-target')))
|
|
582
|
+
return;
|
|
583
|
+
|
|
584
|
+
const row = Number(cell.dataset.row);
|
|
585
|
+
const col = Number(cell.dataset.col);
|
|
586
|
+
const lastRow = this.getPageRows().length;
|
|
587
|
+
const lastCol = this.getGridColumns().length - 1;
|
|
588
|
+
let next: { row: number; col: number } | undefined;
|
|
589
|
+
|
|
590
|
+
switch (event.key) {
|
|
591
|
+
case 'ArrowRight':
|
|
592
|
+
next = { row, col: Math.min(col + 1, lastCol) };
|
|
593
|
+
break;
|
|
594
|
+
case 'ArrowLeft':
|
|
595
|
+
next = { row, col: Math.max(col - 1, 0) };
|
|
596
|
+
break;
|
|
597
|
+
case 'ArrowDown':
|
|
598
|
+
next = { row: Math.min(row + 1, lastRow), col };
|
|
599
|
+
break;
|
|
600
|
+
case 'ArrowUp':
|
|
601
|
+
next = { row: Math.max(row - 1, 0), col };
|
|
602
|
+
break;
|
|
603
|
+
case 'Home':
|
|
604
|
+
next = { row: event.ctrlKey ? 0 : row, col: 0 };
|
|
605
|
+
break;
|
|
606
|
+
case 'End':
|
|
607
|
+
next = { row: event.ctrlKey ? lastRow : row, col: lastCol };
|
|
608
|
+
break;
|
|
609
|
+
case ' ':
|
|
610
|
+
if (row > 0 && this.selectionType === 'checkbox') {
|
|
611
|
+
event.preventDefault();
|
|
612
|
+
const pageRows = this.getPageRows();
|
|
613
|
+
this.onRowSelectClick(pageRows[row - 1], pageRows);
|
|
614
|
+
}
|
|
615
|
+
return;
|
|
616
|
+
case 'Enter':
|
|
617
|
+
if (row > 0) {
|
|
618
|
+
const pageRows = this.getPageRows();
|
|
619
|
+
const column = this.getGridColumns()[col]?.column;
|
|
620
|
+
if (column) this.dispatchCellEvents(pageRows[row - 1], column, event);
|
|
621
|
+
}
|
|
622
|
+
return;
|
|
623
|
+
case 'c':
|
|
624
|
+
case 'C':
|
|
625
|
+
if ((event.ctrlKey || event.metaKey) && row > 0) {
|
|
626
|
+
window.navigator.clipboard?.writeText(cell.innerText).catch(() => {});
|
|
627
|
+
}
|
|
628
|
+
return;
|
|
629
|
+
default:
|
|
630
|
+
return;
|
|
280
631
|
}
|
|
281
632
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
if (col.fixed) {
|
|
314
|
-
fixedCols.push(colEl);
|
|
315
|
-
} else {
|
|
316
|
-
scrollCols.push(colEl);
|
|
317
|
-
}
|
|
633
|
+
event.preventDefault();
|
|
634
|
+
this.activeCell = next;
|
|
635
|
+
this.pendingFocus = true;
|
|
636
|
+
// The cell may already be active (an edge was reached): focus it anyway.
|
|
637
|
+
if (next.row === row && next.col === col) {
|
|
638
|
+
this.pendingFocus = false;
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
// ── Render helpers ───────────────────────────────────────────────────────
|
|
643
|
+
|
|
644
|
+
private renderCellContent(row: TableRow, column: TableColumn) {
|
|
645
|
+
if (column.template) {
|
|
646
|
+
const content = column.template(row, column);
|
|
647
|
+
return html`<div class="cell-template">
|
|
648
|
+
${typeof content === 'string' ? unsafeHTML(content) : content}
|
|
649
|
+
</div>`;
|
|
650
|
+
}
|
|
651
|
+
const value = row?.[column.name];
|
|
652
|
+
return html`<div class="cell-text" title=${value ?? ''}>${value}</div>`;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
private cellClasses(entry: GridColumn, extra: Record<string, boolean> = {}) {
|
|
656
|
+
const { column } = entry;
|
|
657
|
+
return classMap({
|
|
658
|
+
cell: true,
|
|
659
|
+
'cell-checkbox': !column,
|
|
660
|
+
[`align-${column ? Table.getAlign(column) : 'center'}`]: true,
|
|
661
|
+
numeric: column?.type === 'number',
|
|
662
|
+
wrap: !!column?.wrap,
|
|
663
|
+
...extra,
|
|
318
664
|
});
|
|
665
|
+
}
|
|
319
666
|
|
|
667
|
+
private cellStyle(entry: GridColumn) {
|
|
668
|
+
const width = entry.column
|
|
669
|
+
? Table.getWidth(entry.column)
|
|
670
|
+
: CHECKBOX_CELL_WIDTH;
|
|
671
|
+
return styleMap({ width: `${width}rem` });
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
/** Splits a row's cells into the start-pinned, scrolling and end-pinned groups. */
|
|
675
|
+
private renderRowGroups(
|
|
676
|
+
columns: GridColumn[],
|
|
677
|
+
renderCell: (entry: GridColumn, index: number) => TemplateResult,
|
|
678
|
+
) {
|
|
679
|
+
const groups: Record<ColumnPlacement, TemplateResult[]> = {
|
|
680
|
+
start: [],
|
|
681
|
+
scroll: [],
|
|
682
|
+
end: [],
|
|
683
|
+
};
|
|
684
|
+
columns.forEach((entry, index) => {
|
|
685
|
+
groups[entry.placement].push(renderCell(entry, index));
|
|
686
|
+
});
|
|
320
687
|
return html`
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
</div>
|
|
688
|
+
${groups.start.length
|
|
689
|
+
? html`<div class="columns pinned-group pinned-group-start" role="none">
|
|
690
|
+
${groups.start}
|
|
691
|
+
</div>`
|
|
692
|
+
: nothing}
|
|
693
|
+
<div class="columns scroll-group" role="none">${groups.scroll}</div>
|
|
694
|
+
${groups.end.length
|
|
695
|
+
? html`<div class="columns pinned-group pinned-group-end" role="none">
|
|
696
|
+
${groups.end}
|
|
697
|
+
</div>`
|
|
698
|
+
: nothing}
|
|
327
699
|
`;
|
|
328
700
|
}
|
|
329
701
|
|
|
330
|
-
private
|
|
331
|
-
|
|
702
|
+
private renderHeader(columns: GridColumn[], pageRows: TableRow[]) {
|
|
703
|
+
const pageKeys = pageRows.map(row => this.getRowKey(row));
|
|
704
|
+
const selectedOnPage = pageKeys.filter(key =>
|
|
705
|
+
this.selectedRowKeys.includes(key),
|
|
706
|
+
).length;
|
|
707
|
+
const allOnPage = pageKeys.length > 0 && selectedOnPage === pageKeys.length;
|
|
332
708
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
709
|
+
return html`
|
|
710
|
+
<div class="header" role="rowgroup">
|
|
711
|
+
<div
|
|
712
|
+
class="row"
|
|
713
|
+
role="row"
|
|
714
|
+
aria-rowindex="1"
|
|
715
|
+
style="min-width: ${this.totalColumnsWidth}rem"
|
|
716
|
+
>
|
|
717
|
+
${this.renderRowGroups(columns, (entry, index) => {
|
|
718
|
+
const { column } = entry;
|
|
719
|
+
if (!column) {
|
|
720
|
+
return html`
|
|
721
|
+
<div
|
|
722
|
+
class=${this.cellClasses(entry)}
|
|
723
|
+
style=${this.cellStyle(entry)}
|
|
724
|
+
role="columnheader"
|
|
725
|
+
data-cell
|
|
726
|
+
data-row="0"
|
|
727
|
+
data-col=${index}
|
|
728
|
+
tabindex=${this.tabIndexFor(0, index)}
|
|
729
|
+
aria-label="Select all rows on this page"
|
|
730
|
+
@keydown=${(event: KeyboardEvent) => {
|
|
731
|
+
if (event.key === ' ' || event.key === 'Enter') {
|
|
732
|
+
event.preventDefault();
|
|
733
|
+
this.onSelectAllClick(pageRows);
|
|
734
|
+
}
|
|
735
|
+
}}
|
|
736
|
+
>
|
|
737
|
+
<wc-checkbox
|
|
738
|
+
class="checkbox"
|
|
739
|
+
tabindex="-1"
|
|
740
|
+
aria-label="Select all rows on this page"
|
|
741
|
+
.checked=${allOnPage}
|
|
742
|
+
.indeterminate=${selectedOnPage > 0 && !allOnPage}
|
|
743
|
+
?disabled=${pageKeys.length === 0}
|
|
744
|
+
@change=${() => this.onSelectAllClick(pageRows)}
|
|
745
|
+
></wc-checkbox>
|
|
746
|
+
</div>
|
|
747
|
+
`;
|
|
748
|
+
}
|
|
350
749
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
class="checkbox"
|
|
361
|
-
.value=${this.selectedRowKeys.includes(row[this.keyField])}
|
|
362
|
-
@change=${() => this.onRowSelectClick(row)}
|
|
363
|
-
></wc-checkbox>
|
|
364
|
-
</div>
|
|
365
|
-
</div>
|
|
366
|
-
`);
|
|
367
|
-
}
|
|
750
|
+
const sortable = this.isColumnSortable(column);
|
|
751
|
+
const sorted = this.sortBy === column.name;
|
|
752
|
+
let ariaSort: 'ascending' | 'descending' | 'none' | undefined;
|
|
753
|
+
if (sortable) {
|
|
754
|
+
if (!sorted) ariaSort = 'none';
|
|
755
|
+
else
|
|
756
|
+
ariaSort =
|
|
757
|
+
this.sortOrder === 'asc' ? 'ascending' : 'descending';
|
|
758
|
+
}
|
|
368
759
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
760
|
+
return html`
|
|
761
|
+
<div
|
|
762
|
+
class=${this.cellClasses(entry, { sorted, sortable })}
|
|
763
|
+
style=${this.cellStyle(entry)}
|
|
764
|
+
role="columnheader"
|
|
765
|
+
aria-sort=${ariaSort ?? nothing}
|
|
766
|
+
data-cell
|
|
767
|
+
data-row="0"
|
|
768
|
+
data-col=${index}
|
|
769
|
+
tabindex=${sortable ? nothing : this.tabIndexFor(0, index)}
|
|
770
|
+
>
|
|
771
|
+
${sortable
|
|
772
|
+
? html`
|
|
773
|
+
<button
|
|
774
|
+
type="button"
|
|
775
|
+
class="sort-button"
|
|
776
|
+
data-focus-target
|
|
777
|
+
tabindex=${this.tabIndexFor(0, index)}
|
|
778
|
+
@click=${() => this.onSortClick(column)}
|
|
779
|
+
>
|
|
780
|
+
<span class="header-label">${column.label}</span>
|
|
781
|
+
<wc-icon
|
|
782
|
+
class="sort-icon"
|
|
783
|
+
aria-hidden="true"
|
|
784
|
+
name=${sorted && this.sortOrder === 'desc'
|
|
785
|
+
? 'arrow_downward'
|
|
786
|
+
: 'arrow_upward'}
|
|
787
|
+
></wc-icon>
|
|
788
|
+
</button>
|
|
789
|
+
`
|
|
790
|
+
: html`<span class="header-label">${column.label}</span>`}
|
|
791
|
+
</div>
|
|
792
|
+
`;
|
|
793
|
+
})}
|
|
794
|
+
</div>
|
|
795
|
+
${this.loading && pageRows.length
|
|
796
|
+
? html`<wc-linear-progress
|
|
797
|
+
class="loading-bar"
|
|
798
|
+
indeterminate
|
|
799
|
+
aria-label="Loading"
|
|
800
|
+
></wc-linear-progress>`
|
|
801
|
+
: nothing}
|
|
802
|
+
</div>
|
|
803
|
+
`;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
private renderBody(columns: GridColumn[], pageRows: TableRow[]) {
|
|
807
|
+
const rowOffset = this.paginate ? (this.page - 1) * this.pageSize : 0;
|
|
808
|
+
const selectable = this.selectionType === 'checkbox';
|
|
809
|
+
|
|
810
|
+
const rows = pageRows.map((row, rowIndex) => {
|
|
811
|
+
const gridRow = rowIndex + 1;
|
|
812
|
+
const selected =
|
|
813
|
+
selectable && this.selectedRowKeys.includes(this.getRowKey(row));
|
|
417
814
|
|
|
418
815
|
return html`
|
|
419
816
|
<div
|
|
420
|
-
class=${classMap({
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
817
|
+
class=${classMap({ row: true, selected })}
|
|
818
|
+
role="row"
|
|
819
|
+
aria-rowindex=${rowOffset + rowIndex + 2}
|
|
820
|
+
aria-selected=${selectable ? String(selected) : nothing}
|
|
424
821
|
style="min-width: ${this.totalColumnsWidth}rem"
|
|
425
822
|
>
|
|
426
|
-
|
|
427
|
-
|
|
823
|
+
${this.renderRowGroups(columns, (entry, index) => {
|
|
824
|
+
const { column } = entry;
|
|
825
|
+
if (!column) {
|
|
826
|
+
return html`
|
|
827
|
+
<div
|
|
828
|
+
class=${this.cellClasses(entry)}
|
|
829
|
+
style=${this.cellStyle(entry)}
|
|
830
|
+
role="gridcell"
|
|
831
|
+
data-cell
|
|
832
|
+
data-row=${gridRow}
|
|
833
|
+
data-col=${index}
|
|
834
|
+
tabindex=${this.tabIndexFor(gridRow, index)}
|
|
835
|
+
>
|
|
836
|
+
<wc-checkbox
|
|
837
|
+
class="checkbox"
|
|
838
|
+
tabindex="-1"
|
|
839
|
+
aria-label="Select row"
|
|
840
|
+
.checked=${selected}
|
|
841
|
+
@change=${() => this.onRowSelectClick(row, pageRows)}
|
|
842
|
+
></wc-checkbox>
|
|
843
|
+
</div>
|
|
844
|
+
`;
|
|
845
|
+
}
|
|
846
|
+
// Enter on a cell is handled by the grid's keydown listener.
|
|
847
|
+
/* eslint-disable lit-a11y/click-events-have-key-events */
|
|
848
|
+
return html`
|
|
849
|
+
<div
|
|
850
|
+
class=${this.cellClasses(entry)}
|
|
851
|
+
style=${this.cellStyle(entry)}
|
|
852
|
+
role="gridcell"
|
|
853
|
+
data-cell
|
|
854
|
+
data-row=${gridRow}
|
|
855
|
+
data-col=${index}
|
|
856
|
+
tabindex=${this.tabIndexFor(gridRow, index)}
|
|
857
|
+
@click=${(evt: MouseEvent) =>
|
|
858
|
+
this.onCellClick(row, column, evt)}
|
|
859
|
+
>
|
|
860
|
+
${this.renderCellContent(row, column)}
|
|
861
|
+
</div>
|
|
862
|
+
`;
|
|
863
|
+
/* eslint-enable lit-a11y/click-events-have-key-events */
|
|
864
|
+
})}
|
|
428
865
|
</div>
|
|
429
866
|
`;
|
|
430
867
|
});
|
|
431
868
|
|
|
432
|
-
return html`<div class="body">${rows}</div>`;
|
|
869
|
+
return html`<div class="body" role="rowgroup">${rows}</div>`;
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
private renderSkeletonBody(columns: GridColumn[]) {
|
|
873
|
+
const count = this.paginate ? Math.min(this.pageSize, 10) : SKELETON_ROWS;
|
|
874
|
+
return html`
|
|
875
|
+
<div class="body" role="rowgroup">
|
|
876
|
+
${Array.from(
|
|
877
|
+
{ length: count },
|
|
878
|
+
(_, rowIndex) => html`
|
|
879
|
+
<div
|
|
880
|
+
class="row skeleton-row"
|
|
881
|
+
role="row"
|
|
882
|
+
style="min-width: ${this.totalColumnsWidth}rem"
|
|
883
|
+
>
|
|
884
|
+
${this.renderRowGroups(
|
|
885
|
+
columns,
|
|
886
|
+
(entry, index) => html`
|
|
887
|
+
<div
|
|
888
|
+
class=${this.cellClasses(entry)}
|
|
889
|
+
style=${this.cellStyle(entry)}
|
|
890
|
+
role="gridcell"
|
|
891
|
+
>
|
|
892
|
+
<wc-skeleton
|
|
893
|
+
class="skeleton-bar"
|
|
894
|
+
visible
|
|
895
|
+
style=${entry.column
|
|
896
|
+
? `inline-size: ${40 + ((rowIndex * 7 + index * 13) % 45)}%`
|
|
897
|
+
: nothing}
|
|
898
|
+
></wc-skeleton>
|
|
899
|
+
</div>
|
|
900
|
+
`,
|
|
901
|
+
)}
|
|
902
|
+
</div>
|
|
903
|
+
`,
|
|
904
|
+
)}
|
|
905
|
+
</div>
|
|
906
|
+
`;
|
|
433
907
|
}
|
|
434
908
|
|
|
435
909
|
private renderEmptyState() {
|
|
436
910
|
return html`
|
|
437
911
|
<div class="empty-table">
|
|
438
912
|
<wc-empty-state
|
|
439
|
-
class="empty-state
|
|
913
|
+
class="empty-state"
|
|
440
914
|
headline=${this.emptyStateHeadline}
|
|
441
915
|
description=${this.emptyStateDescription}
|
|
442
916
|
></wc-empty-state>
|
|
@@ -444,46 +918,111 @@ export class Table extends LitElement {
|
|
|
444
918
|
`;
|
|
445
919
|
}
|
|
446
920
|
|
|
921
|
+
private renderBulkBar(pageRows: TableRow[]) {
|
|
922
|
+
if (this.selectionType !== 'checkbox') return nothing;
|
|
923
|
+
const count = this.selectedRowKeys.length;
|
|
924
|
+
if (count === 0) return nothing;
|
|
925
|
+
|
|
926
|
+
const total = this.getTotalItems();
|
|
927
|
+
const shownCount =
|
|
928
|
+
this.allPagesSelected && total !== undefined ? total : count;
|
|
929
|
+
const pageFullySelected =
|
|
930
|
+
pageRows.length > 0 &&
|
|
931
|
+
pageRows.every(row => this.selectedRowKeys.includes(this.getRowKey(row)));
|
|
932
|
+
const canSelectAllPages =
|
|
933
|
+
this.paginate &&
|
|
934
|
+
!this.allPagesSelected &&
|
|
935
|
+
pageFullySelected &&
|
|
936
|
+
total !== undefined &&
|
|
937
|
+
total > count;
|
|
938
|
+
|
|
939
|
+
return html`
|
|
940
|
+
<div class="bulk-bar" role="region" aria-label="Selection">
|
|
941
|
+
<span class="bulk-count" aria-live="polite"
|
|
942
|
+
>${shownCount} selected</span
|
|
943
|
+
>
|
|
944
|
+
${canSelectAllPages
|
|
945
|
+
? html`<wc-button
|
|
946
|
+
class="bulk-select-all"
|
|
947
|
+
variant="text"
|
|
948
|
+
size="sm"
|
|
949
|
+
@click=${() => this.selectAllPages(pageRows)}
|
|
950
|
+
>Select all ${total}</wc-button
|
|
951
|
+
>`
|
|
952
|
+
: nothing}
|
|
953
|
+
<span class="bulk-spacer"></span>
|
|
954
|
+
<slot name="bulk-actions"></slot>
|
|
955
|
+
<wc-button
|
|
956
|
+
class="bulk-clear"
|
|
957
|
+
variant="text"
|
|
958
|
+
size="sm"
|
|
959
|
+
@click=${this.clearSelection}
|
|
960
|
+
>Clear</wc-button
|
|
961
|
+
>
|
|
962
|
+
</div>
|
|
963
|
+
`;
|
|
964
|
+
}
|
|
965
|
+
|
|
447
966
|
private renderPagination() {
|
|
448
967
|
if (!this.paginate) return nothing;
|
|
449
968
|
|
|
450
969
|
return html`
|
|
451
|
-
<
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
this.
|
|
457
|
-
this.
|
|
458
|
-
this.
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
composed: true,
|
|
463
|
-
}),
|
|
464
|
-
);
|
|
465
|
-
}}
|
|
466
|
-
></wc-pagination>
|
|
970
|
+
<div class="table-footer">
|
|
971
|
+
<wc-pagination
|
|
972
|
+
.page=${this.page}
|
|
973
|
+
.pageSize=${this.pageSize}
|
|
974
|
+
.totalItems=${this.getTotalItems()}
|
|
975
|
+
.hasNext=${this.hasNext}
|
|
976
|
+
.variant=${this.paginationVariant}
|
|
977
|
+
.pageSizes=${this.pageSizes ?? [10, 25, 50, 100]}
|
|
978
|
+
@page=${this.onPage}
|
|
979
|
+
></wc-pagination>
|
|
980
|
+
</div>
|
|
467
981
|
`;
|
|
468
982
|
}
|
|
469
983
|
|
|
984
|
+
// ── Render ───────────────────────────────────────────────────────────────
|
|
985
|
+
|
|
470
986
|
render() {
|
|
471
|
-
const
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
987
|
+
const columns = this.getGridColumns();
|
|
988
|
+
const pageRows = this.getPageRows();
|
|
989
|
+
const total = this.getTotalItems();
|
|
990
|
+
const hasRows = pageRows.length > 0;
|
|
991
|
+
|
|
992
|
+
let body;
|
|
993
|
+
if (hasRows) body = this.renderBody(columns, pageRows);
|
|
994
|
+
else if (this.loading) body = this.renderSkeletonBody(columns);
|
|
995
|
+
else body = this.renderEmptyState();
|
|
477
996
|
|
|
478
997
|
return html`
|
|
479
|
-
<div
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
998
|
+
<div
|
|
999
|
+
class=${classMap({
|
|
1000
|
+
table: true,
|
|
1001
|
+
'scrolled-from-start': this.scrolledFromStart,
|
|
1002
|
+
'scrolled-from-end': this.scrolledFromEnd,
|
|
1003
|
+
'has-selection': this.selectedRowKeys.length > 0,
|
|
1004
|
+
})}
|
|
1005
|
+
>
|
|
1006
|
+
${this.renderBulkBar(pageRows)}
|
|
1007
|
+
<div
|
|
1008
|
+
class="table-scroll-container"
|
|
1009
|
+
role="grid"
|
|
1010
|
+
aria-label=${this.label || nothing}
|
|
1011
|
+
aria-busy=${this.loading ? 'true' : nothing}
|
|
1012
|
+
aria-multiselectable=${this.selectionType === 'checkbox'
|
|
1013
|
+
? 'true'
|
|
1014
|
+
: nothing}
|
|
1015
|
+
aria-rowcount=${this.paginate && total !== undefined
|
|
1016
|
+
? total + 1
|
|
1017
|
+
: nothing}
|
|
1018
|
+
aria-colcount=${columns.length}
|
|
1019
|
+
@scroll=${this.onScrollContainer}
|
|
1020
|
+
@focusin=${this.onGridFocusIn}
|
|
1021
|
+
@keydown=${this.onGridKeydown}
|
|
1022
|
+
>
|
|
1023
|
+
${this.renderHeader(columns, pageRows)} ${body}
|
|
485
1024
|
</div>
|
|
486
|
-
|
|
1025
|
+
${this.renderPagination()}
|
|
487
1026
|
</div>
|
|
488
1027
|
`;
|
|
489
1028
|
}
|