ui-sdk-table-react 0.2.7 → 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 -21
- package/docs/skills.md +1 -1
- package/docs/webmcp.md +97 -0
- package/package.json +12 -12
- package/src/index.ts +62 -62
- package/src/reference.ts +113 -113
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 FallDownTheSystem
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 FallDownTheSystem
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/docs/skills.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Use the `ui-sdk-table` skill to help a coding agent build and test an interactive table in React, Vue, Svelte, Solid or Web Components.
|
|
4
4
|
|
|
5
|
-
The table skill helps the agent choose parts from your app’s adapter. The separate `ui-sdk` skill helps it build the controls around the table, such as a search field or a column menu. Neither skill installs a table in your app. For that, see [Installation](./installation.md). To let an agent **operate a running table** instead of writing its code, follow UI SDK’s [Connect an agent](https://ui-sdk.dev/docs/agents/connect-an-agent) and the [table actions example](https://ui-sdk-table.fdts.dev/docs/agents.md#connect-an-agent).
|
|
5
|
+
The table skill helps the agent choose parts from your app’s adapter. The separate `ui-sdk` skill helps it build the controls around the table, such as a search field or a column menu. Neither skill installs a table in your app. For that, see [Installation](./installation.md). To let an agent **operate a running table** instead of writing its code, follow UI SDK’s [Connect an agent](https://ui-sdk.dev/docs/agents/connect-an-agent) and the [table actions example](https://ui-sdk-table.fdts.dev/docs/agents.md#connect-an-agent). For an agent in your terminal, such as Claude Code or Codex, see [WebMCP](./webmcp.md).
|
|
6
6
|
|
|
7
7
|
## Install the skill
|
|
8
8
|
|
package/docs/webmcp.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# WebMCP
|
|
2
|
+
|
|
3
|
+
Let a coding agent in your terminal, such as Claude Code or Codex, read and use a table that is open in your browser.
|
|
4
|
+
|
|
5
|
+
## How it works
|
|
6
|
+
|
|
7
|
+
[WebMCP](https://github.com/webmachinelearning/webmcp) is a draft web standard. A page describes what it can do as tools, and an agent calls those tools instead of clicking through the interface. A tool runs the page’s own code, so the screen changes exactly as it would for a person.
|
|
8
|
+
|
|
9
|
+
UI SDK publishes three tools for every component on the page: `snapshot`, `query` and `act`. A table built with `ui-sdk-table` joins the same tools, with the same actions that a person has. You write no tool for the table.
|
|
10
|
+
|
|
11
|
+
Few browsers support WebMCP yet, and a browser gives the tools only to its own agent. The [WebMCP bridge](https://github.com/FallDownTheSystem/WebMCP) carries them to an agent on your machine:
|
|
12
|
+
|
|
13
|
+
1. Your app registers the three tools of UI SDK.
|
|
14
|
+
2. A userscript in the browser loads the WebMCP polyfill when the browser has no WebMCP, and hands the tools of each tab to a small server on your own machine.
|
|
15
|
+
3. The MCP server of the bridge gives your agent two tools: `webmcp_list_tools` lists the tools of the open tabs, and `webmcp_call_tool` calls one.
|
|
16
|
+
|
|
17
|
+
The [implementation status](https://github.com/webmachinelearning/webmcp/blob/main/implementation-status.md) page lists the browsers that support WebMCP.
|
|
18
|
+
|
|
19
|
+
## Register the tools
|
|
20
|
+
|
|
21
|
+
Create one agent for the app and register its tools. Give the table an `aria-label`, so the agent can find it by name.
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { createAgent, registerTools } from '@ui-sdk/react/agent'
|
|
25
|
+
|
|
26
|
+
const agent = createAgent()
|
|
27
|
+
const unregister = registerTools(agent, { allow: (step) => !step.confirm })
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Import the agent from the same UI SDK package as your components, so they share one copy of the core. `allow` runs for every step that the agent asks for. `!step.confirm` lets every step run, except a step on a control that you marked for a person. UI SDK’s [Permissions](https://ui-sdk.dev/docs/agents/connect-an-agent/permissions) page shows the marks, and its [WebMCP](https://ui-sdk.dev/docs/agents/connect-an-agent/webmcp) page shows the tools.
|
|
31
|
+
|
|
32
|
+
In a browser without WebMCP and without the userscript, `registerTools` does nothing. Call `unregister()` to remove the tools.
|
|
33
|
+
|
|
34
|
+
## Install the bridge
|
|
35
|
+
|
|
36
|
+
Follow the [install steps of the bridge](https://github.com/FallDownTheSystem/WebMCP#install):
|
|
37
|
+
|
|
38
|
+
1. Install the `@falldownthesystem/webmcp` package and add its MCP server to your agent.
|
|
39
|
+
2. Install a userscript manager, then the userscript of the bridge.
|
|
40
|
+
3. Open your app, or reload it.
|
|
41
|
+
|
|
42
|
+
The first call to the tools of a tab waits until you approve it in that tab.
|
|
43
|
+
|
|
44
|
+
## Ask the agent
|
|
45
|
+
|
|
46
|
+
Ask the agent to list the tools first, then to read before it acts:
|
|
47
|
+
|
|
48
|
+
```text
|
|
49
|
+
Use webmcp to list the tools of my app.
|
|
50
|
+
Read the snapshot, then sort the table "Orders" by total, highest first.
|
|
51
|
+
Tell me the three largest customers.
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The name of a tool starts with an ID for its tab, such as `b731eb105162fbdd:snapshot`. The ID changes when the tab reloads, so the agent lists the tools again before it calls one.
|
|
55
|
+
|
|
56
|
+
## What the agent sees
|
|
57
|
+
|
|
58
|
+
- `snapshot` lists the table with its name, its state, such as `sort`, `filter` and `selected`, and its actions.
|
|
59
|
+
- `query` finds a component, or the choices inside one, by its text.
|
|
60
|
+
- `act` runs the methods of the controller: sort, search and filter, pages, selection, columns and cells. The reads `exportData`, `getTotals`, `getFacets` and `getSelectionSummary` give the agent the rows and the numbers.
|
|
61
|
+
- A wrong argument fails with a message that lists the valid values, such as `No column "weight" to sort by. Columns: name, port, tons.` The agent reads it and tries again.
|
|
62
|
+
|
|
63
|
+
The three tools see components and their state. They don’t see data that no component holds, the text and pictures of the page, or controls built with plain HTML or another library. For those, register tools of your own beside the three. A read-only `read` tool that gives the text of the page by region covers most of it.
|
|
64
|
+
|
|
65
|
+
## A table in an iframe
|
|
66
|
+
|
|
67
|
+
A design canvas such as [Claude Design](https://claude.ai/design) shows each board in an iframe of another origin. The page around the iframe sees no tool of the board. The bridge has a frame adapter for this case: the board names the parent origin it trusts and passes its tools.
|
|
68
|
+
|
|
69
|
+
Inside the iframe, skip `registerTools`. Turn `agentTools` into tool objects, and pass them to the frame adapter:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { agentTools, createAgent } from '@ui-sdk/web/agent'
|
|
73
|
+
import { exposeFrameTools } from 'https://cdn.jsdelivr.net/npm/@falldownthesystem/webmcp/example/frame-tools.js'
|
|
74
|
+
|
|
75
|
+
const agent = createAgent({ allow: (step) => !step.confirm })
|
|
76
|
+
const tools = agentTools.map((tool) => ({
|
|
77
|
+
...tool,
|
|
78
|
+
execute: (input) => agent[tool.name](input),
|
|
79
|
+
}))
|
|
80
|
+
|
|
81
|
+
exposeFrameTools({ parentOrigin: 'https://claude.ai', tools })
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Here the `allow` of `createAgent` decides each step, because no `registerTools` runs. A board is a standalone HTML page, so it uses `@ui-sdk/web` and the standalone parts of the [Web Components](https://ui-sdk-table.fdts.dev/docs/web-components.md) guide. The bridge has [a full prompt](https://github.com/FallDownTheSystem/WebMCP#example-a-claude-design-board-your-agent-can-use) that builds a board with a ticket table and a `read` tool in Claude Design.
|
|
85
|
+
|
|
86
|
+
## Safety
|
|
87
|
+
|
|
88
|
+
- Your agent sees the tools of every open tab, but it can run one only after you approve that tab or frame in the page.
|
|
89
|
+
- The names, descriptions and results of the tools come from web pages, and a row can hold what a person wrote. Treat them as data, never as instructions.
|
|
90
|
+
- A change that an agent made reaches the events with `details.source` set to `agent`. Give the agent no other rules than a person.
|
|
91
|
+
|
|
92
|
+
The [security section](https://github.com/FallDownTheSystem/WebMCP#security) of the bridge explains the key, the approvals and how to disconnect every tab.
|
|
93
|
+
|
|
94
|
+
## Keep going
|
|
95
|
+
|
|
96
|
+
- [Agents](https://ui-sdk-table.fdts.dev/docs/agents.md) shows the actions in a live example.
|
|
97
|
+
- [Controller and view](./controller-and-view.md) lists every method that `act` can run.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ui-sdk-table-react",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.10",
|
|
4
4
|
"description": "Headless React table parts for ui-sdk-table.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -23,22 +23,22 @@
|
|
|
23
23
|
"access": "public"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"ui-sdk-table-core": "0.2.
|
|
26
|
+
"ui-sdk-table-core": "0.2.10"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@types/react": "^19.
|
|
30
|
-
"@types/react-dom": "^19.
|
|
31
|
-
"@ui-sdk/core": "0.1.0-beta.
|
|
32
|
-
"@ui-sdk/react": "0.1.0-beta.
|
|
29
|
+
"@types/react": "^19.3.3",
|
|
30
|
+
"@types/react-dom": "^19.3.0",
|
|
31
|
+
"@ui-sdk/core": "0.1.0-beta.23",
|
|
32
|
+
"@ui-sdk/react": "0.1.0-beta.23",
|
|
33
33
|
"jsdom": "^31.0.1",
|
|
34
|
-
"react": "^19.
|
|
35
|
-
"react-dom": "^19.
|
|
36
|
-
"vite": "npm:@voidzero-dev/vite-plus-core@8.3.
|
|
37
|
-
"vite-plus": "^1.
|
|
34
|
+
"react": "^19.3.1",
|
|
35
|
+
"react-dom": "^19.3.1",
|
|
36
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@8.3.8001",
|
|
37
|
+
"vite-plus": "^1.8.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"@ui-sdk/core": "0.1.0-beta.
|
|
41
|
-
"@ui-sdk/react": "0.1.0-beta.
|
|
40
|
+
"@ui-sdk/core": "0.1.0-beta.23",
|
|
41
|
+
"@ui-sdk/react": "0.1.0-beta.23",
|
|
42
42
|
"react": ">=19",
|
|
43
43
|
"react-dom": ">=19"
|
|
44
44
|
},
|
package/src/index.ts
CHANGED
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
|
|
3
|
-
import { createController, type StateOf } from '@ui-sdk/core'
|
|
4
|
-
import { createParts, type PartProps } from '@ui-sdk/react/primitive'
|
|
5
|
-
import { table, type FlatRow, type TableController, type TableOptions } from 'ui-sdk-table-core'
|
|
6
|
-
import { tableElements } from 'ui-sdk-table-core/elements'
|
|
7
|
-
|
|
8
|
-
export function createTableController() {
|
|
9
|
-
return createController<TableController>()
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const {
|
|
13
|
-
Root,
|
|
14
|
-
Head: TableHead,
|
|
15
|
-
Body,
|
|
16
|
-
Foot: TableFoot,
|
|
17
|
-
Col: TableCol,
|
|
18
|
-
HeaderRow: TableHeaderRow,
|
|
19
|
-
PlainRow: TablePlainRow,
|
|
20
|
-
Spacer: TableSpacer,
|
|
21
|
-
Row: TableRow,
|
|
22
|
-
Cell: TableCell,
|
|
23
|
-
Editor: TableEditor,
|
|
24
|
-
EditError: TableEditError,
|
|
25
|
-
FillHandle: TableFillHandle,
|
|
26
|
-
Resizer: TableResizer,
|
|
27
|
-
} = createParts(table, tableElements)
|
|
28
|
-
|
|
29
|
-
// Headers and body cells share the primitive's cell slot, but not their native tag.
|
|
30
|
-
const { Cell: TableHeaderCell } = createParts(table, { cell: 'th' })
|
|
31
|
-
|
|
32
|
-
export type TableBodyState<Row = unknown> = Omit<StateOf<typeof table, 'body'>, 'rows'> & {
|
|
33
|
-
rows: readonly FlatRow<Row>[]
|
|
34
|
-
}
|
|
35
|
-
const TableBody = Body as <Row = unknown>(
|
|
36
|
-
props: PartProps<'tbody', TableBodyState<Row>>,
|
|
37
|
-
) => ReturnType<typeof Body>
|
|
38
|
-
|
|
39
|
-
export type TableRootProps<Row = unknown> = TableOptions<Row> &
|
|
40
|
-
Omit<Parameters<typeof Root>[0], keyof TableOptions>
|
|
41
|
-
// The renderer erases the row type; rows, column accessors and callbacks must agree at the call site.
|
|
42
|
-
const TableRoot = Root as <Row>(props: TableRootProps<Row>) => ReturnType<typeof Root>
|
|
43
|
-
|
|
44
|
-
export {
|
|
45
|
-
TableRoot,
|
|
46
|
-
TableRoot as Table,
|
|
47
|
-
TableHead,
|
|
48
|
-
TableBody,
|
|
49
|
-
TableFoot,
|
|
50
|
-
TableCol,
|
|
51
|
-
TableHeaderRow,
|
|
52
|
-
TablePlainRow,
|
|
53
|
-
TableSpacer,
|
|
54
|
-
TableRow,
|
|
55
|
-
TableCell,
|
|
56
|
-
TableHeaderCell,
|
|
57
|
-
TableEditor,
|
|
58
|
-
TableEditError,
|
|
59
|
-
TableFillHandle,
|
|
60
|
-
TableResizer,
|
|
61
|
-
}
|
|
62
|
-
export type { ColumnDef, TableOptions, TableChangeDetails, SortRule } from 'ui-sdk-table-core'
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { createController, type StateOf } from '@ui-sdk/core'
|
|
4
|
+
import { createParts, type PartProps } from '@ui-sdk/react/primitive'
|
|
5
|
+
import { table, type FlatRow, type TableController, type TableOptions } from 'ui-sdk-table-core'
|
|
6
|
+
import { tableElements } from 'ui-sdk-table-core/elements'
|
|
7
|
+
|
|
8
|
+
export function createTableController() {
|
|
9
|
+
return createController<TableController>()
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const {
|
|
13
|
+
Root,
|
|
14
|
+
Head: TableHead,
|
|
15
|
+
Body,
|
|
16
|
+
Foot: TableFoot,
|
|
17
|
+
Col: TableCol,
|
|
18
|
+
HeaderRow: TableHeaderRow,
|
|
19
|
+
PlainRow: TablePlainRow,
|
|
20
|
+
Spacer: TableSpacer,
|
|
21
|
+
Row: TableRow,
|
|
22
|
+
Cell: TableCell,
|
|
23
|
+
Editor: TableEditor,
|
|
24
|
+
EditError: TableEditError,
|
|
25
|
+
FillHandle: TableFillHandle,
|
|
26
|
+
Resizer: TableResizer,
|
|
27
|
+
} = createParts(table, tableElements)
|
|
28
|
+
|
|
29
|
+
// Headers and body cells share the primitive's cell slot, but not their native tag.
|
|
30
|
+
const { Cell: TableHeaderCell } = createParts(table, { cell: 'th' })
|
|
31
|
+
|
|
32
|
+
export type TableBodyState<Row = unknown> = Omit<StateOf<typeof table, 'body'>, 'rows'> & {
|
|
33
|
+
rows: readonly FlatRow<Row>[]
|
|
34
|
+
}
|
|
35
|
+
const TableBody = Body as <Row = unknown>(
|
|
36
|
+
props: PartProps<'tbody', TableBodyState<Row>>,
|
|
37
|
+
) => ReturnType<typeof Body>
|
|
38
|
+
|
|
39
|
+
export type TableRootProps<Row = unknown> = TableOptions<Row> &
|
|
40
|
+
Omit<Parameters<typeof Root>[0], keyof TableOptions>
|
|
41
|
+
// The renderer erases the row type; rows, column accessors and callbacks must agree at the call site.
|
|
42
|
+
const TableRoot = Root as <Row>(props: TableRootProps<Row>) => ReturnType<typeof Root>
|
|
43
|
+
|
|
44
|
+
export {
|
|
45
|
+
TableRoot,
|
|
46
|
+
TableRoot as Table,
|
|
47
|
+
TableHead,
|
|
48
|
+
TableBody,
|
|
49
|
+
TableFoot,
|
|
50
|
+
TableCol,
|
|
51
|
+
TableHeaderRow,
|
|
52
|
+
TablePlainRow,
|
|
53
|
+
TableSpacer,
|
|
54
|
+
TableRow,
|
|
55
|
+
TableCell,
|
|
56
|
+
TableHeaderCell,
|
|
57
|
+
TableEditor,
|
|
58
|
+
TableEditError,
|
|
59
|
+
TableFillHandle,
|
|
60
|
+
TableResizer,
|
|
61
|
+
}
|
|
62
|
+
export type { ColumnDef, TableOptions, TableChangeDetails, SortRule } from 'ui-sdk-table-core'
|
package/src/reference.ts
CHANGED
|
@@ -1,113 +1,113 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
|
|
3
|
-
import { usePart, usePrimitive } from '@ui-sdk/react/primitive'
|
|
4
|
-
import {
|
|
5
|
-
createElement,
|
|
6
|
-
useLayoutEffect,
|
|
7
|
-
useMemo,
|
|
8
|
-
useReducer,
|
|
9
|
-
useRef,
|
|
10
|
-
useSyncExternalStore,
|
|
11
|
-
type ComponentType,
|
|
12
|
-
type MouseEvent,
|
|
13
|
-
type ReactNode,
|
|
14
|
-
} from 'react'
|
|
15
|
-
import { table, type TableOptions } from 'ui-sdk-table-core'
|
|
16
|
-
import {
|
|
17
|
-
referenceStructureKeys,
|
|
18
|
-
referencePresentationKeys,
|
|
19
|
-
renderReference,
|
|
20
|
-
type ReferenceFactory,
|
|
21
|
-
type ReferenceOptions,
|
|
22
|
-
type ReferenceSlots,
|
|
23
|
-
type TableAttrsProps,
|
|
24
|
-
} from 'ui-sdk-table-core/reference'
|
|
25
|
-
import * as parts from './index'
|
|
26
|
-
|
|
27
|
-
const elements = {
|
|
28
|
-
head: parts.TableHead,
|
|
29
|
-
body: parts.TableBody,
|
|
30
|
-
foot: parts.TableFoot,
|
|
31
|
-
col: parts.TableCol,
|
|
32
|
-
'header-row': parts.TableHeaderRow,
|
|
33
|
-
'plain-row': parts.TablePlainRow,
|
|
34
|
-
spacer: parts.TableSpacer,
|
|
35
|
-
row: parts.TableRow,
|
|
36
|
-
cell: parts.TableCell,
|
|
37
|
-
'header-cell': parts.TableHeaderCell,
|
|
38
|
-
editor: parts.TableEditor,
|
|
39
|
-
'edit-error': parts.TableEditError,
|
|
40
|
-
'fill-handle': parts.TableFillHandle,
|
|
41
|
-
resizer: parts.TableResizer,
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// The primitive emits HTML attribute names. React expects property names for native cell spans.
|
|
45
|
-
const renderCell =
|
|
46
|
-
(tag: 'th' | 'td') =>
|
|
47
|
-
({ colspan, rowspan, ...props }: Record<string, unknown>) =>
|
|
48
|
-
createElement(tag, { ...props, colSpan: colspan, rowSpan: rowspan })
|
|
49
|
-
const cell = renderCell('td')
|
|
50
|
-
const headerCell = renderCell('th')
|
|
51
|
-
|
|
52
|
-
const element: ReferenceFactory<ReactNode> = (kind, attrs, children) => {
|
|
53
|
-
const props = { key: kind, ...attrs }
|
|
54
|
-
if (kind === 'button' || kind === 'colgroup') {
|
|
55
|
-
const click = attrs.onClick
|
|
56
|
-
return createElement(
|
|
57
|
-
kind,
|
|
58
|
-
typeof click === 'function'
|
|
59
|
-
? {
|
|
60
|
-
...props,
|
|
61
|
-
onClick: (event: MouseEvent) => click(event.nativeEvent),
|
|
62
|
-
}
|
|
63
|
-
: props,
|
|
64
|
-
children?.({}),
|
|
65
|
-
)
|
|
66
|
-
}
|
|
67
|
-
// Each descriptor supplies the props of its named part. Children receive that part's live state.
|
|
68
|
-
return createElement(elements[kind] as ComponentType<Record<string, unknown>>, {
|
|
69
|
-
...props,
|
|
70
|
-
render: kind === 'cell' ? cell : kind === 'header-cell' ? headerCell : undefined,
|
|
71
|
-
children,
|
|
72
|
-
})
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export type TableProps<Row> = Omit<parts.TableRootProps<Row>, 'children'> &
|
|
76
|
-
TableAttrsProps<Row> & {
|
|
77
|
-
slots?: ReferenceSlots<Row, ReactNode>
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export function Table<Row>({ rowAttrs, cellAttrs, sectionAttrs, slots, ...root }: TableProps<Row>) {
|
|
81
|
-
const options: Record<string, unknown> = {}
|
|
82
|
-
const attrs: Record<string, unknown> = {}
|
|
83
|
-
for (const [name, value] of Object.entries(root)) {
|
|
84
|
-
if (Object.hasOwn(table.options ?? {}, name) || name === 'debug') options[name] = value
|
|
85
|
-
else attrs[name] = value
|
|
86
|
-
}
|
|
87
|
-
const handle = usePrimitive(table, options as TableOptions)
|
|
88
|
-
const [, refresh] = useReducer((n: number) => n + 1, 0)
|
|
89
|
-
const previous = useRef(options)
|
|
90
|
-
useLayoutEffect(() => {
|
|
91
|
-
// Some option changes do not touch primitive state. Render their layout after the SDK commit.
|
|
92
|
-
if (referencePresentationKeys.some((key) => options[key] !== previous.current[key])) refresh()
|
|
93
|
-
previous.current = options
|
|
94
|
-
})
|
|
95
|
-
const structure = useMemo(() => {
|
|
96
|
-
let last: readonly unknown[] = []
|
|
97
|
-
return () => {
|
|
98
|
-
const state = handle.instance.get()
|
|
99
|
-
const next = referenceStructureKeys.map((key) => state[key])
|
|
100
|
-
if (next.some((value, i) => value !== last[i]) || next.length !== last.length) last = next
|
|
101
|
-
return last
|
|
102
|
-
}
|
|
103
|
-
}, [handle.instance])
|
|
104
|
-
useSyncExternalStore(handle.instance.subscribe, structure, structure)
|
|
105
|
-
const view = { rowAttrs, cellAttrs, sectionAttrs, slots } as ReferenceOptions<ReactNode>
|
|
106
|
-
return usePart(
|
|
107
|
-
handle,
|
|
108
|
-
'root',
|
|
109
|
-
{},
|
|
110
|
-
{ ...attrs, children: () => renderReference(handle.api, element, view) },
|
|
111
|
-
'table',
|
|
112
|
-
)
|
|
113
|
-
}
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { usePart, usePrimitive } from '@ui-sdk/react/primitive'
|
|
4
|
+
import {
|
|
5
|
+
createElement,
|
|
6
|
+
useLayoutEffect,
|
|
7
|
+
useMemo,
|
|
8
|
+
useReducer,
|
|
9
|
+
useRef,
|
|
10
|
+
useSyncExternalStore,
|
|
11
|
+
type ComponentType,
|
|
12
|
+
type MouseEvent,
|
|
13
|
+
type ReactNode,
|
|
14
|
+
} from 'react'
|
|
15
|
+
import { table, type TableOptions } from 'ui-sdk-table-core'
|
|
16
|
+
import {
|
|
17
|
+
referenceStructureKeys,
|
|
18
|
+
referencePresentationKeys,
|
|
19
|
+
renderReference,
|
|
20
|
+
type ReferenceFactory,
|
|
21
|
+
type ReferenceOptions,
|
|
22
|
+
type ReferenceSlots,
|
|
23
|
+
type TableAttrsProps,
|
|
24
|
+
} from 'ui-sdk-table-core/reference'
|
|
25
|
+
import * as parts from './index'
|
|
26
|
+
|
|
27
|
+
const elements = {
|
|
28
|
+
head: parts.TableHead,
|
|
29
|
+
body: parts.TableBody,
|
|
30
|
+
foot: parts.TableFoot,
|
|
31
|
+
col: parts.TableCol,
|
|
32
|
+
'header-row': parts.TableHeaderRow,
|
|
33
|
+
'plain-row': parts.TablePlainRow,
|
|
34
|
+
spacer: parts.TableSpacer,
|
|
35
|
+
row: parts.TableRow,
|
|
36
|
+
cell: parts.TableCell,
|
|
37
|
+
'header-cell': parts.TableHeaderCell,
|
|
38
|
+
editor: parts.TableEditor,
|
|
39
|
+
'edit-error': parts.TableEditError,
|
|
40
|
+
'fill-handle': parts.TableFillHandle,
|
|
41
|
+
resizer: parts.TableResizer,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// The primitive emits HTML attribute names. React expects property names for native cell spans.
|
|
45
|
+
const renderCell =
|
|
46
|
+
(tag: 'th' | 'td') =>
|
|
47
|
+
({ colspan, rowspan, ...props }: Record<string, unknown>) =>
|
|
48
|
+
createElement(tag, { ...props, colSpan: colspan, rowSpan: rowspan })
|
|
49
|
+
const cell = renderCell('td')
|
|
50
|
+
const headerCell = renderCell('th')
|
|
51
|
+
|
|
52
|
+
const element: ReferenceFactory<ReactNode> = (kind, attrs, children) => {
|
|
53
|
+
const props = { key: kind, ...attrs }
|
|
54
|
+
if (kind === 'button' || kind === 'colgroup') {
|
|
55
|
+
const click = attrs.onClick
|
|
56
|
+
return createElement(
|
|
57
|
+
kind,
|
|
58
|
+
typeof click === 'function'
|
|
59
|
+
? {
|
|
60
|
+
...props,
|
|
61
|
+
onClick: (event: MouseEvent) => click(event.nativeEvent),
|
|
62
|
+
}
|
|
63
|
+
: props,
|
|
64
|
+
children?.({}),
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
// Each descriptor supplies the props of its named part. Children receive that part's live state.
|
|
68
|
+
return createElement(elements[kind] as ComponentType<Record<string, unknown>>, {
|
|
69
|
+
...props,
|
|
70
|
+
render: kind === 'cell' ? cell : kind === 'header-cell' ? headerCell : undefined,
|
|
71
|
+
children,
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export type TableProps<Row> = Omit<parts.TableRootProps<Row>, 'children'> &
|
|
76
|
+
TableAttrsProps<Row> & {
|
|
77
|
+
slots?: ReferenceSlots<Row, ReactNode>
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function Table<Row>({ rowAttrs, cellAttrs, sectionAttrs, slots, ...root }: TableProps<Row>) {
|
|
81
|
+
const options: Record<string, unknown> = {}
|
|
82
|
+
const attrs: Record<string, unknown> = {}
|
|
83
|
+
for (const [name, value] of Object.entries(root)) {
|
|
84
|
+
if (Object.hasOwn(table.options ?? {}, name) || name === 'debug') options[name] = value
|
|
85
|
+
else attrs[name] = value
|
|
86
|
+
}
|
|
87
|
+
const handle = usePrimitive(table, options as TableOptions)
|
|
88
|
+
const [, refresh] = useReducer((n: number) => n + 1, 0)
|
|
89
|
+
const previous = useRef(options)
|
|
90
|
+
useLayoutEffect(() => {
|
|
91
|
+
// Some option changes do not touch primitive state. Render their layout after the SDK commit.
|
|
92
|
+
if (referencePresentationKeys.some((key) => options[key] !== previous.current[key])) refresh()
|
|
93
|
+
previous.current = options
|
|
94
|
+
})
|
|
95
|
+
const structure = useMemo(() => {
|
|
96
|
+
let last: readonly unknown[] = []
|
|
97
|
+
return () => {
|
|
98
|
+
const state = handle.instance.get()
|
|
99
|
+
const next = referenceStructureKeys.map((key) => state[key])
|
|
100
|
+
if (next.some((value, i) => value !== last[i]) || next.length !== last.length) last = next
|
|
101
|
+
return last
|
|
102
|
+
}
|
|
103
|
+
}, [handle.instance])
|
|
104
|
+
useSyncExternalStore(handle.instance.subscribe, structure, structure)
|
|
105
|
+
const view = { rowAttrs, cellAttrs, sectionAttrs, slots } as ReferenceOptions<ReactNode>
|
|
106
|
+
return usePart(
|
|
107
|
+
handle,
|
|
108
|
+
'root',
|
|
109
|
+
{},
|
|
110
|
+
{ ...attrs, children: () => renderReference(handle.api, element, view) },
|
|
111
|
+
'table',
|
|
112
|
+
)
|
|
113
|
+
}
|