ui-sdk-table-web 0.0.0-stage → 0.2.7
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/LICENSE +21 -0
- package/README.md +7 -2
- package/dist/table-licenses.md +411 -0
- package/dist/table-reference-licenses.md +411 -0
- package/dist/table-reference.js +10631 -0
- package/dist/table.js +10147 -0
- package/docs/api-web-components.md +319 -0
- package/docs/cell-editing.md +234 -0
- package/docs/cells-and-slots.md +106 -0
- package/docs/component-body.md +42 -0
- package/docs/component-cell.md +31 -0
- package/docs/component-col.md +31 -0
- package/docs/component-edit-error.md +31 -0
- package/docs/component-editor.md +31 -0
- package/docs/component-fill-handle.md +31 -0
- package/docs/component-foot.md +33 -0
- package/docs/component-head.md +33 -0
- package/docs/component-header-cell.md +31 -0
- package/docs/component-header-row.md +33 -0
- package/docs/component-plain-row.md +33 -0
- package/docs/component-resizer.md +33 -0
- package/docs/component-root.md +40 -0
- package/docs/component-row.md +33 -0
- package/docs/component-spacer.md +31 -0
- package/docs/controller-and-view.md +108 -0
- package/docs/examples-agents.md +62 -0
- package/docs/examples-column-definitions.md +172 -0
- package/docs/examples-formula-writer.md +262 -0
- package/docs/examples-styling.md +180 -0
- package/docs/examples.md +125 -0
- package/docs/excel-formulas.md +119 -0
- package/docs/export.md +107 -0
- package/docs/fill.md +111 -0
- package/docs/first-table.md +111 -0
- package/docs/formats.md +158 -0
- package/docs/groups-and-totals.md +117 -0
- package/docs/history.md +126 -0
- package/docs/installation.md +52 -0
- package/docs/llms.txt +13 -0
- package/docs/pages-and-server-mode.md +219 -0
- package/docs/range-copy-and-paste.md +108 -0
- package/docs/reference-columns.md +179 -0
- package/docs/reference-controller.md +137 -0
- package/docs/reference-export.md +59 -0
- package/docs/reference-formulas.md +42 -0
- package/docs/reference-implementation.md +29 -0
- package/docs/reference-messages.md +55 -0
- package/docs/reference-view.md +116 -0
- package/docs/row-numbers-and-letters.md +109 -0
- package/docs/rows.md +310 -0
- package/docs/selection-and-keyboard.md +134 -0
- package/docs/sheet-formulas.md +322 -0
- package/docs/sheet-options.md +192 -0
- package/docs/skills.md +41 -0
- package/docs/sort-filter-and-search.md +268 -0
- package/docs/state-and-events.md +86 -0
- package/docs/styled-table.md +229 -0
- package/docs/table-components.md +100 -0
- package/docs/web-components.md +113 -0
- package/package.json +49 -3
- package/src/browser.ts +3 -0
- package/src/index.ts +77 -0
- package/src/reference-browser.ts +3 -0
- package/src/reference-dom.ts +206 -0
- package/src/reference.ts +63 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# Table components
|
|
2
|
+
|
|
3
|
+
Compose a table from native parts. Start with the root, add the header and body, then choose the interactive parts you need.
|
|
4
|
+
|
|
5
|
+
## Choose a component
|
|
6
|
+
|
|
7
|
+
| What you need | Parts |
|
|
8
|
+
| --- | --- |
|
|
9
|
+
| Supply rows, columns, options and change callbacks | [TableRoot](./component-root.md) |
|
|
10
|
+
| Render column headers | [TableHead](./component-head.md), [TableHeaderRow](./component-header-row.md), [TableHeaderCell](./component-header-cell.md) |
|
|
11
|
+
| Render the visible records and their cells | [TableBody](./component-body.md), [TableRow](./component-row.md), [TableCell](./component-cell.md) |
|
|
12
|
+
| Lay out columns or resize a column | [TableCol](./component-col.md), [TableResizer](./component-resizer.md) |
|
|
13
|
+
| Show a footer or a structural row | [TableFoot](./component-foot.md), [TablePlainRow](./component-plain-row.md) |
|
|
14
|
+
| Reserve space for rows outside the window | [TableSpacer](./component-spacer.md) |
|
|
15
|
+
| Edit a value and show a validation error | [TableEditor](./component-editor.md), [TableEditError](./component-edit-error.md) |
|
|
16
|
+
| Fill cells from a selection | [TableFillHandle](./component-fill-handle.md) |
|
|
17
|
+
|
|
18
|
+
Each component page explains its inputs, the events it handles and the behavior supplied by the primitive. Structural parts do not introduce their own change events. Use the selected framework’s example below to see how the parts fit together.
|
|
19
|
+
|
|
20
|
+
```ts title="parts.ts"
|
|
21
|
+
import { type ColumnDef } from 'ui-sdk-table-core'
|
|
22
|
+
import { defineTableElements } from 'ui-sdk-table-web'
|
|
23
|
+
|
|
24
|
+
type Planet = { id: string; name: string; moons: number }
|
|
25
|
+
|
|
26
|
+
const planets: Planet[] = [
|
|
27
|
+
{ id: 'mars', name: 'Mars', moons: 2 },
|
|
28
|
+
{ id: 'jupiter', name: 'Jupiter', moons: 95 },
|
|
29
|
+
{ id: 'saturn', name: 'Saturn', moons: 146 },
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
const columns: ColumnDef[] = [
|
|
33
|
+
{ id: 'name', header: 'Planet' },
|
|
34
|
+
{ id: 'moons', header: 'Moons' },
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
export default function mount(host: HTMLElement) {
|
|
38
|
+
defineTableElements()
|
|
39
|
+
const table = document.createElement('ui-table')
|
|
40
|
+
table.setAttribute('aria-label', 'Planets and their moons')
|
|
41
|
+
table.rows = planets
|
|
42
|
+
table.columns = columns
|
|
43
|
+
const head = document.createElement('ui-table-head')
|
|
44
|
+
const header = document.createElement('ui-table-header-row')
|
|
45
|
+
header.value = 'header'
|
|
46
|
+
header.index = 0
|
|
47
|
+
columns.forEach((column, col) => {
|
|
48
|
+
const cell = document.createElement('ui-table-cell')
|
|
49
|
+
Object.assign(cell, { value: `h|${column.id}`, row: 0, col, header: true })
|
|
50
|
+
cell.textContent = column.header ?? column.id
|
|
51
|
+
header.append(cell)
|
|
52
|
+
})
|
|
53
|
+
head.append(header)
|
|
54
|
+
const body = document.createElement('ui-table-body')
|
|
55
|
+
planets.forEach((planet, i) => {
|
|
56
|
+
const row = document.createElement('ui-table-row')
|
|
57
|
+
row.value = planet.id
|
|
58
|
+
row.index = i + 1
|
|
59
|
+
for (const [col, key] of (['name', 'moons'] as const).entries()) {
|
|
60
|
+
const cell = document.createElement('ui-table-cell')
|
|
61
|
+
Object.assign(cell, { value: `${planet.id}|${key}`, row: i + 1, col })
|
|
62
|
+
cell.textContent = String(planet[key])
|
|
63
|
+
row.append(cell)
|
|
64
|
+
}
|
|
65
|
+
body.append(row)
|
|
66
|
+
})
|
|
67
|
+
table.append(head, body)
|
|
68
|
+
|
|
69
|
+
host.append(table)
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Click Mars, then press ArrowRight to reach its moon count and ArrowDown to reach Jupiter’s. The parts move focus without an arrow-key handler in the example.
|
|
74
|
+
|
|
75
|
+
## The four layers
|
|
76
|
+
|
|
77
|
+
1. **Behaviors** implement individual interactions. For example, `createTableGrid` handles grid focus and `createTableRange` handles a range of cells. These are pieces of the table implementation, not separate imports you need in an app.
|
|
78
|
+
2. **The primitive**, `table` from `ui-sdk-table-core`, connects those behaviors to the row model and table state. It defines what each element does: which cell can take focus, which role and attributes it exposes, and how it responds to a key. It doesn't render the rows.
|
|
79
|
+
3. **Parts** bind the primitive to elements you compose. In the example, the app supplies the header, rows, cells and their text. The cell part supplies the focus and keyboard behavior; your CSS can target it with `[data-slot="table-cell"]`.
|
|
80
|
+
4. **The reference implementation** is an optional composition of those parts. It turns rows and columns into a complete table, including grouped headers, editors and a footer. Read the [reference implementation source code](./reference-implementation.md#source-code) to see where it puts an editor or how it renders a group. You don't need the reference to use the parts.
|
|
81
|
+
|
|
82
|
+
## Two kinds of slot
|
|
83
|
+
|
|
84
|
+
A **primitive slot** describes an element's job: for example, both a column header and a body cell use `cell`. Supplying **content** is a different job. With parts, you put the content inside the cell yourself. With the reference, you can replace the content of a column while keeping the cell element and its behavior.
|
|
85
|
+
|
|
86
|
+
With Web Components, put text or DOM nodes inside `<ui-table-cell>`; a primitive slot isn't an HTML `<slot>`. For a complete table, `defineReferenceTable()` from `ui-sdk-table-web/reference` registers `<ui-reference-table>`. Assign `grid.renderers = { 'col-name': ({ text }) => text.toUpperCase() }` on that element to change just the `name` column. To control selection, set `grid.selected`, listen for `selectedchange` on the root, and assign accepted `event.detail.value` values back to the property.
|
|
87
|
+
|
|
88
|
+
## Build a table from the parts
|
|
89
|
+
|
|
90
|
+
Give the root `rows`, `columns` and an accessible name. Give each row a stable `id` (or supply `getRowId`) and each column an `id`. A row part's `value` is its row id. A cell part's `value` identifies that cell: Mars's moon count stays `mars|moons` even if Mars changes position.
|
|
91
|
+
|
|
92
|
+
**Identity stays fixed; coordinates follow the view.** Here the header row has `value="header"` and `index` 0. The first body row has `index` 1; its moon count has `row` 1 and `col` 1. Set `header` on each column header. If you add sorting, filtering or paging, render the model's visible rows rather than the original array so the coordinates still match what the user sees.
|
|
93
|
+
|
|
94
|
+
Register the parts with `defineTableElements()` from `ui-sdk-table-web`. The root is `<ui-table>`: assign `grid.rows = planets` and `grid.columns = columns` as properties, not HTML attributes. In TypeScript, `document.createElement('ui-table')` has type `TableElement<unknown>`, so the example uses `Planet[]` and `ColumnDef[]`. For a root typed `TableElement<Planet>`, the columns are `ColumnDef<Planet>[]`. Set `value` and `index` on `<ui-table-row>`; use `<ui-table-cell header>` for a column header and `<ui-table-cell>` for a data cell. Read `grid.api.rows()` on the connected root for display order. See the [Web Components guide](./web-components.md) and [Web Components API](./api-web-components.md).
|
|
95
|
+
|
|
96
|
+
Inspect the example: the root has role `grid` and reports `aria-rowcount` and `aria-colcount`. The header and body cells have roles `columnheader` and `gridcell`. Grid coordinates start at zero in code, but `aria-rowindex` and `aria-colindex` start at one. As you move with the arrow keys, `tabindex="0"` follows the active cell.
|
|
97
|
+
|
|
98
|
+
## Names
|
|
99
|
+
|
|
100
|
+
The primitive's structural slots are `root`, `head`, `body`, `foot`, `col`, `header-row`, `row`, `plain-row`, `cell` and `spacer`. For editing, filling and resizing it also defines `editor`, `edit-error`, `fill-handle` and `resizer`. These are the names behind the `data-slot` attributes: for example, `[data-slot="table-header-row"]` targets a header row regardless of which renderer created it.
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# Web Components
|
|
2
|
+
|
|
3
|
+
Use the table primitive from plain HTML with custom elements, JavaScript properties and DOM events.
|
|
4
|
+
|
|
5
|
+
For exact imports, elements and DOM events, see the [Web Components API reference](./api-web-components.md).
|
|
6
|
+
|
|
7
|
+
## Run it without a framework
|
|
8
|
+
|
|
9
|
+
From the repository root, run `pnpm --filter ui-sdk-table-web build`. Copy `packages/web/dist/table.js` beside an `index.html` containing the example below, serve that directory over HTTP, and open the page. No import map, framework, stylesheet or second script is required. For an HTML artifact, include `table.js` as a supporting file. The UI SDK browser script alone does **not** register these table elements.
|
|
10
|
+
|
|
11
|
+
```html
|
|
12
|
+
<!doctype html>
|
|
13
|
+
<html lang="en">
|
|
14
|
+
<meta charset="utf-8">
|
|
15
|
+
<title>People</title>
|
|
16
|
+
<ui-table aria-label="People" selection="multiple">
|
|
17
|
+
<ui-table-head>
|
|
18
|
+
<ui-table-header-row value="heading" index="0">
|
|
19
|
+
<ui-table-cell value="heading-name" header row="0" col="0">Name</ui-table-cell>
|
|
20
|
+
</ui-table-header-row>
|
|
21
|
+
</ui-table-head>
|
|
22
|
+
<ui-table-body></ui-table-body>
|
|
23
|
+
</ui-table>
|
|
24
|
+
<p>Selected: <output>none</output></p>
|
|
25
|
+
<script type="module">
|
|
26
|
+
import './table.js'
|
|
27
|
+
|
|
28
|
+
const grid = document.querySelector('ui-table')
|
|
29
|
+
const body = grid.querySelector('ui-table-body')
|
|
30
|
+
grid.columns = [{ id: 'name', sortable: true }]
|
|
31
|
+
grid.rows = [{ id: 'b', name: 'Bea' }, { id: 'a', name: 'Ada' }]
|
|
32
|
+
|
|
33
|
+
function renderRows() {
|
|
34
|
+
const rows = grid.api.rows().map(({ id, node }, index) => {
|
|
35
|
+
const row = document.createElement('ui-table-row')
|
|
36
|
+
row.value = id
|
|
37
|
+
row.index = index + 1 // Grid row zero is the header.
|
|
38
|
+
const cell = document.createElement('ui-table-cell')
|
|
39
|
+
cell.value = JSON.stringify([id, 'name'])
|
|
40
|
+
cell.row = row.index
|
|
41
|
+
cell.col = 0
|
|
42
|
+
cell.textContent = node.row.name
|
|
43
|
+
row.append(cell)
|
|
44
|
+
return row
|
|
45
|
+
})
|
|
46
|
+
body.replaceChildren(...rows)
|
|
47
|
+
}
|
|
48
|
+
renderRows()
|
|
49
|
+
grid.addEventListener('sortchange', () => queueMicrotask(renderRows))
|
|
50
|
+
grid.addEventListener('selectedchange', (event) => {
|
|
51
|
+
document.querySelector('output').value = event.detail.value.join(', ') || 'none'
|
|
52
|
+
})
|
|
53
|
+
</script>
|
|
54
|
+
</html>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Tab to **Name** and press Enter to sort. Press ArrowDown to move into the first data row and Space to select it. The header reports the sort direction; each selectable row reports its selection. Give each grid its own accessible name with `aria-label` or `aria-labelledby`.
|
|
58
|
+
|
|
59
|
+
The Pages deployment for this revision serves the same module at `https://ui-sdk-table.fdts.dev/table.js` once deployed. That URL follows the site release. Self-host the file from a particular build when the version must stay fixed.
|
|
60
|
+
|
|
61
|
+
## Use it with a bundler
|
|
62
|
+
|
|
63
|
+
In an app that resolves the workspace package, register the elements once in your client entry:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { defineTableElements } from 'ui-sdk-table-web'
|
|
67
|
+
|
|
68
|
+
defineTableElements()
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Use this **instead of** importing `./table.js`; the standalone module bundles the renderer and registers the tags itself. Neither form uses Vue. `defineTableElements()` is safe to call more than once and defines nothing on the server.
|
|
72
|
+
|
|
73
|
+
## Use the opt-in reference table
|
|
74
|
+
|
|
75
|
+
Choose `<ui-reference-table>` when you want a complete grid instead of writing each part yourself. This is the opinionated reference implementation, not the default API. It builds headers, data rows, groups, totals, detail rows, editor fields, row windows and sheet controls from the shared reference composition. The same primitive controls focus, ARIA and events.
|
|
76
|
+
|
|
77
|
+
For plain HTML, copy `packages/web/dist/table-reference.js` beside this page. Load that script **instead of** `table.js`:
|
|
78
|
+
|
|
79
|
+
```html
|
|
80
|
+
<!doctype html>
|
|
81
|
+
<html lang="en">
|
|
82
|
+
<meta charset="utf-8">
|
|
83
|
+
<title>Inventory</title>
|
|
84
|
+
<ui-reference-table aria-label="Inventory" editable></ui-reference-table>
|
|
85
|
+
<script type="module">
|
|
86
|
+
import './table-reference.js'
|
|
87
|
+
|
|
88
|
+
const grid = document.querySelector('ui-reference-table')
|
|
89
|
+
grid.columns = [
|
|
90
|
+
{ id: 'item', header: 'Item', sortable: true, editable: true },
|
|
91
|
+
{ id: 'units', header: 'Units', aggregate: 'sum' },
|
|
92
|
+
]
|
|
93
|
+
grid.rows = [{ id: 'p', item: 'Pens', units: 3 }, { id: 'c', item: 'Clips', units: 7 }]
|
|
94
|
+
grid.addEventListener('celledit', ({ detail }) => {
|
|
95
|
+
const edits = detail.value
|
|
96
|
+
grid.rows = grid.rows.map(row => {
|
|
97
|
+
const edit = edits.find(edit => edit.rowId === row.id && edit.columnId === 'item')
|
|
98
|
+
return edit ? { ...row, item: edit.value } : row
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
</script>
|
|
102
|
+
</html>
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
For a bundled app, call `defineReferenceTable()` from `ui-sdk-table-web/reference` instead. The root's `.api` exposes the table controller while connected. Supply `renderers` as a property for custom content. For example, `grid.renderers = { 'col-item': ({ text }) => text.toUpperCase() }` changes one column. A renderer can return text or a DOM node. Reuse the node if it holds focus or local state. Set `rowAttrs`, `cellAttrs`, and `sectionAttrs` to customize generated elements. For a custom editor, take focus and call its `save` or `cancel` callback. Use change events and controlled properties exactly as you would with `<ui-table>`.
|
|
106
|
+
|
|
107
|
+
## Bind data and state
|
|
108
|
+
|
|
109
|
+
- Assign `rows`, `columns`, callbacks such as `getRowId`, and structured options as **properties**, not JSON attributes. Each row needs an `id` or a `getRowId` function. Assign a new array when data changes.
|
|
110
|
+
- The root owns one table instance. Its connected `.api` exposes model reads (`rows`, `columns`, `headerRows`, `headerRowCount`, `text`) and actions such as `sortBy`, `setSearch` and `select`. These are the same rows and actions the primitive uses.
|
|
111
|
+
- Parts bind behavior and ARIA to the markup you supply. They do **not** generate rows or cell content. Use `value` for a stable part key, not the row's current position. Set `index`, `row` and `col` from the visible model, starting at zero and counting header rows. The example renders ordinary data rows under one header; if you enable filtering, paging, grouping or column changes, update the parts to match those views too. For editors place a native `input` inside `ui-table-editor`.
|
|
112
|
+
- A change callback becomes a lowercase DOM event: `onSortChange` is `sortchange`, `onSelectedChange` is `selectedchange`. Listen on the root; these events don't bubble. The proposed value, reason and source are in `event.detail`. Call `event.preventDefault()` synchronously to refuse a cancelable change. For uncontrolled state read the resulting model after the event, as the example does with `queueMicrotask`.
|
|
113
|
+
- For a starting selection set `grid.defaultSelected` before connection. To control it instead, set `grid.selected` and assign accepted `event.detail.value` values back to that property. The attribute `selection="multiple"` chooses the selection mode; it is not a list of selected rows.
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ui-sdk-table-web",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.7",
|
|
4
|
+
"description": "Headless Web Components for the ui-sdk-table primitive.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/FallDownTheSystem/ui-sdk-table.git",
|
|
9
|
+
"directory": "packages/web"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"docs",
|
|
13
|
+
"dist/table.js",
|
|
14
|
+
"dist/table-licenses.md",
|
|
15
|
+
"dist/table-reference.js",
|
|
16
|
+
"dist/table-reference-licenses.md",
|
|
17
|
+
"src",
|
|
18
|
+
"!src/**/*.test.ts"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"sideEffects": [
|
|
22
|
+
"./dist/table.js",
|
|
23
|
+
"./dist/table-reference.js"
|
|
24
|
+
],
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./src/index.ts",
|
|
27
|
+
"./browser": "./dist/table.js",
|
|
28
|
+
"./reference": "./src/reference.ts",
|
|
29
|
+
"./reference/browser": "./dist/table-reference.js"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"ui-sdk-table-core": "0.2.7"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@playwright/test": "^1.65.1",
|
|
39
|
+
"@ui-sdk/core": "0.1.0-beta.13",
|
|
40
|
+
"@ui-sdk/web": "0.1.0-beta.13",
|
|
41
|
+
"jsdom": "^31.0.1",
|
|
42
|
+
"vite-plus": "^1.7.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@ui-sdk/core": "0.1.0-beta.13",
|
|
46
|
+
"@ui-sdk/web": "0.1.0-beta.13"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "vp build && vp build --mode reference",
|
|
50
|
+
"test:e2e": "pnpm run build && playwright test"
|
|
51
|
+
}
|
|
6
52
|
}
|
package/src/browser.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { PropsOf, SlotName, TypesOf } from '@ui-sdk/core'
|
|
2
|
+
import { createParts, wrappedChild, type UiDetailedChangeEvent, type UiElement } from '@ui-sdk/web'
|
|
3
|
+
import { table, type TableOptions } from 'ui-sdk-table-core'
|
|
4
|
+
|
|
5
|
+
const methods = [
|
|
6
|
+
'rows',
|
|
7
|
+
'columns',
|
|
8
|
+
'headerRows',
|
|
9
|
+
'headerRowCount',
|
|
10
|
+
'text',
|
|
11
|
+
'sortBy',
|
|
12
|
+
'toggleSort',
|
|
13
|
+
'sortOf',
|
|
14
|
+
'setSearch',
|
|
15
|
+
'setColumnFilter',
|
|
16
|
+
'setPage',
|
|
17
|
+
'setPageSize',
|
|
18
|
+
'setGroupBy',
|
|
19
|
+
'toggleExpanded',
|
|
20
|
+
'select',
|
|
21
|
+
'toggleSelected',
|
|
22
|
+
'isSelected',
|
|
23
|
+
'clearSelection',
|
|
24
|
+
'exportData',
|
|
25
|
+
] as const satisfies readonly (keyof TypesOf<typeof table>['api'])[]
|
|
26
|
+
|
|
27
|
+
export type TableElementApi = Pick<TypesOf<typeof table>['api'], (typeof methods)[number]>
|
|
28
|
+
|
|
29
|
+
export type TableEvents<Row = unknown> = {
|
|
30
|
+
[
|
|
31
|
+
K in keyof TableOptions<Row> as K extends `on${infer Name}` ? Lowercase<Name> : never
|
|
32
|
+
]-?: NonNullable<TableOptions<Row>[K]> extends (
|
|
33
|
+
value: infer V,
|
|
34
|
+
details: infer D extends { reason: string },
|
|
35
|
+
) => void
|
|
36
|
+
? UiDetailedChangeEvent<V, D>
|
|
37
|
+
: never
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type TableElement<Row = unknown> = UiElement<TableEvents<Row>> &
|
|
41
|
+
Omit<TableOptions<Row>, 'controller' | `on${string}`> & { readonly api?: TableElementApi }
|
|
42
|
+
|
|
43
|
+
type PartElements = {
|
|
44
|
+
[S in Exclude<SlotName<typeof table>, 'root'> as `ui-table-${S}`]: HTMLElement &
|
|
45
|
+
Partial<PropsOf<typeof table, S>>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
declare global {
|
|
49
|
+
interface HTMLElementTagNameMap extends PartElements {
|
|
50
|
+
'ui-table': TableElement
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function defineTableElements() {
|
|
55
|
+
return createParts(table, {
|
|
56
|
+
root: {
|
|
57
|
+
tag: 'ui-table',
|
|
58
|
+
display: 'table',
|
|
59
|
+
kinds: { controller: null },
|
|
60
|
+
api: [...methods],
|
|
61
|
+
defaults: { groupTotals: true, sortChildren: true },
|
|
62
|
+
},
|
|
63
|
+
head: { tag: 'ui-table-head', display: 'table-header-group' },
|
|
64
|
+
body: { tag: 'ui-table-body', display: 'table-row-group' },
|
|
65
|
+
foot: { tag: 'ui-table-foot', display: 'table-row-group' },
|
|
66
|
+
col: 'ui-table-col',
|
|
67
|
+
'header-row': { tag: 'ui-table-header-row', display: 'table-row' },
|
|
68
|
+
'plain-row': { tag: 'ui-table-plain-row', display: 'table-row' },
|
|
69
|
+
spacer: { tag: 'ui-table-spacer', display: 'table-row' },
|
|
70
|
+
row: { tag: 'ui-table-row', display: 'table-row' },
|
|
71
|
+
cell: { tag: 'ui-table-cell', display: 'table-cell' },
|
|
72
|
+
editor: { tag: 'ui-table-editor', target: wrappedChild('input'), display: 'contents' },
|
|
73
|
+
'edit-error': 'ui-table-edit-error',
|
|
74
|
+
'fill-handle': 'ui-table-fill-handle',
|
|
75
|
+
resizer: 'ui-table-resizer',
|
|
76
|
+
})
|
|
77
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { applyAttrs, attrs, bind, commit, partState, subscribe } from '@ui-sdk/core/renderer'
|
|
2
|
+
import { tableElements } from 'ui-sdk-table-core/elements'
|
|
3
|
+
import type { table } from 'ui-sdk-table-core'
|
|
4
|
+
import {
|
|
5
|
+
renderReference,
|
|
6
|
+
referenceStructureKeys,
|
|
7
|
+
type PartAttrs,
|
|
8
|
+
type ReferenceContent,
|
|
9
|
+
type ReferenceFactory,
|
|
10
|
+
type ReferenceKind,
|
|
11
|
+
type ReferenceOptions,
|
|
12
|
+
type ReferenceState,
|
|
13
|
+
} from 'ui-sdk-table-core/reference'
|
|
14
|
+
|
|
15
|
+
type Instance = ReturnType<typeof table>
|
|
16
|
+
type Item = { kind: ReferenceKind; props: PartAttrs; children?: (state: ReferenceState) => Content }
|
|
17
|
+
type Content = ReferenceContent<Item | Node>
|
|
18
|
+
export type WebContent = Element | Text | string | null | undefined
|
|
19
|
+
const factory: ReferenceFactory<Item | WebContent> = (kind, props, children) => ({
|
|
20
|
+
kind,
|
|
21
|
+
props,
|
|
22
|
+
children,
|
|
23
|
+
})
|
|
24
|
+
const tags = {
|
|
25
|
+
...tableElements,
|
|
26
|
+
'header-cell': 'th',
|
|
27
|
+
button: 'button',
|
|
28
|
+
colgroup: 'colgroup',
|
|
29
|
+
} as const
|
|
30
|
+
const partProps = new Set(['value', 'row', 'col', 'index', 'size', 'header', 'colspan', 'rowspan'])
|
|
31
|
+
|
|
32
|
+
type Entry = {
|
|
33
|
+
node: Node
|
|
34
|
+
update(item: Item | Node | string): void
|
|
35
|
+
destroy(): void
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function write(el: HTMLElement, name: string, value: unknown, old: unknown) {
|
|
39
|
+
if (name === 'key' || (partProps.has(name) && name !== 'colspan' && name !== 'rowspan')) return
|
|
40
|
+
if (/^on/i.test(name)) {
|
|
41
|
+
const event = name.slice(2).toLowerCase()
|
|
42
|
+
if (typeof old === 'function') el.removeEventListener(event, old as EventListener)
|
|
43
|
+
if (typeof value === 'function') el.addEventListener(event, value as EventListener)
|
|
44
|
+
} else if (value === true || typeof value === 'number' || typeof value === 'string') {
|
|
45
|
+
el.setAttribute(name, value === true ? '' : String(value))
|
|
46
|
+
} else {
|
|
47
|
+
el.removeAttribute(name)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function childrenOf(parent: HTMLElement, instance: Instance) {
|
|
52
|
+
let entries = new Map<string, Entry>()
|
|
53
|
+
return {
|
|
54
|
+
update(content: Content) {
|
|
55
|
+
const next = new Map<string, Entry>()
|
|
56
|
+
let at = parent.firstChild
|
|
57
|
+
function visit(item: Content) {
|
|
58
|
+
if (Array.isArray(item)) {
|
|
59
|
+
item.forEach(visit)
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
if (item == null) return
|
|
63
|
+
const descriptor = typeof item === 'object' && !(item instanceof Node) ? item : undefined
|
|
64
|
+
const key = JSON.stringify([
|
|
65
|
+
descriptor?.kind ?? typeof item,
|
|
66
|
+
descriptor?.props.key ?? descriptor?.props.value ?? next.size,
|
|
67
|
+
])
|
|
68
|
+
let entry = entries.get(key)
|
|
69
|
+
if (entry && item instanceof Node && entry.node !== item) {
|
|
70
|
+
if (at === entry.node) at = entry.node.nextSibling
|
|
71
|
+
entry.destroy()
|
|
72
|
+
entry = undefined
|
|
73
|
+
}
|
|
74
|
+
entry ??= descriptor
|
|
75
|
+
? elementEntry(instance, descriptor, parent.ownerDocument)
|
|
76
|
+
: {
|
|
77
|
+
node: item instanceof Node ? item : parent.ownerDocument.createTextNode(''),
|
|
78
|
+
update(value) {
|
|
79
|
+
if (typeof value === 'string' && this.node.nodeValue !== value)
|
|
80
|
+
this.node.nodeValue = value
|
|
81
|
+
},
|
|
82
|
+
destroy() {
|
|
83
|
+
this.node.parentNode?.removeChild(this.node)
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
next.set(key, entry)
|
|
87
|
+
if (entry.node !== at) {
|
|
88
|
+
if (entry.node.parentNode === parent && typeof parent.moveBefore === 'function')
|
|
89
|
+
parent.moveBefore(entry.node, at)
|
|
90
|
+
else if (
|
|
91
|
+
at &&
|
|
92
|
+
entry.node.parentNode === parent &&
|
|
93
|
+
entry.node.contains(parent.ownerDocument.activeElement)
|
|
94
|
+
) {
|
|
95
|
+
// Older browsers blur a focused subtree on insertBefore. Move its siblings instead.
|
|
96
|
+
const after = entry.node.nextSibling
|
|
97
|
+
while (at && at !== entry.node) {
|
|
98
|
+
const sibling: ChildNode | null = at.nextSibling
|
|
99
|
+
parent.insertBefore(at, after)
|
|
100
|
+
at = sibling
|
|
101
|
+
}
|
|
102
|
+
} else parent.insertBefore(entry.node, at)
|
|
103
|
+
}
|
|
104
|
+
entry.update(item)
|
|
105
|
+
at = entry.node.nextSibling
|
|
106
|
+
}
|
|
107
|
+
visit(content)
|
|
108
|
+
for (const [key, entry] of entries) if (!next.has(key)) entry.destroy()
|
|
109
|
+
entries = next
|
|
110
|
+
},
|
|
111
|
+
destroy() {
|
|
112
|
+
for (const entry of entries.values()) entry.destroy()
|
|
113
|
+
entries.clear()
|
|
114
|
+
},
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function elementEntry(instance: Instance, initial: Item, doc: Document): Entry {
|
|
119
|
+
const el = doc.createElement(tags[initial.kind])
|
|
120
|
+
const children = childrenOf(el, instance)
|
|
121
|
+
let current = initial
|
|
122
|
+
const slot = initial.kind === 'header-cell' ? 'cell' : initial.kind
|
|
123
|
+
const part =
|
|
124
|
+
slot === 'button' || slot === 'colgroup' ? undefined : instance.part(slot, initial.props)
|
|
125
|
+
let unbind: (() => void) | undefined
|
|
126
|
+
let unsubscribe: (() => void) | undefined
|
|
127
|
+
let previous: PartAttrs = {}
|
|
128
|
+
const render = () => {
|
|
129
|
+
if (part) applyAttrs(el, attrs(part))
|
|
130
|
+
children.update(current.children?.(part ? (partState(part) as ReferenceState) : {}))
|
|
131
|
+
}
|
|
132
|
+
let queued = false
|
|
133
|
+
let disposed = false
|
|
134
|
+
return {
|
|
135
|
+
node: el,
|
|
136
|
+
update(next) {
|
|
137
|
+
if (typeof next === 'string' || next instanceof Node) return
|
|
138
|
+
current = next
|
|
139
|
+
for (const name of Object.keys(previous))
|
|
140
|
+
if (!(name in next.props)) write(el, name, undefined, previous[name])
|
|
141
|
+
for (const [name, value] of Object.entries(next.props))
|
|
142
|
+
if (value !== previous[name]) write(el, name, value, previous[name])
|
|
143
|
+
if (part) {
|
|
144
|
+
if (!unbind) {
|
|
145
|
+
unbind = bind(part, el)
|
|
146
|
+
unsubscribe = subscribe(part, () => {
|
|
147
|
+
if (queued) return
|
|
148
|
+
queued = true
|
|
149
|
+
queueMicrotask(() => {
|
|
150
|
+
if (!disposed && queued) render()
|
|
151
|
+
queued = false
|
|
152
|
+
})
|
|
153
|
+
})
|
|
154
|
+
} else if ([...partProps].some((key) => previous[key] !== next.props[key]))
|
|
155
|
+
commit(part, next.props)
|
|
156
|
+
}
|
|
157
|
+
previous = next.props
|
|
158
|
+
queued = false
|
|
159
|
+
render()
|
|
160
|
+
},
|
|
161
|
+
destroy() {
|
|
162
|
+
disposed = true
|
|
163
|
+
unsubscribe?.()
|
|
164
|
+
children.destroy()
|
|
165
|
+
unbind?.()
|
|
166
|
+
el.remove()
|
|
167
|
+
},
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function mountReference(
|
|
172
|
+
el: HTMLElement,
|
|
173
|
+
instance: Instance,
|
|
174
|
+
options: () => Omit<ReferenceOptions<WebContent>, 'shown'>,
|
|
175
|
+
) {
|
|
176
|
+
const tree = childrenOf(el, instance)
|
|
177
|
+
const read = () => referenceStructureKeys.map((key) => instance.get()[key])
|
|
178
|
+
let seen = read()
|
|
179
|
+
let live = true
|
|
180
|
+
let pending = false
|
|
181
|
+
const render = () => {
|
|
182
|
+
pending = false
|
|
183
|
+
tree.update(renderReference<Item | WebContent>(instance.api, factory, options()))
|
|
184
|
+
}
|
|
185
|
+
const update = () => {
|
|
186
|
+
if (pending) return
|
|
187
|
+
pending = true
|
|
188
|
+
queueMicrotask(() => {
|
|
189
|
+
if (live && pending) render()
|
|
190
|
+
})
|
|
191
|
+
}
|
|
192
|
+
const stop = instance.subscribe(() => {
|
|
193
|
+
const next = read()
|
|
194
|
+
if (next.some((value, i) => value !== seen[i])) update()
|
|
195
|
+
seen = next
|
|
196
|
+
})
|
|
197
|
+
update()
|
|
198
|
+
return {
|
|
199
|
+
update,
|
|
200
|
+
destroy() {
|
|
201
|
+
live = false
|
|
202
|
+
stop()
|
|
203
|
+
tree.destroy()
|
|
204
|
+
},
|
|
205
|
+
}
|
|
206
|
+
}
|
package/src/reference.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { createParts } from '@ui-sdk/web'
|
|
2
|
+
import { table, type TableController } from 'ui-sdk-table-core'
|
|
3
|
+
import type { ReferenceSlots, TableAttrsProps } from 'ui-sdk-table-core/reference'
|
|
4
|
+
import type { TableElement, TableElementApi } from './index'
|
|
5
|
+
import { mountReference, type WebContent } from './reference-dom'
|
|
6
|
+
|
|
7
|
+
export type ReferenceTableElement<Row = unknown> = Omit<TableElement<Row>, 'api'> &
|
|
8
|
+
TableAttrsProps<Row> & {
|
|
9
|
+
readonly api?: TableController & TableElementApi
|
|
10
|
+
renderers?: ReferenceSlots<Row, WebContent>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
declare global {
|
|
14
|
+
interface HTMLElementTagNameMap {
|
|
15
|
+
'ui-reference-table': ReferenceTableElement
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type View = ReturnType<typeof mountReference>
|
|
20
|
+
const views = new WeakMap<
|
|
21
|
+
HTMLElement,
|
|
22
|
+
{ instance: ReturnType<typeof table>; view: View; generation: number }
|
|
23
|
+
>()
|
|
24
|
+
|
|
25
|
+
export function defineReferenceTable() {
|
|
26
|
+
return createParts(table, {
|
|
27
|
+
root: {
|
|
28
|
+
tag: 'ui-reference-table',
|
|
29
|
+
display: 'table',
|
|
30
|
+
kinds: { controller: null },
|
|
31
|
+
defaults: { groupTotals: true, sortChildren: true },
|
|
32
|
+
properties: ['renderers', 'rowAttrs', 'cellAttrs', 'sectionAttrs'],
|
|
33
|
+
render(host, instance) {
|
|
34
|
+
const el = host as ReferenceTableElement
|
|
35
|
+
let record = views.get(el)
|
|
36
|
+
if (record?.instance !== instance) {
|
|
37
|
+
record?.view.destroy()
|
|
38
|
+
const view = mountReference(el, instance, () => ({
|
|
39
|
+
slots: el.renderers,
|
|
40
|
+
rowAttrs: el.rowAttrs,
|
|
41
|
+
cellAttrs: el.cellAttrs,
|
|
42
|
+
sectionAttrs: el.sectionAttrs,
|
|
43
|
+
}))
|
|
44
|
+
record = { instance, view, generation: 0 }
|
|
45
|
+
views.set(el, record)
|
|
46
|
+
Object.defineProperty(el, 'api', {
|
|
47
|
+
configurable: true,
|
|
48
|
+
get: () => (el.isConnected ? views.get(el)?.instance.api : undefined),
|
|
49
|
+
})
|
|
50
|
+
} else record.view.update()
|
|
51
|
+
const generation = ++record.generation
|
|
52
|
+
return () => {
|
|
53
|
+
// The SDK runs cleanup both before the next property update and on disconnect.
|
|
54
|
+
queueMicrotask(() => {
|
|
55
|
+
if (views.get(el) !== record || record.generation !== generation) return
|
|
56
|
+
record.view.destroy()
|
|
57
|
+
views.delete(el)
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
})
|
|
63
|
+
}
|