@xeplr/ui-table 1.0.6 → 1.0.8
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/README.md +226 -105
- package/package.json +1 -1
- package/src/XeplrTable.jsx +90 -7
- package/src/columnWindow.js +97 -0
- package/src/xeplr-table.css +11 -0
package/README.md
CHANGED
|
@@ -1,151 +1,272 @@
|
|
|
1
1
|
# @xeplr/ui-table
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**A schema-driven React data table on TanStack Table.** Column types are detected from the data and each gets its own filter; rows sort and paginate; with `onCommit` the table edits, adds and deletes rows — nested children included — and hands back one changeset for a transactional save.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
```jsx
|
|
6
|
+
import { XeplrTable } from '@xeplr/ui-table'
|
|
7
|
+
|
|
8
|
+
<XeplrTable
|
|
9
|
+
data={departments}
|
|
10
|
+
schema={{ 0: { key: 'department', columns: [
|
|
11
|
+
{ accessor: 'name', header: 'Department' },
|
|
12
|
+
{ accessor: 'budget', header: 'Budget', render: { type: 'currency', currency: 'INR' } },
|
|
13
|
+
{ accessor: 'isActive', header: 'Active' }
|
|
14
|
+
] } }}
|
|
15
|
+
onCommit={(changeSet) => api.post('/api/departments/save', changeSet)}
|
|
16
|
+
/>
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The changeset is exactly what [`@xeplr/base-apis`](https://www.npmjs.com/package/@xeplr/base-apis)' `genericRoute` `POST /save` takes.
|
|
20
|
+
|
|
21
|
+
## How the pieces fit
|
|
22
|
+
|
|
23
|
+
| what | where |
|
|
24
|
+
|---|---|
|
|
25
|
+
| The component | `XeplrTable` |
|
|
26
|
+
| Sorting, filtering, pagination, type detection | `useTableController` (TanStack wiring) |
|
|
27
|
+
| Add / edit / delete staging, selection, commit | `useActionsController` |
|
|
28
|
+
| The diff sent to `onCommit` | `buildChangeSet` |
|
|
29
|
+
| Conditional cell styles written as sentences | `resolveCellStyle` |
|
|
30
|
+
| Declarative cell renderers | `renderers` |
|
|
31
|
+
| Style layers → CSS for a host's style panel | `tableStyles` exports |
|
|
32
|
+
| Widths sized to content | `useColumnWidths`, `columnWidths` exports |
|
|
6
33
|
|
|
7
34
|
## Install
|
|
8
35
|
|
|
9
36
|
```sh
|
|
10
|
-
npm i @xeplr/ui-table
|
|
37
|
+
npm i @xeplr/ui-table @tanstack/react-table
|
|
11
38
|
```
|
|
12
39
|
|
|
13
|
-
|
|
40
|
+
Peers: `react` 18 or 19, `@tanstack/react-table` 8. Filter panels use `createPortal` from `react-dom`.
|
|
14
41
|
|
|
15
|
-
|
|
42
|
+
The package ships its `src/` as ES modules with JSX and imports its own stylesheet (`xeplr-table.css`), so it needs a bundler that compiles both — Vite does.
|
|
16
43
|
|
|
17
|
-
|
|
18
|
-
import { XeplrTable } from '@xeplr/ui-table';
|
|
44
|
+
## Props
|
|
19
45
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
46
|
+
| prop | default | |
|
|
47
|
+
|---|---|---|
|
|
48
|
+
| `data` | — | row array. Rows need an `id` for editing, selection and the changeset |
|
|
49
|
+
| `schema` | — | `{ 0: { key, columns }, 1: { key, columns, header? }, … }` — see below |
|
|
50
|
+
| `onCommit` | — | `async (changeSet) => …`. **Omit it and the table is read-only** |
|
|
51
|
+
| `rowActions` | — | custom per-row buttons, replacing the built-in set |
|
|
52
|
+
| `childDisplay` | `'popup'` | `'popup'` or `'inner'` — how level-1 children show |
|
|
53
|
+
| `pageSize` | `20` | initial page size (the page-size menu offers 100, 200, 500, 1000) |
|
|
54
|
+
| `enableSorting`, `enableFiltering`, `enablePagination` | `true` | |
|
|
55
|
+
| `minDetectionRows` | `10` | non-empty samples a column needs before its detected type is final |
|
|
56
|
+
| `sorting`, `onSortingChange` | — | controlled sort (`[{ id, desc }]`) — see Sorting |
|
|
57
|
+
| `onHeaderClick` | — | `(columnKey, isGroupHeader)` — **replaces** sort-on-click |
|
|
58
|
+
| `toolbarLeading` | — | `(ctx) => node` after the built-in toolbar buttons |
|
|
59
|
+
| `toolbarActions` | — | `(ctx) => node` replacing Clear Filters / Clear Sort |
|
|
60
|
+
| `rowClassName` | — | `(row) => string` |
|
|
61
|
+
| `columnClassName` | — | `(column, index) => string`, applied to the header and every cell of that column |
|
|
62
|
+
| `cellStyle` | — | `({ row, rowIndex, columnKey, value }) => style` — on top of the schema's conditional styles |
|
|
63
|
+
| `headerStyle` | — | `({ columnKey, isGroupHeader, depth }) => style` |
|
|
64
|
+
| `cellZoom` | `false` | double-click a cell to show it enlarged. Ignored when the schema has children shown as `'popup'`, where double-click opens the record |
|
|
65
|
+
| `columnSizing` | fill | `'content'` sizes columns to their text |
|
|
66
|
+
| `columnWindowing` | `'auto'` | wide tables draw only the columns in view — see Column windowing. `'on'` / `'off'` force it |
|
|
67
|
+
| `columnText`, `sizingSampleRows` (200), `minColumnWidth` (40), `maxColumnWidth` (420), `columnWidths`, `sizingKey`, `onColumnSizing` | | content sizing — see below |
|
|
68
|
+
| `className` | — | added to the container |
|
|
69
|
+
|
|
70
|
+
Toolbar slots receive `{ rowCount, totalCount, selectedCount, resetFilters, resetSorting, enableFiltering }` — `rowCount` after filtering, `totalCount` before.
|
|
71
|
+
|
|
72
|
+
## Schema
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
{
|
|
76
|
+
0: { key: 'department', columns: [...] }, // the rows
|
|
77
|
+
1: { key: 'employees', header: 'Employees', columns: [...] }, // row.employees
|
|
78
|
+
2: { key: 'salaries', columns: [...] } // each employee's salaries
|
|
79
|
+
}
|
|
31
80
|
```
|
|
32
81
|
|
|
33
|
-
|
|
82
|
+
Level N+1's `key` is the array property on each level-N row. `header` titles a child table (defaults to `key`).
|
|
34
83
|
|
|
35
|
-
|
|
84
|
+
### Columns
|
|
36
85
|
|
|
37
|
-
|
|
86
|
+
| field | |
|
|
87
|
+
|---|---|
|
|
88
|
+
| `accessor` | row property — required on a leaf column |
|
|
89
|
+
| `header` | title; defaults to `accessor` |
|
|
90
|
+
| `dataType` | `'string'`, `'number'`, `'date'` or `'boolean'` — skips detection |
|
|
91
|
+
| `render` | `{ type, …config }`, or the renderer name as a string |
|
|
92
|
+
| `cell` | TanStack cell function `(ctx) => node`; wins over `render` |
|
|
93
|
+
| `cellStyle` | `[{ when, …css }]` or `(value, row) => css` |
|
|
94
|
+
| `enableSorting`, `enableColumnFilter` | default `true` |
|
|
38
95
|
|
|
39
|
-
|
|
40
|
-
|---|---|---|
|
|
41
|
-
| `accessor` | string | required — row property |
|
|
42
|
-
| `header` | string | column title |
|
|
43
|
-
| `dataType` | `'string'\|'number'\|'date'\|'boolean'` | optional — auto-detected from data |
|
|
44
|
-
| `render` | `{ type, ...config }` or string | declarative cell — see renderers |
|
|
45
|
-
| `cell` | `(ctx) => ReactNode` | escape hatch — custom JSX (overrides `render`) |
|
|
46
|
-
| `cellStyle` | array | conditional styles — `[{ when: '$.status is active', backgroundColor, color }]` |
|
|
47
|
-
| `enableSorting` | boolean | default true |
|
|
48
|
-
| `enableColumnFilter` | boolean | default true |
|
|
96
|
+
A column may instead be a **group** — `{ header, id?, columns: [...] }` — rendered as one header cell spanning its children (a pivoted date over its measures). Groups nest; leaves under them sort, filter and render as usual.
|
|
49
97
|
|
|
50
|
-
|
|
98
|
+
An unknown `render` type throws, listing the available ones.
|
|
51
99
|
|
|
52
|
-
|
|
100
|
+
## Column types and filters
|
|
53
101
|
|
|
54
|
-
|
|
55
|
-
|---|---|---|---|
|
|
56
|
-
| `avatarName` | `{ subKey?, size? }` | string | avatar+bold name [+ subtitle from `row[subKey]`] |
|
|
57
|
-
| `twoLine` | `{ subKey? }` | string | primary line [+ subtitle from `row[subKey]`] |
|
|
58
|
-
| `tags` | `{ variant?: 'gold'\|'muted'\|'blue', max? }` | `string[]` or comma-string | pill list, `+N` overflow |
|
|
59
|
-
| `currency` | `{ currency?: 'INR', position?: 'before', decimals?: 0, bold? }` | number | symbol + formatted number |
|
|
60
|
-
| `memberChips` | `{ max?: 3, nameKey?: 'name' }` | `Array<{name,...}>` | avatar+name chips, `+N` overflow |
|
|
61
|
-
| `statusBadge` | `{ map: { value: { bg, color, label? } }, default? }` | string | colored pill |
|
|
62
|
-
| `dateDisplay` | `{ format?: 'date'\|'datetime'\|'relative' }` | Date or ISO string | formatted date |
|
|
63
|
-
| `link` | `{ onClick?(row), hrefKey?, target? }` | string | clickable text or `<a>` |
|
|
102
|
+
Detection samples the first 100 rows, ignoring `null`, `undefined` and `''`. A column is:
|
|
64
103
|
|
|
65
|
-
|
|
104
|
+
1. **boolean** if every value is one of `true false 0 1 '0' '1' 'true' 'false' 'True' 'False' 'TRUE' 'FALSE'`
|
|
105
|
+
2. else **number** if every value converts with `Number()`
|
|
106
|
+
3. else **date** if every value is a non-numeric string that `Date.parse` accepts
|
|
107
|
+
4. else **string**
|
|
66
108
|
|
|
67
|
-
|
|
109
|
+
A column with no samples is a string. Once every column has at least `minDetectionRows` samples, the types are kept for the life of the component.
|
|
68
110
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
111
|
+
| type | filter |
|
|
112
|
+
|---|---|
|
|
113
|
+
| string | searchable checklist of the distinct values, Select All / Clear All, Is Null / Is Not Null |
|
|
114
|
+
| number | equals, not equals, `>`, `>=`, `<`, `<=`, between; Is Null / Is Not Null |
|
|
115
|
+
| date | equals, not equals, after, on or after, before, on or before, between — compared by calendar day |
|
|
116
|
+
| boolean | All / True / False |
|
|
75
117
|
|
|
76
|
-
|
|
118
|
+
Filter panels are portalled to `document.body` so a scrolling table cannot clip them; they close on scroll and resize.
|
|
77
119
|
|
|
78
|
-
##
|
|
120
|
+
## Sorting
|
|
79
121
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
1: { key: 'employees', columns: [...] }, // children of each row
|
|
84
|
-
2: { key: 'tasks', columns: [...] } // grandchildren
|
|
85
|
-
}
|
|
86
|
-
```
|
|
122
|
+
Uncontrolled by default: clicking a header sorts the rows. Pass `sorting` and the table stops sorting itself — header clicks are reported through `onSortingChange` and the rows are shown in the order given. That is for data whose order is not the view's to change: a grouped report's subtotals belong with their group, and a running total describes the order it was computed in.
|
|
123
|
+
|
|
124
|
+
## Editing
|
|
87
125
|
|
|
88
|
-
|
|
126
|
+
With `onCommit`:
|
|
89
127
|
|
|
90
|
-
|
|
91
|
-
-
|
|
92
|
-
-
|
|
128
|
+
- **+ Add New** opens a form; its fields are the keys of the first row (or the schema columns when there are no rows).
|
|
129
|
+
- Each row gets a checkbox, and the Actions column shows **view, copy, edit, delete**. Copy opens Add with the row's values and no id.
|
|
130
|
+
- Selected rows: **Delete Selected (n)**.
|
|
131
|
+
- Changes are **staged** — the table shows them at once — and counted on **Commit (n)**. **Discard** drops them.
|
|
132
|
+
- **Commit** diffs the staged rows against `data` and calls `onCommit(changeSet)`. When it resolves, the stage is cleared; when it rejects, the stage is kept. The table does not update `data` itself — pass the saved rows back in.
|
|
93
133
|
|
|
94
|
-
|
|
134
|
+
Children (a schema with level 1): in `'popup'` mode, double-click or view opens the record with its child tables, which have their own add, edit and delete. In `'inner'` mode an expand arrow shows the child table under the row. Without children, view opens a read-only form.
|
|
95
135
|
|
|
96
|
-
|
|
136
|
+
Without `onCommit` there are no checkboxes, toolbar edit buttons or built-in row actions.
|
|
137
|
+
|
|
138
|
+
### The changeset
|
|
139
|
+
|
|
140
|
+
Sparse, nested by child `key`:
|
|
97
141
|
|
|
98
142
|
```js
|
|
99
143
|
[
|
|
100
|
-
{
|
|
101
|
-
|
|
102
|
-
|
|
144
|
+
{ id: 'd1', name: 'Engineering Dept', // changed fields only
|
|
145
|
+
employees: [
|
|
146
|
+
{ id: '', name: 'New Hire', role: 'Junior' }, // new: every field, no usable id
|
|
147
|
+
{ id: 'e2', role: 'Senior' },
|
|
148
|
+
{ id: 'e3', deleted: true }
|
|
149
|
+
] },
|
|
150
|
+
{ id: '', name: 'New Department' },
|
|
151
|
+
{ id: 'd9', deleted: true }
|
|
103
152
|
]
|
|
104
153
|
```
|
|
105
154
|
|
|
106
|
-
|
|
155
|
+
| staged row | entry |
|
|
156
|
+
|---|---|
|
|
157
|
+
| new (no `id`) | all fields, children included; the internal `_tempId` is stripped |
|
|
158
|
+
| changed | `id` plus fields whose value is not `===` the original, plus a child array if its children changed |
|
|
159
|
+
| removed | `{ id, deleted: true }` |
|
|
160
|
+
| unchanged | omitted |
|
|
107
161
|
|
|
108
|
-
|
|
162
|
+
`buildChangeSet(originalData, stagedData, schema)` is exported.
|
|
109
163
|
|
|
110
|
-
|
|
111
|
-
|---|---|---|
|
|
112
|
-
| `data` | `Array<object>` | required |
|
|
113
|
-
| `schema` | `{ 0: { key, columns }, 1?, 2?, ... }` | required |
|
|
114
|
-
| `childDisplay` | `'popup'\|'inner'` | `'popup'` |
|
|
115
|
-
| `pageSize` | number | 20 |
|
|
116
|
-
| `enableSorting` | boolean | true |
|
|
117
|
-
| `enableFiltering` | boolean | true |
|
|
118
|
-
| `enablePagination` | boolean | true |
|
|
119
|
-
| `onCommit` | `async (changeSet) => void` | — (omit to make table read-only) |
|
|
120
|
-
| `className` | string | — |
|
|
164
|
+
### Row actions
|
|
121
165
|
|
|
122
|
-
|
|
166
|
+
```jsx
|
|
167
|
+
rowActions={[
|
|
168
|
+
{ key: 'rollback', label: 'Rollback', onClick: (row) => rollback(row.id),
|
|
169
|
+
visible: (row) => row.status === 'done', disabled: (row) => row.locked, variant: 'danger', icon: <UndoIcon /> }
|
|
170
|
+
]}
|
|
171
|
+
```
|
|
123
172
|
|
|
124
|
-
|
|
173
|
+
Replaces the built-in buttons. Each fires `onClick(row)` immediately — no staging, and `onCommit` is not needed. Button classes: `xeplr-table-action-<key>`, `xeplr-table-action-<variant>`.
|
|
125
174
|
|
|
126
|
-
##
|
|
175
|
+
## Conditional cell styles
|
|
127
176
|
|
|
177
|
+
```js
|
|
178
|
+
{ accessor: 'status', cellStyle: [
|
|
179
|
+
{ when: '$.status is active', backgroundColor: '#1b5e20', color: '#a5d6a7' },
|
|
180
|
+
{ when: '$.budget < 100000', backgroundColor: '#bf360c' }
|
|
181
|
+
] }
|
|
128
182
|
```
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
183
|
+
|
|
184
|
+
Rules are tried in order; **the first match wins** and its keys other than `when` become the inline style. `$.field` reads any field of the row.
|
|
185
|
+
|
|
186
|
+
| operator | example |
|
|
187
|
+
|---|---|
|
|
188
|
+
| `is`, `is not` | `$.status is active` |
|
|
189
|
+
| `is null`, `is not null` | `$.endDate is null` — `''` counts as null |
|
|
190
|
+
| `>`, `<`, `>=`, `<=` | `$.score >= 90` |
|
|
191
|
+
| `between … and …` | `$.age between 18 and 65` |
|
|
192
|
+
| `in [ … ]`, `not in [ … ]` | `$.region in [north, south]` |
|
|
193
|
+
| `starts with`, `ends with`, `contains`, `does not contain` | `$.name starts with 'Jo'` — case-insensitive |
|
|
194
|
+
|
|
195
|
+
Numeric strings compare as numbers. A host `cellStyle` prop result is merged over this.
|
|
196
|
+
|
|
197
|
+
## Renderers
|
|
198
|
+
|
|
199
|
+
`render: { type, …config }`:
|
|
200
|
+
|
|
201
|
+
| type | config | value |
|
|
202
|
+
|---|---|---|
|
|
203
|
+
| `twoLine` | `subKey` | text, with `row[subKey]` beneath |
|
|
204
|
+
| `avatarName` | `subKey`, `size` (`'md'`), `imgKey`, `imgPrefix` | name with an image (`imgPrefix + row[imgKey]`) or coloured initials |
|
|
205
|
+
| `tags` | `variant` (`'gold'`, `'muted'`, `'blue'`), `max` | `string[]` or a comma-separated string; `+N` overflow |
|
|
206
|
+
| `currency` | `currency` (`'INR'`), `position` (`'before'` / `'after'`), `decimals` (0), `bold` (true) | number |
|
|
207
|
+
| `memberChips` | `max` (3), `nameKey` (`'name'`), `imgKey`, `imgPrefix` | array of objects |
|
|
208
|
+
| `statusBadge` | `map: { value: { bg, color, border?, label? } }`, `default` | text |
|
|
209
|
+
| `dateDisplay` | `format`: `'date'` (`15 Sep 2026`), `'datetime'`, `'relative'` (`3h ago`, dates after 30 days) | Date or ISO string |
|
|
210
|
+
| `link` | `hrefKey` (renders `<a href={row[hrefKey]}>`) and `target`, or `onClick(row)` | text |
|
|
211
|
+
|
|
212
|
+
Each renderer is `(config) => (ctx) => node`. The `renderers` object is the registry the table reads, so `renderers.myType = (config) => (ctx) => …` adds one.
|
|
213
|
+
|
|
214
|
+
## Column windowing
|
|
215
|
+
|
|
216
|
+
Rows are paged; columns are not. So a wide table (a pivot of a date by day over a year: 1,095 columns) built every cell of the page at once, and that froze the browser. With `columnWindowing: 'auto'` (the default), a table with **more than 60 leaf columns** draws only:
|
|
217
|
+
- the columns in view, plus 6 on each side
|
|
218
|
+
- one spacer cell at each edge, holding the width of the rest
|
|
219
|
+
|
|
220
|
+
The scrollbar, the scroll position and the layout are the full table's. Scrolling sideways redraws the window, once per frame. A grouped header (a date over its measures) is cut to the part in view.
|
|
221
|
+
|
|
222
|
+
- **Widths:** the measured ones under `columnSizing: 'content'` (pinned `columnWidths` included); otherwise 140px, or `minColumnWidth` if larger. A windowed table is always fixed-layout at exactly those widths.
|
|
223
|
+
- **Rule:** the arithmetic is in `src/columnWindow.js` (`columnWindow`, `windowHeaders`, `shouldWindowColumns`), pure and tested on its own.
|
|
224
|
+
- **Limitation:** columns off screen are not in the DOM, so the browser's find-in-page does not see them.
|
|
225
|
+
|
|
226
|
+
## Content sizing
|
|
227
|
+
|
|
228
|
+
`columnSizing="content"` measures each column's text (canvas, at the table's font) over the first `sizingSampleRows` rows of `data` — not the current page, so columns do not jump when paging.
|
|
229
|
+
|
|
230
|
+
- Everything fits → each column gets its natural width; leftover space stays empty.
|
|
231
|
+
- It does not fit → no empty space; every column drops to its floor (its longest word) and the remainder is shared in proportion to what each wanted.
|
|
232
|
+
- Not even the floors fit → floors, and the table scrolls horizontally.
|
|
233
|
+
|
|
234
|
+
`columnWidths: { [leafIndex]: percent }` pins columns to a share of the width the columns divide (after checkbox, expand and actions columns). **By index**, because a pivot's column keys are data and change on refresh. `columnText(row, column)` supplies the displayed text when cells are formatted; `sizingKey` forces a re-measure (a font change cannot be observed); `onColumnSizing` receives each column's natural width, floor, assigned width and reason (`natural`, `shrunk`, `floor`, `pinned`).
|
|
235
|
+
|
|
236
|
+
## Styling
|
|
237
|
+
|
|
238
|
+
CSS classes are namespaced `xeplr-table-*` (chrome) and `xeplr-r-*` (renderers). Colours come from `@xeplr/ui-account`'s theme tokens when present (`.xeplr-theme-light` / `.xeplr-theme-dark` on an ancestor), with a dark palette as fallback:
|
|
239
|
+
|
|
240
|
+
`--xeplr-text-primary`, `--xeplr-text-secondary`, `--xeplr-text-muted`, `--xeplr-bg-secondary`, `--xeplr-bg-tertiary`, `--xeplr-bg-hover`, `--xeplr-bg-active`, `--xeplr-bg-overlay`, `--xeplr-border-primary`, `--xeplr-border-secondary`, `--xeplr-accent`, `--xeplr-accent-hover`, `--xeplr-accent-muted`, `--xeplr-accent-text`, `--xeplr-input-bg`, `--xeplr-input-border`, `--xeplr-input-text`, `--xeplr-tag-bg`, `--xeplr-tag-border`, `--xeplr-tag-text`, `--xeplr-success`, `--xeplr-success-bg`, `--xeplr-warning`, `--xeplr-warning-bg`, `--xeplr-danger`, `--xeplr-danger-bg`, `--xeplr-shadow-lg`, `--xeplr-table-group-border`.
|
|
241
|
+
|
|
242
|
+
The `gold` / `blue` tag variants and `link` use fixed colours. The header is not sticky by default; add `position: sticky; top: 0` to `.xeplr-table-th` and the table offsets each grouped header row by the real height of the rows above it.
|
|
243
|
+
|
|
244
|
+
### Style layers for a host panel
|
|
245
|
+
|
|
246
|
+
`tableStyles.js` is the vocabulary a host builds a style editor from — no React:
|
|
247
|
+
|
|
248
|
+
| export | |
|
|
249
|
+
|---|---|
|
|
250
|
+
| `STYLE_GROUPS`, `STYLE_FIELDS`, `styleField(path)` | every stylable property by path: `text.*`, `fill.background`, `box.padding.*`, `box.height`, `border.radius`, `border.{all,top,right,bottom,left}.{width,style,color}` |
|
|
251
|
+
| `flattenStyle(style)` | nested → `{ path: value }`, with `border.all` expanded into the sides **inside its own layer** |
|
|
252
|
+
| `resolveStyle(layers)` | per property, the higher `rank` wins; equal ranks are decided by the later `at` |
|
|
253
|
+
| `toCssProperties(resolved)`, `resolveCellCss(layers)` | → a React style object; a border width without a style becomes `solid` |
|
|
254
|
+
| `cssLength(value)` | `12` → `12px`, `{ value, unit }` → `value+unit`, strings unchanged |
|
|
255
|
+
|
|
256
|
+
There is no margin property: a `<td>` ignores margin.
|
|
148
257
|
|
|
149
258
|
## Exports
|
|
150
259
|
|
|
151
|
-
`XeplrTable`, `useTableController`, `useActionsController`, `TYPES`, `
|
|
260
|
+
`XeplrTable`, `CHILD_DISPLAY`, `useTableController`, `useActionsController`, `detectTypes`, `TYPES`, `StringFilter` / `stringFilterFn`, `NumberFilter` / `numberFilterFn`, `DateFilter` / `dateFilterFn`, `BooleanFilter` / `booleanFilterFn`, `FilterWrapper`, `ActionsCell`, `RecordModal`, `RecordDetail`, `ChildTable`, `CellZoom`, `buildChangeSet`, `resolveOperator`, `resolveString`, `resolveNumber`, `resolveDate`, `isNullish`, `resolveCellStyle`, `STYLE_GROUPS`, `STYLE_FIELDS`, `BORDER_SIDES`, `styleField`, `cssLength`, `flattenStyle`, `resolveStyle`, `toCssProperties`, `resolveCellCss`, `useColumnWidths`, `measureColumnDemand`, `allocateColumnWidths`, `resolvePercentWidths`, `longestToken`, `WIDTH_NATURAL`, `WIDTH_SHRUNK`, `WIDTH_FLOOR`, `renderers`, `avatarName`, `tags`, `currency`, `memberChips`, `twoLine`, `statusBadge`, `dateDisplay`, `link`.
|
|
261
|
+
|
|
262
|
+
## Tests
|
|
263
|
+
|
|
264
|
+
```sh
|
|
265
|
+
npm test
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Plain Node, no browser: changeset building, conditional-style parsing, style-layer resolution, column-width allocation, and the staging logic behind `useActionsController`. A Vite demo lives in `demo/`.
|
|
269
|
+
|
|
270
|
+
## License
|
|
271
|
+
|
|
272
|
+
MIT
|
package/package.json
CHANGED
package/src/XeplrTable.jsx
CHANGED
|
@@ -3,6 +3,7 @@ import { flexRender } from '@tanstack/react-table';
|
|
|
3
3
|
import useTableController from './useTableController.js';
|
|
4
4
|
import useActionsController from './useActionsController.js';
|
|
5
5
|
import useColumnWidths from './useColumnWidths.js';
|
|
6
|
+
import { shouldWindowColumns, columnWindow, windowHeaders, DEFAULT_WINDOW_COLUMN_WIDTH } from './columnWindow.js';
|
|
6
7
|
import CellZoom from './CellZoom.jsx';
|
|
7
8
|
import { TYPES } from './detectTypes.js';
|
|
8
9
|
import StringFilter from './filters/StringFilter.jsx';
|
|
@@ -68,6 +69,12 @@ var CHILD_DISPLAY = { POPUP: 'popup', INNER: 'inner' };
|
|
|
68
69
|
* the container, as a table always has. 'content' instead sizes each column
|
|
69
70
|
* to its text at the current font, and leaves any width it doesn't need
|
|
70
71
|
* blank rather than stretching to fill. See columnWidths.js for the rule.
|
|
72
|
+
* @param {string} [props.columnWindowing] - 'auto' (default): a table
|
|
73
|
+
* with more than 60 columns draws only the ones in view, plus a margin, and
|
|
74
|
+
* holds the rest of the width in a spacer each side — a 1,000-column pivot
|
|
75
|
+
* costs what a 30-column one does. 'on' always, 'off' never. Widths are the
|
|
76
|
+
* measured ones under 'content' sizing (and `columnWidths`), otherwise 140px
|
|
77
|
+
* (or `minColumnWidth`, if larger). See columnWindow.js.
|
|
71
78
|
* @param {Function} [props.columnText] - (row, column) => string.
|
|
72
79
|
* Only used by 'content' sizing, and only worth passing if cells are
|
|
73
80
|
* formatted: measuring 36260.8 when the screen shows ₹36,260.80 sizes the
|
|
@@ -228,6 +235,63 @@ export default function XeplrTable(props) {
|
|
|
228
235
|
});
|
|
229
236
|
var sizing = widths.sizing;
|
|
230
237
|
|
|
238
|
+
// ── Column windowing (columnWindow.js) ──
|
|
239
|
+
// Rows are paged; columns were all drawn, and a pivot by day over a year
|
|
240
|
+
// (1,095 columns × 100 rows) froze the page. Above the threshold only the
|
|
241
|
+
// columns in view are drawn. Hooks run unconditionally; the work is skipped
|
|
242
|
+
// when the table is narrow.
|
|
243
|
+
var leafColumns = table.getVisibleLeafColumns();
|
|
244
|
+
var windowing = shouldWindowColumns(leafColumns.length, props.columnWindowing);
|
|
245
|
+
var fallbackWidth = Math.max(DEFAULT_WINDOW_COLUMN_WIDTH, props.minColumnWidth || 0);
|
|
246
|
+
var leafWidths = windowing
|
|
247
|
+
? leafColumns.map(function(c, i) {
|
|
248
|
+
var measured = sizing && sizing.widths ? sizing.widths[i] : null;
|
|
249
|
+
return measured > 0 ? measured : fallbackWidth;
|
|
250
|
+
})
|
|
251
|
+
: null;
|
|
252
|
+
var [scrollView, setScrollView] = useState({ left: 0, width: 0 });
|
|
253
|
+
useLayoutEffect(function() {
|
|
254
|
+
if (!windowing) return undefined;
|
|
255
|
+
var el = widths.scrollRef.current;
|
|
256
|
+
if (!el) return undefined;
|
|
257
|
+
var frame = 0;
|
|
258
|
+
function read() {
|
|
259
|
+
frame = 0;
|
|
260
|
+
var next = { left: el.scrollLeft, width: el.clientWidth };
|
|
261
|
+
setScrollView(function(prev) { return prev.left === next.left && prev.width === next.width ? prev : next; });
|
|
262
|
+
}
|
|
263
|
+
// One read per frame however fast the scroll events come.
|
|
264
|
+
function onScroll() { if (!frame) frame = requestAnimationFrame(read); }
|
|
265
|
+
read();
|
|
266
|
+
el.addEventListener('scroll', onScroll, { passive: true });
|
|
267
|
+
var observer = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(onScroll);
|
|
268
|
+
if (observer) observer.observe(el);
|
|
269
|
+
return function() {
|
|
270
|
+
el.removeEventListener('scroll', onScroll);
|
|
271
|
+
if (observer) observer.disconnect();
|
|
272
|
+
if (frame) cancelAnimationFrame(frame);
|
|
273
|
+
};
|
|
274
|
+
}, [windowing, widths.scrollRef]);
|
|
275
|
+
var extraLeading = (showExpandCol ? ((sizing && sizing.extraWidths.expand) || 36) : 0) +
|
|
276
|
+
(actions.hasDelete ? ((sizing && sizing.extraWidths.checkbox) || 40) : 0);
|
|
277
|
+
var extraTrailing = hasRowActions ? ((sizing && sizing.extraWidths.actions) || 80) : 0;
|
|
278
|
+
var win = windowing
|
|
279
|
+
? columnWindow({ widths: leafWidths, scrollLeft: scrollView.left, viewportWidth: scrollView.width, leadingWidth: extraLeading })
|
|
280
|
+
: null;
|
|
281
|
+
var windowedTableWidth = windowing
|
|
282
|
+
? extraLeading + extraTrailing + leafWidths.reduce(function(sum, w) { return sum + w; }, 0)
|
|
283
|
+
: 0;
|
|
284
|
+
// Each header row cut to the window — or the row as it is.
|
|
285
|
+
function headersOf(headerGroup) {
|
|
286
|
+
return win
|
|
287
|
+
? windowHeaders(headerGroup.headers, win.start, win.end)
|
|
288
|
+
: headerGroup.headers.map(function(h) { return { header: h, colSpan: h.colSpan }; });
|
|
289
|
+
}
|
|
290
|
+
function cellsOf(row) {
|
|
291
|
+
var cells = row.getVisibleCells();
|
|
292
|
+
return win ? cells.slice(win.start, win.end) : cells;
|
|
293
|
+
}
|
|
294
|
+
|
|
231
295
|
// PER-COLUMN CLASS, resolved by leaf index — the same index `columnWidths`
|
|
232
296
|
// uses, so a host that pins column 3 highlights column 3 without a second
|
|
233
297
|
// way of naming it. Header and body share one answer: a selected column is
|
|
@@ -258,7 +322,9 @@ export default function XeplrTable(props) {
|
|
|
258
322
|
enableFiltering: enableFiltering
|
|
259
323
|
};
|
|
260
324
|
|
|
261
|
-
var totalColumns =
|
|
325
|
+
var totalColumns = win
|
|
326
|
+
? (win.end - win.start) + (win.before > 0 ? 1 : 0) + (win.after > 0 ? 1 : 0) || 1
|
|
327
|
+
: headerGroups[0]?.headers.length || 1;
|
|
262
328
|
if (showExpandCol) totalColumns++;
|
|
263
329
|
if (actions.hasDelete) totalColumns++;
|
|
264
330
|
if (hasRowActions) totalColumns++;
|
|
@@ -359,10 +425,22 @@ export default function XeplrTable(props) {
|
|
|
359
425
|
table edge instead of being stretched across empty space. */}
|
|
360
426
|
<table
|
|
361
427
|
ref={widths.tableRef}
|
|
362
|
-
className={'xeplr-table' + (sizing ? ' xeplr-table-sized' : '') + (props.cellStyle ? ' xeplr-table-styled' : '')}
|
|
363
|
-
style={sizing ? { width: sizing.tableWidth + 'px' } : undefined}
|
|
428
|
+
className={'xeplr-table' + (sizing || win ? ' xeplr-table-sized' : '') + (win ? ' xeplr-table-windowed' : '') + (props.cellStyle ? ' xeplr-table-styled' : '')}
|
|
429
|
+
style={win ? { width: windowedTableWidth + 'px' } : (sizing ? { width: sizing.tableWidth + 'px' } : undefined)}
|
|
364
430
|
>
|
|
365
|
-
{
|
|
431
|
+
{win ? (
|
|
432
|
+
<colgroup>
|
|
433
|
+
{showExpandCol && <col style={{ width: ((sizing && sizing.extraWidths.expand) || 36) + 'px' }} />}
|
|
434
|
+
{actions.hasDelete && <col style={{ width: ((sizing && sizing.extraWidths.checkbox) || 40) + 'px' }} />}
|
|
435
|
+
{win.before > 0 && <col style={{ width: win.before + 'px' }} />}
|
|
436
|
+
{leafWidths.slice(win.start, win.end).map(function(width, i) {
|
|
437
|
+
var leaf = leafColumns[win.start + i];
|
|
438
|
+
return <col key={leaf ? leaf.id : win.start + i} style={{ width: width + 'px' }} />;
|
|
439
|
+
})}
|
|
440
|
+
{win.after > 0 && <col style={{ width: win.after + 'px' }} />}
|
|
441
|
+
{hasRowActions && <col style={{ width: extraTrailing + 'px' }} />}
|
|
442
|
+
</colgroup>
|
|
443
|
+
) : sizing && (
|
|
366
444
|
<colgroup>
|
|
367
445
|
{showExpandCol && <col style={{ width: (sizing.extraWidths.expand || 36) + 'px' }} />}
|
|
368
446
|
{actions.hasDelete && <col style={{ width: (sizing.extraWidths.checkbox || 40) + 'px' }} />}
|
|
@@ -391,7 +469,9 @@ export default function XeplrTable(props) {
|
|
|
391
469
|
onChange={function() { actions.toggleSelectAll(visibleIds); }} />
|
|
392
470
|
</th>
|
|
393
471
|
)}
|
|
394
|
-
{
|
|
472
|
+
{win && win.before > 0 && <th className="xeplr-table-th xeplr-table-col-spacer" aria-hidden="true" style={{ top: stickyTop }} />}
|
|
473
|
+
{headersOf(headerGroup).map(function(item) {
|
|
474
|
+
var header = item.header;
|
|
395
475
|
var canSort = header.column.getCanSort();
|
|
396
476
|
var sorted = header.column.getIsSorted();
|
|
397
477
|
var dataType = header.column.columnDef.meta?.dataType || TYPES.STRING;
|
|
@@ -430,7 +510,7 @@ export default function XeplrTable(props) {
|
|
|
430
510
|
// before. It only widens for a GROUP header (a pivoted
|
|
431
511
|
// date spanning the measures under it), which is what
|
|
432
512
|
// TanStack computed it for.
|
|
433
|
-
<th key={header.id} colSpan={
|
|
513
|
+
<th key={header.id} colSpan={item.colSpan} className={thClass}
|
|
434
514
|
// The host's header style sits UNDER `top`, so a
|
|
435
515
|
// sticky header stays stuck whatever it is styled with.
|
|
436
516
|
style={Object.assign(
|
|
@@ -462,6 +542,7 @@ export default function XeplrTable(props) {
|
|
|
462
542
|
</th>
|
|
463
543
|
);
|
|
464
544
|
})}
|
|
545
|
+
{win && win.after > 0 && <th className="xeplr-table-th xeplr-table-col-spacer" aria-hidden="true" style={{ top: stickyTop }} />}
|
|
465
546
|
{hasRowActions && (
|
|
466
547
|
<th className="xeplr-table-th xeplr-table-th-actions" style={{ top: stickyTop }}>
|
|
467
548
|
<div className="xeplr-table-header-cell">
|
|
@@ -518,7 +599,8 @@ export default function XeplrTable(props) {
|
|
|
518
599
|
onChange={function() { actions.toggleSelect(rowId); }} />
|
|
519
600
|
</td>
|
|
520
601
|
)}
|
|
521
|
-
{
|
|
602
|
+
{win && win.before > 0 && <td className="xeplr-table-td xeplr-table-col-spacer" aria-hidden="true" />}
|
|
603
|
+
{cellsOf(row).map(function(cell) {
|
|
522
604
|
var meta = cell.column.columnDef.meta;
|
|
523
605
|
var cellStyleDef = meta?.cellStyle;
|
|
524
606
|
var condStyle = cellStyleDef
|
|
@@ -568,6 +650,7 @@ export default function XeplrTable(props) {
|
|
|
568
650
|
</td>
|
|
569
651
|
);
|
|
570
652
|
})}
|
|
653
|
+
{win && win.after > 0 && <td className="xeplr-table-td xeplr-table-col-spacer" aria-hidden="true" />}
|
|
571
654
|
{hasRowActions && (
|
|
572
655
|
<td className="xeplr-table-td xeplr-table-td-actions">
|
|
573
656
|
<ActionsCell row={original} hasSave={actions.hasSave} hasDelete={actions.hasDelete}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// COLUMN WINDOWING — which columns of a wide table are worth drawing.
|
|
2
|
+
//
|
|
3
|
+
// Rows are already bounded: the table pages them. Columns were not: a pivot
|
|
4
|
+
// of a date by day over a year is 1,095 columns, and 100 rows of it is over a
|
|
5
|
+
// hundred thousand cells, built all at once, which froze the page. Only the
|
|
6
|
+
// columns in view (plus a margin) are drawn; one spacer cell on each side
|
|
7
|
+
// holds the width of everything left out, so the scrollbar, the scroll
|
|
8
|
+
// position and the layout are exactly what the full table would have.
|
|
9
|
+
//
|
|
10
|
+
// Pure functions — no React, no DOM — so the arithmetic is tested on its own.
|
|
11
|
+
|
|
12
|
+
/** Above this many leaf columns windowing switches on (with `columnWindowing: 'auto'`). */
|
|
13
|
+
export var COLUMN_WINDOW_THRESHOLD = 60;
|
|
14
|
+
|
|
15
|
+
/** Width of a column the table has not measured (no content sizing). */
|
|
16
|
+
export var DEFAULT_WINDOW_COLUMN_WIDTH = 140;
|
|
17
|
+
|
|
18
|
+
/** Columns drawn beyond each edge of the view, so a scroll never shows a blank. */
|
|
19
|
+
export var COLUMN_WINDOW_OVERSCAN = 6;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Whether a table with `leafCount` columns draws only its visible ones.
|
|
23
|
+
* @param {number} leafCount
|
|
24
|
+
* @param {'auto'|'on'|'off'} [mode='auto']
|
|
25
|
+
*/
|
|
26
|
+
export function shouldWindowColumns(leafCount, mode) {
|
|
27
|
+
if (mode === 'off') return false;
|
|
28
|
+
if (mode === 'on') return leafCount > 0;
|
|
29
|
+
return leafCount > COLUMN_WINDOW_THRESHOLD;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The run of columns to draw.
|
|
34
|
+
*
|
|
35
|
+
* @param {object} o
|
|
36
|
+
* @param {number[]} o.widths - every leaf column's width, in order (px)
|
|
37
|
+
* @param {number} o.scrollLeft - the scroll port's scrollLeft
|
|
38
|
+
* @param {number} o.viewportWidth - the scroll port's clientWidth
|
|
39
|
+
* @param {number} [o.leadingWidth=0] - fixed columns before the data ones (expand, checkbox)
|
|
40
|
+
* @param {number} [o.overscan] - columns past each edge
|
|
41
|
+
* @returns {{ start: number, end: number, before: number, after: number }}
|
|
42
|
+
* `end` is exclusive; `before`/`after` are the widths the spacers hold.
|
|
43
|
+
*/
|
|
44
|
+
export function columnWindow(o) {
|
|
45
|
+
var widths = o.widths || [];
|
|
46
|
+
var n = widths.length;
|
|
47
|
+
if (!n) return { start: 0, end: 0, before: 0, after: 0 };
|
|
48
|
+
var overscan = o.overscan === undefined ? COLUMN_WINDOW_OVERSCAN : o.overscan;
|
|
49
|
+
// Before the first measure there is no viewport yet: draw a screenful at
|
|
50
|
+
// the left rather than nothing.
|
|
51
|
+
var viewport = o.viewportWidth > 0 ? o.viewportWidth : 1600;
|
|
52
|
+
var left = Math.max(0, (o.scrollLeft || 0) - (o.leadingWidth || 0));
|
|
53
|
+
var right = left + viewport;
|
|
54
|
+
|
|
55
|
+
var x = 0;
|
|
56
|
+
var first = n - 1;
|
|
57
|
+
var last = n - 1;
|
|
58
|
+
var foundFirst = false;
|
|
59
|
+
for (var i = 0; i < n; i++) {
|
|
60
|
+
var w = widths[i] || 0;
|
|
61
|
+
if (!foundFirst && x + w > left) { first = i; foundFirst = true; }
|
|
62
|
+
if (x >= right) { last = i - 1; break; }
|
|
63
|
+
x += w;
|
|
64
|
+
}
|
|
65
|
+
if (!foundFirst) first = n - 1;
|
|
66
|
+
if (last < first) last = first;
|
|
67
|
+
|
|
68
|
+
var start = Math.max(0, first - overscan);
|
|
69
|
+
var end = Math.min(n, last + 1 + overscan);
|
|
70
|
+
var before = 0;
|
|
71
|
+
var after = 0;
|
|
72
|
+
for (var b = 0; b < start; b++) before += widths[b] || 0;
|
|
73
|
+
for (var a = end; a < n; a++) after += widths[a] || 0;
|
|
74
|
+
return { start: start, end: end, before: before, after: after };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A header row cut to the window: each header that reaches into [start, end)
|
|
79
|
+
* with its colSpan reduced to the part inside. Headers of one row cover the
|
|
80
|
+
* leaf columns contiguously, in order (TanStack pads every row to full width),
|
|
81
|
+
* which is what lets the leaf range of each be found by adding up spans.
|
|
82
|
+
*
|
|
83
|
+
* @param {Array<{ colSpan: number }>} headers
|
|
84
|
+
* @returns {Array<{ header: object, colSpan: number }>}
|
|
85
|
+
*/
|
|
86
|
+
export function windowHeaders(headers, start, end) {
|
|
87
|
+
var out = [];
|
|
88
|
+
var at = 0;
|
|
89
|
+
for (var i = 0; i < headers.length; i++) {
|
|
90
|
+
var span = headers[i].colSpan || 1;
|
|
91
|
+
var from = Math.max(at, start);
|
|
92
|
+
var to = Math.min(at + span, end);
|
|
93
|
+
if (to > from) out.push({ header: headers[i], colSpan: to - from });
|
|
94
|
+
at += span;
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
package/src/xeplr-table.css
CHANGED
|
@@ -1057,3 +1057,14 @@
|
|
|
1057
1057
|
cursor: pointer;
|
|
1058
1058
|
user-select: none;
|
|
1059
1059
|
}
|
|
1060
|
+
|
|
1061
|
+
/* ── Column windowing (columnWindow.js) ──
|
|
1062
|
+
The spacers hold the width of the columns not drawn, so the scrollbar and
|
|
1063
|
+
the layout are the full table's. They must hold width and nothing else. */
|
|
1064
|
+
.xeplr-table-col-spacer,
|
|
1065
|
+
.xeplr-table-th.xeplr-table-col-spacer,
|
|
1066
|
+
.xeplr-table-td.xeplr-table-col-spacer {
|
|
1067
|
+
padding: 0;
|
|
1068
|
+
border-left: none;
|
|
1069
|
+
border-right: none;
|
|
1070
|
+
}
|