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.
Files changed (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +7 -2
  3. package/dist/table-licenses.md +411 -0
  4. package/dist/table-reference-licenses.md +411 -0
  5. package/dist/table-reference.js +10631 -0
  6. package/dist/table.js +10147 -0
  7. package/docs/api-web-components.md +319 -0
  8. package/docs/cell-editing.md +234 -0
  9. package/docs/cells-and-slots.md +106 -0
  10. package/docs/component-body.md +42 -0
  11. package/docs/component-cell.md +31 -0
  12. package/docs/component-col.md +31 -0
  13. package/docs/component-edit-error.md +31 -0
  14. package/docs/component-editor.md +31 -0
  15. package/docs/component-fill-handle.md +31 -0
  16. package/docs/component-foot.md +33 -0
  17. package/docs/component-head.md +33 -0
  18. package/docs/component-header-cell.md +31 -0
  19. package/docs/component-header-row.md +33 -0
  20. package/docs/component-plain-row.md +33 -0
  21. package/docs/component-resizer.md +33 -0
  22. package/docs/component-root.md +40 -0
  23. package/docs/component-row.md +33 -0
  24. package/docs/component-spacer.md +31 -0
  25. package/docs/controller-and-view.md +108 -0
  26. package/docs/examples-agents.md +62 -0
  27. package/docs/examples-column-definitions.md +172 -0
  28. package/docs/examples-formula-writer.md +262 -0
  29. package/docs/examples-styling.md +180 -0
  30. package/docs/examples.md +125 -0
  31. package/docs/excel-formulas.md +119 -0
  32. package/docs/export.md +107 -0
  33. package/docs/fill.md +111 -0
  34. package/docs/first-table.md +111 -0
  35. package/docs/formats.md +158 -0
  36. package/docs/groups-and-totals.md +117 -0
  37. package/docs/history.md +126 -0
  38. package/docs/installation.md +52 -0
  39. package/docs/llms.txt +13 -0
  40. package/docs/pages-and-server-mode.md +219 -0
  41. package/docs/range-copy-and-paste.md +108 -0
  42. package/docs/reference-columns.md +179 -0
  43. package/docs/reference-controller.md +137 -0
  44. package/docs/reference-export.md +59 -0
  45. package/docs/reference-formulas.md +42 -0
  46. package/docs/reference-implementation.md +29 -0
  47. package/docs/reference-messages.md +55 -0
  48. package/docs/reference-view.md +116 -0
  49. package/docs/row-numbers-and-letters.md +109 -0
  50. package/docs/rows.md +310 -0
  51. package/docs/selection-and-keyboard.md +134 -0
  52. package/docs/sheet-formulas.md +322 -0
  53. package/docs/sheet-options.md +192 -0
  54. package/docs/skills.md +41 -0
  55. package/docs/sort-filter-and-search.md +268 -0
  56. package/docs/state-and-events.md +86 -0
  57. package/docs/styled-table.md +229 -0
  58. package/docs/table-components.md +100 -0
  59. package/docs/web-components.md +113 -0
  60. package/package.json +49 -3
  61. package/src/browser.ts +3 -0
  62. package/src/index.ts +77 -0
  63. package/src/reference-browser.ts +3 -0
  64. package/src/reference-dom.ts +206 -0
  65. package/src/reference.ts +63 -0
@@ -0,0 +1,111 @@
1
+ # First table
2
+
3
+ Put a table on a page and give it looks.
4
+
5
+ This example uses the complete [reference implementation](./reference-implementation.md). To compose your own table, start with the [native parts](./table-components.md). If you haven’t added the packages yet, start with [Installation](./installation.md).
6
+
7
+ ## Put a table on a page
8
+
9
+ A table needs rows, columns and a name. Each row needs a stable `id`. Each column has an `id`, which is also the key of its value in the row.
10
+
11
+ ```ts title="people.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 Person = { id: string; name: string; team: string; score: number }
17
+
18
+ const people: Person[] = [
19
+ { id: 'p1', name: 'Ines Moreau', team: 'Design', score: 82 },
20
+ { id: 'p2', name: 'Kofi Mensah', team: 'Platform', score: 91 },
21
+ { id: 'p3', name: 'Wren Takahashi', team: 'Design', score: 77 },
22
+ { id: 'p4', name: 'Hollis Grant', team: 'Support', score: 88 },
23
+ ]
24
+
25
+ const columns: ColumnDef<Person>[] = [
26
+ { id: 'name', header: 'Name', sortable: true },
27
+ { id: 'team', header: 'Team', sortable: true },
28
+ { id: 'score', header: 'Score', sortable: true, format: 'integer' },
29
+ ]
30
+
31
+ export default function mount(host: HTMLElement) {
32
+ defineReferenceTable()
33
+ const table = document.createElement('ui-reference-table') as ReferenceTableElement<Person>
34
+ table.setAttribute('aria-label', 'People')
35
+ table.rows = people
36
+ table.columns = columns
37
+
38
+ host.append(table)
39
+ }
40
+ ```
41
+
42
+ Press Score to sort by it. Press it again to turn the order. Press Tab to reach the table, then move between the cells with the arrow keys.
43
+
44
+ `aria-label` gives the table its accessible name. A screen reader and an agent find the table by it.
45
+
46
+ The table never copies or changes your row objects. It reads them, and it reports a change through an event.
47
+
48
+ ## Add looks
49
+
50
+ The table has no looks of its own. Every element carries a `data-slot`, and its state shows as `aria-*` and `data-*` attributes. A style reads those, and no class.
51
+
52
+ | Part | Attribute |
53
+ | --- | --- |
54
+ | The table | `data-slot="table"` |
55
+ | A header row and a body row | `data-slot="table-header-row"`, `data-slot="table-row"` |
56
+ | A header cell and a body cell | `data-slot="table-cell"`, in a `th` or a `td` |
57
+ | The column of a cell | `data-column` with the column id |
58
+ | The format of a column | `data-format` on each of its cells, such as `decimal` |
59
+ | The sort of a column | `aria-sort` and `data-sorted` (`asc` or `desc`) on its header cell |
60
+
61
+ ```ts title="styled.ts"
62
+ import { type ColumnDef } from 'ui-sdk-table-core'
63
+
64
+ import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
65
+
66
+ type Person = { id: string; name: string; team: string; score: number }
67
+
68
+ const people: Person[] = [
69
+ { id: 'p1', name: 'Ines Moreau', team: 'Design', score: 82.5 },
70
+ { id: 'p2', name: 'Kofi Mensah', team: 'Platform', score: 91 },
71
+ { id: 'p3', name: 'Wren Takahashi', team: 'Design', score: 77.25 },
72
+ { id: 'p4', name: 'Hollis Grant', team: 'Support', score: 88 },
73
+ ]
74
+
75
+ const columns: ColumnDef<Person>[] = [
76
+ { id: 'name', header: 'Name', sortable: true },
77
+ { id: 'team', header: 'Team', sortable: true },
78
+ { id: 'score', header: 'Score', sortable: true, format: 'decimal' },
79
+ ]
80
+
81
+ export default function mount(host: HTMLElement) {
82
+ const style = document.createElement('style')
83
+ style.textContent = `
84
+ .people-scores { border-collapse: collapse; font-size: 14px }
85
+ .people-scores [data-slot='table-cell'] { padding: 8px 14px; border-block-end: 1px solid GrayText; text-align: start }
86
+ .people-scores [data-format='decimal'] { text-align: end; font-variant-numeric: tabular-nums }
87
+ .people-scores [data-sorted='asc'] button::after { content: ' ↑' }
88
+ .people-scores [data-sorted='desc'] button::after { content: ' ↓' }
89
+ .people-scores [data-slot='table-cell']:focus-visible { outline: 2px solid Highlight; outline-offset: -2px }
90
+ `
91
+ host.append(style)
92
+ defineReferenceTable()
93
+ const table = document.createElement('ui-reference-table') as ReferenceTableElement<Person>
94
+ table.setAttribute('aria-label', 'People by score')
95
+ table.rows = people
96
+ table.columns = columns
97
+ table.className = 'people-scores'
98
+ table.defaultSort = [{ id: 'score', desc: true }]
99
+ table.alwaysSorted = true
100
+
101
+ host.append(table)
102
+ }
103
+ ```
104
+
105
+ Press Score to turn the order, and Name to sort by name. The arrow comes from data-sorted. Hover a row, or reach a cell with Tab, to see the hover and the focus ring.
106
+
107
+ `alwaysSorted` keeps one column in the sort: a press on it turns the order and never clears it.
108
+
109
+ ## Keep going
110
+
111
+ - [Introduction](https://ui-sdk-table.fdts.dev/docs/introduction.md) says what the library gives and what your app builds.
@@ -0,0 +1,158 @@
1
+ # Formats
2
+
3
+ Let a person make cells bold, give them a colour, or show a number as a percentage, and save that with the document.
4
+
5
+ Use it in a sheet with a toolbar. A format of a cell is data, as the values are: the app saves it and passes it back.
6
+
7
+ ```ts title="formats.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 Fund = { id: string; fund: string; fee: number; year: number }
13
+
14
+ const funds: Fund[] = [
15
+ { id: 'f1', fund: 'Nordic Index', fee: 0.002, year: 0.0784 },
16
+ { id: 'f2', fund: 'Global Bonds', fee: 0.0035, year: -0.0136 },
17
+ { id: 'f3', fund: 'Small Caps', fee: 0.011, year: 0.1207 },
18
+ ]
19
+
20
+ const columns: ColumnDef<Fund>[] = [
21
+ { id: 'fund', header: 'Fund' },
22
+ {
23
+ id: 'fee',
24
+ header: 'Fee',
25
+ format: 'decimal',
26
+ formatOptions: { minimumFractionDigits: 4, maximumFractionDigits: 4 },
27
+ },
28
+ {
29
+ id: 'year',
30
+ header: 'One year',
31
+ format: 'percentage',
32
+ formatOptions: { minimumFractionDigits: 1, maximumFractionDigits: 1, showUnit: true },
33
+ },
34
+ ]
35
+
36
+ function button(label: string, action: () => void) {
37
+ const el = document.createElement('button')
38
+ el.type = 'button'
39
+ el.textContent = label
40
+ el.addEventListener('click', action)
41
+ return el
42
+ }
43
+
44
+ export default function mount(host: HTMLElement) {
45
+ const style = document.createElement('style')
46
+ style.textContent = `[aria-label='Funds'] [data-bold] { font-weight: bold } [aria-label='Funds'] [data-fill='accent'] { background: #dbeafe; color: #172554 } [aria-label='Funds'] [data-color='alert'] { color: #b91c1c }`
47
+ host.append(style)
48
+ defineReferenceTable()
49
+ const table = document.createElement('ui-reference-table') as ReferenceTableElement<Fund>
50
+ table.setAttribute('aria-label', 'Funds')
51
+ table.rows = funds
52
+ table.columns = columns
53
+ table.cellSelection = true
54
+ table.formatColors = ['accent', 'alert']
55
+ table.cellFormats = { cells: { f2: { year: { color: 'alert' } } } }
56
+ table.addEventListener('cellformatschange', ({ detail }) => {
57
+ table.cellFormats = detail.value
58
+ })
59
+ const bold = button('Bold', () => table.api?.toggleCellFormat('bold'))
60
+ host.append(
61
+ bold,
62
+ button('Percent', () => table.api?.formatCells({ format: 'percentage', decimals: 1 })),
63
+ button('Mark', () => table.api?.formatCells({ fill: 'accent' })),
64
+ button('Clear', () => table.api?.clearCellFormats()),
65
+ )
66
+
67
+ host.append(table)
68
+ const api = table.api
69
+ if (!api) return
70
+ function update() {
71
+ const view = table.api?.getView()
72
+ if (!view) return
73
+ const at = view.activeCell
74
+ bold.setAttribute(
75
+ 'aria-pressed',
76
+ String(!!at && table.api?.getCellFormat(at.rowId, at.columnId).bold === true),
77
+ )
78
+ }
79
+ update()
80
+ return api.subscribe(update)
81
+ }
82
+ ```
83
+
84
+ Select the three cells under Fee and press Percent: they read 0.2%, 0.4% and 1.1%. Press Bold: the button shows it pressed. Clear takes the formats of the selected cells away.
85
+
86
+ ## The value
87
+
88
+ `CellFormats` is `{ columns?: { [columnId]: CellFormat }, cells?: { [rowId]: { [columnId]: CellFormat } } }`.
89
+
90
+ - A property of a cell wins over the same property of its column, and both win over the `format` and `formatOptions` of the column definition.
91
+ - A format follows its row through a sort, a filter and a page.
92
+ - Pass `cellFormats` to control the value, or `defaultCellFormats` to choose its initial value.
93
+
94
+ `CellFormat` has `format` (`text`, `integer`, `decimal`, `currency`, `percentage`, `date`, `datetime`), `decimals` (0 to 10), `currency` (an ISO code), `dateStyle` (`short`, `medium`, `long`, `full`), `align` (`start`, `center`, `end`), `bold`, `italic`, `color` and `fill`.
95
+
96
+ ## Change formats
97
+
98
+ ```ts
99
+ controller.formatCells({ bold: true, fill: 'green' }) // the selected cells
100
+ controller.formatCells({ format: 'percentage', decimals: 1 }, { columns: ['share'] })
101
+ controller.formatCells({ fill: null }, { from: { rowId: 'a', columnId: 'price' } })
102
+ controller.clearCellFormats()
103
+ controller.toggleCellFormat('bold') // for a toolbar button
104
+ controller.getCellFormat(rowId, columnId) // for the pressed state
105
+ ```
106
+
107
+ - A patch with `null` for a key takes that property away.
108
+ - A wrong property, name or colour throws with the valid ones.
109
+ - Control or Command with B and with I make the selected cells bold and italic, with `sheet`.
110
+
111
+ ## Looks are attributes
112
+
113
+ - A cell has `data-align`, `data-bold`, `data-italic`, `data-color`, `data-fill`, and `data-format` with the name that decides its text. The table writes no class and no style.
114
+ - A colour is a name of `formatColors` and nothing of CSS. The style of the app gives each name its colours and holds the contrast.
115
+ - Give a fill an opaque background, so a pinned cell with a fill hides what scrolls under it. Decide what a selected cell with a fill looks like.
116
+ - `cellAttrs` of the caller cannot overwrite these attributes.
117
+
118
+ ## The text of a cell
119
+
120
+ - The format makes the text: 0.342 with `{ format: 'percentage', decimals: 1 }` reads `34.2%`.
121
+ - The footer and the totals of a group follow the column layer.
122
+ - A CSV and an Excel file hold the text of the screen. In an Excel file a cell whose own format decides its text is written as text.
123
+ - A copy holds the plain value, as the editor shows it, so a paste into the same columns gives the same values.
124
+ - Custom [cell content](./cells-and-slots.md) receives `format`, the format that the cell has of its own, and `text`.
125
+ - A search and the label of a group read the column definition and do not follow a format of a cell.
126
+
127
+ ## A percentage in the text field
128
+
129
+ A cell that shows `25%` opens with `25`, and `30` or `30%` in the field is 0.3, as in a column with the `percentage` format. Every other text holds the value itself: a paste, a fill and `editCell` read `0.3` as 0.3, as a copy gives it, and a text with a percent sign (`30%`) is that percentage. A column with a `parse`, or with `scale: 'percent'` in its `formatOptions`, keeps the plain value in its field.
130
+
131
+ ## Whole columns
132
+
133
+ When the selected cells are every row that the table has, `formatCells` formats the columns. The format then holds for rows that come later, and the value stays small. Never with `manualPage`, where the table does not have every row.
134
+
135
+ Format a column, not 50,000 cells, when the value is saved as JSON: cells that share a format share one object in memory, but JSON writes one entry for each row.
136
+
137
+ ## History
138
+
139
+ A change of formats is one step: its entry in `getHistory()` has `formats: true` and `cells: 0`. An undo reports the formats of before as a `cellFormats` change with the reason `undo`, property by property, so a format that the owner gave another cell meanwhile stays. `details.cancel()` in the core `onCellFormatsChange` callback refuses a change.
140
+
141
+ ## Rows that left
142
+
143
+ The table keeps the format of a row that is gone, because the row can come back with the next page. `pruneFormats(formats, rowIds, columnIds)` drops them when the owner knows better. `setFormats(formats, place, patch)` and `revertFormats` are the pure functions behind the controller.
144
+
145
+ ## Rendering
146
+
147
+ Let the table hold the formats when the app only needs to save them. Use `defaultCellFormats` and listen for changes rather than passing a new `cellFormats` value back on every edit.
148
+
149
+ See the [Sheet tiles of the sink](https://ui-sdk-table.fdts.dev/?page=sink&only=sheet).
150
+
151
+ ## Names
152
+
153
+ - **Attributes:** `data-align`, `data-bold`, `data-italic`, `data-color`, `data-fill`, `data-format`.
154
+ - **Exports:** `setFormats`, `revertFormats`, `pruneFormats`, `resolveFormat`, `CELL_FORMAT_NAMES`, `CELL_ALIGNS`, `CELL_DATE_STYLES`, and the types `CellFormat`, `CellFormats`, `CellFormatPatch`, `CellFormatName`, `FormatPlace`, `FormatTarget`.
155
+
156
+ For shared methods and result types, see [Controller](./reference-controller.md) and [View](./reference-view.md).
157
+
158
+ For custom elements, properties and DOM events, see the [Web Components API reference](./api-web-components.md).
@@ -0,0 +1,117 @@
1
+ # Groups and totals
2
+
3
+ Group the rows by the values of a column, and show a total of a column under it, for each group and for the table.
4
+
5
+ ```ts title="groups.ts"
6
+ import { type ColumnDef } from 'ui-sdk-table-core'
7
+
8
+ import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
9
+
10
+ type Sale = { id: string; region: string; rep: string; deals: number; amount: number }
11
+
12
+ const sales: Sale[] = [
13
+ { id: 's1', region: 'North', rep: 'Ada Lindqvist', deals: 4, amount: 12400 },
14
+ { id: 's2', region: 'North', rep: 'Leo Brandt', deals: 2, amount: 5300 },
15
+ { id: 's3', region: 'South', rep: 'Tomas Reyes', deals: 6, amount: 18750 },
16
+ { id: 's4', region: 'South', rep: 'Maya Okafor', deals: 3, amount: 9100 },
17
+ { id: 's5', region: 'South', rep: 'Nina Sato', deals: 1, amount: 2200 },
18
+ ]
19
+
20
+ const columns: ColumnDef<Sale>[] = [
21
+ { id: 'region', header: 'Region' },
22
+ { id: 'rep', header: 'Rep', sortable: true },
23
+ { id: 'deals', header: 'Deals', format: 'integer', aggregate: 'sum' },
24
+ {
25
+ id: 'amount',
26
+ header: 'Amount',
27
+ format: 'currency',
28
+ formatOptions: { currency: 'EUR', showUnit: true },
29
+ aggregate: 'sum',
30
+ },
31
+ ]
32
+
33
+ function button(label: string, action: () => void) {
34
+ const el = document.createElement('button')
35
+ el.type = 'button'
36
+ el.textContent = label
37
+ el.addEventListener('click', action)
38
+ return el
39
+ }
40
+
41
+ export default function mount(host: HTMLElement) {
42
+ defineReferenceTable()
43
+ const table = document.createElement('ui-reference-table') as ReferenceTableElement<Sale>
44
+ table.setAttribute('aria-label', 'Sales by region')
45
+ table.rows = sales
46
+ table.columns = columns
47
+ table.groupBy = ['region']
48
+ table.defaultExpanded = ['group/region=North']
49
+ table.defaultColumnVisibility = { region: false }
50
+ table.locale = 'en-US'
51
+ table.renderers = {
52
+ group: ({ text, expanded, toggleExpanded, group }) => {
53
+ const el = button(
54
+ `${expanded ? '▾' : '▸'} ${text} (${group.leaves.length} reps)`,
55
+ toggleExpanded,
56
+ )
57
+ el.setAttribute('aria-expanded', String(expanded))
58
+ return el
59
+ },
60
+ 'foot-rep': () => 'All regions',
61
+ }
62
+
63
+ host.append(table)
64
+ }
65
+ ```
66
+
67
+ Press South to open its group. Each group row shows the deals and the amount of its reps under their columns, and the footer shows the totals of every region.
68
+
69
+ ## Totals
70
+
71
+ A column with `aggregate` has a total: `sum`, `mean`, `min`, `max`, `count`, `uniqueCount` or `first`, or a function `(values, rows) => total`. The table then has a footer row with the total of each column, in the format of the column.
72
+
73
+ - The footer, the group rows, `controller.getTotals()` and the totals of an export read one function. A total counts the rows that pass the filters, on every page, with their child rows.
74
+ - A pinned row that the filter refuses is in view and not in a total.
75
+ - In the [reference implementation](./cells-and-slots.md), `foot` and `foot-{column id}` fill a footer cell. They get `column`, `value`, `text`, `totals` and `aggregate`.
76
+ - With rows from a server, the table has one page of rows and its totals are of that page. Set `totals` to `{ table: { amount: 1234 }, groups: { 'group/team=Design': { amount: 800 } } }` to display the server totals.
77
+
78
+ ## Group rows
79
+
80
+ `groupBy` is a list of column ids. Each one adds a level of groups. `canGroup(row)` keeps a row out of the groups: the rows that it refuses come first, before every group.
81
+
82
+ `groupable: true` on a column is for the controls of your app: it marks the columns that a person may group by. The table itself groups by any column that `groupBy` names.
83
+
84
+ The id of a group is `group/<column id>=<value>`, such as `group/region=North`. `expanded` holds the ids of the open groups, as it holds the ids of open tree rows.
85
+
86
+ A group row has a label cell and, from the first column with an `aggregate` on, one cell for each column: the total of the group stands under its header and over the total of the footer. The label covers the columns before that. It ends where pinned columns end, so it sticks or scrolls as one cell.
87
+
88
+ - `group` fills the label cell. It gets `group`, `text`, `expanded`, `level`, `totals`, `totalTexts`, `toggleExpanded`, the `path` of the group (the groups above it and itself, each with `column`, `value` and `text`, for a label such as "Europe / Finland") and `covered`, the columns under the label. A total of a covered column has no cell of its own.
89
+ - `group-total-{column id}` and `group-total` fill a cell under a column. They get `group`, `column`, `value`, `text`, `totals`, `aggregate`, `expanded`, `level` and `path`. A column without an `aggregate` has an empty cell, with an empty `text`.
90
+ - Setting `groupTotals` to `false` gives one cell as wide as the table, as a table without any `aggregate` has.
91
+ - Hide the column of the groups with `columnVisibility`: the group rows say its values.
92
+
93
+ ## Keys
94
+
95
+ | Key | On a cell of a group row |
96
+ | --- | --- |
97
+ | Enter | Opens or closes the group, from every cell of its row |
98
+ | Space | Selects the rows of the group, with `selection="multiple"` |
99
+ | The arrows | Cross the cells of the row, as in any row |
100
+
101
+ A group row holds no values. Its cells are never part of a cell range, and a copy gives an empty line for it.
102
+
103
+ ## Attributes
104
+
105
+ - `data-kind="group"` on the row, with `aria-expanded` and `aria-level`. A table with group rows has the role `treegrid`.
106
+ - `data-kind` on a cell of a group row: `group-label`, `group-total` or `group-blank`.
107
+ - `cellAttrs` gets the kind `group` for the label cell and `group-total` for the cells under a column. `rowAttrs` gets the kind `group` with the `path`.
108
+
109
+ ## Names
110
+
111
+ - **Types:** `AggregateName`, `AggregateFn`, `TableTotalsSource`.
112
+
113
+ For shared methods and result types, see [Controller](./reference-controller.md) and [View](./reference-view.md).
114
+
115
+ For custom elements, properties and DOM events, see the [Web Components API reference](./api-web-components.md).
116
+
117
+ See the [Group tiles of the sink](https://ui-sdk-table.fdts.dev/?page=sink&only=group).
@@ -0,0 +1,126 @@
1
+ # History
2
+
3
+ Let a person take a change back with Control+Z and put it on again with Control+Y.
4
+
5
+ Use it in any table that a person edits. It needs no code in the owner beyond `applyEdits`.
6
+
7
+ ```ts title="undo.ts"
8
+ import { type ColumnDef, applyEdits } from 'ui-sdk-table-core'
9
+
10
+ import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
11
+
12
+ type Room = { id: string; room: string; seats: number }
13
+
14
+ const columns: ColumnDef<Room>[] = [
15
+ { id: 'room', header: 'Room', editable: false },
16
+ { id: 'seats', header: 'Seats', format: 'integer' },
17
+ ]
18
+
19
+ const rows: Room[] = [
20
+ { id: 'r1', room: 'Oak', seats: 8 },
21
+ { id: 'r2', room: 'Pine', seats: 12 },
22
+ { id: 'r3', room: 'Birch', seats: 4 },
23
+ ]
24
+
25
+ function button(label: string, action: () => void) {
26
+ const el = document.createElement('button')
27
+ el.type = 'button'
28
+ el.textContent = label
29
+ el.addEventListener('click', action)
30
+ return el
31
+ }
32
+
33
+ export default function mount(host: HTMLElement) {
34
+ defineReferenceTable()
35
+ const table = document.createElement('ui-reference-table') as ReferenceTableElement<Room>
36
+ table.setAttribute('aria-label', 'Meeting rooms')
37
+ table.rows = rows
38
+ table.columns = columns
39
+ table.editable = true
40
+ table.history = true
41
+ let last = 'Nothing yet.'
42
+ table.addEventListener('celledit', ({ detail }) => {
43
+ table.rows = applyEdits(table.rows ?? [], detail.value)
44
+ last = `${detail.reason}: ${detail.value.map((edit) => `${edit.row.room} ${String(edit.value)}`).join(', ')}`
45
+ update()
46
+ })
47
+ const actions = document.createElement('div')
48
+ actions.className = 'history-actions'
49
+ actions.append(
50
+ button('Add a seat', () => {
51
+ for (const row of table.rows ?? []) void table.api?.editCell(row.id, 'seats', row.seats + 1)
52
+ }),
53
+ )
54
+ const undo = button('Undo', () => table.api?.undo())
55
+ const redo = button('Redo', () => table.api?.redo())
56
+ actions.append(undo, redo)
57
+ host.append(actions)
58
+ const status = document.createElement('p')
59
+ status.setAttribute('role', 'status')
60
+ host.append(status)
61
+ host.append(table)
62
+ const api = table.api
63
+ if (!api) return
64
+ function update() {
65
+ const view = table.api?.getView()
66
+ if (!view) return
67
+ undo.disabled = !view.canUndo
68
+ redo.disabled = !view.canRedo
69
+ const history = table.api?.getHistory()
70
+ if (history)
71
+ status.textContent = `Step ${history.position} of ${history.entries.length}. Last report: ${last}`
72
+ }
73
+ update()
74
+ return api.subscribe(update)
75
+ }
76
+ ```
77
+
78
+ Press Add a seat to give each room one more seat. Press Undo three times to restore the original counts, then Redo to repeat the first change.
79
+
80
+ ## Turn it on
81
+
82
+ `history` on the table, or `sheet`. The default limits are `{ limit: 100, maxCells: 100000 }`; pass an object as `history` to change them.
83
+
84
+ ## How an undo reaches the owner
85
+
86
+ - An undo is a report to the cell edit listener with the reason `undo` (a redo: `redo`), with the old values as `value`. An owner that puts edits into its rows with `applyEdits` needs no code for undo.
87
+ - `details.cancel()` refuses an undo as it refuses an edit, and the step stays. The table says so.
88
+ - An undo runs no `parse` and no schema. It puts back what the data held.
89
+ - A cell whose row or column left the table is skipped and counted in the announcement. A step with no cell left is dropped. Rows need stable ids.
90
+ - An undo of a row that the sort moved finds the row. An undo of a row far away in a row window scrolls to it.
91
+
92
+ ## What a step is
93
+
94
+ - One report that no listener refused: one edit, one paste, one cut, one clear, one fill, one `editCell`, one change of formats.
95
+ - Typing in an open editor is no step. There, Control+Z is the text field's.
96
+ - Sort, filter, column moves and selection are not in the history.
97
+ - A step with more cells than `maxCells` empties the history, and the table says so.
98
+ - While a check of a slow server runs, a key does nothing and `undo()` throws. The step is there after the answer.
99
+
100
+ ## Another document
101
+
102
+ Call `controller.clearHistory()` when the rows become another document. With `manualPage`, another page, sort or filter empties the history.
103
+
104
+ ## Read the steps
105
+
106
+ `controller.getHistory()` returns `{ entries, position }`. Each entry has `id`, `reason`, `source`, `cells` (how many cells got a new value), `formats` and `formulas` (whether the step changed them). The view has `canUndo` and `canRedo` for the buttons.
107
+
108
+ ## Keys
109
+
110
+ | Key | On a cell, with `history` |
111
+ | --- | --- |
112
+ | Control or Command with Z | Undo |
113
+ | Control or Command with Y, or with Shift+Z | Redo |
114
+
115
+ The keys act on a cell only. In the editor and in a control inside a cell, Control+Z is the field's.
116
+
117
+ See the [Sheet tiles of the sink](https://ui-sdk-table.fdts.dev/?page=sink&only=sheet).
118
+
119
+ ## Names
120
+
121
+ - `undo` and `redo` throw with the reason: no history, nothing to undo, a refused report, a check that runs.
122
+ - **Types:** `TableStepReason`.
123
+
124
+ For shared methods and result types, see [Controller](./reference-controller.md) and [View](./reference-view.md).
125
+
126
+ For custom elements, properties and DOM events, see the [Web Components API reference](./api-web-components.md).
@@ -0,0 +1,52 @@
1
+ # Installation
2
+
3
+ Choose the table package for your framework.
4
+
5
+ Using a coding agent? [Copy the setup prompt](https://ui-sdk-table.fdts.dev/docs/introduction.md#quick-start) instead. To install by hand, choose your framework and package manager below. If behaviors and primitives are new to you, the [UI SDK docs](https://ui-sdk.dev/docs) explain how they fit together.
6
+
7
+ ## Add the packages
8
+
9
+ Run the command for the framework your app uses. An unversioned package name installs the latest release by default. The adapter brings `ui-sdk-table-core` with it.
10
+
11
+ ```bash
12
+ npm install ui-sdk-table-web
13
+ ```
14
+
15
+ The React package is `ui-sdk-table-react`. Use `ui-sdk-table-vue`, `ui-sdk-table-svelte`, `ui-sdk-table-solid` or `ui-sdk-table-web` for the other frameworks. Then build your first table in [React](https://ui-sdk-table.fdts.dev/docs/react.md), [Vue](https://ui-sdk-table.fdts.dev/docs/vue.md), [Svelte](https://ui-sdk-table.fdts.dev/docs/svelte.md), [Solid](https://ui-sdk-table.fdts.dev/docs/solid.md) or [Web Components](./web-components.md).
16
+
17
+ These commands target released packages. If you use docs from an unreleased branch, use the workspace packages until the matching release reaches npm.
18
+
19
+ With npm, use `npm install` instead of `pnpm add`. For a plain HTML page without a bundler, see [Web Components](./web-components.md).
20
+
21
+ ## Peer dependencies
22
+
23
+ The adapter and your app must share one compatible copy of UI SDK. npm and pnpm normally install missing peers automatically. If your project disables automatic peer installation, check the adapter’s `peerDependencies` and install the listed peers. Resolve any conflict before you continue.
24
+
25
+ ## TypeScript source
26
+
27
+ The package entries expose TypeScript source. Use a bundler that compiles TypeScript, such as Vite. Follow your framework guide for its compiler setup.
28
+
29
+ The source uses ES2023 built-ins, such as `toSorted`. Set `lib` to ES2023 or newer:
30
+
31
+ ```jsonc title="tsconfig.app.json"
32
+ {
33
+ "compilerOptions": {
34
+ "lib": ["ES2023", "DOM", "DOM.Iterable"]
35
+ }
36
+ }
37
+ ```
38
+
39
+ ## Package entries
40
+
41
+ Use the adapter’s main entry to compose native parts. Its `/reference` entry provides an optional [complete composition](./reference-implementation.md). The [formula engine](./sheet-formulas.md) and [file writers](./export.md) are separate from the parts; import them only when you use them:
42
+
43
+ ```ts
44
+ import { createFormulaEngine } from 'ui-sdk-table-core/formulas'
45
+
46
+ // In the download handler:
47
+ const { toCsv } = await import('ui-sdk-table-core/export')
48
+ ```
49
+
50
+ ## Vue entries
51
+
52
+ See the [Vue guide](https://ui-sdk-table.fdts.dev/docs/vue.md) for Vue-specific imports.
package/docs/llms.txt ADDED
@@ -0,0 +1,13 @@
1
+ # ui-sdk-table for Web Components
2
+
3
+ Build the app’s table from [Web Components parts](./web-components.md). Read [Table components](./table-components.md) to choose a part and look up its inputs, events and behaviors. For peer dependencies and TypeScript setup see [Installation](./installation.md).
4
+
5
+ Look up imports, parts and types in the [Web Components API reference](./api-web-components.md).
6
+
7
+ For shared behavior, read the local [column types](./reference-columns.md), [controller methods](./reference-controller.md), [view and row types](./reference-view.md), [messages](./reference-messages.md), [formulas](./reference-formulas.md) and [export types](./reference-export.md).
8
+
9
+ Read the [Web Components examples by topic](./examples.md) for complete native source files, from a first table to a formula writer.
10
+
11
+ To study a complete composition, follow the `./reference` export in the installed `ui-sdk-table-web` package.json to its source. The [reference implementation](./reference-implementation.md) is a working example, not the default renderer.
12
+
13
+ To have a coding agent build the table, see [Skills](./skills.md). For controls around the grid, use the ui-sdk skill and the docs/llms.txt in the installed @ui-sdk/web package.