ui-sdk-table-react 0.0.0-stage → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +8 -2
- package/docs/api-react.md +312 -0
- package/docs/cell-editing.md +246 -0
- package/docs/cells-and-slots.md +127 -0
- package/docs/component-body.md +33 -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 +31 -0
- package/docs/component-head.md +31 -0
- package/docs/component-header-cell.md +31 -0
- package/docs/component-header-row.md +33 -0
- package/docs/component-plain-row.md +31 -0
- package/docs/component-resizer.md +33 -0
- package/docs/component-root.md +36 -0
- package/docs/component-row.md +33 -0
- package/docs/component-spacer.md +31 -0
- package/docs/controller-and-view.md +104 -0
- package/docs/examples-agents.md +50 -0
- package/docs/examples-column-definitions.md +179 -0
- package/docs/examples-formula-writer.md +276 -0
- package/docs/examples-styling.md +173 -0
- package/docs/examples.md +114 -0
- package/docs/excel-formulas.md +115 -0
- package/docs/export.md +96 -0
- package/docs/fill.md +126 -0
- package/docs/first-table.md +150 -0
- package/docs/formats.md +176 -0
- package/docs/groups-and-totals.md +118 -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 +246 -0
- package/docs/range-copy-and-paste.md +115 -0
- package/docs/react.md +85 -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 +112 -0
- package/docs/rows.md +350 -0
- package/docs/selection-and-keyboard.md +151 -0
- package/docs/sheet-formulas.md +329 -0
- package/docs/sheet-options.md +214 -0
- package/docs/skills.md +41 -0
- package/docs/sort-filter-and-search.md +260 -0
- package/docs/state-and-events.md +90 -0
- package/docs/styled-table.md +237 -0
- package/docs/table-components.md +105 -0
- package/package.json +45 -3
- package/src/index.ts +62 -0
- package/src/reference.ts +113 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Controller and view
|
|
2
|
+
|
|
3
|
+
Change the table from a control beside it, and read what it shows now.
|
|
4
|
+
|
|
5
|
+
A toolbar, a search field, a filter control and the pages of a table live outside the table. They act through a controller and read a view. The same controller is what an agent calls.
|
|
6
|
+
|
|
7
|
+
```tsx title="outside.tsx"
|
|
8
|
+
'use client'
|
|
9
|
+
|
|
10
|
+
import { useEffect, useState } from 'react'
|
|
11
|
+
import { createTableController } from 'ui-sdk-table-react'
|
|
12
|
+
import { Table } from 'ui-sdk-table-react/reference'
|
|
13
|
+
import type { ColumnDef, TableView } from 'ui-sdk-table-core'
|
|
14
|
+
import { Button } from '@ui-sdk/react/button'
|
|
15
|
+
|
|
16
|
+
type Flight = { id: string; flight: string; to: string; gate: string; delay: number }
|
|
17
|
+
|
|
18
|
+
const flights: Flight[] = [
|
|
19
|
+
{ id: 'f1', flight: 'SK 411', to: 'Oslo', gate: 'A4', delay: 0 },
|
|
20
|
+
{ id: 'f2', flight: 'LH 870', to: 'Frankfurt', gate: 'B12', delay: 25 },
|
|
21
|
+
{ id: 'f3', flight: 'AY 132', to: 'Helsinki', gate: 'A9', delay: 5 },
|
|
22
|
+
{ id: 'f4', flight: 'BA 795', to: 'London', gate: 'C2', delay: 40 },
|
|
23
|
+
{ id: 'f5', flight: 'KL 1170', to: 'Amsterdam', gate: 'B3', delay: 0 },
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
const columns: ColumnDef<Flight>[] = [
|
|
27
|
+
{ id: 'flight', header: 'Flight', sortable: true },
|
|
28
|
+
{ id: 'to', header: 'To', sortable: true },
|
|
29
|
+
{ id: 'gate', header: 'Gate' },
|
|
30
|
+
{ id: 'delay', header: 'Delay, min', sortable: true, format: 'integer' },
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
export default function Outside() {
|
|
34
|
+
const [controller] = useState(createTableController)
|
|
35
|
+
const [view, setView] = useState<TableView>()
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
const read = () => setView(controller.getView())
|
|
38
|
+
const stop = controller.subscribe(read)
|
|
39
|
+
read()
|
|
40
|
+
return stop
|
|
41
|
+
}, [controller])
|
|
42
|
+
return (
|
|
43
|
+
<>
|
|
44
|
+
<div>
|
|
45
|
+
<Button onClick={() => controller.sortBy('delay', 'desc')}>Most delayed first</Button>
|
|
46
|
+
<Button onClick={() => controller.setColumnFilter('delay', { min: 1 })}>
|
|
47
|
+
Only delayed
|
|
48
|
+
</Button>
|
|
49
|
+
<Button onClick={() => controller.clearFilters()}>Every flight</Button>
|
|
50
|
+
</div>
|
|
51
|
+
<p role="status">
|
|
52
|
+
{view?.dataRowCount ?? 0} of {flights.length} flights
|
|
53
|
+
{view?.sort[0] && `, sorted by ${view.sort[0].id}`}
|
|
54
|
+
</p>
|
|
55
|
+
<Table rows={flights} columns={columns} aria-label="Departures" controller={controller} />
|
|
56
|
+
</>
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Press Most delayed first, then Only delayed. The line above the table reads the view: how many flights pass and the sort. Every flight brings the rows back.
|
|
62
|
+
|
|
63
|
+
## The controller
|
|
64
|
+
|
|
65
|
+
Import `createTableController` from `ui-sdk-table-react`. Keep the controller across renders (`const [controller] = useState(createTableController)`) and pass it as `controller`.
|
|
66
|
+
|
|
67
|
+
Use these methods to build controls:
|
|
68
|
+
|
|
69
|
+
- **Sort and filter:** `sortBy`, `toggleSort`, `setSearch`, `inputSearch`, `flushSearch`, `setColumnFilter`, `clearFilters`, `clearColumnFilters`.
|
|
70
|
+
- **Groups and pages:** `setGroupBy`, `setPage`, `setPageSize`, `nextPage`, `previousPage`, `firstPage`, `lastPage`.
|
|
71
|
+
- **Rows:** `expand`, `collapse`, `toggleExpanded`, `expandAll`, `collapseAll`, `select`, `toggleSelected`, `selectAll`, `selectRows`, `deselectRows`, `clearSelection`, `pinRow`, `scrollToRow`.
|
|
72
|
+
- **Columns:** `setColumnVisibility`, `toggleColumnVisibility`, `showAllColumns`, `hideAllColumns`, `moveColumn`, `setColumnOrder`, `setColumnWidth`, `pinColumn`, the `reset…` methods, `setColumnAggregate`.
|
|
73
|
+
- **Cells:** `editCell`, `startEdit`, `cancelEdit`, `selectCells`, `copyCells`, `pasteCells` and the methods of the data sheet.
|
|
74
|
+
- **Reads:** `getView`, `getFacets`, `getTotals`, `getSelectionSummary`, `exportData`, `subscribe`.
|
|
75
|
+
|
|
76
|
+
A method that gets a wrong argument throws a message that lists the valid values, such as `No column "age" to sort by. Columns: name, score.`
|
|
77
|
+
|
|
78
|
+
## The view
|
|
79
|
+
|
|
80
|
+
Call `getView()` for the current state. To keep a control in sync, read once when the table is ready, then read again in a `subscribe` listener. Call the returned function when the control is removed.
|
|
81
|
+
|
|
82
|
+
| Field | What it holds |
|
|
83
|
+
| --- | --- |
|
|
84
|
+
| `sort`, `filter`, `groupBy`, `expanded`, `selected` | The state |
|
|
85
|
+
| `search`, `searchPending` | The text of a search field, and whether it waits for a pause |
|
|
86
|
+
| `page`, `pageSize`, `pageCount`, `rowCount`, `pageUnit`, `lastPageKnown` | The pages |
|
|
87
|
+
| `dataRowCount` | How many data rows pass the filters, child rows too |
|
|
88
|
+
| `columns`, `visibleColumns` | Every column that holds cells, and the columns in view in their order |
|
|
89
|
+
| `selectable` | The ids of the rows that `selectAll` selects |
|
|
90
|
+
| `hasExpandable`, `hasExpanded` | Whether something can open, and whether something is open |
|
|
91
|
+
| `loading` | The `loading` prop |
|
|
92
|
+
| `activeCell`, `cellSelection`, `canUndo`, `canRedo` | For a sheet |
|
|
93
|
+
|
|
94
|
+
The listener receives the keys that changed when state, rows or columns change. Use it to refresh other reads too, such as `getFacets(id)` for a filter control.
|
|
95
|
+
|
|
96
|
+
## Names
|
|
97
|
+
|
|
98
|
+
- **Types:** `PageUnit`.
|
|
99
|
+
|
|
100
|
+
For shared methods and result types, see [Controller](./reference-controller.md) and [View](./reference-view.md).
|
|
101
|
+
|
|
102
|
+
For props, callbacks and render functions, see the [React API reference](./api-react.md).
|
|
103
|
+
|
|
104
|
+
See the [More tiles of the sink](https://ui-sdk-table.fdts.dev/?page=sink&only=more).
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# React: Agents
|
|
2
|
+
|
|
3
|
+
[All examples and setup](./examples.md)
|
|
4
|
+
|
|
5
|
+
## agent
|
|
6
|
+
|
|
7
|
+
```tsx title="agent.tsx"
|
|
8
|
+
'use client'
|
|
9
|
+
|
|
10
|
+
import { useState } from 'react'
|
|
11
|
+
import { Table } from 'ui-sdk-table-react/reference'
|
|
12
|
+
import type { ColumnDef } from 'ui-sdk-table-core'
|
|
13
|
+
import { createAgent } from '@ui-sdk/react/agent'
|
|
14
|
+
import { Button } from '@ui-sdk/react/button'
|
|
15
|
+
|
|
16
|
+
type Ship = { id: string; name: string; port: string; tons: number }
|
|
17
|
+
|
|
18
|
+
const ships: Ship[] = [
|
|
19
|
+
{ id: 's1', name: 'Northern Star', port: 'Bergen', tons: 3200 },
|
|
20
|
+
{ id: 's2', name: 'Blue Heron', port: 'Rotterdam', tons: 5100 },
|
|
21
|
+
{ id: 's3', name: 'Sea Lark', port: 'Bergen', tons: 1800 },
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
const columns: ColumnDef<Ship>[] = [
|
|
25
|
+
{ id: 'name', header: 'Ship', sortable: true },
|
|
26
|
+
{ id: 'port', header: 'Port', sortable: true },
|
|
27
|
+
{ id: 'tons', header: 'Tons', sortable: true, format: 'integer' },
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
export default function Agent() {
|
|
31
|
+
const [agent] = useState(() => createAgent())
|
|
32
|
+
const [answer, setAnswer] = useState('No action yet.')
|
|
33
|
+
async function act(column: string) {
|
|
34
|
+
const result = await agent.act({
|
|
35
|
+
steps: [{ ref: 'Ships in port', action: 'sortBy', args: [column, 'desc'] }],
|
|
36
|
+
})
|
|
37
|
+
setAnswer(result.ok ? 'sortBy done.' : `Refused: ${result.error?.message ?? ''}`)
|
|
38
|
+
}
|
|
39
|
+
return (
|
|
40
|
+
<>
|
|
41
|
+
<div>
|
|
42
|
+
<Button onClick={() => act('tons')}>Heaviest first</Button>
|
|
43
|
+
<Button onClick={() => act('weight')}>Sort by weight</Button>
|
|
44
|
+
</div>
|
|
45
|
+
<p role="status">{answer}</p>
|
|
46
|
+
<Table rows={ships} columns={columns} aria-label="Ships in port" />
|
|
47
|
+
</>
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
```
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# React: Column definitions
|
|
2
|
+
|
|
3
|
+
[All examples and setup](./examples.md)
|
|
4
|
+
|
|
5
|
+
## formats
|
|
6
|
+
|
|
7
|
+
```tsx title="formats.tsx"
|
|
8
|
+
'use client'
|
|
9
|
+
|
|
10
|
+
import { Table } from 'ui-sdk-table-react/reference'
|
|
11
|
+
import type { ColumnDef } from 'ui-sdk-table-core'
|
|
12
|
+
|
|
13
|
+
type Book = {
|
|
14
|
+
id: string
|
|
15
|
+
title: string
|
|
16
|
+
author: { first: string; last: string }
|
|
17
|
+
price: number
|
|
18
|
+
sold: number
|
|
19
|
+
share: number
|
|
20
|
+
published: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const books: Book[] = [
|
|
24
|
+
{
|
|
25
|
+
id: 'b1',
|
|
26
|
+
title: 'The River Road',
|
|
27
|
+
author: { first: 'Ada', last: 'Lindqvist' },
|
|
28
|
+
price: 18.5,
|
|
29
|
+
sold: 12480,
|
|
30
|
+
share: 0.312,
|
|
31
|
+
published: '2024-03-14',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
id: 'b2',
|
|
35
|
+
title: 'Small Hours',
|
|
36
|
+
author: { first: 'Tomas', last: 'Reyes' },
|
|
37
|
+
price: 24,
|
|
38
|
+
sold: 8120,
|
|
39
|
+
share: 0.203,
|
|
40
|
+
published: '2023-09-02',
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
id: 'b3',
|
|
44
|
+
title: 'Salt and Iron',
|
|
45
|
+
author: { first: 'Maya', last: 'Okafor' },
|
|
46
|
+
price: 15.75,
|
|
47
|
+
sold: 19400,
|
|
48
|
+
share: 0.485,
|
|
49
|
+
published: '2025-01-20',
|
|
50
|
+
},
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
const columns: ColumnDef<Book>[] = [
|
|
54
|
+
{ id: 'title', header: 'Title', sortable: true },
|
|
55
|
+
// A value that is not one property of the row comes from a function.
|
|
56
|
+
{
|
|
57
|
+
id: 'author',
|
|
58
|
+
header: 'Author',
|
|
59
|
+
sortable: true,
|
|
60
|
+
value: (book) => `${book.author.last}, ${book.author.first}`,
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
id: 'published',
|
|
64
|
+
header: 'Published',
|
|
65
|
+
sortable: true,
|
|
66
|
+
format: 'date',
|
|
67
|
+
formatOptions: { dateStyle: 'medium', timeZone: 'UTC' },
|
|
68
|
+
},
|
|
69
|
+
// A header group: one header above the columns that it holds.
|
|
70
|
+
{
|
|
71
|
+
id: 'sales',
|
|
72
|
+
header: 'Sales',
|
|
73
|
+
columns: [
|
|
74
|
+
{ id: 'sold', header: 'Copies', sortable: true, format: 'integer' },
|
|
75
|
+
{
|
|
76
|
+
id: 'price',
|
|
77
|
+
header: 'Price',
|
|
78
|
+
sortable: true,
|
|
79
|
+
format: 'currency',
|
|
80
|
+
formatOptions: { currency: 'USD', showUnit: true },
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
id: 'share',
|
|
84
|
+
header: 'Share',
|
|
85
|
+
sortable: true,
|
|
86
|
+
format: 'percentage',
|
|
87
|
+
formatOptions: { showUnit: true, maximumFractionDigits: 1 },
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
]
|
|
92
|
+
|
|
93
|
+
export default function Formats() {
|
|
94
|
+
return <Table rows={books} columns={columns} aria-label="Books and their sales" locale="en-US" />
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## layout
|
|
99
|
+
|
|
100
|
+
```tsx title="layout.tsx"
|
|
101
|
+
'use client'
|
|
102
|
+
|
|
103
|
+
import { useState } from 'react'
|
|
104
|
+
import { createTableController } from 'ui-sdk-table-react'
|
|
105
|
+
import { Table } from 'ui-sdk-table-react/reference'
|
|
106
|
+
import type { ColumnDef, ColumnPinning, ColumnVisibility } from 'ui-sdk-table-core'
|
|
107
|
+
import { Button } from '@ui-sdk/react/button'
|
|
108
|
+
|
|
109
|
+
type City = { id: string; name: string; country: string; people: number; area: number }
|
|
110
|
+
|
|
111
|
+
const cities: City[] = [
|
|
112
|
+
{ id: 'c1', name: 'Lisbon', country: 'Portugal', people: 545000, area: 100 },
|
|
113
|
+
{ id: 'c2', name: 'Vienna', country: 'Austria', people: 1982000, area: 415 },
|
|
114
|
+
{ id: 'c3', name: 'Oslo', country: 'Norway', people: 709000, area: 454 },
|
|
115
|
+
]
|
|
116
|
+
|
|
117
|
+
const columns: ColumnDef<City>[] = [
|
|
118
|
+
// A city has no name to hide: the column stays.
|
|
119
|
+
{ id: 'name', header: 'City', width: 140, hideable: false },
|
|
120
|
+
{ id: 'country', header: 'Country', width: 140 },
|
|
121
|
+
{ id: 'people', header: 'People', width: 130, format: 'integer' },
|
|
122
|
+
{ id: 'area', header: 'Area, km²', width: 130, format: 'integer' },
|
|
123
|
+
]
|
|
124
|
+
|
|
125
|
+
const styles = `
|
|
126
|
+
.demo-column-definitions-layout { max-inline-size: 100%; overflow: auto; }
|
|
127
|
+
.demo-column-definitions-layout table { table-layout: fixed; }
|
|
128
|
+
.demo-column-definitions-layout col { width: var(--table-column-width, auto); }
|
|
129
|
+
.demo-column-definitions-layout th { position: relative; }
|
|
130
|
+
.demo-column-definitions-layout [data-pinned='start'] {
|
|
131
|
+
position: sticky;
|
|
132
|
+
inset-inline-start: var(--table-pin-offset, 0);
|
|
133
|
+
background: Canvas;
|
|
134
|
+
z-index: 1;
|
|
135
|
+
}
|
|
136
|
+
.demo-column-definitions-layout [data-slot='table-resizer'] {
|
|
137
|
+
position: absolute;
|
|
138
|
+
inset-block: 0;
|
|
139
|
+
inset-inline-end: 0;
|
|
140
|
+
inline-size: 8px;
|
|
141
|
+
cursor: col-resize;
|
|
142
|
+
touch-action: none;
|
|
143
|
+
border-inline-end: 1px solid GrayText;
|
|
144
|
+
}
|
|
145
|
+
`
|
|
146
|
+
|
|
147
|
+
export default function Layout() {
|
|
148
|
+
const [controller] = useState(createTableController)
|
|
149
|
+
const [visibility, setVisibility] = useState<ColumnVisibility>({})
|
|
150
|
+
const [pinning, setPinning] = useState<ColumnPinning>({})
|
|
151
|
+
const pinned = pinning.start?.includes('name')
|
|
152
|
+
return (
|
|
153
|
+
<div className="demo-column-definitions-layout">
|
|
154
|
+
<style>{styles}</style>
|
|
155
|
+
<div>
|
|
156
|
+
<Button onClick={() => controller.toggleColumnVisibility('country')}>
|
|
157
|
+
{visibility.country === false ? 'Show' : 'Hide'} the country
|
|
158
|
+
</Button>
|
|
159
|
+
<Button onClick={() => controller.pinColumn('name', pinned ? null : 'start')}>
|
|
160
|
+
{pinned ? 'Free' : 'Pin'} the city
|
|
161
|
+
</Button>
|
|
162
|
+
<Button onClick={() => controller.resetColumnWidths()}>Reset the widths</Button>
|
|
163
|
+
</div>
|
|
164
|
+
<Table
|
|
165
|
+
rows={cities}
|
|
166
|
+
columns={columns}
|
|
167
|
+
aria-label="Cities"
|
|
168
|
+
controller={controller}
|
|
169
|
+
columnVisibility={visibility}
|
|
170
|
+
onColumnVisibilityChange={setVisibility}
|
|
171
|
+
columnPinning={pinning}
|
|
172
|
+
onColumnPinningChange={setPinning}
|
|
173
|
+
resizable
|
|
174
|
+
reorderable
|
|
175
|
+
/>
|
|
176
|
+
</div>
|
|
177
|
+
)
|
|
178
|
+
}
|
|
179
|
+
```
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
# React: A formula writer
|
|
2
|
+
|
|
3
|
+
[All examples and setup](./examples.md)
|
|
4
|
+
|
|
5
|
+
## writer
|
|
6
|
+
|
|
7
|
+
```tsx title="writer.tsx"
|
|
8
|
+
'use client'
|
|
9
|
+
|
|
10
|
+
import { useEffect, useLayoutEffect, useState, useRef, Fragment, type KeyboardEvent } from 'react'
|
|
11
|
+
import { createTableController } from 'ui-sdk-table-react'
|
|
12
|
+
import { Table } from 'ui-sdk-table-react/reference'
|
|
13
|
+
import {
|
|
14
|
+
applyEdits,
|
|
15
|
+
rowNumberColumn,
|
|
16
|
+
type CellFormulas,
|
|
17
|
+
type CellPick,
|
|
18
|
+
type ColumnDef,
|
|
19
|
+
type FormulaView,
|
|
20
|
+
type TableView,
|
|
21
|
+
} from 'ui-sdk-table-core'
|
|
22
|
+
import {
|
|
23
|
+
createFormulaEngine,
|
|
24
|
+
formulaHelp,
|
|
25
|
+
insertReference,
|
|
26
|
+
FORMULA_FUNCTIONS,
|
|
27
|
+
} from 'ui-sdk-table-core/formulas'
|
|
28
|
+
|
|
29
|
+
type Line = { id: string; item: string; price: number | null; qty: number | null; total?: unknown }
|
|
30
|
+
|
|
31
|
+
const initialLines: Line[] = [
|
|
32
|
+
{ id: 'l1', item: 'Desk lamp', price: 39, qty: 4, total: 156 },
|
|
33
|
+
{ id: 'l2', item: 'Monitor arm', price: 89, qty: 2, total: 178 },
|
|
34
|
+
{ id: 'l3', item: 'Keyboard', price: 59, qty: 3, total: 177 },
|
|
35
|
+
{ id: 'l4', item: 'Order total', price: null, qty: null, total: null },
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
const columns: ColumnDef<Line>[] = [
|
|
39
|
+
rowNumberColumn<Line>(),
|
|
40
|
+
{ id: 'item', header: 'Item', allowFormula: false },
|
|
41
|
+
{ id: 'price', header: 'Price', format: 'decimal' },
|
|
42
|
+
{ id: 'qty', header: 'Qty', format: 'integer' },
|
|
43
|
+
{ id: 'total', header: 'Total', format: 'decimal' },
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
const initialFormulas: CellFormulas = {
|
|
47
|
+
cells: {
|
|
48
|
+
l1: { total: '=[price]*[qty]' },
|
|
49
|
+
l2: { total: '=[price]*[qty]' },
|
|
50
|
+
l3: { total: '=[price]*[qty]' },
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export default function Writer() {
|
|
55
|
+
const [lines, setLines] = useState<Line[]>(initialLines)
|
|
56
|
+
const [formulas, setFormulas] = useState<CellFormulas>(initialFormulas)
|
|
57
|
+
const [controller] = useState(createTableController)
|
|
58
|
+
const [engine] = useState(createFormulaEngine)
|
|
59
|
+
const [view, setView] = useState<TableView>()
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
let address: string | undefined
|
|
62
|
+
const read = () => {
|
|
63
|
+
const next = controller.getView()
|
|
64
|
+
if (address !== next?.formula?.address) cancel()
|
|
65
|
+
address = next?.formula?.address
|
|
66
|
+
setView(next)
|
|
67
|
+
}
|
|
68
|
+
const stop = controller.subscribe(read)
|
|
69
|
+
read()
|
|
70
|
+
return stop
|
|
71
|
+
}, [controller])
|
|
72
|
+
const frame = useRef<HTMLDivElement>(null)
|
|
73
|
+
const [draft, setDraft] = useState<{ at: FormulaView; text: string } | null>(null)
|
|
74
|
+
const [error, setError] = useState('')
|
|
75
|
+
const at = draft?.at ?? view?.formula
|
|
76
|
+
const text = draft?.text ?? at?.text ?? ''
|
|
77
|
+
function input(value: string) {
|
|
78
|
+
if (at) {
|
|
79
|
+
setDraft({ at, text: value })
|
|
80
|
+
setError('')
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function cancel() {
|
|
84
|
+
endPicking()
|
|
85
|
+
setDraft(null)
|
|
86
|
+
setError('')
|
|
87
|
+
}
|
|
88
|
+
async function commit() {
|
|
89
|
+
if (draft) {
|
|
90
|
+
try {
|
|
91
|
+
await controller.editCell(draft.at.rowId, draft.at.columnId, draft.text)
|
|
92
|
+
cancel()
|
|
93
|
+
} catch (reason) {
|
|
94
|
+
setError(
|
|
95
|
+
reason instanceof Error
|
|
96
|
+
? typeof reason.cause === 'string'
|
|
97
|
+
? reason.cause
|
|
98
|
+
: reason.message
|
|
99
|
+
: String(reason),
|
|
100
|
+
)
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
frame.current?.querySelector<HTMLElement>('[data-active]')?.focus()
|
|
105
|
+
}
|
|
106
|
+
const field = useRef<HTMLInputElement>(null)
|
|
107
|
+
const [caret, setCaret] = useState({ start: 0, end: 0 })
|
|
108
|
+
const [picked, setPicked] = useState<{ start: number; end: number } | null>(null)
|
|
109
|
+
const [said, setSaid] = useState('')
|
|
110
|
+
const place = useRef<number | null>(null)
|
|
111
|
+
const help = formulaHelp(text, caret.end, { locale: 'en-US' })
|
|
112
|
+
const formula = text.startsWith('=')
|
|
113
|
+
const [editing, setEditing] = useState(false)
|
|
114
|
+
const picking = editing && formula && caret.start > 0 && (help.reference || picked !== null)
|
|
115
|
+
function endPicking() {
|
|
116
|
+
setEditing(false)
|
|
117
|
+
setPicked(null)
|
|
118
|
+
setSaid('')
|
|
119
|
+
}
|
|
120
|
+
const word =
|
|
121
|
+
help.token && (help.token.kind === 'name' || help.token.kind === 'function') ? help.token : null
|
|
122
|
+
const suggestions = word
|
|
123
|
+
? FORMULA_FUNCTIONS.filter((info) => info.name.startsWith(word.text.toUpperCase())).slice(0, 6)
|
|
124
|
+
: []
|
|
125
|
+
const refs = formula && at ? controller.getFormulaReferences(text, at) : []
|
|
126
|
+
const mark = refs.length ? controller.getReferenceMarks(refs) : null
|
|
127
|
+
function readCaret() {
|
|
128
|
+
const el = field.current
|
|
129
|
+
if (!el) return
|
|
130
|
+
const start = el.selectionStart ?? 0
|
|
131
|
+
const end = el.selectionEnd ?? 0
|
|
132
|
+
if (start !== end || picked?.end !== end) setPicked(null)
|
|
133
|
+
setCaret({ start, end })
|
|
134
|
+
}
|
|
135
|
+
function moveCaret(end: number) {
|
|
136
|
+
place.current = end
|
|
137
|
+
setCaret({ start: end, end })
|
|
138
|
+
}
|
|
139
|
+
useLayoutEffect(() => {
|
|
140
|
+
const end = place.current
|
|
141
|
+
if (end !== null) {
|
|
142
|
+
field.current?.setSelectionRange(end, end)
|
|
143
|
+
place.current = null
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
function choose(name: string) {
|
|
147
|
+
if (!word) return
|
|
148
|
+
const next = insertReference(text, word, `${name}(`)
|
|
149
|
+
field.current?.focus()
|
|
150
|
+
setEditing(true)
|
|
151
|
+
input(next.text)
|
|
152
|
+
setPicked(null)
|
|
153
|
+
moveCaret(next.end)
|
|
154
|
+
}
|
|
155
|
+
function pick(cells: CellPick | null) {
|
|
156
|
+
const address = cells && controller.getRangeAddress(cells)
|
|
157
|
+
if (!address) return
|
|
158
|
+
const next = insertReference(text, picked ?? caret, address)
|
|
159
|
+
input(next.text)
|
|
160
|
+
setPicked({ start: next.start, end: next.end })
|
|
161
|
+
setSaid(`Picked ${address}`)
|
|
162
|
+
moveCaret(next.end)
|
|
163
|
+
}
|
|
164
|
+
function arrow(event: KeyboardEvent<HTMLInputElement>) {
|
|
165
|
+
const key = event.key
|
|
166
|
+
if (key === 'Escape') {
|
|
167
|
+
event.preventDefault()
|
|
168
|
+
cancel()
|
|
169
|
+
return
|
|
170
|
+
}
|
|
171
|
+
if (
|
|
172
|
+
!picking ||
|
|
173
|
+
event.altKey ||
|
|
174
|
+
!(key === 'ArrowLeft' || key === 'ArrowDown' || key === 'ArrowUp' || key === 'ArrowRight')
|
|
175
|
+
)
|
|
176
|
+
return
|
|
177
|
+
event.preventDefault()
|
|
178
|
+
controller.movePick(key, {
|
|
179
|
+
extend: event.shiftKey,
|
|
180
|
+
edge: event.ctrlKey || event.metaKey,
|
|
181
|
+
start: picked === null,
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
return (
|
|
185
|
+
<>
|
|
186
|
+
<form
|
|
187
|
+
className="writer"
|
|
188
|
+
onSubmit={(event) => {
|
|
189
|
+
event.preventDefault()
|
|
190
|
+
endPicking()
|
|
191
|
+
void commit()
|
|
192
|
+
}}
|
|
193
|
+
>
|
|
194
|
+
<output data-testid="address">{at?.address || '—'}</output>
|
|
195
|
+
<input
|
|
196
|
+
ref={field}
|
|
197
|
+
aria-label="Formula of the active cell"
|
|
198
|
+
placeholder="Press a cell, then type ="
|
|
199
|
+
autoComplete="off"
|
|
200
|
+
spellCheck={false}
|
|
201
|
+
value={text}
|
|
202
|
+
disabled={!at}
|
|
203
|
+
aria-invalid={!!error}
|
|
204
|
+
onBlur={endPicking}
|
|
205
|
+
onChange={(event) => {
|
|
206
|
+
setEditing(event.currentTarget === document.activeElement)
|
|
207
|
+
setPicked(null)
|
|
208
|
+
setSaid('')
|
|
209
|
+
input(event.currentTarget.value)
|
|
210
|
+
readCaret()
|
|
211
|
+
}}
|
|
212
|
+
onSelect={readCaret}
|
|
213
|
+
onKeyUp={readCaret}
|
|
214
|
+
onClick={readCaret}
|
|
215
|
+
onKeyDown={arrow}
|
|
216
|
+
/>
|
|
217
|
+
{help.call?.info && (
|
|
218
|
+
<p data-testid="hint">
|
|
219
|
+
{help.call.name}(
|
|
220
|
+
{help.call.info.arguments.map((arg, index) => (
|
|
221
|
+
<Fragment key={arg.name}>
|
|
222
|
+
{index > 0 && ', '}
|
|
223
|
+
{index === help.call?.argument ? <strong>{arg.name}</strong> : arg.name}
|
|
224
|
+
</Fragment>
|
|
225
|
+
))}
|
|
226
|
+
)
|
|
227
|
+
</p>
|
|
228
|
+
)}
|
|
229
|
+
{suggestions.length > 0 && (
|
|
230
|
+
<ul aria-label="Functions">
|
|
231
|
+
{suggestions.map((info) => (
|
|
232
|
+
<li key={info.name}>
|
|
233
|
+
<button
|
|
234
|
+
type="button"
|
|
235
|
+
onPointerDown={(event) => event.preventDefault()}
|
|
236
|
+
onClick={() => choose(info.name)}
|
|
237
|
+
>
|
|
238
|
+
<strong>{info.name}</strong> {info.help}
|
|
239
|
+
</button>
|
|
240
|
+
</li>
|
|
241
|
+
))}
|
|
242
|
+
</ul>
|
|
243
|
+
)}
|
|
244
|
+
<p role="status">{error || said}</p>
|
|
245
|
+
</form>
|
|
246
|
+
<div ref={frame}>
|
|
247
|
+
<Table
|
|
248
|
+
rows={lines}
|
|
249
|
+
columns={columns}
|
|
250
|
+
aria-label="An order with a formula writer"
|
|
251
|
+
controller={controller}
|
|
252
|
+
locale="en-US"
|
|
253
|
+
sheet
|
|
254
|
+
columnLetters
|
|
255
|
+
cellFormulas={formulas}
|
|
256
|
+
onCellFormulasChange={setFormulas}
|
|
257
|
+
formulas={engine}
|
|
258
|
+
onCellEdit={(edits) => setLines((current) => applyEdits(current, edits))}
|
|
259
|
+
pickCells={picking}
|
|
260
|
+
onPickedCellsChange={pick}
|
|
261
|
+
cellAttrs={(context) => {
|
|
262
|
+
if (!mark || context.kind !== 'cell') return
|
|
263
|
+
const hit = mark(context.node.id, context.column.id)
|
|
264
|
+
return hit
|
|
265
|
+
? {
|
|
266
|
+
'data-reference': String(hit.index % 4),
|
|
267
|
+
'data-reference-edge': hit.edges || undefined,
|
|
268
|
+
}
|
|
269
|
+
: undefined
|
|
270
|
+
}}
|
|
271
|
+
/>
|
|
272
|
+
</div>
|
|
273
|
+
</>
|
|
274
|
+
)
|
|
275
|
+
}
|
|
276
|
+
```
|