@dakataa/crud-react 1.0.7 → 1.0.10

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 CHANGED
@@ -1,128 +1,599 @@
1
- # CRUD Dashboard React (Frontend)
2
- This is a frontend react implementation of CRUD.
3
- Easy way to create CRUD ADMIN panel / Dashboard for your software.
4
-
5
- This library depend on [@dakataa/crud](https://github.com/dakataa/crud) which is CRUD REST API; however, it can also be used as a standalone
6
- if you provide the correct data structure. The components are based on the Bootstrap Framework.
7
-
8
- ### Table of Contents
9
- 1. [Installation](#installation)
10
- 2. [Configuration](#configuration)
11
- 3. [Examples](#examples)
12
- 4. [Components](#components)
13
- - [Grid](#GridView)
14
- - [Form](#form)
15
- - [Templating](#templates)
16
- - [Dynamic View](#dynamic-view)
17
- - [Modal](#modal)
18
- - [Alert](#alert)
19
- - [Navigation](#navigation)
20
- 5. Layout
21
- 6. [Hooks](#hooks)
22
-
23
-
24
- ### Installation
25
-
26
- #### Using npm:
27
- ```shell
1
+ # CRUD Dashboard React
2
+
3
+ `@dakataa/crud-react` is a React frontend library for building CRUD admin panels and dashboards.
4
+
5
+ It is designed to work with [`@dakataa/crud`](https://github.com/dakataa/crud), but it can also run against any backend that returns the same action and view data structures.
6
+
7
+ The library provides:
8
+
9
+ - automatic CRUD route resolution from backend actions
10
+ - default list/add/edit screens
11
+ - dynamic template overrides per namespace
12
+ - schema-driven form rendering
13
+ - modal and alert helpers
14
+ - Bootstrap-based UI components
15
+
16
+ ## Contents
17
+
18
+ 1. [How it works](#how-it-works)
19
+ 2. [Installation](#installation)
20
+ 3. [Quick start](#quick-start)
21
+ 4. [Configuration](#configuration)
22
+ 5. [Backend contract](#backend-contract)
23
+ 6. [Templates and dynamic views](#templates-and-dynamic-views)
24
+ 7. [Routing](#routing)
25
+ 8. [Forms](#forms)
26
+ 9. [Hooks and contexts](#hooks-and-contexts)
27
+ 10. [Core components](#core-components)
28
+ 11. [Troubleshooting](#troubleshooting)
29
+
30
+ ## How it works
31
+
32
+ The library loads available CRUD actions from `/_crud/actions`, matches the current URL to one of those actions, fetches the data for that action, and renders the matching view.
33
+
34
+ The default built-in views are:
35
+
36
+ - `list`
37
+ - `add`
38
+ - `edit`
39
+
40
+ Each view can be overridden with files loaded through `import.meta.glob('./crud/**')`.
41
+
42
+ ## Installation
43
+
44
+ Install the package:
45
+
46
+ ```bash
28
47
  npm install @dakataa/crud-react
29
48
  ```
30
49
 
31
- #### Using yarn:
32
- ```shell
50
+ or:
51
+
52
+ ```bash
33
53
  yarn add @dakataa/crud-react
34
54
  ```
35
55
 
36
- ### Configuration
56
+ This package also expects these peer dependencies in your app:
37
57
 
58
+ - `react`
59
+ - `react-dom`
60
+ - `@dakataa/requester`
61
+ - `@dakataa/crud-theme`
62
+ - `@popperjs/core`
63
+ - `lottie-web`
38
64
 
39
- ```ts
40
- import {CrudConfiguration} from "@dakataa/crud-react";
65
+ If your project does not already include Bootstrap styles, add your theme or Bootstrap setup in the host application.
66
+
67
+ ## Quick start
68
+
69
+ ```tsx
70
+ import { StrictMode } from 'react';
71
+ import { createRoot } from 'react-dom/client';
72
+ import {
73
+ Crud,
74
+ CrudConfiguration,
75
+ CrudLoader,
76
+ MainLayout,
77
+ Outlet,
78
+ Route,
79
+ Router,
80
+ } from '@dakataa/crud-react';
81
+
82
+ const templates = import.meta.glob('./crud/**');
41
83
 
42
84
  CrudConfiguration({
43
- connection: {
44
- baseURL: 'https://crud-rest-api.local'
45
- }
85
+ connection: {
86
+ baseURL: 'https://project.local',
87
+ headers: {
88
+ Accept: 'application/json',
89
+ },
90
+ },
91
+ templates,
46
92
  });
93
+
94
+ createRoot(document.getElementById('root')!).render(
95
+ <StrictMode>
96
+ <Crud
97
+ config={{
98
+ templates,
99
+ currency: 'BGN',
100
+ locale: 'bg',
101
+ env: import.meta.env.CRUD_ENV,
102
+ }}
103
+ >
104
+ <Router>
105
+ <Route element={<MainLayout><Outlet /></MainLayout>}>
106
+ <Route path="*" element={<CrudLoader />} />
107
+ </Route>
108
+ </Router>
109
+ </Crud>
110
+ </StrictMode>
111
+ );
47
112
  ```
48
113
 
49
- ### Example
114
+ ## Configuration
50
115
 
51
- #### Example with *react-router*
52
- main.tsx
116
+ There are two configuration layers:
53
117
 
54
- ```jsx
55
- import {StrictMode} from 'react'
56
- import {createRoot} from 'react-dom/client'
57
- import {Crud, CrudConfiguration, MainLayout, Router, Route, Outlet} from "@dakataa/crud-react";
118
+ - `CrudConfiguration(...)` configures the shared requester namespace and global templates
119
+ - `<Crud config={...}>` configures runtime behavior for the current React tree
58
120
 
59
- const templates = import.meta.glob('./crud/**');
121
+ ### `CrudConfiguration`
122
+
123
+ ```ts
124
+ import { CrudConfiguration } from '@dakataa/crud-react';
60
125
 
61
126
  CrudConfiguration({
62
- connection: {
63
- baseURL: 'https://project.local',
64
- headers: {
65
- Accept: 'application/json'
66
- }
67
- },
68
- templates: import.meta.glob('./crud/**')
127
+ connection: {
128
+ baseURL: 'https://crud-api.local',
129
+ headers: {
130
+ Accept: 'application/json',
131
+ },
132
+ },
133
+ templates: import.meta.glob('./crud/**'),
69
134
  });
135
+ ```
70
136
 
71
- createRoot(document.getElementById('root')!).render(
72
- <StrictMode>
73
- <Crud
74
- config={{templates, currency: 'BGN', locale: 'bg', env: import.meta.env.CRUD_ENV}}
75
- errorFallback={<CrudErrorFallback/>}
76
- >
77
- <Router>
78
- <Route element={<MainLayout><Outlet/></MainLayout>}>
79
- <Route path={"*"} element={<CrudLoader/>}/>
80
- </Route>
81
- </Router>
82
- </Crud>
83
- </StrictMode>
84
- )
85
- ```
86
-
87
-
88
- ### Legend
89
- * Action
90
- * **Entity** - Model Name (Article)
91
- * **Namespace**
92
- * **Route** - API Endpoint information.
137
+ ### `Crud` config
138
+
139
+ ```ts
140
+ type Config = {
141
+ env?: 'prod' | 'dev' | 'test' | string;
142
+ link?: {
143
+ path?: string;
144
+ prefix?: string;
145
+ includePrefix?: boolean;
146
+ };
147
+ templates?: { [path: string]: () => Promise<any> };
148
+ locale?: string;
149
+ currency?: string;
150
+ options?: {
151
+ HTMLTableElement: React.HTMLAttributes<HTMLTableElement>;
152
+ GridView: React.HTMLAttributes<HTMLDivElement>;
153
+ };
154
+ };
155
+ ```
156
+
157
+ ### Common options
158
+
159
+ - `env`: environment flag exposed through `UseConfig()`
160
+ - `templates`: runtime template overrides merged with global templates
161
+ - `locale`: used by components such as `Money`
162
+ - `currency`: default currency for money formatting
163
+ - `link.prefix`: useful when the CRUD UI is mounted under a sub-path such as `/admin`
164
+ - `link.path`: lets you force `CrudLoader` to resolve a custom path instead of `location.pathname + location.search`
165
+ - `options.HTMLTableElement`: default props for generated tables
166
+ - `options.GridView`: default props for the `GridView` wrapper
167
+
168
+ ## Backend contract
169
+
170
+ For the library to work, your backend must expose action metadata and action payloads in the expected format.
171
+
172
+ ### 1. Actions endpoint
173
+
174
+ The frontend loads available actions from:
175
+
176
+ ```text
177
+ /_crud/actions
178
+ ```
179
+
180
+ Each action looks like this:
181
+
182
+ ```ts
183
+ type ActionType = {
184
+ entity: string;
185
+ namespace?: string;
186
+ name: string;
187
+ title?: string;
188
+ visibility?: 'list' | 'object' | 'internal';
189
+ permission?: string;
190
+ route?: {
191
+ path: string;
192
+ defaults?: { [key: string]: string };
193
+ methods?: string[];
194
+ };
195
+ };
196
+ ```
197
+
198
+ Example:
199
+
200
+ ```json
201
+ [
202
+ {
203
+ "entity": "Article",
204
+ "namespace": "blog",
205
+ "name": "list",
206
+ "title": "Articles",
207
+ "visibility": "list",
208
+ "route": {
209
+ "path": "/article"
210
+ }
211
+ },
212
+ {
213
+ "entity": "Article",
214
+ "namespace": "blog",
215
+ "name": "edit",
216
+ "title": "Edit article",
217
+ "visibility": "object",
218
+ "route": {
219
+ "path": "/article/{id}/edit"
220
+ }
221
+ }
222
+ ]
223
+ ```
224
+
225
+ ### 2. List response
226
+
227
+ The default list view expects a payload compatible with:
228
+
229
+ ```ts
230
+ type ListType = {
231
+ title: string | null;
232
+ entity: {
233
+ name: string;
234
+ columns: ColumnType[];
235
+ primaryColumn: ColumnType;
236
+ data: {
237
+ items: Array<{ [key: string]: any }>;
238
+ meta: ListMetaType;
239
+ };
240
+ acl: { [key: string]: Array<number | string> };
241
+ };
242
+ form: {
243
+ [key: string]: FormType;
244
+ filter: FormType;
245
+ batch: FormType;
246
+ };
247
+ sort: { [key: string]: 'ASC' | 'DESC' | null };
248
+ action: { [key: string]: ActionType };
249
+ };
250
+ ```
251
+
252
+ This powers:
253
+
254
+ - table/grid columns
255
+ - row actions
256
+ - pagination
257
+ - filters
258
+ - batch actions
259
+
260
+ ### 3. Modify response
261
+
262
+ The default add/edit view expects:
263
+
264
+ ```ts
265
+ type ModifyType = {
266
+ title: string | null;
267
+ object: { [key: string]: any } | null;
268
+ form: { [key: string]: FormType };
269
+ messages: { [key: string]: string[] };
270
+ redirect?: RedirectType;
271
+ };
272
+ ```
273
+
274
+ The key part is `form.modify.view`, which contains the schema used to render the form.
275
+
276
+ ## Templates and dynamic views
277
+
278
+ Dynamic views are loaded from the `templates` object you pass through `CrudConfiguration` or `<Crud config={...}>`.
279
+
280
+ Typical setup:
281
+
282
+ ```ts
283
+ const templates = import.meta.glob('./crud/**');
284
+ ```
285
+
286
+ The resolver looks for files in this order:
287
+
288
+ 1. `crud/{namespace}/{prefix}/{ViewName}.tsx`
289
+ 2. `crud/general/{prefix}/{ViewName}.tsx`
290
+
291
+ View names are normalized to PascalCase, so:
292
+
293
+ - `list` -> `List.tsx`
294
+ - `content` -> `Content.tsx`
295
+ - `user_profile` -> `UserProfile.tsx`
296
+ - `user.profile` -> `UserProfile.tsx`
297
+
298
+ ### Useful template locations
299
+
300
+ - `crud/{namespace}/List.tsx`
301
+ - `crud/{namespace}/Add.tsx`
302
+ - `crud/{namespace}/Edit.tsx`
303
+ - `crud/{namespace}/list/Content.tsx`
304
+ - `crud/{namespace}/modify/Navigation.tsx`
305
+ - `crud/{namespace}/modify/Content.tsx`
306
+ - `crud/{namespace}/modify/form/{FormName}.tsx`
307
+ - `crud/general/...` for shared fallbacks
308
+
309
+ ### Minimal override example
310
+
311
+ ```tsx
312
+ import { AsTemplate, Block, Extend, Parent, Translation as T } from '@dakataa/crud-react';
313
+
314
+ export default AsTemplate(() => {
315
+ return (
316
+ <section className="custom-list">
317
+ <header>
318
+ <Block name="title">
319
+ <h2><T>Custom list title</T></h2>
320
+ </Block>
321
+ </header>
322
+
323
+ <main>
324
+ <Block name="content" />
325
+ </main>
326
+ </section>
327
+ );
328
+ }, { name: 'list' });
329
+ ```
330
+
331
+ Example of extending an existing template block:
332
+
333
+ ```tsx
334
+ import { Extend, Parent } from '@dakataa/crud-react';
335
+
336
+ export default function CustomModifyNavigation() {
337
+ return (
338
+ <Extend name="navigation">
339
+ <Parent />
340
+ <a href="/help" className="btn btn-sm btn-outline-secondary">Help</a>
341
+ </Extend>
342
+ );
343
+ }
344
+ ```
93
345
 
94
346
  ## Routing
95
347
 
96
- ## Hooks
97
- ### Data Provider
348
+ The package exports its own lightweight router:
349
+
350
+ ```tsx
351
+ import { Router, Route, Outlet } from '@dakataa/crud-react';
352
+ ```
353
+
354
+ Example:
355
+
356
+ ```tsx
357
+ <Router>
358
+ <Route element={<MainLayout><Outlet /></MainLayout>}>
359
+ <Route path="*" element={<CrudLoader />} />
360
+ </Route>
361
+ </Router>
362
+ ```
363
+
364
+ ### Route generation
365
+
366
+ `UseActions()` exposes helpers for route generation and matching:
367
+
368
+ - `getAction(entity, name, namespace?)`
369
+ - `getActionByPath(path)`
370
+ - `getOnClickActionByPath(path)`
371
+ - `generateActionLink(onClickAction)`
372
+ - `generateRoute(route, parameters?, query?)`
373
+ - `generatePath(pattern, parameters?, query?)`
374
+ - `navigate(to, replace?)`
375
+
376
+ ### Using a URL prefix
377
+
378
+ If your admin panel lives under `/admin`, configure a prefix:
379
+
380
+ ```tsx
381
+ <Crud
382
+ config={{
383
+ link: {
384
+ prefix: 'admin',
385
+ },
386
+ }}
387
+ >
388
+ <Router>
389
+ <Route path="*" element={<CrudLoader />} />
390
+ </Router>
391
+ </Crud>
392
+ ```
393
+
394
+ This lets internal CRUD routes stay backend-friendly while browser URLs include the app prefix.
395
+
396
+ ## Forms
397
+
398
+ The add/edit screens are driven by `form.modify.view`.
399
+
400
+ Default behavior:
98
401
 
99
- `GetDataByAction` is a Hook that allows you to fetch data based on CRUD action (see section [Actions](#legend)).
402
+ 1. `CrudLoader` resolves the current action
403
+ 2. `DataProvider` fetches the payload for that action
404
+ 3. `Modify` renders the page or modal
405
+ 4. `CrudForm` renders fields from `form.modify.view`
406
+ 5. On submit, the current action is re-issued as `POST`
407
+ 6. If the response is successful and contains a redirect, the UI navigates automatically
100
408
 
101
- ```js
102
- const {
103
- results,
104
- setAction,
105
- setParameters,
106
- setQueryParameters,
107
- refresh
108
- } = GetDataByAction({action});
409
+ ### Default form rendering
410
+
411
+ ```tsx
412
+ import { Modify } from '@dakataa/crud-react';
413
+
414
+ export default function EditArticle() {
415
+ return <Modify />;
416
+ }
109
417
  ```
110
418
 
111
- ### Actions
419
+ ### Lower-level form API
112
420
 
113
- CRUD Actions is a collection of entity routes
421
+ If you need custom rendering around the form schema:
114
422
 
115
- `UseActions` is Hook that allows you to get all available [CRUD Actions].
116
- ```js
117
- const {actions, getAction, getActionByPath} = UseActions();
423
+ ```tsx
424
+ import {
425
+ CrudForm,
426
+ FormFieldViewLoader,
427
+ FormRest,
428
+ FormRestError,
429
+ FormViewProvider,
430
+ } from '@dakataa/crud-react';
118
431
  ```
119
432
 
433
+ Useful helpers:
434
+
435
+ - `UseFormView()`: access the current form view node
436
+ - `FormFieldViewLoader`: render fields from the current schema node
437
+ - `FormRest`: render non-explicitly-rendered fields
438
+ - `FormRestError`: render non-explicitly-rendered errors
439
+
440
+ ## Hooks and contexts
441
+
442
+ ### `UseActions()`
443
+
444
+ Access loaded actions, route matching, link generation and programmatic navigation.
445
+
446
+ ### `UseCurrentAction()`
447
+
448
+ Get the current resolved CRUD action and update it from inside a view.
449
+
450
+ ### `UseDataProvider()`
451
+
452
+ Access the current action payload loaded by `DataProvider`.
453
+
454
+ ### `GetData(...)`
455
+
456
+ Fetch arbitrary data through the configured CRUD requester.
457
+
458
+ ```tsx
459
+ const { results, status, refresh, cancel } = GetData({
460
+ path: '/article',
461
+ });
462
+ ```
463
+
464
+ ### `GetDataByAction(...)`
465
+
466
+ Fetch data using an `OnClickAction`.
467
+
468
+ ```tsx
469
+ const data = GetDataByAction({
470
+ action: {
471
+ action: {
472
+ entity: 'Article',
473
+ name: 'list',
474
+ },
475
+ },
476
+ });
477
+ ```
478
+
479
+ Returned shape:
480
+
481
+ ```ts
482
+ type GetDataType = {
483
+ url: string;
484
+ status: number;
485
+ results: any;
486
+ response?: Response;
487
+ refresh: () => void;
488
+ cancel: () => void;
489
+ };
490
+ ```
491
+
492
+ ### `UseList()` and `UseListItem()`
493
+
494
+ Helpers for building custom list layouts and row renderers.
495
+
496
+ ### `UseModal()`
497
+
498
+ Open CRUD actions inside modals.
499
+
500
+ ```tsx
501
+ const { openModal, closeModal } = UseModal();
502
+ ```
503
+
504
+ ### `UseAlert()`
505
+
506
+ Open confirm, success, warning and error dialogs.
507
+
508
+ ```tsx
509
+ const { open } = UseAlert();
510
+ ```
511
+
512
+ ### `UseConfig()`
513
+
514
+ Access the merged CRUD configuration from context.
515
+
516
+ ## Core components
517
+
518
+ ### `Crud`
519
+
520
+ Top-level provider that wires config, alerts, modals, preloaders and action state.
521
+
522
+ ### `CrudLoader`
523
+
524
+ Resolves the current URL to a CRUD action and loads the matching view.
525
+
526
+ ### `List`
527
+
528
+ Default list view with filters, sorting, batch actions and pagination.
529
+
530
+ ### `Modify`
531
+
532
+ Default add/edit view with built-in success and error handling.
533
+
534
+ ### `GridView`
535
+
536
+ Bootstrap table renderer used by the default list template. Can be customized with `config.options` or component props.
537
+
538
+ ### `DynamicView`
539
+
540
+ Loads a namespace-aware template file and falls back to children when no override exists.
541
+
542
+ ### `Modal` and `UseModal`
543
+
544
+ Render CRUD screens inside nested modals.
545
+
546
+ ### `UseAlert`
547
+
548
+ Display animated alerts and confirmation dialogs.
549
+
550
+ ### `Link`, `Button`, `Translation`, `Money`, `Dropdown`
551
+
552
+ Reusable building blocks for custom CRUD templates.
553
+
554
+ ## Troubleshooting
555
+
556
+ ### `Invalid Configuration.`
557
+
558
+ `CrudLoader` throws this when `CrudConfiguration(...)` has not been called before rendering the CRUD tree.
559
+
560
+ ### `404 Not Found`
561
+
562
+ This usually means the current browser URL does not match any action returned by `/_crud/actions`.
563
+
564
+ Check:
565
+
566
+ - the backend action route
567
+ - the current browser path
568
+ - `config.link.prefix`
569
+ - whether actions were loaded successfully
570
+
571
+ ### `View "..." Not found`
572
+
573
+ This means no built-in view or dynamic template matched the resolved action name.
574
+
575
+ Check:
576
+
577
+ - that your action name is one of the supported defaults, or
578
+ - that you created the corresponding template file in `./crud/**`
579
+
580
+ ### Templates are not loading
581
+
582
+ Check:
583
+
584
+ - that `templates: import.meta.glob('./crud/**')` is passed to `CrudConfiguration(...)` or `<Crud config={...}>`
585
+ - that the file name matches the normalized PascalCase view name
586
+ - that the file is under the expected namespace or `crud/general`
587
+
588
+ ### Actions are stale
589
+
590
+ The action list is cached in `sessionStorage`. If your backend action definitions changed, clear session storage and reload.
591
+
592
+ ## Notes
593
+
594
+ If you want, the next useful step is to split this README into:
595
+
596
+ - a shorter package README for npm/GitHub
597
+ - a `/docs` folder with standalone guides such as `backend-contract.md`, `templates.md`, `forms.md`, and `recipes.md`
120
598
 
121
- ## Components
122
- ### Dynamic View
123
- ### Router
124
- ### Templates
125
- ### Modal
126
- ### Dropdown
127
- ### Translation
128
- ### Types
599
+ That usually makes the onboarding flow easier for new users.