Skip to content

Commit 12cd3f2

Browse files
committed
clean up table
1 parent 69e3feb commit 12cd3f2

File tree

7 files changed

+54
-25
lines changed

7 files changed

+54
-25
lines changed

frontend/src/components/ui/data-grid/controllers/rows.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class DataGridRowsController implements ReactiveController {
2727

2828
#prevItems?: GridItem[];
2929

30-
public rows: GridRows = new Map<GridRowId, GridItem>();
30+
public rows: GridRows<GridItem> = new Map<GridRowId, GridItem>();
3131

3232
constructor(host: ReactiveControllerHost & EventTarget) {
3333
this.#host = host;
@@ -46,7 +46,7 @@ export class DataGridRowsController implements ReactiveController {
4646
}
4747
}
4848

49-
private setRowsFromItems(items: GridItem[]) {
49+
private setRowsFromItems<T extends GridItem = GridItem>(items: T[]) {
5050
const rowKey = this.#host.rowKey;
5151

5252
this.rows = new Map(
@@ -61,15 +61,20 @@ export class DataGridRowsController implements ReactiveController {
6161
);
6262
}
6363

64-
public setItems(items: GridItem[]) {
64+
public setItems<T extends GridItem = GridItem>(items: T[]) {
6565
if (!this.#prevItems || items !== this.#prevItems) {
6666
this.setRowsFromItems(items);
6767

68+
// this.#host.requestUpdate();
69+
6870
this.#prevItems = items;
6971
}
7072
}
7173

72-
public addRows(defaultItem: GridItem | EmptyObject = {}, count = 1) {
74+
public addRows<T extends GridItem = GridItem>(
75+
defaultItem: T | EmptyObject = {},
76+
count = 1,
77+
) {
7378
for (let i = 0; i < count; i++) {
7479
const id = nanoid();
7580

frontend/src/components/ui/data-grid/data-grid-cell.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,13 @@ export class DataGridCell extends TableCell {
134134
?required=${col.required}
135135
hoist
136136
>
137-
<!-- TODO Cache -->
138-
${(col as GridColumnSelectType).renderSelectOptions()}
137+
${(col as GridColumnSelectType).selectOptions.map(
138+
(opt) => html`
139+
<sl-option value=${opt.value}>
140+
${opt.label ?? opt.value}
141+
</sl-option>
142+
`,
143+
)}
139144
</sl-select>
140145
</div>
141146
`;

frontend/src/components/ui/data-grid/data-grid-row.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export class DataGridRow extends TableRow {
182182
class=${clsx(tw`border-l p-0`, cellStyle)}
183183
@keydown=${this.onKeydown}
184184
>
185-
<sl-tooltip content=${msg("Remove")}>
185+
<sl-tooltip content=${msg("Remove")} hoist>
186186
<sl-icon-button
187187
class="p-1 text-base hover:text-danger"
188188
name="trash3"

frontend/src/components/ui/data-grid/data-grid.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import clsx from "clsx";
44
import { css, html, nothing } from "lit";
55
import { customElement, property } from "lit/decorators.js";
66
import { ifDefined } from "lit/directives/if-defined.js";
7-
import { repeat } from "lit/directives/repeat.js";
87
import { nanoid } from "nanoid";
98
import type { EmptyObject } from "type-fest";
109

1110
import { DataGridRowsController } from "./controllers/rows";
1211
import type { DataGridRow, RowRemoveEventDetail } from "./data-grid-row";
12+
import { renderRows } from "./renderRows";
1313
import type { GridColumn, GridItem } from "./types";
1414

1515
import { TailwindElement } from "@/classes/TailwindElement";
@@ -185,7 +185,7 @@ export class DataGrid extends TailwindElement {
185185
)}
186186
${this.removeRows
187187
? html`<btrix-table-header-cell>
188-
<span class="sr-only">${msg("Remove")}</span>
188+
<span class="sr-only">${msg("Remove row")}</span>
189189
</btrix-table-header-cell>`
190190
: nothing}
191191
</btrix-table-head>
@@ -257,10 +257,9 @@ export class DataGrid extends TailwindElement {
257257
return html`
258258
<slot name="rows" class="contents" @slotchange=${this.onRowSlotChange}>
259259
${this.items
260-
? repeat(
260+
? renderRows(
261261
this.rowsController.rows,
262-
([id]) => id,
263-
([id, item]) => html`
262+
({ id, item }) => html`
264263
<btrix-data-grid-row
265264
key=${id}
266265
.item=${item}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { type TemplateResult } from "lit";
2+
import { repeat } from "lit/directives/repeat.js";
3+
4+
import type { GridItem, GridRowId, GridRows } from "./types";
5+
6+
export function renderRows<T = GridItem>(
7+
rows: GridRows<GridItem>,
8+
renderRow: ({ id, item }: { id: GridRowId; item: T }) => TemplateResult,
9+
) {
10+
return repeat(
11+
rows,
12+
([id]) => id,
13+
([id, item]) => renderRow({ id, item: item as T }),
14+
);
15+
}

frontend/src/components/ui/data-grid/types.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export enum GridColumnType {
1616

1717
export type GridColumnSelectType = {
1818
inputType: GridColumnType.Select;
19-
renderSelectOptions: () => TemplateResult;
19+
selectOptions: {
20+
value: string;
21+
label?: string | TemplateResult;
22+
}[];
2023
};
2124

2225
export type GridColumn<T = string> = {
@@ -39,4 +42,4 @@ export type GridColumn<T = string> = {
3942
const rowIdSchema = z.string().nanoid();
4043
export type GridRowId = z.infer<typeof rowIdSchema>;
4144

42-
export interface GridRows extends Map<GridRowId, GridItem> {}
45+
export interface GridRows<T> extends Map<GridRowId, T> {}

frontend/src/stories/components/DataGrid.stories.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Meta, StoryObj } from "@storybook/web-components";
22
import { html } from "lit";
3-
import { repeat } from "lit/directives/repeat.js";
43

54
import { defaultArgs, renderComponent, type RenderProps } from "./DataGrid";
65
import {
@@ -9,6 +8,7 @@ import {
98
} from "./decorators/dataGridDecorator";
109

1110
import { DataGridRowsController } from "@/components/ui/data-grid/controllers/rows";
11+
import { renderRows } from "@/components/ui/data-grid/renderRows";
1212
import { GridColumnType } from "@/components/ui/data-grid/types";
1313

1414
const meta = {
@@ -149,6 +149,7 @@ export const EditCells: Story = {
149149
*
150150
* A few helpers are included to make managing rows easier:
151151
* - `DataGridController` to add and remove slotted rows
152+
* - `renderRows` to render `<btrix-data-grid-row>`
152153
* - `serializeDeep` to parse form values
153154
*
154155
* Open console logs to view the form value submitted in this example.
@@ -180,7 +181,7 @@ export const FormControl: Story = {
180181
return html`
181182
<btrix-syntax-input
182183
name="selector"
183-
class="flex-1 [--sl-input-border-color:transparent] [--sl-input-border-radius-medium:0]"
184+
class="flex-1 [--sl-input-border-radius-medium:0] [--sl-input-border-color:transparent]"
184185
value=${item.selector || ""}
185186
language="css"
186187
></btrix-syntax-input>
@@ -199,12 +200,14 @@ export const FormControl: Story = {
199200
label: "Status",
200201
editable: true,
201202
inputType: GridColumnType.Select,
202-
renderSelectOptions() {
203-
return html`
204-
<sl-option value="Pending">Pending</sl-option>
205-
<sl-option value="Approved">Approved</sl-option>
206-
`;
207-
},
203+
selectOptions: [
204+
{
205+
value: "Pending",
206+
},
207+
{
208+
value: "Approved",
209+
},
210+
],
208211
},
209212
],
210213
items: [
@@ -244,10 +247,9 @@ export const FormControl: Story = {
244247
removeRows
245248
editCells
246249
>
247-
${repeat(
250+
${renderRows(
248251
rows,
249-
([id]) => id,
250-
([id, item]) => html`
252+
({ id, item }) => html`
251253
<btrix-data-grid-row
252254
slot="rows"
253255
name="${formControlName}"

0 commit comments

Comments
 (0)