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,319 @@
|
|
|
1
|
+
# Web Components API reference
|
|
2
|
+
|
|
3
|
+
Public imports, parts, state changes and content contracts for `ui-sdk-table-web`.
|
|
4
|
+
|
|
5
|
+
## Which Table do I import?
|
|
6
|
+
|
|
7
|
+
Call `defineTableElements()` to register `<ui-table>` and its parts. Compose their light DOM children yourself. Call `defineReferenceTable()` from `/reference` to register `<ui-reference-table>` with the complete structure.
|
|
8
|
+
|
|
9
|
+
## Parts
|
|
10
|
+
|
|
11
|
+
Set part coordinates as properties or attributes. Use `<ui-table-cell header>` for a header cell. Put an input inside `<ui-table-editor>` as its editing target.
|
|
12
|
+
|
|
13
|
+
Give the root an accessible name. Use stable row ids. Grid coordinates start at zero and include the header rows.
|
|
14
|
+
|
|
15
|
+
- `ui-table`.
|
|
16
|
+
- `ui-table-head`.
|
|
17
|
+
- `ui-table-body`.
|
|
18
|
+
- `ui-table-foot`.
|
|
19
|
+
- `ui-table-col`. Inputs: `value: string`.
|
|
20
|
+
- `ui-table-header-row`. Inputs: `value?: string | undefined; index: number`.
|
|
21
|
+
- `ui-table-spacer`. Inputs: `value: string; size: number`.
|
|
22
|
+
- `ui-table-plain-row`. Inputs: `index: number; footer?: boolean | undefined`.
|
|
23
|
+
- `ui-table-row`. Inputs: `value: string; index: number`.
|
|
24
|
+
- `ui-table-cell`. Inputs: `value: string; row: number; col: number; header?: boolean | undefined; colspan?: number | undefined; rowspan?: number | undefined`.
|
|
25
|
+
- `ui-table-editor`. Inputs: `value: string`.
|
|
26
|
+
- `ui-table-fill-handle`.
|
|
27
|
+
- `ui-table-edit-error`. Inputs: `value: string`.
|
|
28
|
+
- `ui-table-resizer`. Inputs: `value: string`.
|
|
29
|
+
|
|
30
|
+
## Table options
|
|
31
|
+
|
|
32
|
+
Properties on `TableElement<Row>` and `ReferenceTableElement<Row>`; use `api`, not a `controller` property.
|
|
33
|
+
|
|
34
|
+
- `rows?` (`readonly Row[]`): The rows of the table. Each row needs a stable id (see `getRowId`). The table never changes or copies a row.
|
|
35
|
+
- `columns?` (`readonly ColumnDef<Row>[]`): The columns, in the order of the definitions. A column with `columns` is a header group.
|
|
36
|
+
- `getRowId?` (`(row: Row, index: number, parent: DataRow<Row> | null) => string | number`): A stable id for a row. Without it, the `id` property of the row is used.
|
|
37
|
+
- `getChildren?` (`(row: Row) => readonly Row[] | undefined`): The child rows of a row. With it, the table is a tree.
|
|
38
|
+
- `locale?` (`string`): The locale of the texts that the table writes: numbers, dates, the search without case, and the signs of a formula that a person types. Without it, the formats use `en-US`. A format whose `formatOptions` has a `locale` keeps its own.
|
|
39
|
+
- `searchMode?` (`'contains' | 'fuzzy'`): How the search matches. `'contains'` (the default): the text of a cell holds the search, without case. `'fuzzy'`: the letters of the search are in the text in order.
|
|
40
|
+
- `fuzzyThreshold?` (`number`): With `searchMode: 'fuzzy'`, the score that a match must pass, from 0 to 1. Defaults to 0: every match in order passes.
|
|
41
|
+
- `sortChildren?` (`boolean`): Also sort the tree children of each row. Defaults to true.
|
|
42
|
+
- `canGroup?` (`(row: Row) => boolean`): Which rows go into groups. A row that it refuses stays out of every group and comes first, before the groups.
|
|
43
|
+
- `selection?` (`'single' | 'multiple'`): Row selection. Without it, rows cannot be selected.
|
|
44
|
+
- `canSelect?` (`(row: Row) => boolean`): Which rows can be selected. A row that it refuses has no selection, and `select` throws for it.
|
|
45
|
+
- `resizable?` (`boolean`): Whether a person can resize columns. A column can say otherwise. Defaults to false.
|
|
46
|
+
- `reorderable?` (`boolean`): Whether a person can move columns. A column can say otherwise. Defaults to false.
|
|
47
|
+
- `columnMinWidth?` (`number`): The smallest width of a column that has no `minWidth`, in pixels. Defaults to 48.
|
|
48
|
+
- `columnMaxWidth?` (`number`): The largest width of a column that has no `maxWidth`, in pixels. Without it, no limit.
|
|
49
|
+
- `messages?` (`Partial<TableMessages>`): Texts of the table in another language, or with other words.
|
|
50
|
+
- `multiSort?` (`MultiSortMode`): Whether a press on a header can add its column to the sort. `'none'`: one column at a time, and a modifier key changes nothing. `'modifier'` (the default): a press with Shift, Control or Command adds the column. `'always'`: each press adds the column. Enter on a header follows the same rule. A `sort` that the owner sets is never cut to the mode.
|
|
51
|
+
- `alwaysSorted?` (`boolean`): Whether the table keeps a sort once it has one. With it, a press on the only column of the sort turns the order, ascending and descending, and never takes the sort away. Without it, the third press does. Defaults to false. It does not sort a table that starts without a sort; give `defaultSort` for that.
|
|
52
|
+
- `searchDebounce?` (`number`): How long the typing in a search field must pause before the table filters, in milliseconds, for a text from `inputSearch`. Defaults to 200. With 0, each text filters at once.
|
|
53
|
+
- `groupTotals?` (`boolean`): Whether a group row shows the total of each column in a cell under that column, as the footer does. The label of the group then covers the columns before the first total. It applies while a column in view has an `aggregate`; without one, and with `false`, a group row is one cell as wide as the table. Defaults to true.
|
|
54
|
+
- `totals?` (`TableTotalsSource`): Totals as the owner of the rows knows them. The footer, the group rows, `getTotals` and the totals of an export show them in place of what the rows of the table give. For rows from a server: the table has one page of them, and its own totals are of that page.
|
|
55
|
+
- `sort?` (`readonly SortRule[]`): The sort: the columns in the order in which they sort. Pass it to control the sort.
|
|
56
|
+
- `defaultSort?` (`readonly SortRule[]`)
|
|
57
|
+
- `filter?` (`FilterState`): The search and the filter of each column. Pass it to control the filter.
|
|
58
|
+
- `defaultFilter?` (`FilterState`)
|
|
59
|
+
- `groupBy?` (`readonly string[]`): The ids of the columns that group the rows, outermost first. Pass it to control the groups.
|
|
60
|
+
- `defaultGroupBy?` (`readonly string[]`)
|
|
61
|
+
- `page?` (`PageState`): With a page, the table shows one page of rows. Without it, all rows.
|
|
62
|
+
- `defaultPage?` (`PageState`)
|
|
63
|
+
- `expanded?` (`readonly string[]`): The ids of the expanded tree rows and groups.
|
|
64
|
+
- `defaultExpanded?` (`readonly string[]`)
|
|
65
|
+
- `selected?` (`string[]`): The ids of the selected rows.
|
|
66
|
+
- `defaultSelected?` (`string[]`)
|
|
67
|
+
- `columnVisibility?` (`ColumnVisibility`): Columns with `false` are hidden. The id of a header group hides all its columns.
|
|
68
|
+
- `defaultColumnVisibility?` (`ColumnVisibility`)
|
|
69
|
+
- `columnOrder?` (`readonly string[]`): Column ids in display order. Columns that are not listed come after, in definition order.
|
|
70
|
+
- `defaultColumnOrder?` (`readonly string[]`)
|
|
71
|
+
- `columnWidths?` (`ColumnWidths`): Column widths in pixels, by column id. A column without an entry has the width of its definition.
|
|
72
|
+
- `defaultColumnWidths?` (`ColumnWidths`)
|
|
73
|
+
- `columnPinning?` (`ColumnPinning`): The columns that stay at the start or the end edge while the rest scrolls.
|
|
74
|
+
- `defaultColumnPinning?` (`ColumnPinning`)
|
|
75
|
+
- `rowWindow?` (`boolean`): Render only the rows near the scroll frame, with a spacer for the rest. The frame is the nearest parent that scrolls, or the page. Defaults to false.
|
|
76
|
+
- `rowHeight?` (`number`): One height for every row, in pixels. Without it, the row window measures each row. With `hasDetail` it measures too, because a detail row has a height of its own, and this height is what a row counts with before.
|
|
77
|
+
- `estimatedRowHeight?` (`number`): The height of a row that the row window did not measure yet, in pixels. Defaults to 36.
|
|
78
|
+
- `overscan?` (`number`): How many rows more than the frame shows the row window renders on each side. Defaults to 8.
|
|
79
|
+
- `stickyHeader?` (`boolean`): Whether the header rows stay at the top of the scroll frame. Defaults to false.
|
|
80
|
+
- `stickyFooter?` (`boolean`): Whether the footer row stays at the bottom of the scroll frame. Defaults to false.
|
|
81
|
+
- `hasDetail?` (`(row: Row) => boolean`): Which rows have a detail row: one wide row under the row, which opens and closes as a tree row does. Pass the same function while its answers are the same: a new function makes the table list its rows again.
|
|
82
|
+
- `rowPinning?` (`RowPinning`): The rows that stay at the top or the bottom edge. A filter or a page does not hide them. The totals are of the rows that pass the filter, so a pinned row that the filter refuses is in view and not in the totals. A row with child rows cannot be pinned.
|
|
83
|
+
- `defaultRowPinning?` (`RowPinning`)
|
|
84
|
+
- `columnAggregates?` (`ColumnAggregates`): The function of the total of a column where a person chose one, by column id: `sum`, `mean`, `min`, `max`, `count`, `uniqueCount` or `first`. An entry wins over the `aggregate` of the column, and `null` takes the total of the column away. The footer, the group rows, `getTotals` and an export all follow it. With `totals` of the owner, the owner hears the change and gives the number of the chosen function.
|
|
85
|
+
- `defaultColumnAggregates?` (`ColumnAggregates`)
|
|
86
|
+
- `cellFormats?` (`CellFormats`): The formats of cells and of whole columns: how a value becomes text and how the cell looks, by row id and column id. A property of a cell wins over the same property of its column, and both win over the `format` of the column definition. The looks reach a cell as `data-align`, `data-bold`, `data-italic`, `data-color` and `data-fill`, and the style of the app gives them a meaning. The value is reported whole and shares what did not change, so an owner saves it as it is. Colour alone must not carry a meaning: a screen reader says the text of a cell, not its looks.
|
|
87
|
+
- `defaultCellFormats?` (`CellFormats`)
|
|
88
|
+
- `formulas?` (`FormulaEngine`): Turns formulas on: the value of `createFormulaEngine()` of the entry `ui-sdk-table-core/formulas`. Pass one engine for the life of the table; a new engine at each render computes every formula again.
|
|
89
|
+
- `cellFormulas?` (`CellFormulas`): The formulas of the table, as data of the owner, in the stored form. `columns` makes a column computed: each row gets the value of the formula, also a row that comes later, and its cells cannot be edited. A formula that cannot be read, or column formulas that read each other in a circle, show an error in every row. Read only with `formulas`.
|
|
90
|
+
- `defaultCellFormulas?` (`CellFormulas`)
|
|
91
|
+
- `formatColors?` (`readonly string[]`): The names that `color` and `fill` of a cell format can have, such as `['red', 'amber', 'green']`. The table writes a name into `data-color` or `data-fill`, and the style of the app maps it to its own colours for light and dark. Without the option, a format can carry no colour.
|
|
92
|
+
- `sheet?` (`boolean`): The rules of a data sheet. In the editor, Enter saves and moves down (with Shift up), and an arrow key saves and moves when a character opened the editor. Inside a selected range, Enter and Tab walk the range and Tab on a cell stays in it; without a range, Tab on a cell leaves the table as before. Delete and Backspace empty the selected cells, a cut empties them too, and a paste repeats a block that fits the range a whole number of times. It is also a preset: `editable`, `cellSelection` and `history` are on unless one of them is set to false. Defaults to false.
|
|
93
|
+
- `history?` (`boolean | TableHistoryOptions`): Undo and redo for cell values: Control or Command with Z, and with Shift+Z or Y, on a cell, and `undo` and `redo` of the controller. A step is one report of `onCellEdit` that no listener refused. An undo reports the old values through `onCellEdit` with the reason `undo`, without `parse` and without the schema, and the owner of the rows puts them in. Rows need stable ids for it. With `manualPage`, a change of the page, the sort or the filter empties the history. Pass limits in place of `true` to change them. Defaults to the `sheet` option.
|
|
94
|
+
- `fillHandle?` (`boolean | 'vertical' | 'horizontal'`): The fill handle: a small part at the corner of the selected cells that a person drags to fill more cells, and with `sheet` the keys Control+D (fill down) and Control+R (fill forward). `'vertical'` and `'horizontal'` let a fill go along one axis only. Needs cell selection. Defaults to the `sheet` option.
|
|
95
|
+
- `editable?` (`boolean`): Whether a person can edit cells. A column can say otherwise. Defaults to the `sheet` option, else to `entry`.
|
|
96
|
+
- `entry?` (`boolean`): A mode for entering data, which an app can turn on and off, for example with an Edit button. While it is on, cells can be edited unless `editable` is false or a column says otherwise, one plain press on a cell opens its editor, and Tab and Shift+Tab on a cell and in the editor go to the next and the previous cell that can be edited, across the rows. From the last such cell Tab leaves the table, and from the first Shift+Tab. Inside a selected range of more than one cell, Tab keeps the rule of the range. Defaults to false.
|
|
97
|
+
- `pickCells?` (`boolean`): The pick mode, for a field that writes a formula: while it is on, a press or a drag on cells of the body picks them, and a press on a letter of `columnLetters` picks its column. A pick changes nothing else: the active cell, the selected cells, an open editor, focus and the caret of a field stay. The picked cells go to `pickedCells` and show `data-picked`. An app turns it on and off, for example while the text of its field starts with `=`, and puts the address of the pick (`getRangeAddress`) into the text. The root shows `data-pick`. Defaults to false.
|
|
98
|
+
- `pickedCells?` (`CellPick | null`): The cells of the last pick, as places in the grid like `cellRange`, or `null`. It goes back to `null` when `pickCells` goes off.
|
|
99
|
+
- `defaultPickedCells?` (`CellPick | null`)
|
|
100
|
+
- `cellSelection?` (`boolean`): Whether a person can select a range of cells, to copy it or to paste into it. Defaults to the `sheet` option.
|
|
101
|
+
- `columnLetters?` (`boolean`): A row of letters above the header rows, as a spreadsheet has: A, B, C, then AA. It is the place where a person selects whole columns, as the row numbers of `rowNumberColumn()` are for whole rows: a press on a letter selects the cells of its column, with Shift the columns from the active cell to it, and a drag over letters selects those columns. Space on a letter selects its column, and Shift with an arrow takes the next columns. The cell above the row numbers selects every cell. A column that leads the table has an empty letter cell that selects nothing. A letter is the place of the column among the columns in view. The row is grid row 0: it counts in `aria-rowindex`, `aria-rowcount` and the rows of `cellRange`, and it sticks with `stickyHeader`. An export does not hold it. Selecting needs `cellSelection`; this option is not part of `sheet`. Defaults to false.
|
|
102
|
+
- `cellRange?` (`CellRange | null`): The selected range of cells, from the cell where it started to the cell it reaches, or `null`. A cell is a place in the grid: `row` counts the header rows first, as `aria-rowindex` does, from 0. A range holds body rows only. It goes away when other rows or columns come to its places.
|
|
103
|
+
- `defaultCellRange?` (`CellRange | null`)
|
|
104
|
+
- `manualSort?` (`boolean`): The rows come from a server in sort order. The table does not sort them. The `sort` state still changes, so the caller knows what to ask for.
|
|
105
|
+
- `manualFilter?` (`boolean`): The rows come from a server with the search and the column filters applied. The table does not filter them. Facets and totals are of the rows that the table has; give `facets` for the choices of every row, and `totals` for the totals of every row.
|
|
106
|
+
- `facets?` (`Readonly<Record<string, ColumnFacetsSource>>`): The facets of columns as the owner of the rows knows them, by column id. `getFacets` answers with them in place of what the rows of the table hold. A server that filters knows every value of a column; the rows that it sends hold only the values that pass.
|
|
107
|
+
- `manualPage?` (`boolean`): The rows are one page from a server. The table shows them all, and the pages come from `rowCount`. Every other feature works on the rows that the table has: an export, the totals and "select all" are of this page. Give `totals` for the totals that the server knows.
|
|
108
|
+
- `rowCount?` (`number`): How many rows the server has after its filter, for `manualPage`. Without it, a full page has a next page and a page that is not full is the last. An empty page after the first then sends the table one page back, so keep the rows of the page before, or set `loading`, while a page loads. `lastPage` needs the count.
|
|
109
|
+
- `loading?` (`boolean`): Whether new rows are on their way. The table says so to assistive technology and keeps its rows.
|
|
110
|
+
|
|
111
|
+
## Models and change notifications
|
|
112
|
+
|
|
113
|
+
Assign arrays, objects and functions as JavaScript properties, not HTML strings. Listen on the element itself: `sortchange` does not bubble. Read `event.detail.value` and `event.detail.reason`. Call `event.preventDefault()` to refuse a cancelable change. For controlled state, assign the accepted value back to the property.
|
|
114
|
+
|
|
115
|
+
Use the `default` form for initial state without owner updates. A commit reports the end of an operation, not each intermediate change.
|
|
116
|
+
|
|
117
|
+
- `sortchange` (`UiDetailedChangeEvent<readonly SortRule[], TableChangeDetails>`)
|
|
118
|
+
- `filterchange` (`UiDetailedChangeEvent<FilterState, TableChangeDetails>`)
|
|
119
|
+
- `groupbychange` (`UiDetailedChangeEvent<readonly string[], TableChangeDetails>`)
|
|
120
|
+
- `pagechange` (`UiDetailedChangeEvent<PageState | undefined, TableChangeDetails>`)
|
|
121
|
+
- `expandedchange` (`UiDetailedChangeEvent<readonly string[], TableChangeDetails>`)
|
|
122
|
+
- `selectedchange` (`UiDetailedChangeEvent<string[], TableChangeDetails>`)
|
|
123
|
+
- `columnvisibilitychange` (`UiDetailedChangeEvent<Readonly<Record<string, boolean>>, TableChangeDetails>`)
|
|
124
|
+
- `columnorderchange` (`UiDetailedChangeEvent<readonly string[], TableChangeDetails>`)
|
|
125
|
+
- `columnwidthschange` (`UiDetailedChangeEvent<Readonly<Record<string, number>>, TableChangeDetails>`)
|
|
126
|
+
- `columnwidthscommit` (`UiDetailedChangeEvent<Readonly<Record<string, number>>, TableCommitDetails>`)
|
|
127
|
+
- `columnpinningchange` (`UiDetailedChangeEvent<ColumnPinning, TableChangeDetails>`)
|
|
128
|
+
- `rowpinningchange` (`UiDetailedChangeEvent<RowPinning, TableChangeDetails>`)
|
|
129
|
+
- `columnaggregateschange` (`UiDetailedChangeEvent<Readonly<Record<string, AggregateName | null>>, TableChangeDetails>`)
|
|
130
|
+
- `cellformatschange` (`UiDetailedChangeEvent<CellFormats, TableChangeDetails>`)
|
|
131
|
+
- `cellformulaschange` (`UiDetailedChangeEvent<CellFormulas, ChangeDetails<"recalculate" | "undo" | "redo" | "press" | "item-press" | "list-navigation" | "input-change" | "imperative" | "reset" | "drag" | "move-key" | "resize-key" | "escape-key" | "shortcut" | "enter-key" | "tab-key" | "focus-out" | "paste" | "cut" | "clear" | "fill">>`)
|
|
132
|
+
- `pickedcellschange` (`UiDetailedChangeEvent<CellPick | null, TableChangeDetails>`)
|
|
133
|
+
- `pickedcellscommit` (`UiDetailedChangeEvent<CellPick, TableCommitDetails>`)
|
|
134
|
+
- `celledit` (`UiDetailedChangeEvent<readonly CellEdit<Row>[], TableEditDetails>`)
|
|
135
|
+
- `cellrangechange` (`UiDetailedChangeEvent<CellRange | null, TableChangeDetails>`)
|
|
136
|
+
- `cellrangecommit` (`UiDetailedChangeEvent<CellRange | null, TableCommitDetails>`)
|
|
137
|
+
|
|
138
|
+
## Reference content
|
|
139
|
+
|
|
140
|
+
Set the `renderers` property of `ReferenceTableElement<Row>` to a map of functions. Return an `Element`, `Text`, string, `null` or `undefined`. Use `renderers.cell` or a more specific key such as `renderers['col-name']`.
|
|
141
|
+
|
|
142
|
+
Column-specific keys take precedence over their generic counterparts. Each entry below names the argument, not the return type.
|
|
143
|
+
|
|
144
|
+
- `header-{column id}`: `HeaderSlotProps<Row>`.
|
|
145
|
+
- `col-{column id}`: `CellSlotProps<Row>`.
|
|
146
|
+
- `foot-{column id}`: `FootSlotProps<Row>`.
|
|
147
|
+
- `group-total-{column id}`: `GroupTotalSlotProps<Row>`.
|
|
148
|
+
- `edit-{column id}`: `EditorSlotProps<Row>`.
|
|
149
|
+
- `editor`: `EditorSlotProps<Row>`.
|
|
150
|
+
- `header`: `HeaderSlotProps<Row>`.
|
|
151
|
+
- `cell`: `CellSlotProps<Row>`.
|
|
152
|
+
- `foot`: `FootSlotProps<Row>`.
|
|
153
|
+
- `group`: `GroupSlotProps<Row>`.
|
|
154
|
+
- `group-total`: `GroupTotalSlotProps<Row>`.
|
|
155
|
+
- `row-header`: `RowHeaderSlotProps<Row>`.
|
|
156
|
+
- `letter`: `LetterSlotProps<Row>`.
|
|
157
|
+
- `detail`: `DetailSlotProps<Row>`.
|
|
158
|
+
- `empty`: no argument.
|
|
159
|
+
- `loading`: no argument.
|
|
160
|
+
|
|
161
|
+
### HeaderSlotProps
|
|
162
|
+
|
|
163
|
+
- `column` (`ColumnDef<Row>`): The definition of the column.
|
|
164
|
+
- `label` (`string`): The header text: `column.header`, or the column id.
|
|
165
|
+
- `leaf` (`boolean`): False for a header group, which holds columns and no cells.
|
|
166
|
+
- `sort` (`SortDirection | undefined`): The direction of the column in the sort, `'asc'` or `'desc'`, or `undefined` when it does not sort.
|
|
167
|
+
- `sortIndex` (`number | undefined`): The place of the column in the sort, from 1. With several columns in the sort, the first one sorts first.
|
|
168
|
+
- `toggleSort` (`(event?: Event) => void`): Sorts by the column as a press on the header does. Pass the event: with `multiSort`, its keys decide whether the column joins the sort.
|
|
169
|
+
- `pin` (`'start' | 'end' | undefined`): The edge that the column is pinned to, or `undefined`.
|
|
170
|
+
- `selectColumn` (`(event?: Event) => void`): Selects the cells of the column, for a control in its header. With Shift in the event, the columns from the active cell to this one. It throws for a header group and without the `cellSelection` option.
|
|
171
|
+
- `formula` (`string | undefined`): The formula of a computed column, as a person reads it. `undefined` for a column without one.
|
|
172
|
+
|
|
173
|
+
### CellSlotProps
|
|
174
|
+
|
|
175
|
+
- `row` (`Row`): The row object of the data, as the owner passed it.
|
|
176
|
+
- `node` (`DataRow<Row>`): The node of the row in the row model: its id, its parent and its children.
|
|
177
|
+
- `column` (`ColumnDef<Row>`): The definition of the column.
|
|
178
|
+
- `value` (`unknown`): The value of the cell: `value` of the column, else the property that `key` names.
|
|
179
|
+
- `text` (`string`): The value as text, with the format of the cell, else of its column.
|
|
180
|
+
- `format` (`CellFormat | undefined`): The format that the cell has of its own, from `cellFormats`: the format of its column with the format of the cell over it. `undefined` when neither says anything. Its looks are also on the cell as `data-align`, `data-bold`, `data-italic`, `data-color` and `data-fill`.
|
|
181
|
+
- `selected` (`boolean`): Whether the row is selected.
|
|
182
|
+
- `expanded` (`boolean`): Whether the row is open: its child rows or its detail row show.
|
|
183
|
+
- `expandable` (`boolean`): Whether the row can open: it has child rows or a detail row.
|
|
184
|
+
- `level` (`number`): Tree and group levels above this row, from 0.
|
|
185
|
+
- `pinned` (`RowPin | undefined`): The edge that the row is pinned to.
|
|
186
|
+
- `toggleSelected` (`(event?: Event) => void`): Selects the row or takes it out of the selection, for a checkbox in the cell.
|
|
187
|
+
- `toggleExpanded` (`(event?: Event) => void`): Opens or closes the row, for a button in the cell.
|
|
188
|
+
- `formula` (`{ text: string; error?: { code: FormulaError; message: string } } | undefined`): With formulas: the formula of the cell as a person reads it, and the code and the words of an error value. `undefined` for a cell with neither. The error is also on the cell as `data-formula-error`.
|
|
189
|
+
|
|
190
|
+
### FootSlotProps
|
|
191
|
+
|
|
192
|
+
- `column` (`ColumnDef<Row>`): The column of the cell.
|
|
193
|
+
- `value` (`unknown`): The total of the column, or `undefined` for a column without an `aggregate`.
|
|
194
|
+
- `text` (`string`): The total as text, with the format of the column, or an empty text.
|
|
195
|
+
- `totals` (`Record<string, unknown>`): The totals of every column that has one, by column id.
|
|
196
|
+
- `aggregate` (`AggregateName | 'custom' | undefined`): The function of the total: one of the built-in names, `custom` for a function of the column, or `undefined` without a total.
|
|
197
|
+
- `setAggregate` (`(aggregate: AggregateName | null | undefined, event?: Event) => void`): Chooses the function of the total of this column, for a picker in the footer. `null` takes the total away, and `undefined` gives the column the `aggregate` of its definition back. The group rows and an export follow.
|
|
198
|
+
|
|
199
|
+
### GroupTotalSlotProps
|
|
200
|
+
|
|
201
|
+
- `group` (`GroupRow<Row>`): The group of the row.
|
|
202
|
+
- `column` (`ColumnDef<Row>`): The column of the cell.
|
|
203
|
+
- `value` (`unknown`): The total of the column over the rows of the group. `undefined` for a column without an `aggregate`.
|
|
204
|
+
- `text` (`string`): The total as text, with the format of the column, or an empty text.
|
|
205
|
+
- `totals` (`Record<string, unknown>`): The totals of every column that has one, by column id.
|
|
206
|
+
- `aggregate` (`AggregateName | 'custom' | undefined`): The function of the total: one of the built-in names, `custom` for a function of the column, or `undefined` without a total.
|
|
207
|
+
- `expanded` (`boolean`): Whether the group is open.
|
|
208
|
+
- `level` (`number`): The groups above this one, from 0.
|
|
209
|
+
- `path` (`readonly GroupPathEntry<Row>[]`): The groups from the outermost one down to this group.
|
|
210
|
+
|
|
211
|
+
### EditorSlotProps
|
|
212
|
+
|
|
213
|
+
- `row` (`Row`): The row object of the data, as the owner passed it.
|
|
214
|
+
- `node` (`DataRow<Row>`): The node of the row in the row model: its id, its parent and its children.
|
|
215
|
+
- `column` (`ColumnDef<Row>`): The definition of the column.
|
|
216
|
+
- `value` (`unknown`): The value of the cell now.
|
|
217
|
+
- `text` (`string`): The value as plain text, without a format: 0.25 for a cell that shows 25 %. `save` takes a value.
|
|
218
|
+
- `error` (`string | undefined`): Why the last value was refused.
|
|
219
|
+
- `checking` (`boolean`): A check that takes time runs for a saved value. A save does nothing until it answers.
|
|
220
|
+
- `mode` (`'replace' | 'edit'`): How the editor was opened. `replace`: the person typed a character on the cell, which is `typed`, and means to write over the value. `edit`: Enter, F2, a double press or code opened it, to change the value.
|
|
221
|
+
- `typed` (`string | undefined`): The character that opened the editor, or `undefined`.
|
|
222
|
+
- `save` (`(value: unknown, event?: Event) => void`): Saves a value, with the check of the column, and closes the editor. A value that is refused keeps the editor open, and `error` says why.
|
|
223
|
+
- `cancel` (`() => void`): Closes the editor without a save. Focus goes back to the cell.
|
|
224
|
+
- `formula` (`string | undefined`): With formulas: the formula of the cell as a person reads it, such as `=C2*D2`. A save of a text that starts with `=` sets a formula.
|
|
225
|
+
- `inputMode` (`'decimal' | undefined`): `decimal` when a field of numbers should ask a phone for its number keyboard (`numberKeyboard` of the column), else `undefined`. Put it on the field as `inputmode`.
|
|
226
|
+
|
|
227
|
+
### GroupSlotProps
|
|
228
|
+
|
|
229
|
+
- `group` (`GroupRow<Row>`): The group: its id, the column and the value that make it, and its rows.
|
|
230
|
+
- `column` (`ColumnDef<Row> | undefined`): The column that makes the group.
|
|
231
|
+
- `text` (`string`): The value of the group as text, with the format of its column.
|
|
232
|
+
- `expanded` (`boolean`): Whether the group is open.
|
|
233
|
+
- `level` (`number`): The groups above this one, from 0.
|
|
234
|
+
- `totals` (`Record<string, unknown>`): Column totals of the rows in the group.
|
|
235
|
+
- `totalTexts` (`Record<string, string>`): The totals as text, with the format of their columns, by column id.
|
|
236
|
+
- `path` (`readonly GroupPathEntry<Row>[]`): The groups from the outermost one down to this group, each with its column, its value and its text: "Europe", then "Finland".
|
|
237
|
+
- `covered` (`readonly ColumnDef<Row>[]`): The columns that this cell covers. With totals under their columns, a total of one of these columns has no cell of its own.
|
|
238
|
+
- `toggleExpanded` (`(event?: Event) => void`): Opens or closes the group, for a button in the cell.
|
|
239
|
+
|
|
240
|
+
### RowHeaderSlotProps
|
|
241
|
+
|
|
242
|
+
- `row` (`Row`): The row object of the data, as the owner passed it.
|
|
243
|
+
- `node` (`DataRow<Row>`): The node of the row in the row model: its id, its parent and its children.
|
|
244
|
+
- `column` (`ColumnDef<Row>`): The definition of the column with `rowHeader`.
|
|
245
|
+
- `number` (`number`): The number of the row, from 1: its place among the rows that the table shows, counted over the pages.
|
|
246
|
+
- `selectRow` (`(event?: Event) => void`): Selects the cells of the row. With Shift in the event, the rows from the active cell to this one. A press on the cell does the same; this is for a control inside it. It throws without the `cellSelection` option.
|
|
247
|
+
|
|
248
|
+
### LetterSlotProps
|
|
249
|
+
|
|
250
|
+
- `column` (`ColumnDef<Row>`): The definition of the column under the letter.
|
|
251
|
+
- `letter` (`string | undefined`): The letter of the column: its place among the columns in view. `undefined` for a column that leads the table.
|
|
252
|
+
- `corner` (`boolean`): Whether the cell is the corner, above the row numbers, which selects every cell.
|
|
253
|
+
- `selectColumn` (`(event?: Event) => void`): Selects what a press on the cell selects: the cells of the column, with Shift in the event from the active cell on, or every cell from the corner. A press on the cell does the same; this is for a control inside it. It throws for an empty cell of a column that leads, and without the `cellSelection` option.
|
|
254
|
+
|
|
255
|
+
### DetailSlotProps
|
|
256
|
+
|
|
257
|
+
- `row` (`Row`): The row object of the data, as the owner passed it.
|
|
258
|
+
- `node` (`DataRow<Row>`): The node of the row in the row model: its id, its parent and its children.
|
|
259
|
+
- `toggleExpanded` (`(event?: Event) => void`): Closes the detail row.
|
|
260
|
+
|
|
261
|
+
### Attributes of generated elements
|
|
262
|
+
|
|
263
|
+
- `rowAttrs?` (`(context: RowAttrsContext<Row>) => PartAttrs | undefined`): Attributes of a `tr` element.
|
|
264
|
+
- `cellAttrs?` (`(context: CellAttrsContext<Row>) => PartAttrs | undefined`): Attributes of a `th` or `td` element.
|
|
265
|
+
- `sectionAttrs?` (`{ head?: PartAttrs; body?: PartAttrs; foot?: PartAttrs }`): Attributes of the `thead`, `tbody` and `tfoot` elements.
|
|
266
|
+
|
|
267
|
+
## Controller and ref
|
|
268
|
+
|
|
269
|
+
On a connected `<ui-table>`, `element.api` exposes the following `TableElementApi` methods. A connected `<ui-reference-table>` additionally exposes `TableController`. Its `api` is undefined after disconnect.
|
|
270
|
+
|
|
271
|
+
- `rows`
|
|
272
|
+
- `columns`
|
|
273
|
+
- `sortBy`
|
|
274
|
+
- `toggleSort`
|
|
275
|
+
- `setSearch`
|
|
276
|
+
- `setColumnFilter`
|
|
277
|
+
- `setGroupBy`
|
|
278
|
+
- `setPage`
|
|
279
|
+
- `setPageSize`
|
|
280
|
+
- `toggleExpanded`
|
|
281
|
+
- `select`
|
|
282
|
+
- `toggleSelected`
|
|
283
|
+
- `clearSelection`
|
|
284
|
+
- `exportData`
|
|
285
|
+
- `text`
|
|
286
|
+
- `headerRows`
|
|
287
|
+
- `headerRowCount`
|
|
288
|
+
- `isSelected`
|
|
289
|
+
- `sortOf`
|
|
290
|
+
|
|
291
|
+
See the [controller methods](./reference-controller.md), [view fields](./reference-view.md), [column definitions](./reference-columns.md) and [messages](./reference-messages.md).
|
|
292
|
+
|
|
293
|
+
## Optional modules
|
|
294
|
+
|
|
295
|
+
Import the formula engine from `ui-sdk-table-core/formulas` and the export writers from `ui-sdk-table-core/export`. `ui-sdk-table-web` has neither adapter subpath. Import shared row, format and formula types from `ui-sdk-table-core` unless listed below.
|
|
296
|
+
|
|
297
|
+
## Public entries and exports
|
|
298
|
+
|
|
299
|
+
Each heading is an import path. Use `import type` for a type-only export.
|
|
300
|
+
|
|
301
|
+
### ui-sdk-table-web
|
|
302
|
+
|
|
303
|
+
- `defineTableElements` (value)
|
|
304
|
+
- `TableElement` (type)
|
|
305
|
+
- `TableElementApi` (type)
|
|
306
|
+
- `TableEvents` (type)
|
|
307
|
+
|
|
308
|
+
### ui-sdk-table-web/browser
|
|
309
|
+
|
|
310
|
+
Side-effect import: registers the custom elements. No named exports.
|
|
311
|
+
|
|
312
|
+
### ui-sdk-table-web/reference
|
|
313
|
+
|
|
314
|
+
- `defineReferenceTable` (value)
|
|
315
|
+
- `ReferenceTableElement` (type)
|
|
316
|
+
|
|
317
|
+
### ui-sdk-table-web/reference/browser
|
|
318
|
+
|
|
319
|
+
Side-effect import: registers the custom elements. No named exports.
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# Editing
|
|
2
|
+
|
|
3
|
+
Let a person change the value of a cell, check it first, and put it into your rows.
|
|
4
|
+
|
|
5
|
+
The table never writes a row. It reports the edits, and the app decides whether to apply them to its rows. `applyEdits` does that for plain rows.
|
|
6
|
+
|
|
7
|
+
```ts title="prices.ts"
|
|
8
|
+
import { type ColumnDef, type StandardSchemaV1, applyEdits } from 'ui-sdk-table-core'
|
|
9
|
+
|
|
10
|
+
import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
|
|
11
|
+
|
|
12
|
+
type Product = { id: string; name: string; price: number; stock: number }
|
|
13
|
+
|
|
14
|
+
// Any Standard Schema works: Zod, Valibot, ArkType. This one is written by hand.
|
|
15
|
+
function check(test: (value: unknown) => string | undefined): StandardSchemaV1 {
|
|
16
|
+
return {
|
|
17
|
+
'~standard': {
|
|
18
|
+
version: 1,
|
|
19
|
+
vendor: 'example',
|
|
20
|
+
validate: (value) => {
|
|
21
|
+
const message = test(value)
|
|
22
|
+
return message === undefined ? { value } : { issues: [{ message }] }
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const columns: ColumnDef<Product>[] = [
|
|
29
|
+
{ id: 'name', header: 'Product', editable: false },
|
|
30
|
+
{
|
|
31
|
+
id: 'price',
|
|
32
|
+
header: 'Price',
|
|
33
|
+
format: 'decimal',
|
|
34
|
+
numberKeyboard: true,
|
|
35
|
+
schema: check((value) =>
|
|
36
|
+
typeof value === 'number' && value >= 0 ? undefined : 'A price is a number, zero or more.',
|
|
37
|
+
),
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
id: 'stock',
|
|
41
|
+
header: 'In stock',
|
|
42
|
+
format: 'integer',
|
|
43
|
+
numberKeyboard: true,
|
|
44
|
+
schema: check((value) =>
|
|
45
|
+
Number.isInteger(value) && Number(value) >= 0 ? undefined : 'Stock is a whole number.',
|
|
46
|
+
),
|
|
47
|
+
},
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
// The rows are yours. The table reports an edit and never writes a row.
|
|
51
|
+
const rows: Product[] = [
|
|
52
|
+
{ id: 'p1', name: 'Green tea', price: 6.5, stock: 40 },
|
|
53
|
+
{ id: 'p2', name: 'Dark coffee', price: 9.9, stock: 12 },
|
|
54
|
+
{ id: 'p3', name: 'Oat cookies', price: 3.2, stock: 75 },
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
export default function mount(host: HTMLElement) {
|
|
58
|
+
defineReferenceTable()
|
|
59
|
+
const table = document.createElement('ui-reference-table') as ReferenceTableElement<Product>
|
|
60
|
+
table.setAttribute('aria-label', 'Prices')
|
|
61
|
+
table.rows = rows
|
|
62
|
+
table.columns = columns
|
|
63
|
+
table.editable = true
|
|
64
|
+
table.addEventListener('celledit', ({ detail }) => {
|
|
65
|
+
table.rows = applyEdits(table.rows ?? [], detail.value)
|
|
66
|
+
const [edit] = detail.value
|
|
67
|
+
if (edit)
|
|
68
|
+
status.textContent = `${edit.row.name}: ${edit.column.header} ${String(edit.previous)} to ${String(edit.value)}, by ${detail.reason}`
|
|
69
|
+
})
|
|
70
|
+
const status = document.createElement('p')
|
|
71
|
+
status.setAttribute('role', 'status')
|
|
72
|
+
host.append(status)
|
|
73
|
+
status.textContent = 'Nothing edited yet.'
|
|
74
|
+
host.append(table)
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Double-press a price, type 7, and press Enter. The line under the table says what changed. Type -1 and the editor stays open with the reason. Product names cannot be edited.
|
|
79
|
+
|
|
80
|
+
## Turn it on
|
|
81
|
+
|
|
82
|
+
- `editable` on the table lets a person edit every cell. `editable` on a column says otherwise for its cells: `false`, or a function `(row) => boolean` for some rows.
|
|
83
|
+
- The core `onCellEdit` callback gets `(edits, details)`. Each edit has `rowId`, `columnId`, `row` (your object, as it was before), `column`, `value` and `previous`. `details.reason` says how it came: `enter-key`, `tab-key`, `focus-out`, `paste` or `imperative`. `details.cancel()` refuses the edit.
|
|
84
|
+
- `applyEdits(rows, edits, getRowId?)` returns a new array with a new object for each row that changed. The other rows stay the same objects. It finds the row of an edit by its object, and by its id when the object was replaced already. Pass the `getRowId` of the table when it has one. It reads the top level only: the child rows of a tree need your own code.
|
|
85
|
+
|
|
86
|
+
Listen for the `celledit` DOM event. Its `detail.value` holds the edits, and `detail.reason` says what triggered them. Call `event.preventDefault()` to refuse the report.
|
|
87
|
+
|
|
88
|
+
## Keys
|
|
89
|
+
|
|
90
|
+
| Key | On a cell that can be edited |
|
|
91
|
+
| --- | --- |
|
|
92
|
+
| Enter, F2 or a double press | Opens the editor with the value |
|
|
93
|
+
| A character | Opens the editor with that character |
|
|
94
|
+
| Enter in the editor | Saves, and focus goes back to the cell |
|
|
95
|
+
| Tab, Shift+Tab in the editor | Saves and goes to the next or the cell before |
|
|
96
|
+
| Escape in the editor | Closes it and puts the value back |
|
|
97
|
+
| A press elsewhere | Saves |
|
|
98
|
+
|
|
99
|
+
The arrows in the editor move the caret. Without an editor, Tab leaves the table; in the [entry mode](./cell-editing.md#an-entry-mode-with-save-and-cancel) it goes to the next cell that can be edited.
|
|
100
|
+
|
|
101
|
+
## Check a value
|
|
102
|
+
|
|
103
|
+
A text becomes a value in two steps:
|
|
104
|
+
|
|
105
|
+
1. `parse(text, row)` of the column makes the value. Without it, the value that the cell holds now decides: a number stays a number, a boolean a boolean, a date a date, and anything else gets the text. An empty text empties a number or a date.
|
|
106
|
+
2. `schema` of the column checks the value. Any Standard Schema works: Zod, Valibot, ArkType. A refusal keeps the editor open with the message of the schema.
|
|
107
|
+
|
|
108
|
+
A check can answer with a promise. The editor then waits (`data-checking`), and Escape takes the edit back.
|
|
109
|
+
|
|
110
|
+
`numberKeyboard: true` on a column gives the field of its editor `inputmode="decimal"`, so a phone shows its number keyboard. The field stays a text field and never `type="number"`.
|
|
111
|
+
|
|
112
|
+
## Your own editor
|
|
113
|
+
|
|
114
|
+
In the [reference implementation](./reference-implementation.md), `editor` and `edit-{column id}` replace the built-in editor for every column or just one. They get `row`, `column`, `value`, `text`, `error`, `checking`, `mode`, `typed`, `inputMode`, `save(value, event)` and `cancel()`. The control must take focus itself. A `save` goes through the same check as the field of the table.
|
|
115
|
+
|
|
116
|
+
## From code
|
|
117
|
+
|
|
118
|
+
`controller.editCell(rowId, columnId, value)` gives a cell a value with the check of its column, and reports it with the reason `imperative`. It throws with the reason when the value is refused. `startEdit` opens the editor of a cell in view, and `cancelEdit` closes it.
|
|
119
|
+
|
|
120
|
+
## An entry mode with Save and Cancel
|
|
121
|
+
|
|
122
|
+
`entry` turns the table into a form for many cells at once: a person moves with Tab and types. Turn it on and off from your own Edit button; while it is on, cells can be edited as with `editable`, unless `editable` is false or a column says otherwise.
|
|
123
|
+
|
|
124
|
+
```ts title="entry.ts"
|
|
125
|
+
import { type ColumnDef, type CellEdit, cellKey, sameValue, applyEdits } from 'ui-sdk-table-core'
|
|
126
|
+
|
|
127
|
+
import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
|
|
128
|
+
|
|
129
|
+
type Shift = { id: string; person: string; day: string; hours: number; note: string }
|
|
130
|
+
|
|
131
|
+
const columns: ColumnDef<Shift>[] = [
|
|
132
|
+
{ id: 'person', header: 'Person', editable: false },
|
|
133
|
+
{ id: 'day', header: 'Day' },
|
|
134
|
+
{ id: 'hours', header: 'Hours', format: 'decimal', numberKeyboard: true },
|
|
135
|
+
{ id: 'note', header: 'Note' },
|
|
136
|
+
]
|
|
137
|
+
|
|
138
|
+
// The rows are yours. The draft holds the edits until Save.
|
|
139
|
+
const rows: Shift[] = [
|
|
140
|
+
{ id: 's1', person: 'Kofi', day: 'Monday', hours: 8, note: '' },
|
|
141
|
+
{ id: 's2', person: 'Ines', day: 'Tuesday', hours: 6.5, note: 'Late start' },
|
|
142
|
+
{ id: 's3', person: 'Tomas', day: 'Friday', hours: 4, note: '' },
|
|
143
|
+
]
|
|
144
|
+
|
|
145
|
+
function button(label: string, action: () => void) {
|
|
146
|
+
const el = document.createElement('button')
|
|
147
|
+
el.type = 'button'
|
|
148
|
+
el.textContent = label
|
|
149
|
+
el.addEventListener('click', action)
|
|
150
|
+
return el
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export default function mount(host: HTMLElement) {
|
|
154
|
+
const style = document.createElement('style')
|
|
155
|
+
style.textContent = `ui-reference-table[data-entry] td[data-editable] { cursor: text; box-shadow: inset 0 -1px GrayText } ui-reference-table td[data-changed] { background: #fef9c3; color: #422006 }`
|
|
156
|
+
host.append(style)
|
|
157
|
+
defineReferenceTable()
|
|
158
|
+
const table = document.createElement('ui-reference-table') as ReferenceTableElement<Shift>
|
|
159
|
+
table.setAttribute('aria-label', 'Shifts')
|
|
160
|
+
table.rows = rows
|
|
161
|
+
table.columns = columns
|
|
162
|
+
let saved = rows
|
|
163
|
+
const changes = new Map<string, CellEdit<Shift>>()
|
|
164
|
+
table.cellAttrs = (cell) =>
|
|
165
|
+
cell.kind === 'cell' && changes.has(cellKey(cell.node.id, cell.column.id))
|
|
166
|
+
? { 'data-changed': '' }
|
|
167
|
+
: undefined
|
|
168
|
+
const edit = button('Edit', () => {
|
|
169
|
+
table.entry = true
|
|
170
|
+
update()
|
|
171
|
+
})
|
|
172
|
+
const save = button('Save', () => {
|
|
173
|
+
saved = applyEdits(saved, [...changes.values()])
|
|
174
|
+
status.textContent = `Saved ${changes.size} ${changes.size === 1 ? 'cell' : 'cells'}.`
|
|
175
|
+
finish()
|
|
176
|
+
})
|
|
177
|
+
const cancel = button('Cancel', finish)
|
|
178
|
+
const count = document.createElement('span')
|
|
179
|
+
function finish() {
|
|
180
|
+
changes.clear()
|
|
181
|
+
table.rows = saved
|
|
182
|
+
table.entry = false
|
|
183
|
+
update()
|
|
184
|
+
}
|
|
185
|
+
function update() {
|
|
186
|
+
edit.hidden = !!table.entry
|
|
187
|
+
save.hidden = cancel.hidden = count.hidden = !table.entry
|
|
188
|
+
count.textContent = `${changes.size} changed`
|
|
189
|
+
}
|
|
190
|
+
table.addEventListener('celledit', ({ detail }) => {
|
|
191
|
+
for (const edit of detail.value) {
|
|
192
|
+
const key = cellKey(edit.rowId, edit.columnId)
|
|
193
|
+
const first = changes.get(key) ?? edit
|
|
194
|
+
if (sameValue(edit.value, first.previous)) changes.delete(key)
|
|
195
|
+
else changes.set(key, { ...first, value: edit.value })
|
|
196
|
+
}
|
|
197
|
+
table.rows = applyEdits(saved, [...changes.values()])
|
|
198
|
+
update()
|
|
199
|
+
})
|
|
200
|
+
host.append(edit, save, cancel, count)
|
|
201
|
+
update()
|
|
202
|
+
const status = document.createElement('p')
|
|
203
|
+
status.setAttribute('role', 'status')
|
|
204
|
+
host.append(status)
|
|
205
|
+
status.textContent = 'Nothing saved yet.'
|
|
206
|
+
host.append(table)
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
Press Edit, then press a day: its editor opens. Type Sunday and press Tab: the day is saved and focus goes to the hours, where typing 7 replaces the value. Press Tab, then Save. Cancel puts every cell back.
|
|
211
|
+
|
|
212
|
+
- **Tab and Shift+Tab** on a cell and in the editor go to the next and the previous cell that can be edited, in reading order and across the rows. They pass over a cell that cannot be edited, a group row and a column that leads. From the last such cell Tab leaves the table, and from the first Shift+Tab: the table is never a trap. Inside a selected range of more than one cell, Tab walks the range.
|
|
213
|
+
- **One press** on a cell that can be edited opens its editor. A press with Shift, Control, Command or Alt, and a drag, keep their meaning, also with `cellSelection`.
|
|
214
|
+
- **A character** on a cell replaces its value, and Tab, Enter or a press elsewhere saves it, as without the mode.
|
|
215
|
+
- The root has `data-entry`, so a style can show which cells take input: `[data-entry] [data-editable]`.
|
|
216
|
+
|
|
217
|
+
Keep a draft when the app needs Save and Cancel: display the rows with pending edits applied, commit them on Save, and discard them on Cancel. The example above shows how to keep this state in your framework. If the table accepts formulas, draft `cellFormulas` alongside the values so cancelling restores both.
|
|
218
|
+
|
|
219
|
+
## Attributes
|
|
220
|
+
|
|
221
|
+
- `data-entry` on the root in the entry mode.
|
|
222
|
+
- `data-editable` on a cell that can be edited, `data-editing` while its editor is open, `data-checking` while a check runs.
|
|
223
|
+
- The editor is `[data-slot='table-editor']`, with `aria-invalid` and `data-invalid` after a refusal. Its name is "Edit Price" (message `editCell`).
|
|
224
|
+
- The reason of a refusal is `[data-slot='table-edit-error']`, which the editor names with `aria-errormessage`.
|
|
225
|
+
|
|
226
|
+
## Names
|
|
227
|
+
|
|
228
|
+
- **Exports:** `applyEdits`, `cellValue`, `emptyValue`, and the types `TableEditDetails`, `TableEditReason`, `StandardSchemaV1`.
|
|
229
|
+
|
|
230
|
+
For shared methods and result types, see [Controller](./reference-controller.md) and [View](./reference-view.md).
|
|
231
|
+
|
|
232
|
+
For custom elements, properties and DOM events, see the [Web Components API reference](./api-web-components.md).
|
|
233
|
+
|
|
234
|
+
See the [Edit tiles of the sink](https://ui-sdk-table.fdts.dev/?page=sink&only=edit).
|