@dakataa/crud-react 1.0.10 → 2.1.0
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/CHANGELOG.md +108 -0
- package/README.md +482 -169
- package/dist/Crud.d.ts +15 -0
- package/dist/component/BaseButtonContent.d.ts +10 -0
- package/dist/component/Button.d.ts +9 -0
- package/dist/component/Dropdown.d.ts +17 -0
- package/dist/component/Empty.d.ts +5 -0
- package/dist/component/Link.d.ts +6 -0
- package/dist/component/LottieAnimation.d.ts +8 -0
- package/dist/component/Modal.d.ts +22 -0
- package/dist/component/Money.d.ts +7 -0
- package/dist/component/Preloader.d.ts +20 -0
- package/dist/component/Translation.d.ts +14 -0
- package/dist/component/crud/ActionLink.d.ts +10 -0
- package/dist/component/crud/CrudLoader.d.ts +29 -0
- package/dist/component/crud/DynamicView.d.ts +19 -0
- package/dist/component/crud/FiltersView.d.ts +7 -0
- package/dist/component/crud/GridView.d.ts +27 -0
- package/dist/component/crud/IsListItemActionGranted.d.ts +10 -0
- package/dist/component/crud/ItemAction.d.ts +9 -0
- package/dist/component/crud/ItemActions.d.ts +8 -0
- package/dist/component/crud/ItemLabel.d.ts +7 -0
- package/dist/component/crud/ItemValue.d.ts +7 -0
- package/dist/component/crud/List.d.ts +19 -0
- package/dist/component/crud/ListItem.d.ts +5 -0
- package/dist/component/crud/ListView.d.ts +8 -0
- package/dist/component/crud/Modify.d.ts +9 -0
- package/dist/component/crud/Navigation.d.ts +19 -0
- package/dist/component/crud/Paginator.d.ts +3 -0
- package/dist/component/crud/ViewLoader.d.ts +15 -0
- package/dist/component/crud/batch/BatchActionSelector.d.ts +3 -0
- package/dist/component/crud/batch/BatchActionsContext.d.ts +17 -0
- package/dist/component/crud/batch/BatchItemSelector.d.ts +5 -0
- package/dist/component/crud/form/Form.d.ts +31 -0
- package/dist/component/crud/form/FormError.d.ts +6 -0
- package/dist/component/crud/form/FormField.d.ts +11 -0
- package/dist/component/crud/form/FormGroup.d.ts +76 -0
- package/dist/component/crud/form/FormGroupViewLoader.d.ts +2 -0
- package/dist/component/crud/form/FormLabel.d.ts +4 -0
- package/dist/component/crud/form/FormRequestError.d.ts +14 -0
- package/dist/component/crud/form/FormRest.d.ts +2 -0
- package/dist/component/crud/form/FormRestError.d.ts +4 -0
- package/dist/component/error/ErrorBoundary.d.ts +13 -0
- package/dist/component/error/Exception.d.ts +9 -0
- package/dist/component/error/HttpException.d.ts +16 -0
- package/dist/component/form/Choice.d.ts +24 -0
- package/dist/component/form/Collection.d.ts +27 -0
- package/dist/component/form/Form.d.ts +62 -0
- package/dist/component/form/FormAlert.d.ts +3 -0
- package/dist/component/form/FormFieldError.d.ts +6 -0
- package/dist/component/form/FormGroup.d.ts +8 -0
- package/dist/component/form/FormHelp.d.ts +5 -0
- package/dist/component/form/FormLabel.d.ts +7 -0
- package/dist/component/form/FormSetting.d.ts +11 -0
- package/dist/component/form/FormStore.d.ts +93 -0
- package/dist/component/form/Input.d.ts +13 -0
- package/dist/component/form/Textarea.d.ts +15 -0
- package/dist/component/form/constraint/Contraint.d.ts +6 -0
- package/dist/component/form/constraint/NotBlank.d.ts +11 -0
- package/dist/component/form/formChoices.d.ts +8 -0
- package/dist/component/form/formSession.d.ts +3 -0
- package/dist/component/form/formView.d.ts +35 -0
- package/dist/component/router/Route.d.ts +12 -0
- package/dist/component/templating/Template.d.ts +17 -0
- package/dist/component/templating/TemplateBlock.d.ts +8 -0
- package/dist/component/templating/TemplateExtend.d.ts +9 -0
- package/dist/component/templating/TemplateParentBlock.d.ts +5 -0
- package/dist/context/ActionContext.d.ts +46 -0
- package/dist/context/AlertContext.d.ts +52 -0
- package/dist/context/ConfigContext.d.ts +34 -0
- package/dist/context/CrudProvider.d.ts +7 -0
- package/dist/context/GetData.d.ts +43 -0
- package/dist/context/ListContext.d.ts +69 -0
- package/dist/context/ListItemContext.d.ts +8 -0
- package/dist/context/LoaderContext.d.ts +5 -0
- package/dist/context/ModalContext.d.ts +18 -0
- package/dist/context/ModalScopeContext.d.ts +5 -0
- package/dist/context/NamespaceContext.d.ts +5 -0
- package/dist/crud-react.css +2 -1
- package/dist/crud-react.js +4004 -2956
- package/dist/denied-Cg2igDWh.js +688 -0
- package/dist/helper/ObjectUtils.d.ts +3 -0
- package/dist/helper/PathUtils.d.ts +12 -0
- package/dist/helper/StringUtils.d.ts +3 -0
- package/dist/info-Dfyx45FU.js +626 -0
- package/dist/layout/default/Base.d.ts +5 -0
- package/dist/layout/default/Error.d.ts +11 -0
- package/dist/layout/default/Main.d.ts +5 -0
- package/dist/main.d.ts +92 -79
- package/dist/setupTests.d.ts +0 -0
- package/dist/success-B5xn1-UX.js +559 -0
- package/dist/type/ActionRequestType.d.ts +17 -0
- package/dist/type/ActionType.d.ts +15 -0
- package/dist/type/ColumnType.d.ts +24 -0
- package/dist/type/ExceptionType.d.ts +6 -0
- package/dist/type/FormType.d.ts +6 -0
- package/dist/type/FormViewType.d.ts +96 -0
- package/dist/type/ListMetaType.d.ts +8 -0
- package/dist/type/ListType.d.ts +32 -0
- package/dist/type/ModifyType.d.ts +15 -0
- package/dist/type/RedirectType.d.ts +7 -0
- package/dist/type/RouteType.d.ts +7 -0
- package/dist/type/ViewType.d.ts +17 -0
- package/dist/warning-BaaC-X6i.js +630 -0
- package/package.json +86 -81
- package/dist/denied-CgoMpaA7.js +0 -27
- package/dist/info-D-NzIEYs.js +0 -144
- package/dist/success-Bu_BJcf1.js +0 -27
- package/dist/warning-B1nr7TsB.js +0 -27
package/README.md
CHANGED
|
@@ -26,6 +26,7 @@ The library provides:
|
|
|
26
26
|
9. [Hooks and contexts](#hooks-and-contexts)
|
|
27
27
|
10. [Core components](#core-components)
|
|
28
28
|
11. [Troubleshooting](#troubleshooting)
|
|
29
|
+
12. [Code style](#code-style)
|
|
29
30
|
|
|
30
31
|
## How it works
|
|
31
32
|
|
|
@@ -67,47 +68,52 @@ If your project does not already include Bootstrap styles, add your theme or Boo
|
|
|
67
68
|
## Quick start
|
|
68
69
|
|
|
69
70
|
```tsx
|
|
70
|
-
import { StrictMode } from
|
|
71
|
-
import { createRoot } from
|
|
71
|
+
import { StrictMode } from "react";
|
|
72
|
+
import { createRoot } from "react-dom/client";
|
|
72
73
|
import {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
} from
|
|
74
|
+
Crud,
|
|
75
|
+
CrudConfiguration,
|
|
76
|
+
CrudLoader,
|
|
77
|
+
MainLayout,
|
|
78
|
+
Outlet,
|
|
79
|
+
Route,
|
|
80
|
+
Router
|
|
81
|
+
} from "@dakataa/crud-react";
|
|
81
82
|
|
|
82
|
-
const templates = import.meta.glob(
|
|
83
|
+
const templates = import.meta.glob("./crud/**");
|
|
83
84
|
|
|
84
85
|
CrudConfiguration({
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
86
|
+
connection: {
|
|
87
|
+
baseURL: "https://project.local",
|
|
88
|
+
headers: {
|
|
89
|
+
Accept: "application/json"
|
|
90
|
+
}
|
|
89
91
|
},
|
|
90
|
-
|
|
91
|
-
templates,
|
|
92
|
+
templates
|
|
92
93
|
});
|
|
93
94
|
|
|
94
|
-
createRoot(document.getElementById(
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
95
|
+
createRoot(document.getElementById("root")!).render(
|
|
96
|
+
<StrictMode>
|
|
97
|
+
<Crud
|
|
98
|
+
config={{
|
|
99
|
+
templates,
|
|
100
|
+
currency: "BGN",
|
|
101
|
+
locale: "bg",
|
|
102
|
+
timezone: "UTC",
|
|
103
|
+
env: import.meta.env.CRUD_ENV
|
|
104
|
+
}}>
|
|
105
|
+
<Router>
|
|
106
|
+
<Route
|
|
107
|
+
element={
|
|
108
|
+
<MainLayout>
|
|
109
|
+
<Outlet />
|
|
110
|
+
</MainLayout>
|
|
111
|
+
}>
|
|
112
|
+
<Route path="*" element={<CrudLoader />} />
|
|
113
|
+
</Route>
|
|
114
|
+
</Router>
|
|
115
|
+
</Crud>
|
|
116
|
+
</StrictMode>
|
|
111
117
|
);
|
|
112
118
|
```
|
|
113
119
|
|
|
@@ -121,16 +127,16 @@ There are two configuration layers:
|
|
|
121
127
|
### `CrudConfiguration`
|
|
122
128
|
|
|
123
129
|
```ts
|
|
124
|
-
import { CrudConfiguration } from
|
|
130
|
+
import { CrudConfiguration } from "@dakataa/crud-react";
|
|
125
131
|
|
|
126
132
|
CrudConfiguration({
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
133
|
+
connection: {
|
|
134
|
+
baseURL: "https://crud-api.local",
|
|
135
|
+
headers: {
|
|
136
|
+
Accept: "application/json"
|
|
137
|
+
}
|
|
131
138
|
},
|
|
132
|
-
|
|
133
|
-
templates: import.meta.glob('./crud/**'),
|
|
139
|
+
templates: import.meta.glob("./crud/**")
|
|
134
140
|
});
|
|
135
141
|
```
|
|
136
142
|
|
|
@@ -138,19 +144,26 @@ CrudConfiguration({
|
|
|
138
144
|
|
|
139
145
|
```ts
|
|
140
146
|
type Config = {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
147
|
+
env?: "prod" | "dev" | "test" | string;
|
|
148
|
+
link?: {
|
|
149
|
+
path?: string;
|
|
150
|
+
prefix?: string;
|
|
151
|
+
includePrefix?: boolean;
|
|
152
|
+
};
|
|
153
|
+
templates?: { [path: string]: () => Promise<any> };
|
|
154
|
+
locale?: string;
|
|
155
|
+
currency?: string;
|
|
156
|
+
translate?: (
|
|
157
|
+
key: string,
|
|
158
|
+
options?: {
|
|
159
|
+
domain?: string;
|
|
160
|
+
properties?: { [key: string]: string | number | null };
|
|
161
|
+
}
|
|
162
|
+
) => string;
|
|
163
|
+
options?: {
|
|
164
|
+
HTMLTableElement: React.HTMLAttributes<HTMLTableElement>;
|
|
165
|
+
GridView: React.HTMLAttributes<HTMLDivElement>;
|
|
166
|
+
};
|
|
154
167
|
};
|
|
155
168
|
```
|
|
156
169
|
|
|
@@ -160,11 +173,37 @@ type Config = {
|
|
|
160
173
|
- `templates`: runtime template overrides merged with global templates
|
|
161
174
|
- `locale`: used by components such as `Money`
|
|
162
175
|
- `currency`: default currency for money formatting
|
|
176
|
+
- `translate`: used by `Translation` and form components for labels, help text, placeholders, tooltips (`title`), accessible labels (`aria-label`, `aria-description`), and choice group labels. `Translation` calls this callback directly; it no longer loads a template. It translates `translationKey` when provided, otherwise joins text children into a single key, and forwards `domain` and `properties`. Without this callback, children are preserved; when children are absent, the translation key is returned unchanged. Children containing React elements are preserved unless an explicit translation key is provided.
|
|
163
177
|
- `link.prefix`: useful when the CRUD UI is mounted under a sub-path such as `/admin`
|
|
164
178
|
- `link.path`: lets you force `CrudLoader` to resolve a custom path instead of `location.pathname + location.search`
|
|
165
179
|
- `options.HTMLTableElement`: default props for generated tables
|
|
166
180
|
- `options.GridView`: default props for the `GridView` wrapper
|
|
167
181
|
|
|
182
|
+
Connect your translation library once when configuring `Crud`. Forms can then pass translation keys directly:
|
|
183
|
+
|
|
184
|
+
```tsx
|
|
185
|
+
<Crud
|
|
186
|
+
config={{
|
|
187
|
+
translate: (key, options) =>
|
|
188
|
+
t(key, {
|
|
189
|
+
...options?.properties,
|
|
190
|
+
...(options?.domain ? { ns: options.domain } : {})
|
|
191
|
+
}),
|
|
192
|
+
templates
|
|
193
|
+
}}>
|
|
194
|
+
<FormGroup
|
|
195
|
+
name="vat"
|
|
196
|
+
options={{
|
|
197
|
+
label: "VAT",
|
|
198
|
+
attr: { "aria-label": "Registered for VAT" },
|
|
199
|
+
label_attr: { title: "Registered for VAT" }
|
|
200
|
+
}}
|
|
201
|
+
/>
|
|
202
|
+
</Crud>
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Custom form templates can use `UseTranslate()` where they render text that needs translation. Form attribute objects are passed through; only the specific text properties above are translated at their render sites.
|
|
206
|
+
|
|
168
207
|
## Backend contract
|
|
169
208
|
|
|
170
209
|
For the library to work, your backend must expose action metadata and action payloads in the expected format.
|
|
@@ -181,17 +220,17 @@ Each action looks like this:
|
|
|
181
220
|
|
|
182
221
|
```ts
|
|
183
222
|
type ActionType = {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
223
|
+
entity: string;
|
|
224
|
+
namespace?: string;
|
|
225
|
+
name: string;
|
|
226
|
+
title?: string;
|
|
227
|
+
visibility?: "list" | "object" | "internal";
|
|
228
|
+
permission?: string;
|
|
229
|
+
route?: {
|
|
230
|
+
path: string;
|
|
231
|
+
defaults?: { [key: string]: string };
|
|
232
|
+
methods?: string[];
|
|
233
|
+
};
|
|
195
234
|
};
|
|
196
235
|
```
|
|
197
236
|
|
|
@@ -199,26 +238,26 @@ Example:
|
|
|
199
238
|
|
|
200
239
|
```json
|
|
201
240
|
[
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
241
|
+
{
|
|
242
|
+
"entity": "Article",
|
|
243
|
+
"namespace": "blog",
|
|
244
|
+
"name": "list",
|
|
245
|
+
"title": "Articles",
|
|
246
|
+
"visibility": "list",
|
|
247
|
+
"route": {
|
|
248
|
+
"path": "/article"
|
|
249
|
+
}
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
"entity": "Article",
|
|
253
|
+
"namespace": "blog",
|
|
254
|
+
"name": "edit",
|
|
255
|
+
"title": "Edit article",
|
|
256
|
+
"visibility": "object",
|
|
257
|
+
"route": {
|
|
258
|
+
"path": "/article/{id}/edit"
|
|
259
|
+
}
|
|
220
260
|
}
|
|
221
|
-
}
|
|
222
261
|
]
|
|
223
262
|
```
|
|
224
263
|
|
|
@@ -228,24 +267,24 @@ The default list view expects a payload compatible with:
|
|
|
228
267
|
|
|
229
268
|
```ts
|
|
230
269
|
type ListType = {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
270
|
+
title: string | null;
|
|
271
|
+
entity: {
|
|
272
|
+
name: string;
|
|
273
|
+
columns: ColumnType[];
|
|
274
|
+
primaryColumn: ColumnType;
|
|
275
|
+
data: {
|
|
276
|
+
items: Array<{ [key: string]: any }>;
|
|
277
|
+
meta: ListMetaType;
|
|
278
|
+
};
|
|
279
|
+
acl: { [key: string]: Array<number | string> };
|
|
239
280
|
};
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
sort: { [key: string]: 'ASC' | 'DESC' | null };
|
|
248
|
-
action: { [key: string]: ActionType };
|
|
281
|
+
form: {
|
|
282
|
+
[key: string]: FormType;
|
|
283
|
+
filter: FormType;
|
|
284
|
+
batch: FormType;
|
|
285
|
+
};
|
|
286
|
+
sort: { [key: string]: "ASC" | "DESC" | null };
|
|
287
|
+
action: { [key: string]: ActionType };
|
|
249
288
|
};
|
|
250
289
|
```
|
|
251
290
|
|
|
@@ -263,11 +302,11 @@ The default add/edit view expects:
|
|
|
263
302
|
|
|
264
303
|
```ts
|
|
265
304
|
type ModifyType = {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
305
|
+
title: string | null;
|
|
306
|
+
object: { [key: string]: any } | null;
|
|
307
|
+
form: { [key: string]: FormType };
|
|
308
|
+
messages: { [key: string]: string[] };
|
|
309
|
+
redirect?: RedirectType;
|
|
271
310
|
};
|
|
272
311
|
```
|
|
273
312
|
|
|
@@ -280,7 +319,7 @@ Dynamic views are loaded from the `templates` object you pass through `CrudConfi
|
|
|
280
319
|
Typical setup:
|
|
281
320
|
|
|
282
321
|
```ts
|
|
283
|
-
const templates = import.meta.glob(
|
|
322
|
+
const templates = import.meta.glob("./crud/**");
|
|
284
323
|
```
|
|
285
324
|
|
|
286
325
|
The resolver looks for files in this order:
|
|
@@ -309,37 +348,44 @@ View names are normalized to PascalCase, so:
|
|
|
309
348
|
### Minimal override example
|
|
310
349
|
|
|
311
350
|
```tsx
|
|
312
|
-
import { AsTemplate, Block, Extend, Parent, Translation as T } from
|
|
313
|
-
|
|
314
|
-
export default AsTemplate(
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
351
|
+
import { AsTemplate, Block, Extend, Parent, Translation as T } from "@dakataa/crud-react";
|
|
352
|
+
|
|
353
|
+
export default AsTemplate(
|
|
354
|
+
() => {
|
|
355
|
+
return (
|
|
356
|
+
<section className="custom-list">
|
|
357
|
+
<header>
|
|
358
|
+
<Block name="title">
|
|
359
|
+
<h2>
|
|
360
|
+
<T>Custom list title</T>
|
|
361
|
+
</h2>
|
|
362
|
+
</Block>
|
|
363
|
+
</header>
|
|
364
|
+
|
|
365
|
+
<main>
|
|
366
|
+
<Block name="content" />
|
|
367
|
+
</main>
|
|
368
|
+
</section>
|
|
369
|
+
);
|
|
370
|
+
},
|
|
371
|
+
{ name: "list" }
|
|
372
|
+
);
|
|
329
373
|
```
|
|
330
374
|
|
|
331
375
|
Example of extending an existing template block:
|
|
332
376
|
|
|
333
377
|
```tsx
|
|
334
|
-
import { Extend, Parent } from
|
|
378
|
+
import { Extend, Parent } from "@dakataa/crud-react";
|
|
335
379
|
|
|
336
380
|
export default function CustomModifyNavigation() {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
381
|
+
return (
|
|
382
|
+
<Extend name="navigation">
|
|
383
|
+
<Parent />
|
|
384
|
+
<a href="/help" className="btn btn-sm btn-outline-secondary">
|
|
385
|
+
Help
|
|
386
|
+
</a>
|
|
387
|
+
</Extend>
|
|
388
|
+
);
|
|
343
389
|
}
|
|
344
390
|
```
|
|
345
391
|
|
|
@@ -348,16 +394,21 @@ export default function CustomModifyNavigation() {
|
|
|
348
394
|
The package exports its own lightweight router:
|
|
349
395
|
|
|
350
396
|
```tsx
|
|
351
|
-
import { Router, Route, Outlet } from
|
|
397
|
+
import { Router, Route, Outlet } from "@dakataa/crud-react";
|
|
352
398
|
```
|
|
353
399
|
|
|
354
400
|
Example:
|
|
355
401
|
|
|
356
402
|
```tsx
|
|
357
403
|
<Router>
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
404
|
+
<Route
|
|
405
|
+
element={
|
|
406
|
+
<MainLayout>
|
|
407
|
+
<Outlet />
|
|
408
|
+
</MainLayout>
|
|
409
|
+
}>
|
|
410
|
+
<Route path="*" element={<CrudLoader />} />
|
|
411
|
+
</Route>
|
|
361
412
|
</Router>
|
|
362
413
|
```
|
|
363
414
|
|
|
@@ -367,8 +418,8 @@ Example:
|
|
|
367
418
|
|
|
368
419
|
- `getAction(entity, name, namespace?)`
|
|
369
420
|
- `getActionByPath(path)`
|
|
370
|
-
- `
|
|
371
|
-
- `generateActionLink(
|
|
421
|
+
- `getActionRequestByPath(path)`
|
|
422
|
+
- `generateActionLink(actionRequest)`
|
|
372
423
|
- `generateRoute(route, parameters?, query?)`
|
|
373
424
|
- `generatePath(pattern, parameters?, query?)`
|
|
374
425
|
- `navigate(to, replace?)`
|
|
@@ -379,15 +430,14 @@ If your admin panel lives under `/admin`, configure a prefix:
|
|
|
379
430
|
|
|
380
431
|
```tsx
|
|
381
432
|
<Crud
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
>
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
</Router>
|
|
433
|
+
config={{
|
|
434
|
+
link: {
|
|
435
|
+
prefix: "admin"
|
|
436
|
+
}
|
|
437
|
+
}}>
|
|
438
|
+
<Router>
|
|
439
|
+
<Route path="*" element={<CrudLoader />} />
|
|
440
|
+
</Router>
|
|
391
441
|
</Crud>
|
|
392
442
|
```
|
|
393
443
|
|
|
@@ -397,6 +447,10 @@ This lets internal CRUD routes stay backend-friendly while browser URLs include
|
|
|
397
447
|
|
|
398
448
|
The add/edit screens are driven by `form.modify.view`.
|
|
399
449
|
|
|
450
|
+
See the [Form API reference](src/component/form/README.md) for props, hooks,
|
|
451
|
+
value lifecycle and short implementation examples. Version 2 removes the tuple
|
|
452
|
+
`UseForm` API; see the [migration notes](src/component/form/README.md#migration-from-v1).
|
|
453
|
+
|
|
400
454
|
Default behavior:
|
|
401
455
|
|
|
402
456
|
1. `CrudLoader` resolves the current action
|
|
@@ -409,10 +463,10 @@ Default behavior:
|
|
|
409
463
|
### Default form rendering
|
|
410
464
|
|
|
411
465
|
```tsx
|
|
412
|
-
import { Modify } from
|
|
466
|
+
import { Modify } from "@dakataa/crud-react";
|
|
413
467
|
|
|
414
468
|
export default function EditArticle() {
|
|
415
|
-
|
|
469
|
+
return <Modify />;
|
|
416
470
|
}
|
|
417
471
|
```
|
|
418
472
|
|
|
@@ -422,28 +476,180 @@ If you need custom rendering around the form schema:
|
|
|
422
476
|
|
|
423
477
|
```tsx
|
|
424
478
|
import {
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
479
|
+
CrudForm,
|
|
480
|
+
FormFieldViewLoader,
|
|
481
|
+
FormGroup,
|
|
482
|
+
FormRest,
|
|
483
|
+
FormRestError,
|
|
484
|
+
FormViewProvider
|
|
485
|
+
} from "@dakataa/crud-react";
|
|
431
486
|
```
|
|
432
487
|
|
|
433
488
|
Useful helpers:
|
|
434
489
|
|
|
435
490
|
- `UseFormView()`: access the current form view node
|
|
491
|
+
- `useFormValue(field)`: observe one store value without registering a control
|
|
492
|
+
- `useFormValues(fields)`: observe several values for dependent UI or calculations
|
|
493
|
+
- `choiceToken`, `choiceList`, `selectionTokens`: normalize choices and selection values
|
|
436
494
|
- `FormFieldViewLoader`: render fields from the current schema node
|
|
495
|
+
- `FormGroup`: render the current schema node or one of its direct children
|
|
437
496
|
- `FormRest`: render non-explicitly-rendered fields
|
|
438
497
|
- `FormRestError`: render non-explicitly-rendered errors
|
|
439
498
|
|
|
499
|
+
### Current values and field lifecycle
|
|
500
|
+
|
|
501
|
+
`FormView` describes the backend schema and initial values. Treat its nodes as
|
|
502
|
+
immutable: modifying `UseFormView().form.data` does not update an editable field.
|
|
503
|
+
The base `Form` owns current values and collection rows, so hiding and remounting
|
|
504
|
+
fields preserves drafts, additions, and deletions. A field subscribes to its own
|
|
505
|
+
value and errors rather than rerendering the entire form on each edit.
|
|
506
|
+
|
|
507
|
+
Use `UseFormView().setValue(relativeName, value)` or the existing `FormRef`
|
|
508
|
+
`setValue`, `setValues`, and `setFormData` methods for programmatic changes.
|
|
509
|
+
Values can be set while a field is unmounted. `setValue()` followed immediately
|
|
510
|
+
by `getFormData()` or `submit()` sees the updated values. Ordinary native inputs
|
|
511
|
+
in custom templates continue to participate in native submission; use the
|
|
512
|
+
binding hook when their drafts must also survive unmounting.
|
|
513
|
+
|
|
514
|
+
```tsx
|
|
515
|
+
import { useFormField, UseFormView } from "@dakataa/crud-react";
|
|
516
|
+
|
|
517
|
+
function CustomTitle() {
|
|
518
|
+
const { form: view } = UseFormView();
|
|
519
|
+
const field = useFormField(view);
|
|
520
|
+
|
|
521
|
+
return (
|
|
522
|
+
<input
|
|
523
|
+
name={field.name}
|
|
524
|
+
value={Array.isArray(field.value) ? "" : (field.value ?? "")}
|
|
525
|
+
aria-invalid={field.errors.length > 0}
|
|
526
|
+
onChange={(event) => {
|
|
527
|
+
field.change(event.target.value);
|
|
528
|
+
}}
|
|
529
|
+
/>
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
The hook returns `value`, `errors`, `dirty`, the HTML `name`, and `change`.
|
|
535
|
+
`change` also accepts a function of the current value for grouped selections.
|
|
536
|
+
Checkbox/radio components control `checked` separately from their submitted
|
|
537
|
+
option token. File inputs remain native; reset clears their selection, and
|
|
538
|
+
remounting their DOM node does not restore the browser file picker.
|
|
539
|
+
|
|
540
|
+
`FormField` and `FormGroup` still shallowly apply `options` without mutating the
|
|
541
|
+
backend node. An override of `data`, `value`, or `checked` supplies a local
|
|
542
|
+
initial default once per field/session. Update a current value through the form
|
|
543
|
+
API rather than changing that override. Binding-owned HTML attributes (`value`,
|
|
544
|
+
`defaultValue`, `checked`, `defaultChecked`, and `name`) cannot be overridden via
|
|
545
|
+
`attr`. Existing event callbacks are composed with the binding; preventing the
|
|
546
|
+
change event cancels the edit.
|
|
547
|
+
|
|
548
|
+
A refresh of the same record updates pristine fields and the reset baseline
|
|
549
|
+
while retaining drafts and local collection changes. An invalid submit keeps
|
|
550
|
+
the original baseline and applies backend errors. A successful submit adopts
|
|
551
|
+
persisted values as the new baseline, retaining any edits made after submission.
|
|
552
|
+
Collection responses must retain stable row names; row identity is never inferred
|
|
553
|
+
from its position. Changing the edited record starts a new form session. Filter
|
|
554
|
+
forms start a new session when the applied query changes, excluding refresh
|
|
555
|
+
`_ts` parameters.
|
|
556
|
+
|
|
557
|
+
Both a native reset button and `FormRef.reset()` restore the current baseline,
|
|
558
|
+
including collection rows, and clear errors and submission status. Preserving a
|
|
559
|
+
cached value does not automatically submit it: unmounted and disabled controls
|
|
560
|
+
are excluded, while mounted hidden controls, repeated names, native controls,
|
|
561
|
+
files, and the submit button retain standard `FormData` behavior.
|
|
562
|
+
|
|
563
|
+
### `FormGroup`
|
|
564
|
+
|
|
565
|
+
`FormGroup` renders one node from the form schema exposed by the nearest
|
|
566
|
+
`FormViewProvider`. In a standard add/edit screen, `CrudForm` supplies this
|
|
567
|
+
provider automatically.
|
|
568
|
+
|
|
569
|
+
```tsx
|
|
570
|
+
import { FormGroup } from "@dakataa/crud-react";
|
|
571
|
+
|
|
572
|
+
export default function ArticleFields() {
|
|
573
|
+
return (
|
|
574
|
+
<>
|
|
575
|
+
<FormGroup name="title" />
|
|
576
|
+
<FormGroup name="content" />
|
|
577
|
+
</>
|
|
578
|
+
);
|
|
579
|
+
}
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
The `name` prop selects a direct child of the current schema node. If `name` is
|
|
583
|
+
omitted, the current node itself is rendered. Container nodes are traversed
|
|
584
|
+
recursively; leaf nodes use the standard field group with its label, input,
|
|
585
|
+
validation error, and help text.
|
|
586
|
+
|
|
587
|
+
#### Props
|
|
588
|
+
|
|
589
|
+
The component accepts the exported `FormGroupProps` type:
|
|
590
|
+
|
|
591
|
+
```ts
|
|
592
|
+
export type FormGroupProps = {
|
|
593
|
+
name?: string;
|
|
594
|
+
options?: Omit<FormViewType, "type">;
|
|
595
|
+
optional?: boolean;
|
|
596
|
+
};
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
Consumers can import it with
|
|
600
|
+
`import type { FormGroupProps } from '@dakataa/crud-react'`.
|
|
601
|
+
|
|
602
|
+
`FormGroupProps` describes the complete props object. The table below shows the
|
|
603
|
+
type of each individual property; therefore `options` is listed with its own
|
|
604
|
+
`Omit<FormViewType, 'type'>` type.
|
|
605
|
+
|
|
606
|
+
| Prop | Type | Default | Description |
|
|
607
|
+
| ---------- | ---------------------------- | ------------ | ----------------------------------------------------------------------------------- |
|
|
608
|
+
| `name` | `string` | current view | Selects a direct child of the current form view. Dotted paths are not supported. |
|
|
609
|
+
| `options` | `Omit<FormViewType, 'type'>` | `{}` | Applies shallow schema overrides for this render. The field type cannot be changed. |
|
|
610
|
+
| `optional` | `boolean` | `false` | Returns `null` when the selected child is missing instead of throwing an error. |
|
|
611
|
+
|
|
612
|
+
Use `options` to customize the selected schema node without changing the
|
|
613
|
+
backend response:
|
|
614
|
+
|
|
615
|
+
```tsx
|
|
616
|
+
<FormGroup
|
|
617
|
+
name="price"
|
|
618
|
+
options={{
|
|
619
|
+
label: "Unit price",
|
|
620
|
+
help: "Enter the amount before tax.",
|
|
621
|
+
attr: { className: "form-control text-end" }
|
|
622
|
+
}}
|
|
623
|
+
/>
|
|
624
|
+
```
|
|
625
|
+
|
|
626
|
+
Overrides are shallow. Supplying an object-valued property such as `attr` or
|
|
627
|
+
`children` replaces that property completely; it is not deep-merged.
|
|
628
|
+
|
|
629
|
+
For schemas where a field is conditional, opt into missing-field handling
|
|
630
|
+
explicitly:
|
|
631
|
+
|
|
632
|
+
```tsx
|
|
633
|
+
<FormGroup name="companyNumber" optional />
|
|
634
|
+
<FormField name="companyNumber" optional />
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
Without `optional`, a missing child throws
|
|
638
|
+
`Missing Provided Form View to Form Group: companyNumber`. Rendering outside a
|
|
639
|
+
`FormViewProvider` throws `UseFormView must be used within a FormViewProvider.`
|
|
640
|
+
|
|
641
|
+
`FormField` also accepts `optional`, which defaults to `false`. When the named
|
|
642
|
+
child is missing, it returns `null` if `optional` is set; otherwise it throws
|
|
643
|
+
`Missing Form View for FormField: companyNumber`. Existing fields still render
|
|
644
|
+
normally, including their `required` validation.
|
|
645
|
+
|
|
440
646
|
## Hooks and contexts
|
|
441
647
|
|
|
442
648
|
### `UseActions()`
|
|
443
649
|
|
|
444
650
|
Access loaded actions, route matching, link generation and programmatic navigation.
|
|
445
651
|
|
|
446
|
-
### `
|
|
652
|
+
### `UseCurrentActionRequest()`
|
|
447
653
|
|
|
448
654
|
Get the current resolved CRUD action and update it from inside a view.
|
|
449
655
|
|
|
@@ -457,22 +663,22 @@ Fetch arbitrary data through the configured CRUD requester.
|
|
|
457
663
|
|
|
458
664
|
```tsx
|
|
459
665
|
const { results, status, refresh, cancel } = GetData({
|
|
460
|
-
|
|
666
|
+
path: "/article"
|
|
461
667
|
});
|
|
462
668
|
```
|
|
463
669
|
|
|
464
670
|
### `GetDataByAction(...)`
|
|
465
671
|
|
|
466
|
-
Fetch data using an `
|
|
672
|
+
Fetch data using an `ActionRequest`.
|
|
467
673
|
|
|
468
674
|
```tsx
|
|
469
675
|
const data = GetDataByAction({
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
676
|
+
actionRequest: {
|
|
677
|
+
action: {
|
|
678
|
+
entity: "Article",
|
|
679
|
+
name: "list"
|
|
680
|
+
}
|
|
681
|
+
}
|
|
476
682
|
});
|
|
477
683
|
```
|
|
478
684
|
|
|
@@ -480,12 +686,13 @@ Returned shape:
|
|
|
480
686
|
|
|
481
687
|
```ts
|
|
482
688
|
type GetDataType = {
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
689
|
+
url: string;
|
|
690
|
+
status: number;
|
|
691
|
+
results: any;
|
|
692
|
+
response?: Response;
|
|
693
|
+
refresh: () => void;
|
|
694
|
+
cancel: () => void;
|
|
695
|
+
registerErrorHandler?: (handler: (error: unknown) => boolean) => () => void;
|
|
489
696
|
};
|
|
490
697
|
```
|
|
491
698
|
|
|
@@ -501,6 +708,11 @@ Open CRUD actions inside modals.
|
|
|
501
708
|
const { openModal, closeModal } = UseModal();
|
|
502
709
|
```
|
|
503
710
|
|
|
711
|
+
### `useIsModal()`
|
|
712
|
+
|
|
713
|
+
Returns whether the current component belongs to a modal view. See
|
|
714
|
+
[modal context](#modal-and-usemodal) for usage and the distinction from `UseModal()`.
|
|
715
|
+
|
|
504
716
|
### `UseAlert()`
|
|
505
717
|
|
|
506
718
|
Open confirm, success, warning and error dialogs.
|
|
@@ -543,6 +755,44 @@ Loads a namespace-aware template file and falls back to children when no overrid
|
|
|
543
755
|
|
|
544
756
|
Render CRUD screens inside nested modals.
|
|
545
757
|
|
|
758
|
+
Use `useIsModal()` to detect whether a component is inside a modal:
|
|
759
|
+
|
|
760
|
+
```tsx
|
|
761
|
+
import { Modify, UseModal, useIsModal } from "@dakataa/crud-react";
|
|
762
|
+
|
|
763
|
+
function EditActions() {
|
|
764
|
+
const isModal = useIsModal();
|
|
765
|
+
const { closeModal } = UseModal();
|
|
766
|
+
|
|
767
|
+
return isModal ? (
|
|
768
|
+
<button type="button" onClick={closeModal}>
|
|
769
|
+
Cancel
|
|
770
|
+
</button>
|
|
771
|
+
) : null;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
function Edit() {
|
|
775
|
+
return <Modify />;
|
|
776
|
+
}
|
|
777
|
+
```
|
|
778
|
+
|
|
779
|
+
The hook returns `false` outside a modal. For actions opened with `openModal()`,
|
|
780
|
+
it returns `true` in the loaded `Edit`/`View` component itself and its descendants.
|
|
781
|
+
It also returns `true` for children of a directly rendered `<Modal>`, including
|
|
782
|
+
portal content and nested modals. The page behind an open modal remains outside
|
|
783
|
+
that scope and receives `false`.
|
|
784
|
+
|
|
785
|
+
`UseModal()` controls the modal stack; its `modal` value describes the currently
|
|
786
|
+
open modal and is shared with the underlying page. It does not indicate that the
|
|
787
|
+
calling component belongs to that modal.
|
|
788
|
+
|
|
789
|
+
`Modify` uses `useIsModal()` when its `modal` prop is omitted. An explicit
|
|
790
|
+
`modal={true}` or `modal={false}` overrides its layout and embedded-form behavior.
|
|
791
|
+
This override does not change the surrounding context: a component rendered
|
|
792
|
+
inside a modal still receives `true` even when a nested `Modify modal={false}`
|
|
793
|
+
uses a page template. Existing `modal` props supplied to loaded views remain
|
|
794
|
+
supported for compatibility.
|
|
795
|
+
|
|
546
796
|
### `UseAlert`
|
|
547
797
|
|
|
548
798
|
Display animated alerts and confirmation dialogs.
|
|
@@ -597,3 +847,66 @@ If you want, the next useful step is to split this README into:
|
|
|
597
847
|
- a `/docs` folder with standalone guides such as `backend-contract.md`, `templates.md`, `forms.md`, and `recipes.md`
|
|
598
848
|
|
|
599
849
|
That usually makes the onboarding flow easier for new users.
|
|
850
|
+
|
|
851
|
+
## Tests
|
|
852
|
+
|
|
853
|
+
Tests live in `tests/`, grouped by the behavior they verify:
|
|
854
|
+
|
|
855
|
+
- `tests/form/`: form APIs, controls, values, state and value lifecycle.
|
|
856
|
+
- `tests/form/choices.test.tsx`: choice helpers, selection, placeholders and transforms.
|
|
857
|
+
- `tests/crud/`: CRUD form requests and response handling.
|
|
858
|
+
|
|
859
|
+
Use descriptive file names without repeating the feature folder name. Add related
|
|
860
|
+
cases to the existing file and organize them with shallow `describe` groups.
|
|
861
|
+
Split large files by behavior within their feature folder when needed. A test
|
|
862
|
+
that uses a Choice control to verify a general form API stays with that API.
|
|
863
|
+
|
|
864
|
+
```bash
|
|
865
|
+
npm test
|
|
866
|
+
npm run test:form
|
|
867
|
+
npm run test:crud
|
|
868
|
+
npm test -- tests/form/choices.test.tsx
|
|
869
|
+
```
|
|
870
|
+
|
|
871
|
+
## Code style
|
|
872
|
+
|
|
873
|
+
This project and the related React projects use the same formatting profile:
|
|
874
|
+
|
|
875
|
+
- Four spaces, UTF-8, LF line endings, and a final newline.
|
|
876
|
+
- Double quotes by default, semicolons, no trailing commas, and a target line width of 100.
|
|
877
|
+
- Braces for all control statements, no nested ternaries, and top-level `import type` declarations.
|
|
878
|
+
- Concise bodies for simple arrow functions; multiline blocks for multiple statements or complex logic.
|
|
879
|
+
|
|
880
|
+
`.editorconfig` provides basic editor settings. `.prettierrc.json` controls formatting,
|
|
881
|
+
and `eslint.code-style.js` defines the shared code rules. ESLint uses
|
|
882
|
+
`eslint-config-prettier` to disable formatting rules that conflict with Prettier.
|
|
883
|
+
The formatter and ESLint versions are pinned in `package.json`.
|
|
884
|
+
|
|
885
|
+
For specific files, use the project-local tools:
|
|
886
|
+
|
|
887
|
+
```bash
|
|
888
|
+
npm exec -- eslint --fix path/to/file.tsx
|
|
889
|
+
npm exec -- prettier --write path/to/file.tsx
|
|
890
|
+
npm exec -- eslint path/to/file.tsx
|
|
891
|
+
npm exec -- prettier --check path/to/file.tsx
|
|
892
|
+
```
|
|
893
|
+
|
|
894
|
+
Repository-wide commands are available as `npm run format`, `npm run format:check`,
|
|
895
|
+
`npm run lint`, and `npm run lint:fix`. The checks also report legacy code that
|
|
896
|
+
has not yet adopted this profile. Format edited files during normal work;
|
|
897
|
+
repository-wide style migration should be a separate change.
|
|
898
|
+
|
|
899
|
+
### PhpStorm
|
|
900
|
+
|
|
901
|
+
1. Enable EditorConfig support under **Settings → Editor → Code Style**.
|
|
902
|
+
2. Open **Settings → Languages & Frameworks → JavaScript → Prettier**.
|
|
903
|
+
3. Select the project's `node_modules/prettier` package. Enable **Run on save** and
|
|
904
|
+
**Run on 'Reformat Code' action**. If necessary, select manual configuration to
|
|
905
|
+
expose the reformat setting. Use `**/*.{js,jsx,ts,tsx,cjs,mjs,json,css,scss}` as the
|
|
906
|
+
file pattern and keep the repository's `.prettierignore` enabled.
|
|
907
|
+
4. Enable automatic ESLint configuration under **JavaScript → Code Quality Tools → ESLint**.
|
|
908
|
+
|
|
909
|
+
Other editors and agents should use the same project-local formatter, ESLint,
|
|
910
|
+
and configuration files. When adding a related project, copy the shared
|
|
911
|
+
`.editorconfig`, `.prettierrc.json`, `.prettierignore`, and `eslint.code-style.js`,
|
|
912
|
+
import the code rules into its ESLint configuration, and pin the same tool versions.
|