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.
Files changed (58) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +8 -2
  3. package/docs/api-react.md +312 -0
  4. package/docs/cell-editing.md +246 -0
  5. package/docs/cells-and-slots.md +127 -0
  6. package/docs/component-body.md +33 -0
  7. package/docs/component-cell.md +31 -0
  8. package/docs/component-col.md +31 -0
  9. package/docs/component-edit-error.md +31 -0
  10. package/docs/component-editor.md +31 -0
  11. package/docs/component-fill-handle.md +31 -0
  12. package/docs/component-foot.md +31 -0
  13. package/docs/component-head.md +31 -0
  14. package/docs/component-header-cell.md +31 -0
  15. package/docs/component-header-row.md +33 -0
  16. package/docs/component-plain-row.md +31 -0
  17. package/docs/component-resizer.md +33 -0
  18. package/docs/component-root.md +36 -0
  19. package/docs/component-row.md +33 -0
  20. package/docs/component-spacer.md +31 -0
  21. package/docs/controller-and-view.md +104 -0
  22. package/docs/examples-agents.md +50 -0
  23. package/docs/examples-column-definitions.md +179 -0
  24. package/docs/examples-formula-writer.md +276 -0
  25. package/docs/examples-styling.md +173 -0
  26. package/docs/examples.md +114 -0
  27. package/docs/excel-formulas.md +115 -0
  28. package/docs/export.md +96 -0
  29. package/docs/fill.md +126 -0
  30. package/docs/first-table.md +150 -0
  31. package/docs/formats.md +176 -0
  32. package/docs/groups-and-totals.md +118 -0
  33. package/docs/history.md +126 -0
  34. package/docs/installation.md +52 -0
  35. package/docs/llms.txt +13 -0
  36. package/docs/pages-and-server-mode.md +246 -0
  37. package/docs/range-copy-and-paste.md +115 -0
  38. package/docs/react.md +85 -0
  39. package/docs/reference-columns.md +179 -0
  40. package/docs/reference-controller.md +137 -0
  41. package/docs/reference-export.md +59 -0
  42. package/docs/reference-formulas.md +42 -0
  43. package/docs/reference-implementation.md +29 -0
  44. package/docs/reference-messages.md +55 -0
  45. package/docs/reference-view.md +116 -0
  46. package/docs/row-numbers-and-letters.md +112 -0
  47. package/docs/rows.md +350 -0
  48. package/docs/selection-and-keyboard.md +151 -0
  49. package/docs/sheet-formulas.md +329 -0
  50. package/docs/sheet-options.md +214 -0
  51. package/docs/skills.md +41 -0
  52. package/docs/sort-filter-and-search.md +260 -0
  53. package/docs/state-and-events.md +90 -0
  54. package/docs/styled-table.md +237 -0
  55. package/docs/table-components.md +105 -0
  56. package/package.json +45 -3
  57. package/src/index.ts +62 -0
  58. package/src/reference.ts +113 -0
