ui-sdk-table-web 0.0.0-stage → 0.2.10
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 +11119 -0
- package/dist/table.js +10635 -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/docs/webmcp.md +97 -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,268 @@
|
|
|
1
|
+
# Sort, filter and search
|
|
2
|
+
|
|
3
|
+
Sort by one or more columns, filter each column, and search every column with a pause in the typing.
|
|
4
|
+
|
|
5
|
+
The rows pass a sort, then the filters. Sort is first on purpose: a filter keeps the order of its input, so a key in the search field never sorts. The `sort` and `filter` states hold what a person chose.
|
|
6
|
+
|
|
7
|
+
## Sort
|
|
8
|
+
|
|
9
|
+
A column with `sortable: true` gets a button in its header. A press sorts up, the next press down, and the third press takes the sort away.
|
|
10
|
+
|
|
11
|
+
```ts title="sort.ts"
|
|
12
|
+
import { type ColumnDef } from 'ui-sdk-table-core'
|
|
13
|
+
|
|
14
|
+
import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
|
|
15
|
+
|
|
16
|
+
type Runner = { id: string; name: string; club: string; time: number }
|
|
17
|
+
|
|
18
|
+
const runners: Runner[] = [
|
|
19
|
+
{ id: 'r1', name: 'Jack Ellis', club: 'Harriers', time: 34.12 },
|
|
20
|
+
{ id: 'r2', name: 'Ivy Adams', club: 'Striders', time: 33.48 },
|
|
21
|
+
{ id: 'r3', name: 'Cleo Vance', club: 'Harriers', time: 35.02 },
|
|
22
|
+
{ id: 'r4', name: 'Ben Nolan', club: 'Striders', time: 32.57 },
|
|
23
|
+
{ id: 'r5', name: 'Rosa Diaz', club: 'Pacers', time: 33.9 },
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
const columns: ColumnDef<Runner>[] = [
|
|
27
|
+
{ id: 'name', header: 'Runner', sortable: true },
|
|
28
|
+
{ id: 'club', header: 'Club', sortable: true },
|
|
29
|
+
{
|
|
30
|
+
id: 'time',
|
|
31
|
+
header: '10 km',
|
|
32
|
+
sortable: true,
|
|
33
|
+
format: 'decimal',
|
|
34
|
+
formatOptions: { suffix: ' min' },
|
|
35
|
+
},
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
export default function mount(host: HTMLElement) {
|
|
39
|
+
const style = document.createElement('style')
|
|
40
|
+
style.textContent = `.race-results th[data-sort-index]::before { content: attr(data-sort-index) ' '; font-weight: bold }`
|
|
41
|
+
host.append(style)
|
|
42
|
+
defineReferenceTable()
|
|
43
|
+
const table = document.createElement('ui-reference-table') as ReferenceTableElement<Runner>
|
|
44
|
+
table.setAttribute('aria-label', 'Results of the 10 km run')
|
|
45
|
+
table.rows = runners
|
|
46
|
+
table.columns = columns
|
|
47
|
+
table.className = 'race-results'
|
|
48
|
+
|
|
49
|
+
host.append(table)
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Press Club, then hold Shift and press 10 km. The clubs stay together, and inside each club the fastest runner comes first. The number before each header is its place in the sort.
|
|
54
|
+
|
|
55
|
+
- `sort` is a list of `{ id, desc? }`. The first rule sorts first.
|
|
56
|
+
- `multiSort` decides how a press adds a column. `'modifier'` (the default): a press replaces the sort, and a press with Shift, Control or Command adds the column. `'none'`: one column at a time, whatever key is held. `'always'`: each press adds the column. Enter on a header follows the same rule.
|
|
57
|
+
- `alwaysSorted`: a press on the only column of the sort turns the order and never clears it. It does not sort a table that starts without a sort; give `defaultSort` for that.
|
|
58
|
+
- `compare(a, b, rowA, rowB)` on a column gives it its own order, such as Urgent before Low.
|
|
59
|
+
- `sortChildren: false` keeps the child rows of a tree in their order.
|
|
60
|
+
- `toggleSort(event)` in custom `header` content passes the event, and the table reads the keys. `controller.toggleSort(id, { append: true })` and `sortBy(id, direction, { append: true })` throw in the mode `none`.
|
|
61
|
+
- A `sort` of the owner with more columns than the mode allows is left as it is.
|
|
62
|
+
|
|
63
|
+
## Search with a pause
|
|
64
|
+
|
|
65
|
+
```ts title="search.ts"
|
|
66
|
+
import { type ColumnDef } from 'ui-sdk-table-core'
|
|
67
|
+
|
|
68
|
+
import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
|
|
69
|
+
|
|
70
|
+
type Member = { id: string; name: string; team: string; city: string }
|
|
71
|
+
|
|
72
|
+
const members: Member[] = [
|
|
73
|
+
{ id: 'm1', name: 'Ada Lindqvist', team: 'Design', city: 'Stockholm' },
|
|
74
|
+
{ id: 'm2', name: 'Tomas Reyes', team: 'Platform', city: 'Madrid' },
|
|
75
|
+
{ id: 'm3', name: 'Maya Okafor', team: 'Design', city: 'Lagos' },
|
|
76
|
+
{ id: 'm4', name: 'Leo Brandt', team: 'Support', city: 'Hamburg' },
|
|
77
|
+
{ id: 'm5', name: 'Nina Sato', team: 'Platform', city: 'Osaka' },
|
|
78
|
+
{ id: 'm6', name: 'Omar Haddad', team: 'Support', city: 'Amman' },
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
const columns: ColumnDef<Member>[] = [
|
|
82
|
+
{ id: 'name', header: 'Name' },
|
|
83
|
+
{ id: 'team', header: 'Team' },
|
|
84
|
+
{ id: 'city', header: 'City' },
|
|
85
|
+
]
|
|
86
|
+
|
|
87
|
+
export default function mount(host: HTMLElement) {
|
|
88
|
+
defineReferenceTable()
|
|
89
|
+
const table = document.createElement('ui-reference-table') as ReferenceTableElement<Member>
|
|
90
|
+
table.setAttribute('aria-label', 'Members of the team')
|
|
91
|
+
table.rows = members
|
|
92
|
+
table.columns = columns
|
|
93
|
+
table.searchDebounce = 300
|
|
94
|
+
table.renderers = { empty: () => 'No member matches the search.' }
|
|
95
|
+
const field = document.createElement('input')
|
|
96
|
+
field.type = 'search'
|
|
97
|
+
field.setAttribute('aria-label', 'Search the members')
|
|
98
|
+
field.placeholder = 'Name, team or city'
|
|
99
|
+
field.addEventListener('input', (event) => table.api?.inputSearch(field.value, event))
|
|
100
|
+
field.addEventListener('keydown', (event) => {
|
|
101
|
+
if (event.key === 'Escape') table.api?.setSearch('')
|
|
102
|
+
if (event.key === 'Enter') table.api?.flushSearch()
|
|
103
|
+
})
|
|
104
|
+
host.append(field)
|
|
105
|
+
const status = document.createElement('p')
|
|
106
|
+
status.setAttribute('role', 'status')
|
|
107
|
+
host.append(status)
|
|
108
|
+
host.append(table)
|
|
109
|
+
const api = table.api
|
|
110
|
+
if (!api) return
|
|
111
|
+
function update() {
|
|
112
|
+
const view = table.api?.getView()
|
|
113
|
+
if (!view) return
|
|
114
|
+
if (field.value !== view.search) field.value = view.search
|
|
115
|
+
status.textContent = view.searchPending
|
|
116
|
+
? 'Waiting for a pause'
|
|
117
|
+
: `${view.dataRowCount} members found`
|
|
118
|
+
}
|
|
119
|
+
update()
|
|
120
|
+
return api.subscribe(update)
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Type desi. The field shows each letter at once, and the table filters when the typing pauses. Escape clears the field, and Enter filters without the wait.
|
|
125
|
+
|
|
126
|
+
Send the field’s text to `inputSearch(text, event)` and display `getView().search`. The field updates immediately; `searchPending` stays true until the table filters after `searchDebounce` milliseconds (200 by default, or 0 for no pause). Call `flushSearch()` on Enter or `setSearch('')` on Escape.
|
|
127
|
+
|
|
128
|
+
The filter changes once after the pause, with the reason `input-change`. `setSearch`, `clearFilters` or new search text in a controlled `filter` cancels the pending search.
|
|
129
|
+
|
|
130
|
+
- The search reads the text of each cell, as the screen shows it. `searchable: false` leaves a column out.
|
|
131
|
+
- `searchMode: 'fuzzy'` also finds near matches, with letters in order but apart. `fuzzyThreshold` (0 to 1) is the score that a match needs.
|
|
132
|
+
- The table says nothing to a screen reader about a search. Announce the rows found when `filter` changes, which is one time after the pause.
|
|
133
|
+
|
|
134
|
+
`createDebounce(run, { debounce })` is the underlying timer. It provides `call`, `cancel`, `flush` and `pending`.
|
|
135
|
+
|
|
136
|
+
## Filter a column
|
|
137
|
+
|
|
138
|
+
`filter.columns` holds one value for each column that filters. `controller.setColumnFilter(columnId, value)` sets it. The shape of the value decides how it matches, unless the column says otherwise with `filter`:
|
|
139
|
+
|
|
140
|
+
| Value | Kind | Matches |
|
|
141
|
+
| --- | --- | --- |
|
|
142
|
+
| A text | `text` | Cells whose text holds it |
|
|
143
|
+
| A list | `select` | Cells whose value is in the list |
|
|
144
|
+
| `{ min, max }` | `range` | Numbers between the ends. One end can be left out |
|
|
145
|
+
| `{ from, to }` | `dateRange` | Dates between the days. A day as `YYYY-MM-DD` means the whole day |
|
|
146
|
+
|
|
147
|
+
A function `filter(value, filterValue, row)` on a column decides for itself. An empty text, an empty list and an object of empty ends ask for nothing.
|
|
148
|
+
|
|
149
|
+
## Facets for filter controls
|
|
150
|
+
|
|
151
|
+
```ts title="facets.ts"
|
|
152
|
+
import { type ColumnDef } from 'ui-sdk-table-core'
|
|
153
|
+
|
|
154
|
+
import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
|
|
155
|
+
|
|
156
|
+
type Shoe = { id: string; model: string; brand: string; color: string }
|
|
157
|
+
|
|
158
|
+
const shoes: Shoe[] = [
|
|
159
|
+
{ id: 's1', model: 'Trail 2', brand: 'Northway', color: 'Black' },
|
|
160
|
+
{ id: 's2', model: 'Glide', brand: 'Sola', color: 'White' },
|
|
161
|
+
{ id: 's3', model: 'Ridge', brand: 'Northway', color: 'Green' },
|
|
162
|
+
{ id: 's4', model: 'Tempo', brand: 'Sola', color: 'Black' },
|
|
163
|
+
{ id: 's5', model: 'Cloud 9', brand: 'Arc', color: 'White' },
|
|
164
|
+
{ id: 's6', model: 'Summit', brand: 'Northway', color: 'Black' },
|
|
165
|
+
]
|
|
166
|
+
|
|
167
|
+
const columns: ColumnDef<Shoe>[] = [
|
|
168
|
+
{ id: 'model', header: 'Model' },
|
|
169
|
+
{ id: 'brand', header: 'Brand', filter: 'select' },
|
|
170
|
+
{ id: 'color', header: 'Color', filter: 'select' },
|
|
171
|
+
]
|
|
172
|
+
|
|
173
|
+
const LISTS = [
|
|
174
|
+
{ id: 'brand', label: 'Brand' },
|
|
175
|
+
{ id: 'color', label: 'Color' },
|
|
176
|
+
]
|
|
177
|
+
|
|
178
|
+
function button(label: string, action: () => void) {
|
|
179
|
+
const el = document.createElement('button')
|
|
180
|
+
el.type = 'button'
|
|
181
|
+
el.textContent = label
|
|
182
|
+
el.addEventListener('click', action)
|
|
183
|
+
return el
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export default function mount(host: HTMLElement) {
|
|
187
|
+
defineReferenceTable()
|
|
188
|
+
const table = document.createElement('ui-reference-table') as ReferenceTableElement<Shoe>
|
|
189
|
+
table.setAttribute('aria-label', 'Running shoes')
|
|
190
|
+
table.rows = shoes
|
|
191
|
+
table.columns = columns
|
|
192
|
+
|
|
193
|
+
const groups = LISTS.map(({ id, label }) => {
|
|
194
|
+
const group = document.createElement('fieldset')
|
|
195
|
+
const legend = document.createElement('legend')
|
|
196
|
+
legend.textContent = label
|
|
197
|
+
group.append(legend)
|
|
198
|
+
host.append(group)
|
|
199
|
+
return { id, group, buttons: new Map<string, HTMLButtonElement>() }
|
|
200
|
+
})
|
|
201
|
+
host.append(table)
|
|
202
|
+
const api = table.api
|
|
203
|
+
if (!api) return
|
|
204
|
+
function update() {
|
|
205
|
+
const view = table.api?.getView()
|
|
206
|
+
if (!view) return
|
|
207
|
+
for (const { id, group, buttons } of groups) {
|
|
208
|
+
const values = table.api?.getFacets(id).values ?? []
|
|
209
|
+
const chosen = view.filter.columns?.[id]
|
|
210
|
+
const selected = Array.isArray(chosen) ? chosen : []
|
|
211
|
+
for (const facet of values) {
|
|
212
|
+
const value = String(facet.value)
|
|
213
|
+
let el = buttons.get(value)
|
|
214
|
+
if (!el) {
|
|
215
|
+
el = button('', () => {
|
|
216
|
+
const filter = table.api?.getView().filter.columns?.[id]
|
|
217
|
+
const current: unknown[] = Array.isArray(filter) ? filter : []
|
|
218
|
+
table.api?.setColumnFilter(
|
|
219
|
+
id,
|
|
220
|
+
current.includes(value)
|
|
221
|
+
? current.filter((item) => item !== value)
|
|
222
|
+
: [...current, value],
|
|
223
|
+
)
|
|
224
|
+
})
|
|
225
|
+
buttons.set(value, el)
|
|
226
|
+
group.append(el)
|
|
227
|
+
}
|
|
228
|
+
el.textContent = `${facet.text} (${facet.count})`
|
|
229
|
+
el.setAttribute('aria-pressed', String(selected.includes(value)))
|
|
230
|
+
}
|
|
231
|
+
for (const [value, el] of buttons)
|
|
232
|
+
if (!values.some((facet) => String(facet.value) === value)) {
|
|
233
|
+
el.remove()
|
|
234
|
+
buttons.delete(value)
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
update()
|
|
239
|
+
return api.subscribe(update)
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
Press Northway. The colors list counts only the shoes of Northway. Press Black too: two shoes match both. Press Northway again to let it go.
|
|
244
|
+
|
|
245
|
+
`controller.getFacets(columnId)` answers with what a control offers:
|
|
246
|
+
|
|
247
|
+
| Control | Read |
|
|
248
|
+
| --- | --- |
|
|
249
|
+
| A list of values | `values`: each with `value`, `text` in the format of the column, and `count`. A combobox can search the list by `text`; `fuzzyScore` of `@ui-sdk/core/text-match` is the search of the table |
|
|
250
|
+
| A slider | `range`: `min`, `max` and `step`, the smallest change that the format of the column shows |
|
|
251
|
+
| A date picker | `days`: the first and the last day as `YYYY-MM-DD`, in the time zone of the column; `dates` holds the two moments |
|
|
252
|
+
|
|
253
|
+
- The facets follow the search and the filters of the other columns, and never the filter of the column itself, so a filter does not take away its own choices. `getFacets(columnId, { rows: 'all' })` gives the facets of every row.
|
|
254
|
+
- A value that the filter names stays in the list with the count 0 when no row holds it any more.
|
|
255
|
+
- `kind` says which filter fits the column: its `filter` option, or what its format says. A column of dates gives `dateRange`, a column of numbers `range`, any other column `text`.
|
|
256
|
+
- `sliderBounds(range, { steps: 100 })` gives a slider its ends and a step that a thumb can use: 1, 2 or 5 times a power of ten of the step of a cell, and about a hundred stops.
|
|
257
|
+
- A facet is counted when it is asked for, and kept until the search or another filter changes.
|
|
258
|
+
|
|
259
|
+
## Names
|
|
260
|
+
|
|
261
|
+
- **Exports:** `createDebounce`, `sliderBounds`, `isEmptyFilter`, and the types `FilterKind`, `FilterMatcher`, `MultiSortMode`.
|
|
262
|
+
- **Attributes:** `aria-sort`, `data-sorted` and `data-sort-index` on a header cell.
|
|
263
|
+
|
|
264
|
+
For shared methods and result types, see [Controller](./reference-controller.md) and [View](./reference-view.md).
|
|
265
|
+
|
|
266
|
+
For custom elements, properties and DOM events, see the [Web Components API reference](./api-web-components.md).
|
|
267
|
+
|
|
268
|
+
See the [Sort tiles](https://ui-sdk-table.fdts.dev/?page=sink&only=sort) and the [Filter tiles](https://ui-sdk-table.fdts.dev/?page=sink&only=filter) of the sink.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# State and events
|
|
2
|
+
|
|
3
|
+
Start a state where you want it, hold it in your app, and hear each change with its reason.
|
|
4
|
+
|
|
5
|
+
Every state of the table works the same way, as the state of a UI SDK component does. Use `default<Key>` to choose an initial value, or pass the current value and handle change requests to keep the state in your app.
|
|
6
|
+
|
|
7
|
+
```ts title="controlled.ts"
|
|
8
|
+
import { type ColumnDef } from 'ui-sdk-table-core'
|
|
9
|
+
|
|
10
|
+
import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
|
|
11
|
+
|
|
12
|
+
type Player = { id: string; name: string; club: string; goals: number }
|
|
13
|
+
|
|
14
|
+
const players: Player[] = [
|
|
15
|
+
{ id: 'p1', name: 'Lena Fischer', club: 'Rovers', goals: 14 },
|
|
16
|
+
{ id: 'p2', name: 'Sam Carter', club: 'United', goals: 21 },
|
|
17
|
+
{ id: 'p3', name: 'Noor Haddad', club: 'Rovers', goals: 9 },
|
|
18
|
+
{ id: 'p4', name: 'Ivan Petrov', club: 'Athletic', goals: 17 },
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
const columns: ColumnDef<Player>[] = [
|
|
22
|
+
{ id: 'name', header: 'Player', sortable: true },
|
|
23
|
+
{ id: 'club', header: 'Club', sortable: true },
|
|
24
|
+
{ id: 'goals', header: 'Goals', sortable: true, format: 'integer' },
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
export default function mount(host: HTMLElement) {
|
|
28
|
+
defineReferenceTable()
|
|
29
|
+
const table = document.createElement('ui-reference-table') as ReferenceTableElement<Player>
|
|
30
|
+
table.setAttribute('aria-label', 'Goals of the season')
|
|
31
|
+
table.rows = players
|
|
32
|
+
table.columns = columns
|
|
33
|
+
table.sort = [{ id: 'goals', desc: true }]
|
|
34
|
+
table.addEventListener('sortchange', (event) => {
|
|
35
|
+
const { value, reason } = event.detail
|
|
36
|
+
if (value.length === 1 && value[0]?.id === 'club') {
|
|
37
|
+
event.preventDefault()
|
|
38
|
+
status.textContent =
|
|
39
|
+
'Refused: sort by goals or by player first, then add the club with Shift.'
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
table.sort = value
|
|
43
|
+
status.textContent = `${value.map((rule) => `${rule.id} ${rule.desc ? 'down' : 'up'}`).join(', ') || 'no sort'}, by ${reason}`
|
|
44
|
+
})
|
|
45
|
+
const status = document.createElement('p')
|
|
46
|
+
status.setAttribute('role', 'status')
|
|
47
|
+
host.append(status)
|
|
48
|
+
host.append(table)
|
|
49
|
+
status.textContent = 'No change yet.'
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Press a header to sort. The line under the table says the sort and its reason. Press Club alone and the app refuses it; press Goals, then Club with Shift, and the sort holds both.
|
|
54
|
+
|
|
55
|
+
## Three props for each state
|
|
56
|
+
|
|
57
|
+
| Prop | What it does |
|
|
58
|
+
| --- | --- |
|
|
59
|
+
| `<key>` | The current value when the app holds the state |
|
|
60
|
+
| `default<Key>` | Where the state starts when the table holds it |
|
|
61
|
+
| `on<Key>Change` | The core callback for a requested change, with `(value, details)` |
|
|
62
|
+
|
|
63
|
+
Listen for `sortchange` on the element. Read `event.detail.value` and `event.detail.reason`; assign the accepted value to `table.sort` or refuse it with `event.preventDefault()`.
|
|
64
|
+
|
|
65
|
+
The keys are `sort`, `filter`, `groupBy`, `page`, `expanded`, `selected`, `columnVisibility`, `columnOrder`, `columnWidths`, `columnPinning`, `rowPinning`, `columnAggregates`, `cellFormats`, `cellFormulas` and `cellRange`.
|
|
66
|
+
|
|
67
|
+
`columnWidths` and `cellRange` change at each step of a drag. The core callbacks `onColumnWidthsCommit` and `onCellRangeCommit` run once, when the drag ends.
|
|
68
|
+
|
|
69
|
+
## The details of a change
|
|
70
|
+
|
|
71
|
+
- `details.reason` says why the state changed: `press`, `item-press`, `list-navigation`, `input-change`, `imperative` (a call of the controller), `reset`, `drag`, `move-key`, `resize-key`, `escape-key`, `shortcut`, `undo` or `redo`. The type is `TableReason`.
|
|
72
|
+
- `details.event` is the event behind the change, when there was one.
|
|
73
|
+
- `details.source` says how the person caused it: `mouse`, `touch`, `pen`, `keyboard`, `virtual`, `agent` for a call of an agent, or `programmatic`. Read it for analytics, never for behavior.
|
|
74
|
+
- `details.cancel()` refuses the change. If your app controls the value, leave it unchanged when refusing the request.
|
|
75
|
+
|
|
76
|
+
## The table never changes your data
|
|
77
|
+
|
|
78
|
+
The rows and the columns are yours. The table never copies or changes a row object. Each row needs a stable id: the `id` property, or what `getRowId(row, index, parent)` returns.
|
|
79
|
+
|
|
80
|
+
An edit of a cell is no state. The table reports the edits (`onCellEdit` in the core API), and the app puts the value into its rows: see [Editing](./cell-editing.md).
|
|
81
|
+
|
|
82
|
+
If your app only needs to hear a change, let the table hold the value: use `default<Key>` and listen for changes without passing `<key>` back.
|
|
83
|
+
|
|
84
|
+
## Keep going
|
|
85
|
+
|
|
86
|
+
- [Controller and view](./controller-and-view.md) changes a state from code, and reads what the table shows.
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# A styled table for an app
|
|
2
|
+
|
|
3
|
+
Build the table component of your design system on the headless table: the order of the work, and for each control what the library gives and what your app writes.
|
|
4
|
+
|
|
5
|
+
The example uses the [reference table](./reference-implementation.md) to show how search, column visibility and pagination work together. For your own design system, build on the [native parts](./table-components.md). Each page can then pass rows and columns to the same styled table.
|
|
6
|
+
|
|
7
|
+
```ts title="toolbar.ts"
|
|
8
|
+
import { type ColumnDef } from 'ui-sdk-table-core'
|
|
9
|
+
import '@ui-sdk/web/toolbar'
|
|
10
|
+
import '@ui-sdk/web/button'
|
|
11
|
+
import '@ui-sdk/web/checkbox'
|
|
12
|
+
import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
|
|
13
|
+
|
|
14
|
+
type Book = { id: string; title: string; author: string; year: number }
|
|
15
|
+
|
|
16
|
+
const books: Book[] = [
|
|
17
|
+
{ id: 'b1', title: 'The Long Field', author: 'Ines Moreau', year: 2019 },
|
|
18
|
+
{ id: 'b2', title: 'Salt and Iron', author: 'Tom Ashby', year: 2021 },
|
|
19
|
+
{ id: 'b3', title: 'Small Hours', author: 'Ines Moreau', year: 2016 },
|
|
20
|
+
{ id: 'b4', title: 'North Light', author: 'Kai Lund', year: 2023 },
|
|
21
|
+
{ id: 'b5', title: 'The River Road', author: 'Tom Ashby', year: 2012 },
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
const columns: ColumnDef<Book>[] = [
|
|
25
|
+
// The title stays: a person cannot hide it.
|
|
26
|
+
{ id: 'title', header: 'Title', sortable: true, hideable: false },
|
|
27
|
+
{ id: 'author', header: 'Author', sortable: true },
|
|
28
|
+
{ id: 'year', header: 'Year', sortable: true },
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
function button(label: string, action: () => void) {
|
|
32
|
+
const wrapper = document.createElement('ui-button')
|
|
33
|
+
const control = document.createElement('button')
|
|
34
|
+
control.type = 'button'
|
|
35
|
+
control.textContent = label
|
|
36
|
+
control.addEventListener('click', action)
|
|
37
|
+
wrapper.append(control)
|
|
38
|
+
return wrapper
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export default function mount(host: HTMLElement) {
|
|
42
|
+
defineReferenceTable()
|
|
43
|
+
const table = document.createElement('ui-reference-table') as ReferenceTableElement<Book>
|
|
44
|
+
table.setAttribute('aria-label', 'Books')
|
|
45
|
+
table.rows = books
|
|
46
|
+
table.columns = columns
|
|
47
|
+
table.defaultPage = { page: 1, pageSize: 2 }
|
|
48
|
+
const toolbar = document.createElement('ui-toolbar')
|
|
49
|
+
toolbar.setAttribute('aria-label', 'Books')
|
|
50
|
+
const search = document.createElement('input')
|
|
51
|
+
search.type = 'search'
|
|
52
|
+
search.setAttribute('aria-label', 'Search the books')
|
|
53
|
+
const field = document.createElement('ui-toolbar-input')
|
|
54
|
+
field.append(search)
|
|
55
|
+
toolbar.append(field)
|
|
56
|
+
search.addEventListener('input', (event) => table.api?.inputSearch(search.value, event))
|
|
57
|
+
const controls = columns
|
|
58
|
+
.filter((column) => column.hideable !== false)
|
|
59
|
+
.map((column) => {
|
|
60
|
+
const name = column.header ?? column.id
|
|
61
|
+
const label = document.createElement('label')
|
|
62
|
+
const box = document.createElement('ui-checkbox')
|
|
63
|
+
box.setAttribute('aria-label', name)
|
|
64
|
+
const indicator = document.createElement('ui-checkbox-indicator')
|
|
65
|
+
indicator.innerHTML = `<svg aria-hidden="true" width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3.5 8.5 6.5 11.5 12.5 4.5" /></svg>`
|
|
66
|
+
box.append(indicator)
|
|
67
|
+
box.addEventListener('checkedchange', ({ detail }) =>
|
|
68
|
+
table.api?.setColumnVisibility(column.id, detail.value === true),
|
|
69
|
+
)
|
|
70
|
+
label.append(box, name)
|
|
71
|
+
toolbar.append(label)
|
|
72
|
+
return { id: column.id, box }
|
|
73
|
+
})
|
|
74
|
+
const shelf = document.createElement('div')
|
|
75
|
+
shelf.className = 'book-shelf'
|
|
76
|
+
const style = document.createElement('style')
|
|
77
|
+
style.textContent = `
|
|
78
|
+
.book-shelf {
|
|
79
|
+
container-type: inline-size;
|
|
80
|
+
display: grid;
|
|
81
|
+
grid-template-columns: minmax(0, 1fr);
|
|
82
|
+
gap: 16px;
|
|
83
|
+
inline-size: 100%;
|
|
84
|
+
}
|
|
85
|
+
.book-shelf :where([role='toolbar'], nav, label) {
|
|
86
|
+
display: flex;
|
|
87
|
+
flex-wrap: wrap;
|
|
88
|
+
align-items: center;
|
|
89
|
+
gap: 8px 12px;
|
|
90
|
+
}
|
|
91
|
+
.book-shelf label {
|
|
92
|
+
gap: 6px;
|
|
93
|
+
cursor: pointer;
|
|
94
|
+
}
|
|
95
|
+
.book-shelf input[type='search'] {
|
|
96
|
+
min-inline-size: 0;
|
|
97
|
+
max-inline-size: 100%;
|
|
98
|
+
flex: 1 1 12rem;
|
|
99
|
+
}
|
|
100
|
+
@container (max-width: 26rem) {
|
|
101
|
+
.book-shelf [role='toolbar'] > :first-child {
|
|
102
|
+
flex: 1 0 100%;
|
|
103
|
+
inline-size: 100%;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
.book-shelf [data-slot='table'] {
|
|
107
|
+
inline-size: 100%;
|
|
108
|
+
}
|
|
109
|
+
.book-shelf nav {
|
|
110
|
+
justify-content: flex-end;
|
|
111
|
+
}
|
|
112
|
+
.book-shelf [data-slot='checkbox'] {
|
|
113
|
+
box-sizing: border-box;
|
|
114
|
+
display: inline-grid;
|
|
115
|
+
place-items: center;
|
|
116
|
+
inline-size: 18px;
|
|
117
|
+
block-size: 18px;
|
|
118
|
+
padding: 0;
|
|
119
|
+
border: 1px solid var(--control-line, GrayText);
|
|
120
|
+
border-radius: 4px;
|
|
121
|
+
background: var(--surface-1, Canvas);
|
|
122
|
+
cursor: pointer;
|
|
123
|
+
}
|
|
124
|
+
.book-shelf [data-slot='checkbox'][aria-checked='true'] {
|
|
125
|
+
border-color: var(--ink, CanvasText);
|
|
126
|
+
background: var(--ink, CanvasText);
|
|
127
|
+
color: var(--surface-1, Canvas);
|
|
128
|
+
}
|
|
129
|
+
.book-shelf [aria-checked='true'] [data-slot='checkbox-indicator'] {
|
|
130
|
+
display: grid;
|
|
131
|
+
}
|
|
132
|
+
.book-shelf [data-slot='checkbox']:focus-visible {
|
|
133
|
+
outline: 2px solid var(--accent-line, Highlight);
|
|
134
|
+
outline-offset: 2px;
|
|
135
|
+
}
|
|
136
|
+
`
|
|
137
|
+
shelf.append(style, toolbar, table)
|
|
138
|
+
host.append(shelf)
|
|
139
|
+
const nav = document.createElement('nav')
|
|
140
|
+
nav.setAttribute('aria-label', 'Pages of the books')
|
|
141
|
+
const previous = button('Previous', () => table.api?.previousPage())
|
|
142
|
+
const next = button('Next', () => table.api?.nextPage())
|
|
143
|
+
const status = document.createElement('span')
|
|
144
|
+
status.setAttribute('role', 'status')
|
|
145
|
+
nav.append(previous, status, next)
|
|
146
|
+
shelf.append(nav)
|
|
147
|
+
const api = table.api
|
|
148
|
+
if (!api) return
|
|
149
|
+
function update() {
|
|
150
|
+
const view = table.api?.getView()
|
|
151
|
+
if (!view) return
|
|
152
|
+
previous.disabled = view.page <= 1
|
|
153
|
+
next.disabled = view.page >= view.pageCount
|
|
154
|
+
if (search.value !== view.search) search.value = view.search
|
|
155
|
+
for (const { id, box } of controls)
|
|
156
|
+
box.checked = view.visibleColumns.some((column) => column.id === id)
|
|
157
|
+
status.textContent = `Page ${view.page} of ${view.pageCount}`
|
|
158
|
+
}
|
|
159
|
+
update()
|
|
160
|
+
return api.subscribe(update)
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Type an author into the search field, take Year out of the table with its box, and step through the pages. Each control reads the view and calls the controller; the table holds the state.
|
|
165
|
+
|
|
166
|
+
## The order of the work
|
|
167
|
+
|
|
168
|
+
1. **The wrapper.** Wrap your renderer’s table and forward its options, change events and custom cell content. Expose its controller so the controls can read and change the same table.
|
|
169
|
+
2. **The looks of the table.** Style the parts by `data-slot` and the state by `data-*` and `aria-*` attributes. See [Styling](https://ui-sdk-table.fdts.dev/docs/styling.md).
|
|
170
|
+
3. **The controls beside the table.** A toolbar, a search field, filters, a column menu, the pages, an export menu. Read the current view with `getView()`, follow changes with `subscribe()`, and call the controller when a control changes.
|
|
171
|
+
4. **The sheet controls**, when the app needs them: a sheet toolbar, a formula bar, a summary of the selected cells.
|
|
172
|
+
5. **The words.** Pass `messages` with the texts of your language.
|
|
173
|
+
|
|
174
|
+
## The wrapper
|
|
175
|
+
|
|
176
|
+
- **What the library gives:** the reference table and its controller; `rowAttrs`, `cellAttrs` and `sectionAttrs` for classes.
|
|
177
|
+
- **What the app writes:** the options, change notifications and custom content it forwards, and its own lead columns, such as a checkbox column (`lead: true`).
|
|
178
|
+
- An option that a page does not set must reach the table as not set, so the table keeps its own default.
|
|
179
|
+
- Keep `rowAttrs` and `cellAttrs` the same functions for the life of the table, and read the props inside them. A new function is a new option, and the table renders again.
|
|
180
|
+
- Keep arrays and objects stable while their contents are unchanged.
|
|
181
|
+
- If your design system also exports a table, give the imported table a local alias. On the web, wrap `ui-reference-table` in your own element.
|
|
182
|
+
|
|
183
|
+
## Looks
|
|
184
|
+
|
|
185
|
+
- **What the library gives:** `data-slot` on every part, state as attributes, the CSS variables of widths, pins and sticky rows. See [Styling](https://ui-sdk-table.fdts.dev/docs/styling.md).
|
|
186
|
+
- **What the app writes:** one place for the classes of the design system. A class reaches a row or a cell through `rowAttrs` and `cellAttrs`, which get the `kind` of the row or cell (`header`, `row`, `cell`, `foot`, `group`, `group-total`, `detail`, `empty`, `letters`, `letter`). A caller cannot replace a state attribute of the table with them.
|
|
187
|
+
- A state shows through a selector of an attribute, such as `[data-pinned]` or `[aria-selected='true']`, never through a class that a script switches.
|
|
188
|
+
- A cell that sticks needs an opaque background, so the rows under it do not show through.
|
|
189
|
+
|
|
190
|
+
## The toolbar and the search field
|
|
191
|
+
|
|
192
|
+
- **What the library gives:** the current search text in the view, `inputSearch`, `setSearch`, `flushSearch` and the option `searchDebounce`.
|
|
193
|
+
- **What the app writes:** the field of the design system, bound to the view’s `search`. Send input to `inputSearch`; let Escape clear the search and Enter flush the pending text. See [Sort, filter and search](./sort-filter-and-search.md).
|
|
194
|
+
- The table says nothing to a screen reader about a search. Announce the rows found when `filter` changes.
|
|
195
|
+
|
|
196
|
+
## Filter controls
|
|
197
|
+
|
|
198
|
+
- **What the library gives:** `controller.getFacets(columnId)` with `kind`, `values`, `range` and `days`; `getFacets(columnId, { rows: 'all' })`; `setColumnFilter`, `clearColumnFilters`, `clearFilters`; `sliderBounds`, `isEmptyFilter`, `toText`.
|
|
199
|
+
- **What the app writes:** a control for each `kind`: a list of values, a slider with two fields, a date range. A search field in a long list. Which columns have a filter control.
|
|
200
|
+
- Read the ends of a slider and of a calendar from `getFacets(columnId, { rows: 'all' })`, so they do not move while a person filters another column. Read the counts of a list from `getFacets(columnId)`.
|
|
201
|
+
- A hidden column can still filter. List its filter, so a person can see why rows are gone.
|
|
202
|
+
|
|
203
|
+
## The column menu
|
|
204
|
+
|
|
205
|
+
- **What the library gives:** `setColumnVisibility`, `showAllColumns`, `hideAllColumns`, `resetColumnVisibility`, `resetColumnOrder`, `resetColumnPinning`, `resetColumnWidths`, `pinColumn`; the view fields `columns` and `visibleColumns`. `setColumnVisibility` throws for a column with `hideable: false`.
|
|
206
|
+
- **What the app writes:** the menu, a box for each column that can be hidden, and the entries for pins and resets that the app wants. See [Columns](https://ui-sdk-table.fdts.dev/docs/column-definitions.md).
|
|
207
|
+
|
|
208
|
+
## The pages
|
|
209
|
+
|
|
210
|
+
- **What the library gives:** `setPage`, `setPageSize` (`null` for one page with every row); the view fields `page` (from 1), `pageSize`, `pageCount`, `rowCount`, `pageUnit` and `lastPageKnown`.
|
|
211
|
+
- **What the app writes:** the pagination of the design system, a list of page sizes, and the text "11–20 of 23". With pages from a server that gave no count, `lastPageKnown` is false. See [Pages and server mode](./pages-and-server-mode.md).
|
|
212
|
+
|
|
213
|
+
## The export menu
|
|
214
|
+
|
|
215
|
+
- **What the library gives:** `controller.exportData(options)`; `toCsv`, `toXlsx`, `downloadCsv` and `downloadXlsx` in `ui-sdk-table-core/export`; `excelFormulas()` in `ui-sdk-table-core/formulas`.
|
|
216
|
+
- **What the app writes:** the menu, and a message when the file fails. Load the entry with `import()` when a person asks for a file, never with a static import. See [Export](./export.md).
|
|
217
|
+
|
|
218
|
+
## The sheet toolbar, the formula bar and the summary
|
|
219
|
+
|
|
220
|
+
- **What the library gives:** `formatCells`, `toggleCellFormat`, `clearCellFormats`, `getCellFormat`, `undo`, `redo`; the view fields `activeCell`, `cellSelection`, `canUndo`, `canRedo`; `getSelectionSummary`; `getColumnFormula`, `setColumnFormula`, `getFormulaFunctions`.
|
|
221
|
+
- **What the app writes:** the buttons and lists, their pressed state from `getCellFormat` of the active cell, and focus back to the active cell (`[data-active]`) after a choice. A method that refuses throws: show the `cause` of the error when it is a text, else its `message`. See [Formats](./formats.md), [History](./history.md) and [Formulas](./sheet-formulas.md).
|
|
222
|
+
- Show the summary in a plain element, not in a live region: a new sum at each step of a selection is noise.
|
|
223
|
+
|
|
224
|
+
## The words
|
|
225
|
+
|
|
226
|
+
- **What the library gives:** `defaultMessages` and the type `TableMessages`. Pass your own as `messages`. `describeFormulaIssue` of the formulas entry gives the English words of a formula issue.
|
|
227
|
+
- **What the app writes:** the texts of its own controls in its language, and the texts of the table it wants other than English.
|
|
228
|
+
|
|
229
|
+
See the [More tiles of the sink](https://ui-sdk-table.fdts.dev/?page=sink&only=more): `more-app` is one table with many options in one scene.
|