Skip to content

Commit 22d79a8

Browse files
committed
support dynamic types for cells
1 parent 61feb52 commit 22d79a8

3 files changed

Lines changed: 8 additions & 2 deletions

File tree

src/components/pg/table/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ The `createTableItem` unrolls the `{ key: value }` shorthand to `items: [{ key,
7575

7676
- Primitive values pick the default cell by type: `string`, `number`, or `boolean`.
7777
- Object values are spread onto the cell as props and must include a `type` cell component (e.g. `{ type, icon, value }`). A `key` field inside the object is ignored; the shorthand key wins.
78+
- `type` may also be a factory `(item) => CellComponent` that picks the cell editor from the item's data (e.g. `{ type: (item) => item.value < 0 ? PgTableCellText : PgTableCellNumber, value: 42 }`). Returning `null`/`undefined` falls back to the default cell for the value type.
7879
- `null`/`undefined` values have no default cell and throw; use an object value with an explicit `type`.
7980

8081
```typescript

src/components/pg/tableRow/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
See `PgTable`. Internal row component; created by `PgTable` from `data` items.
44

5-
Renders a cell per item, picking the component from the item `type` or defaulting by `typeof item.value` (`string``PgTableCellText`, `number``PgTableCellNumber`, `boolean``PgTableCellCheck`). Re-dispatches cell `action` events with `index`, `key`, `getColumn`, `getRow`, and `getRows` added to the detail.
5+
Renders a cell per item, picking the component from the item `type` — a cell component class or a factory `(item) => CellComponent`or defaulting by `typeof item.value` (`string``PgTableCellText`, `number``PgTableCellNumber`, `boolean``PgTableCellCheck`). Re-dispatches cell `action` events with `index`, `key`, `getColumn`, `getRow`, and `getRows` added to the detail.
66

77
| Attributes | Tested | Default | Description |
88
| ---------- | -------- | ------- | ----------- |

src/components/pg/tableRow/tableRow.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ export default class PgTableRow extends HTMLElement {
3737
container: this.$cells,
3838
items: this.items,
3939
type: (item) => {
40-
const type = item.type ?? types.get(typeof item.value);
40+
let type = item.type;
41+
if (typeof type === 'function' && !(type.prototype instanceof HTMLElement)) {
42+
// factory form: (item) => cell component class
43+
type = type(item);
44+
}
45+
type ??= types.get(typeof item.value);
4146
if (!type) {
4247
throw new Error(`no cell component for key '${item.key}'; use a string, number, or boolean value, or set an explicit 'type'`);
4348
}

0 commit comments

Comments
 (0)