@xeplr/ui-table 1.0.8 → 1.1.1
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 +25 -3
- package/package.json +6 -2
- package/src/XeplrTable.jsx +12 -1
- package/src/tableUrlState.js +127 -0
- package/src/useActionsController.js +88 -52
package/README.md
CHANGED
|
@@ -34,10 +34,10 @@ The changeset is exactly what [`@xeplr/base-apis`](https://www.npmjs.com/package
|
|
|
34
34
|
## Install
|
|
35
35
|
|
|
36
36
|
```sh
|
|
37
|
-
npm i @xeplr/ui-table @tanstack/react-table
|
|
37
|
+
npm i @xeplr/ui-table @tanstack/react-table react-router-dom
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
Peers: `react` 18 or 19, `@tanstack/react-table` 8. Filter panels use `createPortal` from `react-dom`.
|
|
40
|
+
Peers: `react` 18 or 19, `@tanstack/react-table` 8, `react-router-dom` 6 or 7 (the URL state below comes from [`@xeplr/ui-utils`](https://www.npmjs.com/package/@xeplr/ui-utils)' `useUrlState`, which imports it; without a router above the table, everything stays in memory). Filter panels use `createPortal` from `react-dom`.
|
|
41
41
|
|
|
42
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.
|
|
43
43
|
|
|
@@ -52,6 +52,7 @@ The package ships its `src/` as ES modules with JSX and imports its own styleshe
|
|
|
52
52
|
| `childDisplay` | `'popup'` | `'popup'` or `'inner'` — how level-1 children show |
|
|
53
53
|
| `pageSize` | `20` | initial page size (the page-size menu offers 100, 200, 500, 1000) |
|
|
54
54
|
| `enableSorting`, `enableFiltering`, `enablePagination` | `true` | |
|
|
55
|
+
| `showToolbar` | `true` | `false` leaves the toolbar row out (a small table that offers no filtering or sorting). Editing tables keep it for Add/Delete/Commit. |
|
|
55
56
|
| `minDetectionRows` | `10` | non-empty samples a column needs before its detected type is final |
|
|
56
57
|
| `sorting`, `onSortingChange` | — | controlled sort (`[{ id, desc }]`) — see Sorting |
|
|
57
58
|
| `onHeaderClick` | — | `(columnKey, isGroupHeader)` — **replaces** sort-on-click |
|
|
@@ -66,6 +67,7 @@ The package ships its `src/` as ES modules with JSX and imports its own styleshe
|
|
|
66
67
|
| `columnWindowing` | `'auto'` | wide tables draw only the columns in view — see Column windowing. `'on'` / `'off'` force it |
|
|
67
68
|
| `columnText`, `sizingSampleRows` (200), `minColumnWidth` (40), `maxColumnWidth` (420), `columnWidths`, `sizingKey`, `onColumnSizing` | | content sizing — see below |
|
|
68
69
|
| `className` | — | added to the container |
|
|
70
|
+
| `urlKey` | — | prefix for the query keys that keep the open record and modal in the URL — see below. Omit it and they stay in memory |
|
|
69
71
|
|
|
70
72
|
Toolbar slots receive `{ rowCount, totalCount, selectedCount, resetFilters, resetSorting, enableFiltering }` — `rowCount` after filtering, `totalCount` before.
|
|
71
73
|
|
|
@@ -135,6 +137,26 @@ Children (a schema with level 1): in `'popup'` mode, double-click or view opens
|
|
|
135
137
|
|
|
136
138
|
Without `onCommit` there are no checkboxes, toolbar edit buttons or built-in row actions.
|
|
137
139
|
|
|
140
|
+
### Where you are lives in the URL: `urlKey`
|
|
141
|
+
|
|
142
|
+
The record detail popup and the view / edit / add / copy modal are places a person navigates to, so with `urlKey` they are written to the URL's query and a refresh or a shared link lands on them again:
|
|
143
|
+
|
|
144
|
+
| key | holds | example |
|
|
145
|
+
|---|---|---|
|
|
146
|
+
| `<urlKey>Row` | the id of the row open in the detail popup | `?ordersRow=17` |
|
|
147
|
+
| `<urlKey>Edit` | the modal: mode, then the record — for a child record, the root id and the path to it first | `?ordersEdit=edit:17`, `?ordersEdit=add`, `?ordersEdit=view:17/employees/e6` |
|
|
148
|
+
|
|
149
|
+
```jsx
|
|
150
|
+
<XeplrTable urlKey="orders" data={orders} schema={schema} onCommit={save} />
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
- **The key is the host's, never a default.** One page can render several tables, and the host app has its own keys, so the table only writes the URL when given a prefix — give each table on a page its own. **Without `urlKey` both stay in memory**, behaving as before.
|
|
154
|
+
- **Ids, not rows.** The URL holds the row's id (`_tempId` for an unsaved row), and the row is looked up in the current rows on every render, so a link opened before the data has loaded lands once it arrives — and a row that is gone opens nothing. Rows need an `id` for this, as for editing. An unsaved row's `_tempId` does not survive a refresh, because the staged changes do not.
|
|
155
|
+
- **The form being typed is not in the URL.** Reopening a record starts from the row again.
|
|
156
|
+
- **Opening or closing** adds a history entry, so Back closes the popup or modal; switching an open modal (View → Edit) replaces it.
|
|
157
|
+
- Needs a router above the table (`react-router-dom`). The rule is in `src/tableUrlState.js` (`tableUrlKeys`, `encodeModal`, `decodeModal`, `resolvePlace`).
|
|
158
|
+
- `useActionsController({ …, urlKey })` takes the same option when you build your own table around the controller.
|
|
159
|
+
|
|
138
160
|
### The changeset
|
|
139
161
|
|
|
140
162
|
Sparse, nested by child `key`:
|
|
@@ -265,7 +287,7 @@ There is no margin property: a `<td>` ignores margin.
|
|
|
265
287
|
npm test
|
|
266
288
|
```
|
|
267
289
|
|
|
268
|
-
Plain Node, no browser: changeset building, conditional-style parsing, style-layer resolution, column-width allocation,
|
|
290
|
+
Plain Node, no browser: changeset building, conditional-style parsing, style-layer resolution, column-width allocation, the staging logic behind `useActionsController`, and the URL state: the key format (`test/tableUrlState.test.js`) and the controller reading its places back from a real `MemoryRouter`, and every opener, close and save played through in memory, rendered with `react-dom/server` (`test/actionsUrlState.test.js`). A Vite demo lives in `demo/`.
|
|
269
291
|
|
|
270
292
|
## License
|
|
271
293
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xeplr/ui-table",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Controlled TanStack Table wrapper with auto-detected column types and smart filters",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -26,8 +26,12 @@
|
|
|
26
26
|
"publishConfig": {
|
|
27
27
|
"access": "public"
|
|
28
28
|
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@xeplr/ui-utils": "^1.1.2"
|
|
31
|
+
},
|
|
29
32
|
"peerDependencies": {
|
|
30
33
|
"react": "^18.0.0 || ^19.0.0",
|
|
31
|
-
"@tanstack/react-table": "^8.0.0"
|
|
34
|
+
"@tanstack/react-table": "^8.0.0",
|
|
35
|
+
"react-router-dom": "^6.0.0 || ^7.0.0"
|
|
32
36
|
}
|
|
33
37
|
}
|
package/src/XeplrTable.jsx
CHANGED
|
@@ -49,6 +49,10 @@ var CHILD_DISPLAY = { POPUP: 'popup', INNER: 'inner' };
|
|
|
49
49
|
* @param {Function} [props.onSortingChange] - Called with the next sort state.
|
|
50
50
|
* @param {boolean} [props.enableFiltering] - Default: true
|
|
51
51
|
* @param {boolean} [props.enablePagination] - Default: true
|
|
52
|
+
* @param {boolean} [props.showToolbar] - Default: true. False
|
|
53
|
+
* leaves the toolbar row out entirely — for a small table on a dashboard
|
|
54
|
+
* that offers no filtering or sorting, where an empty bar with a Clear Sort
|
|
55
|
+
* button is clutter. Editing (Add/Delete/Commit) keeps it whatever this says.
|
|
52
56
|
* @param {Function} [props.rowClassName] - (row) => string|falsy. Extra
|
|
53
57
|
* CSS class per row, so a host can mark rows that mean something to IT.
|
|
54
58
|
* @param {Function} [props.toolbarActions] - (ctx) => ReactNode.
|
|
@@ -132,6 +136,10 @@ var CHILD_DISPLAY = { POPUP: 'popup', INNER: 'inner' };
|
|
|
132
136
|
* longer belongs to. Resolved by the host from the same vocabulary
|
|
133
137
|
* (tableStyles.js); this component only applies what it's handed.
|
|
134
138
|
* @param {string} [props.className] - Additional CSS class
|
|
139
|
+
* @param {string} [props.urlKey] - Prefix for the query keys
|
|
140
|
+
* that keep the open record detail (`<urlKey>Row`) and record modal
|
|
141
|
+
* (`<urlKey>Edit`) in the URL, so a refresh or shared link reopens them.
|
|
142
|
+
* Omit it and they stay in memory. Give each table on a page its own.
|
|
135
143
|
* @param {Function} [props.onCommit] - async (changeSet[]) => void
|
|
136
144
|
* @param {Array<{key: string, label: string, icon?: any, onClick: (row) => void,
|
|
137
145
|
* visible?: (row) => boolean, disabled?: (row) => boolean, variant?: string}>} [props.rowActions]
|
|
@@ -153,7 +161,8 @@ export default function XeplrTable(props) {
|
|
|
153
161
|
var actions = useActionsController({
|
|
154
162
|
onCommit: props.onCommit,
|
|
155
163
|
schema: schema,
|
|
156
|
-
data: props.data
|
|
164
|
+
data: props.data,
|
|
165
|
+
urlKey: props.urlKey
|
|
157
166
|
});
|
|
158
167
|
|
|
159
168
|
var { table, detectedTypes, resetFilters, resetSorting } = useTableController({
|
|
@@ -358,6 +367,7 @@ export default function XeplrTable(props) {
|
|
|
358
367
|
|
|
359
368
|
return (
|
|
360
369
|
<div className={'xeplr-table-container' + (props.className ? ' ' + props.className : '')}>
|
|
370
|
+
{(props.showToolbar !== false || actions.hasSave || actions.hasDelete || actions.hasPending) && (
|
|
361
371
|
<div className="xeplr-table-toolbar">
|
|
362
372
|
{actions.hasSave && (
|
|
363
373
|
<button type="button" className="xeplr-table-toolbar-btn xeplr-table-toolbar-btn-add" onClick={actions.openAdd}>
|
|
@@ -417,6 +427,7 @@ export default function XeplrTable(props) {
|
|
|
417
427
|
</>
|
|
418
428
|
)}
|
|
419
429
|
</div>
|
|
430
|
+
)}
|
|
420
431
|
|
|
421
432
|
<div className="xeplr-table-scroll" ref={widths.scrollRef}>
|
|
422
433
|
{/* When sized to content the table stops at the width it needs; the
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// WHERE YOU ARE IN A TABLE LIVES IN THE URL — the pure half, no React.
|
|
2
|
+
//
|
|
3
|
+
// The record detail popup and the view / edit / add / copy modal are places a
|
|
4
|
+
// person navigates to, so a refresh or a shared link must land on them again.
|
|
5
|
+
// The URL holds only what NAMES the place — the row's id, the mode, the
|
|
6
|
+
// nesting path — never the row object or a half-typed form: rows are looked
|
|
7
|
+
// up again once they have loaded.
|
|
8
|
+
//
|
|
9
|
+
// Keys come from the host (`urlKey`), because one page can render several
|
|
10
|
+
// tables and a host app has keys of its own. Without one the table keeps
|
|
11
|
+
// these in memory, exactly as before.
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The query keys a table writes, derived from the host's `urlKey`.
|
|
15
|
+
* `null` when there is none: the state then stays in memory.
|
|
16
|
+
*
|
|
17
|
+
* tableUrlKeys('orders') → { row: 'ordersRow', edit: 'ordersEdit' }
|
|
18
|
+
*/
|
|
19
|
+
export function tableUrlKeys(urlKey) {
|
|
20
|
+
if (!urlKey) return null;
|
|
21
|
+
return { row: urlKey + 'Row', edit: urlKey + 'Edit' };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A row's identity, as the table uses it everywhere: `_tempId`, else `id`. */
|
|
25
|
+
export function rowKey(row) {
|
|
26
|
+
if (!row) return null;
|
|
27
|
+
var k = row._tempId || row.id;
|
|
28
|
+
return k === undefined || k === null || k === '' ? null : k;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The row whose id is `key`, or null. Compared as strings, because a key read
|
|
33
|
+
* back from the URL is always a string while a row's id may be a number.
|
|
34
|
+
*/
|
|
35
|
+
export function findRowByKey(rows, key) {
|
|
36
|
+
if (key === null || key === undefined || key === '' || !rows) return null;
|
|
37
|
+
var want = String(key);
|
|
38
|
+
for (var i = 0; i < rows.length; i++) {
|
|
39
|
+
var k = rowKey(rows[i]);
|
|
40
|
+
if (k !== null && String(k) === want) return rows[i];
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
var MODES = { view: true, edit: true, add: true, copy: true };
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A modal place → one query value.
|
|
49
|
+
*
|
|
50
|
+
* { mode: 'edit', id: 5 } → 'edit:5'
|
|
51
|
+
* { mode: 'add' } → 'add'
|
|
52
|
+
* { mode: 'view', rootId: 3, path: ['employees'], id: 'e6' } → 'view:3/employees/e6'
|
|
53
|
+
* { mode: 'add', rootId: 3, path: ['employees'] } → 'add:3/employees'
|
|
54
|
+
*
|
|
55
|
+
* Every segment is URI-encoded, so an id holding '/' or ':' still splits back
|
|
56
|
+
* apart exactly. A nested path alternates field, id, field (odd length), which
|
|
57
|
+
* is what tells a nested place from a parent one when it is read back.
|
|
58
|
+
*/
|
|
59
|
+
export function encodeModal(place) {
|
|
60
|
+
if (!place || !MODES[place.mode]) return null;
|
|
61
|
+
var segs = [];
|
|
62
|
+
if (place.path && place.path.length) {
|
|
63
|
+
segs.push(place.rootId);
|
|
64
|
+
for (var i = 0; i < place.path.length; i++) segs.push(place.path[i]);
|
|
65
|
+
}
|
|
66
|
+
if (place.mode !== 'add') {
|
|
67
|
+
if (place.id === undefined || place.id === null || place.id === '') return null;
|
|
68
|
+
segs.push(place.id);
|
|
69
|
+
}
|
|
70
|
+
if (segs.length === 0) return place.mode;
|
|
71
|
+
return place.mode + ':' + segs.map(function(s) { return encodeURIComponent(String(s)); }).join('/');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The query value back to a modal place, or null when it is not one. */
|
|
75
|
+
export function decodeModal(value) {
|
|
76
|
+
if (!value || typeof value !== 'string') return null;
|
|
77
|
+
var colon = value.indexOf(':');
|
|
78
|
+
var mode = colon === -1 ? value : value.slice(0, colon);
|
|
79
|
+
if (!MODES[mode]) return null;
|
|
80
|
+
var segs = colon === -1 ? [] : value.slice(colon + 1).split('/').map(safeDecode);
|
|
81
|
+
if (segs.some(function(s) { return s === null || s === ''; })) return null;
|
|
82
|
+
|
|
83
|
+
var id = null;
|
|
84
|
+
if (mode !== 'add') {
|
|
85
|
+
if (segs.length === 0) return null;
|
|
86
|
+
id = segs.pop();
|
|
87
|
+
}
|
|
88
|
+
if (segs.length === 0) return { mode: mode, id: id, rootId: null, path: null };
|
|
89
|
+
// rootId + a path of odd length (field, id, field, …)
|
|
90
|
+
if (segs.length < 2 || (segs.length - 1) % 2 !== 1) return null;
|
|
91
|
+
return { mode: mode, id: id, rootId: segs[0], path: segs.slice(1) };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function safeDecode(s) {
|
|
95
|
+
try { return decodeURIComponent(s); } catch (e) { return null; }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* A place in context terms the modal already uses: the root's REAL id (the
|
|
100
|
+
* URL holds a string; the queue matches on the row's own value) and a path
|
|
101
|
+
* whose id segments are the real ids too. Null while the rows it names have
|
|
102
|
+
* not loaded, or once they are gone.
|
|
103
|
+
*
|
|
104
|
+
* Returns { root, rows, context } where `rows` is the array the record lives
|
|
105
|
+
* in (the table's rows for a parent place).
|
|
106
|
+
*/
|
|
107
|
+
export function resolvePlace(rows, place) {
|
|
108
|
+
if (!place) return null;
|
|
109
|
+
if (!place.path) return { rows: rows || [], context: { level: 'parent' } };
|
|
110
|
+
var root = findRowByKey(rows, place.rootId);
|
|
111
|
+
if (!root) return null;
|
|
112
|
+
var realPath = [];
|
|
113
|
+
var current = root;
|
|
114
|
+
for (var i = 0; i < place.path.length; i++) {
|
|
115
|
+
if (i % 2 === 0) {
|
|
116
|
+
realPath.push(place.path[i]);
|
|
117
|
+
current = current ? current[place.path[i]] : null;
|
|
118
|
+
} else {
|
|
119
|
+
var hit = Array.isArray(current) ? findRowByKey(current, place.path[i]) : null;
|
|
120
|
+
if (!hit) return null;
|
|
121
|
+
realPath.push(rowKey(hit));
|
|
122
|
+
current = hit;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (!Array.isArray(current)) current = [];
|
|
126
|
+
return { rows: current, context: { level: 'nested', rootId: rowKey(root), path: realPath } };
|
|
127
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { useState, useCallback, useMemo, useRef } from 'react';
|
|
2
|
+
import { useUrlState } from '@xeplr/ui-utils/src/url/useUrlState.js';
|
|
2
3
|
import { buildChangeSet } from './buildChangeSet.js';
|
|
4
|
+
import { tableUrlKeys, rowKey, findRowByKey, encodeModal, decodeModal, resolvePlace } from './tableUrlState.js';
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Controller for table CRUD actions with transactional queue.
|
|
@@ -13,6 +15,11 @@ import { buildChangeSet } from './buildChangeSet.js';
|
|
|
13
15
|
* @param {Function} [options.onCommit] - async (changeSet[]) => void
|
|
14
16
|
* @param {object} options.schema - { 0: { key, columns }, 1: { key, columns }, ... }
|
|
15
17
|
* @param {Array} options.data - Original data
|
|
18
|
+
* @param {string} [options.urlKey] - Prefix for the query keys that hold
|
|
19
|
+
* the open record detail (`<urlKey>Row`) and modal (`<urlKey>Edit`), so a
|
|
20
|
+
* refresh or a shared link reopens them. Without it they stay in memory —
|
|
21
|
+
* a page can render several tables, and only the host knows which keys are
|
|
22
|
+
* free (see tableUrlState.js).
|
|
16
23
|
*/
|
|
17
24
|
export default function useActionsController(options) {
|
|
18
25
|
var onCommit = options.onCommit || null;
|
|
@@ -133,74 +140,116 @@ export default function useActionsController(options) {
|
|
|
133
140
|
});
|
|
134
141
|
}, []);
|
|
135
142
|
|
|
143
|
+
// ── Where you are: in the URL when the host names the keys ──
|
|
144
|
+
// The detail popup and the modal are places; the query holds the row's id
|
|
145
|
+
// and the modal's mode + path, and both are resolved against the rows on
|
|
146
|
+
// every render — so a link opened before the rows arrive lands once they
|
|
147
|
+
// do. Without `urlKey` the same values live in memory (inUrl: false).
|
|
148
|
+
var urlKeys = tableUrlKeys(options.urlKey);
|
|
149
|
+
var inUrl = !!urlKeys;
|
|
150
|
+
|
|
136
151
|
// ── Detail popup (for childDisplay === 'popup') ──
|
|
137
|
-
var [
|
|
152
|
+
var [detailKey, setDetailKey] = useUrlState(inUrl ? urlKeys.row : 'row', { inUrl: inUrl });
|
|
153
|
+
var detailRow = useMemo(function() {
|
|
154
|
+
return findRowByKey(stagedData, detailKey);
|
|
155
|
+
}, [stagedData, detailKey]);
|
|
138
156
|
|
|
139
157
|
var openDetail = useCallback(function(row) {
|
|
140
|
-
|
|
141
|
-
}, []);
|
|
158
|
+
setDetailKey(rowKey(row));
|
|
159
|
+
}, [setDetailKey]);
|
|
142
160
|
|
|
143
161
|
var closeDetail = useCallback(function() {
|
|
144
|
-
|
|
145
|
-
}, []);
|
|
162
|
+
setDetailKey(null);
|
|
163
|
+
}, [setDetailKey]);
|
|
146
164
|
|
|
147
165
|
// ── Modal ──
|
|
148
|
-
// context: { level,
|
|
149
|
-
|
|
166
|
+
// context: { level, rootId?, path? }
|
|
167
|
+
// The URL names WHICH record in WHICH mode; the form being typed is a
|
|
168
|
+
// draft kept here, tied to the value it was started under, and dropped
|
|
169
|
+
// whenever a modal is opened or closed.
|
|
170
|
+
var [modalValue, setModalValue] = useUrlState(inUrl ? urlKeys.edit : 'edit', { inUrl: inUrl });
|
|
171
|
+
var [draft, setDraft] = useState({ value: null, record: null });
|
|
172
|
+
|
|
173
|
+
var opened = useMemo(function() {
|
|
174
|
+
var place = decodeModal(modalValue);
|
|
175
|
+
var where = resolvePlace(stagedData, place);
|
|
176
|
+
if (!where) return null;
|
|
177
|
+
var record;
|
|
178
|
+
if (place.mode === 'add') {
|
|
179
|
+
var level = where.context.path ? (where.context.path.length + 1) / 2 : 0;
|
|
180
|
+
record = buildEmptyRecord(schema, level, level === 0 ? data : where.rows);
|
|
181
|
+
} else {
|
|
182
|
+
var hit = findRowByKey(where.rows, place.id);
|
|
183
|
+
if (!hit) return null;
|
|
184
|
+
record = Object.assign({}, hit);
|
|
185
|
+
if (place.mode === 'copy') {
|
|
186
|
+
record.id = '';
|
|
187
|
+
delete record._tempId;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return { mode: place.mode === 'copy' ? 'add' : place.mode, record: record, context: where.context };
|
|
191
|
+
}, [modalValue, stagedData, schema, data]);
|
|
192
|
+
|
|
193
|
+
var hasDraft = !!opened && draft.value === modalValue && !!draft.record;
|
|
194
|
+
var modal = opened
|
|
195
|
+
? { mode: opened.mode, record: hasDraft ? draft.record : opened.record, context: opened.context }
|
|
196
|
+
: { mode: null, record: null, context: null };
|
|
197
|
+
|
|
198
|
+
// Opening pushes a history entry, so Back closes it; switching an open
|
|
199
|
+
// modal (View → Edit) replaces the entry instead.
|
|
200
|
+
var openModal = useCallback(function(place) {
|
|
201
|
+
setDraft({ value: null, record: null });
|
|
202
|
+
setModalValue(encodeModal(place), { replace: !!modalValue });
|
|
203
|
+
}, [setModalValue, modalValue]);
|
|
150
204
|
|
|
151
205
|
var openView = useCallback(function(row) {
|
|
152
|
-
|
|
153
|
-
}, []);
|
|
206
|
+
openModal({ mode: 'view', id: rowKey(row) });
|
|
207
|
+
}, [openModal]);
|
|
154
208
|
|
|
155
209
|
var openEdit = useCallback(function(row) {
|
|
156
|
-
|
|
157
|
-
}, []);
|
|
210
|
+
openModal({ mode: 'edit', id: rowKey(row) });
|
|
211
|
+
}, [openModal]);
|
|
158
212
|
|
|
159
213
|
var openCopy = useCallback(function(row) {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
delete copy._tempId;
|
|
163
|
-
setModal({ mode: 'add', record: copy, context: { level: 'parent' } });
|
|
164
|
-
}, []);
|
|
214
|
+
openModal({ mode: 'copy', id: rowKey(row) });
|
|
215
|
+
}, [openModal]);
|
|
165
216
|
|
|
166
217
|
var openAdd = useCallback(function() {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}, [schema, data]);
|
|
218
|
+
openModal({ mode: 'add' });
|
|
219
|
+
}, [openModal]);
|
|
170
220
|
|
|
171
221
|
// Nested modal openers
|
|
172
222
|
var openNestedView = useCallback(function(rootId, path, row) {
|
|
173
|
-
|
|
174
|
-
}, []);
|
|
223
|
+
openModal({ mode: 'view', rootId: rootId, path: path, id: rowKey(row) });
|
|
224
|
+
}, [openModal]);
|
|
175
225
|
|
|
176
226
|
var openNestedEdit = useCallback(function(rootId, path, row) {
|
|
177
|
-
|
|
178
|
-
}, []);
|
|
227
|
+
openModal({ mode: 'edit', rootId: rootId, path: path, id: rowKey(row) });
|
|
228
|
+
}, [openModal]);
|
|
179
229
|
|
|
180
230
|
var openNestedCopy = useCallback(function(rootId, path, row) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
delete copy._tempId;
|
|
184
|
-
setModal({ mode: 'add', record: copy, context: { level: 'nested', rootId: rootId, path: path } });
|
|
185
|
-
}, []);
|
|
231
|
+
openModal({ mode: 'copy', rootId: rootId, path: path, id: rowKey(row) });
|
|
232
|
+
}, [openModal]);
|
|
186
233
|
|
|
187
|
-
var openNestedAdd = useCallback(function(rootId, path
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}, [schema, stagedData]);
|
|
234
|
+
var openNestedAdd = useCallback(function(rootId, path) {
|
|
235
|
+
openModal({ mode: 'add', rootId: rootId, path: path });
|
|
236
|
+
}, [openModal]);
|
|
191
237
|
|
|
192
238
|
var closeModal = useCallback(function() {
|
|
193
|
-
|
|
194
|
-
|
|
239
|
+
setDraft({ value: null, record: null });
|
|
240
|
+
setModalValue(null);
|
|
241
|
+
}, [setModalValue]);
|
|
195
242
|
|
|
243
|
+
var openRecord = opened ? opened.record : null;
|
|
196
244
|
var updateField = useCallback(function(field, value) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
245
|
+
setDraft(function(prev) {
|
|
246
|
+
var base = prev.value === modalValue && prev.record ? prev.record : openRecord;
|
|
247
|
+
if (!base) return prev;
|
|
248
|
+
var updated = Object.assign({}, base);
|
|
200
249
|
updated[field] = value;
|
|
201
|
-
return {
|
|
250
|
+
return { value: modalValue, record: updated };
|
|
202
251
|
});
|
|
203
|
-
}, []);
|
|
252
|
+
}, [modalValue, openRecord]);
|
|
204
253
|
|
|
205
254
|
// ── Queue: save ──
|
|
206
255
|
var handleSave = useCallback(function() {
|
|
@@ -438,16 +487,3 @@ function buildEmptyRecord(schema, schemaLevel, existingRecords) {
|
|
|
438
487
|
empty.id = '';
|
|
439
488
|
return empty;
|
|
440
489
|
}
|
|
441
|
-
|
|
442
|
-
/**
|
|
443
|
-
* Find the nested array for a given rootId and path in stagedData.
|
|
444
|
-
*/
|
|
445
|
-
function findNestedArray(stagedData, rootId, path) {
|
|
446
|
-
for (var i = 0; i < stagedData.length; i++) {
|
|
447
|
-
var rid = stagedData[i]._tempId || stagedData[i].id;
|
|
448
|
-
if (rid === rootId) {
|
|
449
|
-
return navigatePath(stagedData[i], path) || [];
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
return [];
|
|
453
|
-
}
|