@@ -0,0 +1,105 @@
1
+ # Table components
2
+
3
+ Compose a table from native parts. Start with the root, add the header and body, then choose the interactive parts you need.
4
+
5
+ ## Choose a component
6
+
7
+ | What you need | Parts |
8
+ | --- | --- |
9
+ | Supply rows, columns, options and change callbacks | [TableRoot](./component-root.md) |
10
+ | Render column headers | [TableHead](./component-head.md), [TableHeaderRow](./component-header-row.md), [TableHeaderCell](./component-header-cell.md) |
11
+ | Render the visible records and their cells | [TableBody](./component-body.md), [TableRow](./component-row.md), [TableCell](./component-cell.md) |
12
+ | Lay out columns or resize a column | [TableCol](./component-col.md), [TableResizer](./component-resizer.md) |
13
+ | Show a footer or a structural row | [TableFoot](./component-foot.md), [TablePlainRow](./component-plain-row.md) |
14
+ | Reserve space for rows outside the window | [TableSpacer](./component-spacer.md) |
15
+ | Edit a value and show a validation error | [TableEditor](./component-editor.md), [TableEditError](./component-edit-error.md) |
16
+ | Fill cells from a selection | [TableFillHandle](./component-fill-handle.md) |
17
+
18
+ Each component page explains its inputs, the events it handles and the behavior supplied by the primitive. Structural parts do not introduce their own change events. Use the selected framework’s example below to see how the parts fit together.
19
+
20
+ ```tsx title="parts.tsx"
21
+ 'use client'
22
+
23
+ import {
24
+ TableBody,
25
+ TableCell,
26
+ TableHead,
27
+ TableHeaderCell,
28
+ TableHeaderRow,
29
+ TableRoot,
30
+ TableRow,
31
+ } from 'ui-sdk-table-react'
32
+ import type { ColumnDef } from 'ui-sdk-table-core'
33
+
34
+ type Planet = { id: string; name: string; moons: number }
35
+
36
+ const planets: Planet[] = [
37
+ { id: 'mars', name: 'Mars', moons: 2 },
38
+ { id: 'jupiter', name: 'Jupiter', moons: 95 },
39
+ { id: 'saturn', name: 'Saturn', moons: 146 },
40
+ ]
41
+
42
+ const columns: ColumnDef<Planet>[] = [
43
+ { id: 'name', header: 'Planet' },
44
+ { id: 'moons', header: 'Moons' },
45
+ ]
46
+
47
+ export default function Parts() {
48
+ return (
49
+ <>
50
+ <TableRoot rows={planets} columns={columns} aria-label="Planets and their moons">
51
+ <TableHead>
52
+ <TableHeaderRow value="header" index={0}>
53
+ {columns.map((column, col) => (
54
+ <TableHeaderCell key={column.id} value={`h|${column.id}`} row={0} col={col} header>
55
+ {column.header}
56
+ </TableHeaderCell>
57
+ ))}
58
+ </TableHeaderRow>
59
+ </TableHead>
60
+ <TableBody>
61
+ {planets.map((planet, index) => (
62
+ <TableRow key={planet.id} value={planet.id} index={index + 1}>
63
+ <TableCell value={`${planet.id}|name`} row={index + 1} col={0}>
64
+ {planet.name}
65
+ </TableCell>
66
+ <TableCell value={`${planet.id}|moons`} row={index + 1} col={1}>
67
+ {planet.moons}
68
+ </TableCell>
69
+ </TableRow>
70
+ ))}
71
+ </TableBody>
72
+ </TableRoot>
73
+ </>
74
+ )
75
+ }
76
+ ```
77
+
78
+ Click Mars, then press ArrowRight to reach its moon count and ArrowDown to reach Jupiter’s. The parts move focus without an arrow-key handler in the example.
79
+
80
+ ## The four layers
81
+
82
+ 1. **Behaviors** implement individual interactions. For example, `createTableGrid` handles grid focus and `createTableRange` handles a range of cells. These are pieces of the table implementation, not separate imports you need in an app.
83
+ 2. **The primitive**, `table` from `ui-sdk-table-core`, connects those behaviors to the row model and table state. It defines what each element does: which cell can take focus, which role and attributes it exposes, and how it responds to a key. It doesn't render the rows.
84
+ 3. **Parts** bind the primitive to elements you compose. In the example, the app supplies the header, rows, cells and their text. The cell part supplies the focus and keyboard behavior; your CSS can target it with `[data-slot="table-cell"]`.
85
+ 4. **The reference implementation** is an optional composition of those parts. It turns rows and columns into a complete table, including grouped headers, editors and a footer. Read the [reference implementation source code](./reference-implementation.md#source-code) to see where it puts an editor or how it renders a group. You don't need the reference to use the parts.
86
+
87
+ ## Two kinds of slot
88
+
89
+ A **primitive slot** describes an element's job: for example, both a column header and a body cell use `cell`. Supplying **content** is a different job. With parts, you put the content inside the cell yourself. With the reference, you can replace the content of a column while keeping the cell element and its behavior.
90
+
91
+ In React, put JSX children inside `TableCell`, or pass a function as children to read part state. The reference `Table` from `ui-sdk-table-react/reference` accepts render functions: `slots={{ 'col-name': ({ text }) => <strong>{text}</strong> }}` changes just the `name` column. To control selection with React state, pass `selected={selected} onSelectedChange={setSelected}`.
92
+
93
+ ## Build a table from the parts
94
+
95
+ Give the root `rows`, `columns` and an accessible name. Give each row a stable `id` (or supply `getRowId`) and each column an `id`. A row part's `value` is its row id. A cell part's `value` identifies that cell: Mars's moon count stays `mars|moons` even if Mars changes position.
96
+
97
+ **Identity stays fixed; coordinates follow the view.** Here the header row has `value="header"` and `index` 0. The first body row has `index` 1; its moon count has `row` 1 and `col` 1. Set `header` on each column header. If you add sorting, filtering or paging, render the model's visible rows rather than the original array so the coordinates still match what the user sees.
98
+
99
+ Import `TableRoot`, `TableRow` and `TableCell` from `ui-sdk-table-react`. The root infers `Planet` from `planets: Planet[]` and accepts `ColumnDef<Planet>[]`. In a list, `<TableRow key={planet.id} value={planet.id} index={i + 1}>` gives both React and the primitive the row's identity. Use a `<TableBody<Planet>>` child function to render the model's rows. See the [React guide](./react.md) and [React API](./api-react.md).
100
+
101
+ Inspect the example: the root has role `grid` and reports `aria-rowcount` and `aria-colcount`. The header and body cells have roles `columnheader` and `gridcell`. Grid coordinates start at zero in code, but `aria-rowindex` and `aria-colindex` start at one. As you move with the arrow keys, `tabindex="0"` follows the active cell.
102
+
103
+ ## Names
104
+
105
+ The primitive's structural slots are `root`, `head`, `body`, `foot`, `col`, `header-row`, `row`, `plain-row`, `cell` and `spacer`. For editing, filling and resizing it also defines `editor`, `edit-error`, `fill-handle` and `resizer`. These are the names behind the `data-slot` attributes: for example, `[data-slot="table-header-row"]` targets a header row regardless of which renderer created it.
package/package.json CHANGED
@@ -1,6 +1,48 @@
1
1
  {
2
2
  "name": "ui-sdk-table-react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.7",
4
+ "description": "Headless React table parts for ui-sdk-table.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/FallDownTheSystem/ui-sdk-table.git",
9
+ "directory": "packages/react"
10
+ },
11
+ "files": [
12
+ "docs",
13
+ "src",
14
+ "!src/**/*.test.*"
15
+ ],
16
+ "type": "module",
17
+ "sideEffects": false,
18
+ "exports": {
19
+ ".": "./src/index.ts",
20
+ "./reference": "./src/reference.ts"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "dependencies": {
26
+ "ui-sdk-table-core": "0.2.7"
27
+ },
28
+ "devDependencies": {
29
+ "@types/react": "^19.2.10",
30
+ "@types/react-dom": "^19.2.0",
31
+ "@ui-sdk/core": "0.1.0-beta.13",
32
+ "@ui-sdk/react": "0.1.0-beta.13",
33
+ "jsdom": "^31.0.1",
34
+ "react": "^19.2.0",
35
+ "react-dom": "^19.2.0",
36
+ "vite": "npm:@voidzero-dev/vite-plus-core@8.3.8000",
37
+ "vite-plus": "^1.7.0"
38
+ },
39
+ "peerDependencies": {
40
+ "@ui-sdk/core": "0.1.0-beta.13",
41
+ "@ui-sdk/react": "0.1.0-beta.13",
42
+ "react": ">=19",
43
+ "react-dom": ">=19"
44
+ },
45
+ "scripts": {
46
+ "build": "vp pack src/index.ts src/reference.ts --platform neutral"
47
+ }
6
48
  }
package/src/index.ts ADDED
@@ -0,0 +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'
@@ -0,0 +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
+ }