@donotdev/ui 0.2.3 → 0.3.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/dist/ChildRowsPicker-63M4EQUQ.js +1 -0
- package/dist/ChildRowsPicker-ISP365ON.js +1 -0
- package/dist/ChildRowsPicker-PQFE6NCR.js +1 -0
- package/dist/ChildRowsPicker-XXFFSMWY.js +1 -0
- package/dist/GoToDialog-XBK5EZF4.js +1 -0
- package/dist/chunk-3CIZFGST.js +1 -0
- package/dist/chunk-LHMKU342.js +1 -0
- package/dist/chunk-P2DHZWPY.js +1 -0
- package/dist/components/layout/components/Breadcrumbs.d.ts.map +1 -1
- package/dist/components/layout/components/Breadcrumbs.js +4 -3
- package/dist/components/layout/components/header/HeaderMenu.d.ts +17 -16
- package/dist/components/layout/components/header/HeaderMenu.d.ts.map +1 -1
- package/dist/components/layout/components/header/HeaderMenu.js +51 -23
- package/dist/components/layout/components/header/SettingsMenu.js +2 -2
- package/dist/crud/components/ChildRowsPicker.d.ts +21 -0
- package/dist/crud/components/ChildRowsPicker.d.ts.map +1 -0
- package/dist/crud/components/ChildRowsPicker.js +68 -0
- package/dist/crud/components/EntityCardList.d.ts.map +1 -1
- package/dist/crud/components/EntityCardList.js +9 -17
- package/dist/crud/components/EntityDisplayRenderer.js +2 -2
- package/dist/crud/components/EntityFormRenderer.d.ts +4 -2
- package/dist/crud/components/EntityFormRenderer.d.ts.map +1 -1
- package/dist/crud/components/EntityFormRenderer.js +137 -55
- package/dist/crud/components/EntityList.d.ts +3 -1
- package/dist/crud/components/EntityList.d.ts.map +1 -1
- package/dist/crud/components/EntityList.js +324 -56
- package/dist/crud/components/EntityRecommendations.js +1 -1
- package/dist/crud/components/EntityWorkflow.d.ts.map +1 -1
- package/dist/crud/components/EntityWorkflow.js +6 -4
- package/dist/crud/components/cellEditFor.d.ts +23 -8
- package/dist/crud/components/cellEditFor.d.ts.map +1 -1
- package/dist/crud/components/cellEditFor.js +40 -20
- package/dist/crud/components/entityParent.d.ts +8 -15
- package/dist/crud/components/entityParent.d.ts.map +1 -1
- package/dist/crud/components/entityParent.js +30 -8
- package/dist/crud/components/useRecordBreadcrumb.d.ts +1 -1
- package/dist/crud/components/useRecordBreadcrumb.d.ts.map +1 -1
- package/dist/crud/components/useRecordBreadcrumb.js +12 -3
- package/dist/crud/crudImports.d.ts +11 -2
- package/dist/crud/crudImports.d.ts.map +1 -1
- package/dist/crud/crudImports.js +10 -1
- package/dist/dndev-next.css +374 -57
- package/dist/dndev.css +376 -57
- package/dist/index.js +4 -4
- package/dist/internal/initializers/NextJsStoresInitializer.d.ts +3 -0
- package/dist/internal/initializers/NextJsStoresInitializer.d.ts.map +1 -1
- package/dist/internal/initializers/NextJsStoresInitializer.js +8 -1
- package/dist/internal/layout/components/AutoMetaTags.d.ts.map +1 -1
- package/dist/internal/layout/components/AutoMetaTags.js +3 -2
- package/dist/internal/layout/components/FontPreloadLinks.d.ts +7 -4
- package/dist/internal/layout/components/FontPreloadLinks.d.ts.map +1 -1
- package/dist/internal/layout/components/FontPreloadLinks.js +12 -25
- package/dist/internal/layout/components/footer/FooterCopyright.d.ts +2 -0
- package/dist/internal/layout/components/footer/FooterCopyright.d.ts.map +1 -1
- package/dist/internal/layout/components/footer/FooterCopyright.js +4 -0
- package/dist/internal/layout/components/footer/useLegalLinks.d.ts +2 -1
- package/dist/internal/layout/components/footer/useLegalLinks.d.ts.map +1 -1
- package/dist/internal/layout/components/footer/useLegalLinks.js +4 -3
- package/dist/internal/providers/UIProviders.d.ts +0 -8
- package/dist/internal/providers/UIProviders.d.ts.map +1 -1
- package/dist/internal/providers/UIProviders.js +39 -1
- package/dist/providers/NextJsAppProviders.d.ts.map +1 -1
- package/dist/providers/NextJsAppProviders.js +1 -2
- package/dist/providers/ViteAppProviders.d.ts.map +1 -1
- package/dist/providers/ViteAppProviders.js +1 -4
- package/dist/routing/404.d.ts +8 -1
- package/dist/routing/404.d.ts.map +1 -1
- package/dist/routing/404.js +6 -2
- package/dist/routing/hooks/hooks.next.js +1 -1
- package/dist/routing/hooks/useRedirectGuard.next.d.ts +3 -1
- package/dist/routing/hooks/useRedirectGuard.next.d.ts.map +1 -1
- package/dist/routing/hooks/useRedirectGuard.next.js +11 -9
- package/dist/routing/parentRoute.d.ts +27 -0
- package/dist/routing/parentRoute.d.ts.map +1 -0
- package/dist/routing/parentRoute.js +43 -0
- package/dist/routing/useNavigation.d.ts.map +1 -1
- package/dist/routing/useNavigation.js +5 -24
- package/dist/routing/useRouteDiscovery.d.ts +2 -2
- package/dist/routing/useRouteDiscovery.d.ts.map +1 -1
- package/dist/routing/useRouteDiscovery.js +6 -5
- package/dist/styles/index.css +373 -56
- package/dist/symbol-index.json +1 -1
- package/guidelines/COMPONENTS.md +1 -1
- package/guidelines/GOTCHAS.md +2 -0
- package/guidelines/LAYOUTS.md +4 -0
- package/guidelines/PAGES.md +23 -3
- package/guidelines/ROUTING.md +26 -1
- package/guidelines/SEO.md +1 -1
- package/package.json +8 -8
|
@@ -16,24 +16,94 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
|
|
|
16
16
|
* **Editing (default):** list fields are edited in their cells (DataTable `editable`),
|
|
17
17
|
* checked rows take one value at once; Save writes every change in one `bulk({ updates })`.
|
|
18
18
|
* Partial success: rows that fail stay pending (still marked) with an error count.
|
|
19
|
-
* `editable={false}`: read-only cells, a row click opens the record.
|
|
19
|
+
* `editable={false}`: read-only cells (no Add, selection or delete), a row click opens the record.
|
|
20
20
|
*
|
|
21
21
|
* **Nested list:** `parent={{ field, value }}` lists one parent's children; the add
|
|
22
22
|
* button opens the create form with the parent prefilled.
|
|
23
23
|
*
|
|
24
|
+
* **Joined to the parent's form:** with `parent`, `editable` (default) and an
|
|
25
|
+
* edit form open on that record, the list is a display of `useChildRows`: no own Save, deletes wait for
|
|
26
|
+
* the form's Save, pending counts + Discard; add opens a picker (link table:
|
|
27
|
+
* one reference besides the parent's) or adds a blank row. `parent.value: null`
|
|
28
|
+
* (parent not created yet) only shows "Create the draft before adding …".
|
|
29
|
+
*
|
|
30
|
+
* **Overrides:** `extraColumns` (app columns from the row with unsaved edits
|
|
31
|
+
* applied), `actions` (app toolbar content), `extend` (1:1 extensions: their
|
|
32
|
+
* columns, edited and saved with the rows; the list is then a display of
|
|
33
|
+
* `useChildRows`).
|
|
34
|
+
*
|
|
35
|
+
* **Read-only** (`editable={false}`): no Add, no row selection, no delete.
|
|
36
|
+
*
|
|
24
37
|
* @version 0.1.0
|
|
25
38
|
* @since 0.0.1
|
|
26
39
|
* @author AMBROISE PARK Consulting
|
|
27
40
|
*/
|
|
28
|
-
import {
|
|
29
|
-
import { useCallback, useState } from 'react';
|
|
41
|
+
import { Search, Undo2 } from 'lucide-react';
|
|
42
|
+
import { Suspense, lazy, useCallback, useState } from 'react';
|
|
30
43
|
import { DataTable, Button, Stack, ActionButton, Section, Input, Alert, Text, } from '@donotdev/components';
|
|
31
|
-
import { useTranslation } from '@donotdev/core';
|
|
44
|
+
import { describeEntity, getEntityByCollection, useTranslation, } from '@donotdev/core';
|
|
32
45
|
import { Link, useNavigate } from '../../routing';
|
|
33
46
|
import { useAuthSafe } from '../../utils/useAuthSafe';
|
|
34
47
|
import { cellEditFor } from './cellEditFor';
|
|
35
48
|
import { assertEntityParent, childCreatePath } from './entityParent';
|
|
36
|
-
import { isCrudModuleAvailable, translateFieldLabel, useCrud, useCrudFilters, useCrudList, useCrudPageSize, EntityFilters, formatValue, useReferenceResolver, } from '../crudImports';
|
|
49
|
+
import { isCrudModuleAvailable, translateFieldLabel, useCrud, useCrudFilters, useCrudList, useCrudPageSize, EntityFilters, formatValue, useReferenceResolver, useChildRows, useJoinedForm, applyFilters, applySearch, } from '../crudImports';
|
|
50
|
+
/** Link table picker: loaded on first use. */
|
|
51
|
+
const ChildRowsPicker = lazy(() => import('./ChildRowsPicker'));
|
|
52
|
+
/**
|
|
53
|
+
* Toolbar grid, by the number of cells after the first (search): refresh, add,
|
|
54
|
+
* app actions. Module-level: no style object per render.
|
|
55
|
+
*/
|
|
56
|
+
const TOOLBAR_STYLES = [0, 1, 2, 3, 4].map((after) => ({
|
|
57
|
+
display: 'grid',
|
|
58
|
+
gridTemplateColumns: ['1fr', ...Array(after).fill('auto')].join(' '),
|
|
59
|
+
}));
|
|
60
|
+
/**
|
|
61
|
+
* Columns of the entity's list fields (the parent column hidden), formatted,
|
|
62
|
+
* edited in their cells when `editing`.
|
|
63
|
+
*/
|
|
64
|
+
function entityColumns(entity, fieldsToShow, options) {
|
|
65
|
+
const { t, viewerRole, referenceData, editing, interactive } = options;
|
|
66
|
+
return fieldsToShow
|
|
67
|
+
.map((fieldName) => {
|
|
68
|
+
const fieldConfig = entity.fields[fieldName];
|
|
69
|
+
if (!fieldConfig)
|
|
70
|
+
return null;
|
|
71
|
+
const label = translateFieldLabel(fieldName, fieldConfig, t);
|
|
72
|
+
const fieldType = fieldConfig.type;
|
|
73
|
+
const isNumeric = fieldType === 'number' || fieldType === 'range';
|
|
74
|
+
const align = isNumeric ? 'end' : 'start';
|
|
75
|
+
return {
|
|
76
|
+
key: fieldName,
|
|
77
|
+
title: label,
|
|
78
|
+
dataIndex: fieldName,
|
|
79
|
+
sortable: interactive,
|
|
80
|
+
filterable: interactive,
|
|
81
|
+
align,
|
|
82
|
+
...(editing && {
|
|
83
|
+
edit: cellEditFor(entity, fieldName, viewerRole, referenceData, t),
|
|
84
|
+
}),
|
|
85
|
+
render: (value, record) => formatValue(value, fieldConfig, t, {
|
|
86
|
+
compact: true,
|
|
87
|
+
referenceData,
|
|
88
|
+
item: record,
|
|
89
|
+
}),
|
|
90
|
+
};
|
|
91
|
+
})
|
|
92
|
+
.filter(Boolean);
|
|
93
|
+
}
|
|
94
|
+
/** App columns (`extraColumns`), rendered from the row with its unsaved edits applied. */
|
|
95
|
+
function appColumns(extraColumns, edited) {
|
|
96
|
+
return (extraColumns ?? []).map((column) => ({
|
|
97
|
+
key: column.key,
|
|
98
|
+
title: column.title,
|
|
99
|
+
align: column.align ?? 'start',
|
|
100
|
+
render: (_value, record) => column.render(edited(record)),
|
|
101
|
+
}));
|
|
102
|
+
}
|
|
103
|
+
/** App toolbar content (`actions`), one toolbar cell. */
|
|
104
|
+
function ToolbarActions({ actions }) {
|
|
105
|
+
return (_jsx(Stack, { direction: "row", gap: "tight", align: "center", children: actions }));
|
|
106
|
+
}
|
|
37
107
|
/**
|
|
38
108
|
* Entity List Component - Table view for admin/internal operations
|
|
39
109
|
*
|
|
@@ -43,14 +113,41 @@ import { isCrudModuleAvailable, translateFieldLabel, useCrud, useCrudFilters, us
|
|
|
43
113
|
* - Excel-like table display with formatted values
|
|
44
114
|
* - Open-link icon per row, bulk select, bulk delete of the checked rows
|
|
45
115
|
* - Auto-routing when handlers not provided
|
|
116
|
+
* - With `parent`, `editable` (default) and an edit form open on that record:
|
|
117
|
+
* joined to the form (`useChildRows`), saved by its Save
|
|
46
118
|
*/
|
|
47
|
-
export function EntityList(
|
|
119
|
+
export function EntityList(props) {
|
|
48
120
|
// Safe guard: isCrudModuleAvailable is a module-level constant (immutable after load).
|
|
49
121
|
if (!isCrudModuleAvailable)
|
|
50
122
|
return null;
|
|
51
|
-
const
|
|
123
|
+
const { entity, parent, preview, tone, editable = true } = props;
|
|
52
124
|
if (parent)
|
|
53
125
|
assertEntityParent(entity, parent);
|
|
126
|
+
// An editable list of child rows joins the edit form open on the parent record
|
|
127
|
+
// (latest opened wins). Read-only lists (`editable={false}`) never join.
|
|
128
|
+
const joinedForm = useJoinedForm(entity, parent);
|
|
129
|
+
if (parent && parent.value === null) {
|
|
130
|
+
return _jsx(ChildRowsCreateFirst, { entity: entity, tone: tone });
|
|
131
|
+
}
|
|
132
|
+
const savedParent = parent && parent.value !== null
|
|
133
|
+
? { field: parent.field, value: parent.value }
|
|
134
|
+
: undefined;
|
|
135
|
+
// With 1:1 extensions, or joined to the parent's form: a display of useChildRows.
|
|
136
|
+
if (!preview && (props.extend || (joinedForm && savedParent && editable))) {
|
|
137
|
+
return _jsx(RowsEntityList, { ...props, parent: savedParent });
|
|
138
|
+
}
|
|
139
|
+
return _jsx(StandaloneEntityList, { ...props, parent: savedParent });
|
|
140
|
+
}
|
|
141
|
+
/** `parent.value: null`: the parent is not created yet, rows cannot be added. */
|
|
142
|
+
function ChildRowsCreateFirst({ entity, tone, }) {
|
|
143
|
+
const { t } = useTranslation([entity.namespace, 'crud']);
|
|
144
|
+
return (_jsx(Section, { tone: tone, children: _jsx(Text, { level: "body", children: t('crud:childRows.createFirst', {
|
|
145
|
+
entity: t('name', { defaultValue: entity.name }),
|
|
146
|
+
}) }) }));
|
|
147
|
+
}
|
|
148
|
+
/** No form open on the parent: own Save, immediate delete, add opens the create page. */
|
|
149
|
+
function StandaloneEntityList({ entity, basePath, onClick, hideFilters = false, pagination = 'auto', pageSize: pageSizeProp, queryOptions, exportable = true, editable = true, parent, preview, tone, extraColumns, actions, }) {
|
|
150
|
+
const isPreview = !!preview;
|
|
54
151
|
const navigate = useNavigate();
|
|
55
152
|
const base = basePath ?? `/${entity.collection}`;
|
|
56
153
|
// Pre-fetch referenced collections for display resolution
|
|
@@ -165,34 +262,20 @@ export function EntityList({ entity, basePath, onClick, hideFilters = false, pag
|
|
|
165
262
|
// Generate columns from entity.listFields or entity.fields.
|
|
166
263
|
// A nested list hides its parent column: every row has the same parent.
|
|
167
264
|
const fieldsToShow = (entity.listFields || Object.keys(entity.fields)).filter((fieldName) => fieldName !== parent?.field);
|
|
168
|
-
const dataColumns = fieldsToShow
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
filterable: !isPreview,
|
|
183
|
-
align,
|
|
184
|
-
...(isEditing && {
|
|
185
|
-
edit: cellEditFor(fieldConfig, viewerRole, referenceData, t),
|
|
186
|
-
}),
|
|
187
|
-
render: (value, record) => formatValue(value, fieldConfig, t, {
|
|
188
|
-
compact: true,
|
|
189
|
-
referenceData,
|
|
190
|
-
item: record,
|
|
191
|
-
}),
|
|
192
|
-
};
|
|
193
|
-
})
|
|
194
|
-
.filter(Boolean);
|
|
195
|
-
const columns = dataColumns;
|
|
265
|
+
const dataColumns = entityColumns(entity, fieldsToShow, {
|
|
266
|
+
t,
|
|
267
|
+
viewerRole,
|
|
268
|
+
referenceData,
|
|
269
|
+
editing: isEditing,
|
|
270
|
+
interactive: !isPreview,
|
|
271
|
+
});
|
|
272
|
+
const columns = [
|
|
273
|
+
...dataColumns,
|
|
274
|
+
...appColumns(extraColumns, (row) => ({
|
|
275
|
+
...row,
|
|
276
|
+
...pendingChanges[row.id],
|
|
277
|
+
})),
|
|
278
|
+
];
|
|
196
279
|
// Entity name for section title
|
|
197
280
|
const entityName = t('name', { defaultValue: entity.name });
|
|
198
281
|
// Result count
|
|
@@ -200,24 +283,20 @@ export function EntityList({ entity, basePath, onClick, hideFilters = false, pag
|
|
|
200
283
|
// Always show table structure - DataTable will show skeleton rows when loading
|
|
201
284
|
return (_jsxs(_Fragment, { children: [!hideFilters && (_jsx(Section, { title: tCrud('filters.title', {
|
|
202
285
|
entity: entityName,
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
? tCrud('results.title.fetching', {
|
|
206
|
-
defaultValue: 'Fetching...',
|
|
207
|
-
})
|
|
286
|
+
}), collapsible: true, defaultOpen: hasActiveFilters, tone: tone, children: _jsx(EntityFilters, { entity: entity, data: rawData, fieldsToFilter: fieldsToShow, viewerRole: viewerRole }) })), _jsx(Section, { title: loading
|
|
287
|
+
? tCrud('results.title.fetching')
|
|
208
288
|
: tCrud('results.title.count', {
|
|
209
289
|
count: resultCount,
|
|
210
|
-
|
|
211
|
-
? 'Found 1 occurrence'
|
|
212
|
-
: `Found ${resultCount} occurrences`,
|
|
213
|
-
}), tone: tone, children: _jsxs(Stack, { children: [_jsxs(Stack, { direction: "row", gap: "tight", align: "center", className: "dndev-w-full", style: { display: 'grid', gridTemplateColumns: '1fr auto auto' }, children: [_jsx(Input, { placeholder: tCrud('actions.searchPlaceholder'), value: searchQuery, onChange: (e) => setSearchQuery(e.target.value), icon: Search, className: "dndev-w-full", disabled: isPreview }), _jsx(Button, { icon: RefreshCw, variant: "outline", onClick: handleRefresh, disabled: isPreview || loading, display: "compact", "aria-label": tCrud('actions.refresh') }), isPreview ? (_jsx(Button, { icon: Plus, display: "compact", disabled: true, children: tCrud('addNew', { defaultValue: 'Add New' }) })) : (_jsx(Button, { icon: Plus, display: "compact", render: ({ children, ...renderProps }) => (_jsx(Link, { path: parent ? childCreatePath(base, parent) : `${base}/new`, ...renderProps, children: children })), children: tCrud('addNew', { defaultValue: 'Add New' }) }))] }), isEditing && pendingCount > 0 && (_jsxs(Stack, { direction: "row", gap: "tight", align: "center", wrap: "wrap", children: [_jsx(Text, { level: "body", children: tCrud('bulkEdit.pending', { count: pendingCount }) }), _jsx(Button, { icon: Save, display: "compact", onClick: handleSave, disabled: saving, children: saving ? tCrud('form.saving') : tCrud('actions.save') }), _jsx(Button, { icon: Undo2, variant: "outline", display: "compact", onClick: () => {
|
|
290
|
+
}), tone: tone, children: _jsxs(Stack, { children: [_jsxs(Stack, { direction: "row", gap: "tight", align: "center", className: "dndev-w-full", style: TOOLBAR_STYLES[1 + (editable ? 1 : 0) + (actions ? 1 : 0)], children: [_jsx(Input, { placeholder: tCrud('actions.searchPlaceholder'), value: searchQuery, onChange: (e) => setSearchQuery(e.target.value), icon: Search, className: "dndev-w-full", disabled: isPreview }), _jsx(Button, { intent: "reload", variant: "outline", onClick: handleRefresh, disabled: isPreview || loading, display: "compact", "aria-label": tCrud('actions.refresh') }), !editable ? null : isPreview ? (_jsx(Button, { intent: "add", display: "compact", disabled: true, children: tCrud('addNew') })) : (_jsx(Button, { intent: "add", display: "compact", render: ({ children, ...renderProps }) => (_jsx(Link, { path: parent ? childCreatePath(base, parent) : `${base}/new`, ...renderProps, children: children })), children: tCrud('addNew') })), actions && _jsx(ToolbarActions, { actions: actions })] }), isEditing && pendingCount > 0 && (_jsxs(Stack, { direction: "row", gap: "tight", align: "center", wrap: "wrap", children: [_jsx(Text, { level: "body", children: tCrud('bulkEdit.pending', { count: pendingCount }) }), _jsx(Button, { intent: "save", display: "compact", onClick: handleSave, disabled: saving, children: saving ? tCrud('form.saving') : tCrud('actions.save') }), _jsx(Button, { icon: Undo2, variant: "outline", display: "compact", onClick: () => {
|
|
214
291
|
setPendingChanges({});
|
|
215
292
|
setFailedCount(0);
|
|
216
|
-
}, disabled: saving, children: tCrud('bulkEdit.discard') })] })), failedCount > 0 && (_jsx(Alert, { variant: "error", title: tCrud('bulkEdit.failed', { count: failedCount }) })), !isPreview && checkedIds.length > 0 && (_jsx(Stack, { direction: "row", gap: "tight", align: "center", wrap: "wrap", children: _jsx(ActionButton, { action: handleDeleteSelected, confirmTitle: tCrud('delete.titleSelected', {
|
|
293
|
+
}, disabled: saving, children: tCrud('bulkEdit.discard') })] })), failedCount > 0 && (_jsx(Alert, { variant: "error", title: tCrud('bulkEdit.failed', { count: failedCount }) })), !isPreview && editable && checkedIds.length > 0 && (_jsx(Stack, { direction: "row", gap: "tight", align: "center", wrap: "wrap", children: _jsx(ActionButton, { action: handleDeleteSelected, confirmTitle: tCrud('delete.titleSelected', {
|
|
217
294
|
count: checkedIds.length,
|
|
218
295
|
}), confirmText: tCrud('delete.confirmSelected', {
|
|
219
296
|
count: checkedIds.length,
|
|
220
|
-
}), confirmLabel: tCrud('actions.delete'), cancelLabel: tCrud('actions.cancel'), variant: "outline",
|
|
297
|
+
}), confirmLabel: tCrud('actions.delete'), cancelLabel: tCrud('actions.cancel'), variant: "outline", intent: "delete", display: "compact", children: tCrud('bulkEdit.deleteSelected', { count: checkedIds.length }) }) })), _jsx(DataTable, { data: filteredData, columns: columns, sortable: !isPreview, searchable: false, pagination: true, loading: loading, onRowClick: isPreview ? undefined : (item) => handleView(item.id),
|
|
298
|
+
// Read-only list (editable={false}): no selection, no delete
|
|
299
|
+
selectable: !isPreview && editable, getRowId: (row) => row.id, onSelect: (rows) => setSelectedIds(rows.map((row) => row.id)), openLabel: tCrud('actions.open'), ...(!isPreview &&
|
|
221
300
|
(onClick
|
|
222
301
|
? { onRowOpen: (row) => onClick(row.id) }
|
|
223
302
|
: { getRowHref: (row) => `${base}/${row.id}` })), ...(isEditing && {
|
|
@@ -232,15 +311,7 @@ export function EntityList({ entity, basePath, onClick, hideFilters = false, pag
|
|
|
232
311
|
},
|
|
233
312
|
}),
|
|
234
313
|
// Pagination labels (translated)
|
|
235
|
-
showingLabel: tCrud('pagination.showing',
|
|
236
|
-
defaultValue: 'Showing {{from}} to {{to}} of {{total}} entries',
|
|
237
|
-
}), paginationPreviousLabel: tCrud('pagination.previous', {
|
|
238
|
-
defaultValue: 'Previous',
|
|
239
|
-
}), paginationNextLabel: tCrud('pagination.next', {
|
|
240
|
-
defaultValue: 'Next',
|
|
241
|
-
}), paginationItemsPerPagePlaceholder: tCrud('pagination.itemsPerPagePlaceholder', {
|
|
242
|
-
defaultValue: 'Items per page',
|
|
243
|
-
}),
|
|
314
|
+
showingLabel: tCrud('pagination.showing'), paginationPreviousLabel: tCrud('pagination.previous'), paginationNextLabel: tCrud('pagination.next'), paginationItemsPerPagePlaceholder: tCrud('pagination.itemsPerPagePlaceholder'),
|
|
244
315
|
// Persisted page size — always controlled via CrudStore
|
|
245
316
|
pageSize: storedPageSize, onPageSizeChange: setStoredPageSize, ...(!isPreview &&
|
|
246
317
|
effectiveMode === 'server' && {
|
|
@@ -249,5 +320,202 @@ export function EntityList({ entity, basePath, onClick, hideFilters = false, pag
|
|
|
249
320
|
onPageChange: setCurrentPage,
|
|
250
321
|
}),
|
|
251
322
|
// Export functionality
|
|
252
|
-
exportable: isPreview ? false : exportable, exportFilename: `${entity.collection}-export`, exportLabel: tCrud('export'
|
|
323
|
+
exportable: isPreview ? false : exportable, exportFilename: `${entity.collection}-export`, exportLabel: tCrud('export') })] }) })] }));
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* The other reference field of a link table (the child has exactly one
|
|
327
|
+
* reference field besides the parent's: descriptor `value.kind === 'ref'`, any
|
|
328
|
+
* field type with `validation.reference`), or null.
|
|
329
|
+
*/
|
|
330
|
+
function linkFieldOf(entity, parentField) {
|
|
331
|
+
const others = Object.entries(describeEntity(entity).fields).filter(([name, { value }]) => name !== parentField && value.kind === 'ref' && value.ref !== undefined);
|
|
332
|
+
return others.length === 1 ? others[0][0] : null;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* The list as a display of `useChildRows`:
|
|
336
|
+
* - **joined** (an edit form open on the parent record, `editable`): edits live
|
|
337
|
+
* in the form's draft and its Save writes them; no own Save, deletes wait for
|
|
338
|
+
* that Save, pending counts + Discard; all lines shown (no filters section,
|
|
339
|
+
* search, export or pagination); Add opens the picker (link table) or adds a
|
|
340
|
+
* blank row, never navigates;
|
|
341
|
+
* - **standalone with `extend`**: filters, search, pagination, export, own Save
|
|
342
|
+
* (rows and extensions in one write; deletes wait for it), Add opens the
|
|
343
|
+
* create page.
|
|
344
|
+
* `editable={false}`: read-only (no Add, no selection, no delete).
|
|
345
|
+
*/
|
|
346
|
+
function RowsEntityList({ entity, basePath, onClick, hideFilters = false, pageSize: pageSizeProp, exportable = true, editable = true, parent, tone, extraColumns, actions, extend, }) {
|
|
347
|
+
const navigate = useNavigate();
|
|
348
|
+
const base = basePath ?? `/${entity.collection}`;
|
|
349
|
+
const viewerRole = useAuthSafe('userRole');
|
|
350
|
+
const rows = useChildRows(entity, parent, {
|
|
351
|
+
viewerRole,
|
|
352
|
+
...(extend && { extend }),
|
|
353
|
+
});
|
|
354
|
+
const joined = rows.joined && editable;
|
|
355
|
+
const referenceData = useReferenceResolver(entity);
|
|
356
|
+
const { pageSize, setPageSize } = useCrudPageSize({
|
|
357
|
+
collection: entity.collection,
|
|
358
|
+
...(pageSizeProp != null && { defaultPageSize: pageSizeProp }),
|
|
359
|
+
});
|
|
360
|
+
const [searchQuery, setSearchQuery] = useState('');
|
|
361
|
+
const { filters } = useCrudFilters({ collection: entity.collection });
|
|
362
|
+
const hasActiveFilters = Object.keys(filters).length > 0;
|
|
363
|
+
const [selectedIds, setSelectedIds] = useState([]);
|
|
364
|
+
const [picking, setPicking] = useState(false);
|
|
365
|
+
const [saving, setSaving] = useState(false);
|
|
366
|
+
/** Why the last own Save did not write (invalid rows count, or the write error). */
|
|
367
|
+
const [saveError, setSaveError] = useState(null);
|
|
368
|
+
const { t: tCrud } = useTranslation('crud');
|
|
369
|
+
const { t } = useTranslation([entity.namespace, 'crud']);
|
|
370
|
+
// Link table (joined): add picks records of the other side (already linked excluded).
|
|
371
|
+
const linkField = parent && joined ? linkFieldOf(entity, parent.field) : null;
|
|
372
|
+
const linkCollection = linkField
|
|
373
|
+
? entity.fields[linkField]?.validation?.reference
|
|
374
|
+
: undefined;
|
|
375
|
+
const linkEntity = typeof linkCollection === 'string'
|
|
376
|
+
? getEntityByCollection(linkCollection)
|
|
377
|
+
: undefined;
|
|
378
|
+
if (linkCollection && !linkEntity) {
|
|
379
|
+
throw new Error(`EntityList: no entity defines collection "${String(linkCollection)}" (the other side of link table "${entity.collection}"). Define it with defineEntity().`);
|
|
380
|
+
}
|
|
381
|
+
const linkedIds = new Set(linkField
|
|
382
|
+
? rows.items.flatMap((row) => {
|
|
383
|
+
const linked = row[linkField];
|
|
384
|
+
return typeof linked === 'string' ? [linked] : [];
|
|
385
|
+
})
|
|
386
|
+
: []);
|
|
387
|
+
// Joined: every line shown. Standalone: filters and search on the rows as
|
|
388
|
+
// edited. The table shows them without their cell changes (overlaid as pending).
|
|
389
|
+
const baseById = new Map(rows.base.map((row) => [row.id, row]));
|
|
390
|
+
const addedIds = new Set(rows.edits.added.map((row) => row.id));
|
|
391
|
+
const shownIds = joined
|
|
392
|
+
? null
|
|
393
|
+
: new Set(applySearch(applyFilters(rows.items, filters, entity), searchQuery, entity).map((row) => row.id));
|
|
394
|
+
const data = shownIds ? rows.base.filter((row) => shownIds.has(row.id)) : rows.base;
|
|
395
|
+
const checkedIds = selectedIds.filter((id) => baseById.has(id));
|
|
396
|
+
const isEditing = editable &&
|
|
397
|
+
(rows.access.update || rows.extensionFields.some((field) => field.editable));
|
|
398
|
+
// Table edits (cells, paste, bulk apply, undo) → row values.
|
|
399
|
+
const applyPending = (next) => {
|
|
400
|
+
const previous = rows.edits.changes;
|
|
401
|
+
for (const id of new Set([...Object.keys(previous), ...Object.keys(next)])) {
|
|
402
|
+
const row = baseById.get(id);
|
|
403
|
+
if (!row)
|
|
404
|
+
continue;
|
|
405
|
+
const before = previous[id] ?? {};
|
|
406
|
+
const after = next[id] ?? {};
|
|
407
|
+
const patch = {};
|
|
408
|
+
for (const field of new Set([...Object.keys(before), ...Object.keys(after)])) {
|
|
409
|
+
if (field in after) {
|
|
410
|
+
if (!(field in before) || before[field] !== after[field]) {
|
|
411
|
+
patch[field] = after[field];
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
patch[field] = row[field];
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (Object.keys(patch).length > 0)
|
|
419
|
+
rows.update(id, patch);
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
const open = (id) => {
|
|
423
|
+
if (addedIds.has(id))
|
|
424
|
+
return; // not saved yet: no record to open
|
|
425
|
+
if (onClick)
|
|
426
|
+
onClick(id);
|
|
427
|
+
else
|
|
428
|
+
navigate(`${base}/${id}`);
|
|
429
|
+
};
|
|
430
|
+
const save = async () => {
|
|
431
|
+
setSaving(true);
|
|
432
|
+
setSaveError(null);
|
|
433
|
+
try {
|
|
434
|
+
await rows.save();
|
|
435
|
+
}
|
|
436
|
+
catch (error) {
|
|
437
|
+
// Not written: every edit stays pending. Invalid rows: the count (cells show each field).
|
|
438
|
+
setSaveError(error instanceof Error ? error.message : tCrud('form.saveFailed'));
|
|
439
|
+
}
|
|
440
|
+
finally {
|
|
441
|
+
setSaving(false);
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
const fieldsToShow = (entity.listFields || Object.keys(entity.fields)).filter((fieldName) => fieldName !== parent?.field);
|
|
445
|
+
// Extension columns: each extension entity's list fields the viewer may see, prefixed.
|
|
446
|
+
const extensionColumns = rows.extensionFields
|
|
447
|
+
.filter((field) => {
|
|
448
|
+
const ext = field.scope ? extend?.[field.scope] : undefined;
|
|
449
|
+
const unprefixed = field.name.slice((field.scope ?? '').length + 1);
|
|
450
|
+
return !ext?.entity.listFields || ext.entity.listFields.includes(unprefixed);
|
|
451
|
+
})
|
|
452
|
+
.map((field) => {
|
|
453
|
+
const { scope } = field;
|
|
454
|
+
const ext = scope ? extend?.[scope] : undefined;
|
|
455
|
+
if (!scope || !ext) {
|
|
456
|
+
throw new Error(`EntityList: extension field "${field.name}" has no extension "${scope}".`);
|
|
457
|
+
}
|
|
458
|
+
const unprefixed = field.name.slice(scope.length + 1);
|
|
459
|
+
const isNumeric = field.config.type === 'number' || field.config.type === 'range';
|
|
460
|
+
return {
|
|
461
|
+
key: field.name,
|
|
462
|
+
title: translateFieldLabel(field.name, field.config, t),
|
|
463
|
+
dataIndex: field.name,
|
|
464
|
+
sortable: true,
|
|
465
|
+
filterable: !joined,
|
|
466
|
+
align: isNumeric ? 'end' : 'start',
|
|
467
|
+
...(isEditing &&
|
|
468
|
+
field.editable && {
|
|
469
|
+
edit: cellEditFor(ext.entity, unprefixed, viewerRole, referenceData, t),
|
|
470
|
+
}),
|
|
471
|
+
render: (value, record) => formatValue(value, field.config, t, {
|
|
472
|
+
compact: true,
|
|
473
|
+
referenceData,
|
|
474
|
+
item: record,
|
|
475
|
+
}),
|
|
476
|
+
};
|
|
477
|
+
});
|
|
478
|
+
const columns = [
|
|
479
|
+
...entityColumns(entity, fieldsToShow, {
|
|
480
|
+
t,
|
|
481
|
+
viewerRole,
|
|
482
|
+
referenceData,
|
|
483
|
+
editing: isEditing && rows.access.update,
|
|
484
|
+
interactive: true,
|
|
485
|
+
}),
|
|
486
|
+
...extensionColumns,
|
|
487
|
+
...appColumns(extraColumns, (row) => ({
|
|
488
|
+
...row,
|
|
489
|
+
...rows.edits.changes[row.id],
|
|
490
|
+
})),
|
|
491
|
+
];
|
|
492
|
+
const entityName = t('name', { defaultValue: entity.name });
|
|
493
|
+
const showAdd = editable && rows.access.create;
|
|
494
|
+
const toolbarCells = 1 + (showAdd ? 1 : 0) + (actions ? 1 : 0);
|
|
495
|
+
return (_jsxs(_Fragment, { children: [!joined && !hideFilters && (_jsx(Section, { title: tCrud('filters.title', { entity: entityName }), collapsible: true, defaultOpen: hasActiveFilters, tone: tone, children: _jsx(EntityFilters, { entity: entity, data: rows.items, fieldsToFilter: fieldsToShow, viewerRole: viewerRole }) })), _jsx(Section, { title: rows.loading
|
|
496
|
+
? tCrud('results.title.fetching')
|
|
497
|
+
: tCrud('results.title.count', { count: data.length }), tone: tone, children: _jsxs(Stack, { children: [_jsxs(Stack, { direction: "row", gap: "tight", align: "center", className: "dndev-w-full", style: TOOLBAR_STYLES[joined ? toolbarCells - 1 : toolbarCells], children: [!joined && (_jsx(Input, { placeholder: tCrud('actions.searchPlaceholder'), value: searchQuery, onChange: (e) => setSearchQuery(e.target.value), icon: Search, className: "dndev-w-full" })), _jsx(Button, { intent: "reload", variant: "outline", onClick: () => void rows.refresh(), disabled: rows.loading, display: "compact", "aria-label": tCrud('actions.refresh') }), showAdd &&
|
|
498
|
+
(joined ? (
|
|
499
|
+
// Joined: rows are added in place (picker or blank row), never a navigation.
|
|
500
|
+
_jsx(Button, { type: "button", intent: "add", display: "compact", onClick: () => (linkField ? setPicking(true) : rows.add([{}])), disabled: rows.loading, children: tCrud('addNew') })) : (_jsx(Button, { intent: "add", display: "compact", render: ({ children, ...renderProps }) => (_jsx(Link, { path: parent ? childCreatePath(base, parent) : `${base}/new`, ...renderProps, children: children })), children: tCrud('addNew') }))), actions && _jsx(ToolbarActions, { actions: actions })] }), rows.pendingCount > 0 && (_jsxs(Stack, { direction: "row", gap: "tight", align: "center", wrap: "wrap", children: [_jsx(Text, { level: "body", children: tCrud('bulkEdit.pending', { count: rows.pendingCount }) }), !joined && (_jsx(Button, { type: "button", intent: "save", display: "compact", onClick: () => void save(), disabled: saving, children: saving ? tCrud('form.saving') : tCrud('actions.save') })), _jsx(Button, { type: "button", icon: Undo2, variant: "outline", display: "compact", onClick: () => {
|
|
501
|
+
rows.discard();
|
|
502
|
+
setSaveError(null);
|
|
503
|
+
}, disabled: saving, children: tCrud('bulkEdit.discard') })] })), saveError && _jsx(Alert, { variant: "error", title: saveError }), editable &&
|
|
504
|
+
checkedIds.length > 0 &&
|
|
505
|
+
(rows.access.delete || checkedIds.every((id) => addedIds.has(id))) && (_jsx(Stack, { direction: "row", gap: "tight", align: "center", wrap: "wrap", children: _jsx(Button, { type: "button", variant: "outline", intent: "delete", display: "compact", onClick: () => {
|
|
506
|
+
rows.remove(checkedIds);
|
|
507
|
+
setSelectedIds([]);
|
|
508
|
+
}, children: tCrud('bulkEdit.deleteSelected', { count: checkedIds.length }) }) })), _jsx(DataTable, { data: data, columns: columns, sortable: true, searchable: false, pagination: !joined, loading: rows.loading, onRowClick: (row) => open(row.id), selectable: editable, getRowId: (row) => row.id, onSelect: (selected) => setSelectedIds(selected.map((row) => row.id)), openLabel: tCrud('actions.open'), onRowOpen: (row) => open(row.id),
|
|
509
|
+
// Fields breaking the entity's rules, checked on every change
|
|
510
|
+
cellErrors: rows.errors, ...(isEditing && {
|
|
511
|
+
editable: true,
|
|
512
|
+
pendingChanges: rows.edits.changes,
|
|
513
|
+
onPendingChange: applyPending,
|
|
514
|
+
editLabels: {
|
|
515
|
+
selected: tCrud('bulkEdit.selected'),
|
|
516
|
+
field: tCrud('bulkEdit.field'),
|
|
517
|
+
value: tCrud('bulkEdit.value'),
|
|
518
|
+
apply: tCrud('bulkEdit.apply'),
|
|
519
|
+
},
|
|
520
|
+
}), showingLabel: tCrud('pagination.showing'), paginationPreviousLabel: tCrud('pagination.previous'), paginationNextLabel: tCrud('pagination.next'), paginationItemsPerPagePlaceholder: tCrud('pagination.itemsPerPagePlaceholder'), pageSize: pageSize, onPageSizeChange: setPageSize, exportable: !joined && exportable, exportFilename: `${entity.collection}-export`, exportLabel: tCrud('export') })] }) }), picking && linkField && linkEntity && (_jsx(Suspense, { fallback: null, children: _jsx(ChildRowsPicker, { entity: linkEntity, viewerRole: viewerRole, excludedIds: linkedIds, onPick: (ids) => rows.add(ids.map((id) => ({ [linkField]: id }))), onClose: () => setPicking(false) }) }))] }));
|
|
253
521
|
}
|
|
@@ -68,7 +68,7 @@ export function EntityRecommendations({ entity, queryOptions, title, basePath, c
|
|
|
68
68
|
return null;
|
|
69
69
|
}
|
|
70
70
|
const resolvedBasePath = basePath ?? `/${entity.collection}`;
|
|
71
|
-
const resolvedTitle = title ?? t('recommendations.title'
|
|
71
|
+
const resolvedTitle = title ?? t('recommendations.title');
|
|
72
72
|
const bpIndex = {
|
|
73
73
|
mobile: 0,
|
|
74
74
|
tablet: 1,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EntityWorkflow.d.ts","sourceRoot":"","sources":["../../../src/crud/components/EntityWorkflow.tsx"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAIvC,OAAO,KAAK,EACV,cAAc,EAEd,oBAAoB,EACrB,MAAM,gBAAgB,CAAC;AAKxB,+BAA+B;AAC/B,MAAM,WAAW,mBAAmB;IAClC,mDAAmD;IACnD,QAAQ,EAAE,cAAc,CAAC;IACzB,4CAA4C;IAC5C,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACxE,iCAAiC;IACjC,YAAY,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5D,8CAA8C;IAC9C,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACxC,qCAAqC;IACrC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8DAA8D;IAC9D,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,oBAAoB,KAAK,SAAS,CAAC;IACnD,2BAA2B;IAC3B,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,EAC7B,QAAQ,EAAE,MAAM,EAChB,UAAU,EACV,YAAY,EACZ,aAAa,EACb,UAAU,EACV,QAAQ,EACR,SAAS,GACV,EAAE,mBAAmB,
|
|
1
|
+
{"version":3,"file":"EntityWorkflow.d.ts","sourceRoot":"","sources":["../../../src/crud/components/EntityWorkflow.tsx"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAIvC,OAAO,KAAK,EACV,cAAc,EAEd,oBAAoB,EACrB,MAAM,gBAAgB,CAAC;AAKxB,+BAA+B;AAC/B,MAAM,WAAW,mBAAmB;IAClC,mDAAmD;IACnD,QAAQ,EAAE,cAAc,CAAC;IACzB,4CAA4C;IAC5C,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACxE,iCAAiC;IACjC,YAAY,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5D,8CAA8C;IAC9C,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACxC,qCAAqC;IACrC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8DAA8D;IAC9D,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,oBAAoB,KAAK,SAAS,CAAC;IACnD,2BAA2B;IAC3B,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,EAC7B,QAAQ,EAAE,MAAM,EAChB,UAAU,EACV,YAAY,EACZ,aAAa,EACb,UAAU,EACV,QAAQ,EACR,SAAS,GACV,EAAE,mBAAmB,sCA6FrB"}
|
|
@@ -45,9 +45,11 @@ export function EntityWorkflow({ workflow: config, onComplete, onStepChange, def
|
|
|
45
45
|
...(!isLast && { status: 'draft' }),
|
|
46
46
|
}, onSubmit: async (data) => {
|
|
47
47
|
await wf.goNext(data);
|
|
48
|
-
},
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
},
|
|
49
|
+
// Steps advance in place; the caller's onComplete decides where to go.
|
|
50
|
+
successPath: null, submitText: isLast
|
|
51
|
+
? t('workflow.submit')
|
|
52
|
+
: t('workflow.next'), cancelText: isFirst
|
|
51
53
|
? null
|
|
52
|
-
: t('workflow.previous'
|
|
54
|
+
: t('workflow.previous'), onCancel: isFirst ? undefined : wf.goPrevious, loading: isSubmitting, hideVisibilityInfo: true })) : null, currentStep.allowSkip && !isLast && (_jsx(Button, { variant: "ghost", onClick: wf.skipStep, className: "dndev-w-full", children: t('workflow.skip') }))] }));
|
|
53
55
|
}
|
|
@@ -1,28 +1,43 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Cell editor of an entity field
|
|
3
3
|
* @description Maps an entity field to the DataTable cell editor used by an
|
|
4
|
-
* `editable` EntityList.
|
|
4
|
+
* `editable` EntityList. Editability and references come from the entity
|
|
5
|
+
* descriptor (`describeEntity`), the same rules as forms.
|
|
5
6
|
*
|
|
6
|
-
* @version 0.
|
|
7
|
+
* @version 0.2.0
|
|
7
8
|
* @since 0.2.3
|
|
8
9
|
* @author AMBROISE PARK Consulting
|
|
9
10
|
*/
|
|
10
11
|
import type { TableCellEdit } from '@donotdev/components';
|
|
11
|
-
import type {
|
|
12
|
+
import type { AnyEntity } from '@donotdev/core';
|
|
12
13
|
type Translate = (key: string, options?: Record<string, unknown>) => string;
|
|
13
14
|
/**
|
|
14
15
|
* Cell editor of a list field, or `undefined` when the field is not edited in
|
|
15
16
|
* the table (type without a cell editor, or not editable by the viewer).
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
17
|
+
* Editability is the form's (`editableFor(viewerRole, 'edit')`): generated,
|
|
18
|
+
* computed, `editable: false`, create-only, role-denied and technical fields stay
|
|
19
|
+
* read-only. References (any field type with `validation.reference`, descriptor
|
|
20
|
+
* `value.kind === 'ref'`) list the referenced records by their resolved label
|
|
21
|
+
* (`useReferenceResolver`); a reference whose collection is not resolved yet stays
|
|
22
|
+
* read-only.
|
|
23
|
+
*
|
|
24
|
+
* @param entity - Entity owning the field (the extension entity for an extension column)
|
|
25
|
+
* @param name - Field name in `entity.fields` (unprefixed)
|
|
26
|
+
* @param viewerRole - Viewer's role
|
|
27
|
+
* @param referenceData - Resolved reference labels by collection
|
|
28
|
+
* @param t - Translator for option labels
|
|
29
|
+
* @returns The cell editor, or `undefined`
|
|
30
|
+
* @throws When `entity` has no field `name`
|
|
19
31
|
*
|
|
20
32
|
* @example
|
|
21
33
|
* ```ts
|
|
22
|
-
* cellEditFor(
|
|
34
|
+
* cellEditFor(productEntity, 'price', 'admin', {}, t);
|
|
23
35
|
* // { type: 'number' }
|
|
24
36
|
* ```
|
|
37
|
+
*
|
|
38
|
+
* @version 0.2.0
|
|
39
|
+
* @since 0.2.3
|
|
25
40
|
*/
|
|
26
|
-
export declare function cellEditFor(
|
|
41
|
+
export declare function cellEditFor(entity: AnyEntity, name: string, viewerRole: string, referenceData: Record<string, Record<string, string>>, t: Translate): TableCellEdit | undefined;
|
|
27
42
|
export {};
|
|
28
43
|
//# sourceMappingURL=cellEditFor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cellEditFor.d.ts","sourceRoot":"","sources":["../../../src/crud/components/cellEditFor.ts"],"names":[],"mappings":"AAEA
|
|
1
|
+
{"version":3,"file":"cellEditFor.d.ts","sourceRoot":"","sources":["../../../src/crud/components/cellEditFor.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE1D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAShD,KAAK,SAAS,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,CAAC;AAE5E;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,SAAS,EACjB,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,EAClB,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EACrD,CAAC,EAAE,SAAS,GACX,aAAa,GAAG,SAAS,CAkC3B"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// packages/ui/src/crud/components/cellEditFor.ts
|
|
2
|
-
import {
|
|
2
|
+
import { describeEntity } from '@donotdev/core';
|
|
3
|
+
import { translateLabel } from '../crudImports';
|
|
3
4
|
/** Field types edited as free text in an editable list. */
|
|
4
5
|
const TEXT_TYPES = new Set(['text', 'email', 'url', 'tel', 'textarea']);
|
|
5
6
|
/** Field types edited as a dropdown of their `validation.options`. */
|
|
@@ -7,20 +8,51 @@ const OPTION_TYPES = new Set(['select', 'radio']);
|
|
|
7
8
|
/**
|
|
8
9
|
* Cell editor of a list field, or `undefined` when the field is not edited in
|
|
9
10
|
* the table (type without a cell editor, or not editable by the viewer).
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
11
|
+
* Editability is the form's (`editableFor(viewerRole, 'edit')`): generated,
|
|
12
|
+
* computed, `editable: false`, create-only, role-denied and technical fields stay
|
|
13
|
+
* read-only. References (any field type with `validation.reference`, descriptor
|
|
14
|
+
* `value.kind === 'ref'`) list the referenced records by their resolved label
|
|
15
|
+
* (`useReferenceResolver`); a reference whose collection is not resolved yet stays
|
|
16
|
+
* read-only.
|
|
17
|
+
*
|
|
18
|
+
* @param entity - Entity owning the field (the extension entity for an extension column)
|
|
19
|
+
* @param name - Field name in `entity.fields` (unprefixed)
|
|
20
|
+
* @param viewerRole - Viewer's role
|
|
21
|
+
* @param referenceData - Resolved reference labels by collection
|
|
22
|
+
* @param t - Translator for option labels
|
|
23
|
+
* @returns The cell editor, or `undefined`
|
|
24
|
+
* @throws When `entity` has no field `name`
|
|
13
25
|
*
|
|
14
26
|
* @example
|
|
15
27
|
* ```ts
|
|
16
|
-
* cellEditFor(
|
|
28
|
+
* cellEditFor(productEntity, 'price', 'admin', {}, t);
|
|
17
29
|
* // { type: 'number' }
|
|
18
30
|
* ```
|
|
31
|
+
*
|
|
32
|
+
* @version 0.2.0
|
|
33
|
+
* @since 0.2.3
|
|
19
34
|
*/
|
|
20
|
-
export function cellEditFor(
|
|
21
|
-
|
|
35
|
+
export function cellEditFor(entity, name, viewerRole, referenceData, t) {
|
|
36
|
+
const field = entity.fields[name];
|
|
37
|
+
const descriptor = describeEntity(entity).fields[name];
|
|
38
|
+
if (!field || !descriptor) {
|
|
39
|
+
throw new Error(`cellEditFor: entity "${entity.name}" has no field "${name}".`);
|
|
40
|
+
}
|
|
41
|
+
if (!descriptor.editableFor(viewerRole, 'edit'))
|
|
22
42
|
return undefined;
|
|
23
|
-
const
|
|
43
|
+
const { value } = descriptor;
|
|
44
|
+
if (value.kind === 'ref' && value.ref !== undefined) {
|
|
45
|
+
const labels = referenceData[value.ref];
|
|
46
|
+
if (!labels)
|
|
47
|
+
return undefined;
|
|
48
|
+
return {
|
|
49
|
+
type: 'select',
|
|
50
|
+
options: Object.entries(labels)
|
|
51
|
+
.map(([id, label]) => ({ value: id, label }))
|
|
52
|
+
.sort((a, b) => a.label.localeCompare(b.label)),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const { type } = field;
|
|
24
56
|
if (TEXT_TYPES.has(type))
|
|
25
57
|
return { type: 'text' };
|
|
26
58
|
if (type === 'number')
|
|
@@ -38,17 +70,5 @@ export function cellEditFor(field, viewerRole, referenceData, t) {
|
|
|
38
70
|
})),
|
|
39
71
|
};
|
|
40
72
|
}
|
|
41
|
-
if (type === 'reference') {
|
|
42
|
-
const collection = field.validation?.reference;
|
|
43
|
-
const labels = typeof collection === 'string' ? referenceData[collection] : undefined;
|
|
44
|
-
if (!labels)
|
|
45
|
-
return undefined;
|
|
46
|
-
return {
|
|
47
|
-
type: 'select',
|
|
48
|
-
options: Object.entries(labels)
|
|
49
|
-
.map(([value, label]) => ({ value, label }))
|
|
50
|
-
.sort((a, b) => a.label.localeCompare(b.label)),
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
73
|
return undefined;
|
|
54
74
|
}
|