@object-ui/react 3.0.2 → 3.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.
Files changed (61) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +8 -0
  3. package/dist/SchemaRenderer.js +61 -2
  4. package/dist/context/SchemaRendererContext.d.ts +4 -1
  5. package/dist/context/SchemaRendererContext.js +6 -4
  6. package/dist/hooks/index.d.ts +3 -0
  7. package/dist/hooks/index.js +3 -0
  8. package/dist/hooks/useActionEngine.d.ts +27 -0
  9. package/dist/hooks/useActionEngine.js +39 -0
  10. package/dist/hooks/useDebugMode.d.ts +43 -0
  11. package/dist/hooks/useDebugMode.js +71 -0
  12. package/dist/hooks/useGlobalUndo.d.ts +35 -0
  13. package/dist/hooks/useGlobalUndo.js +86 -0
  14. package/dist/hooks/useNavigationOverlay.d.ts +2 -0
  15. package/dist/hooks/useNavigationOverlay.js +12 -5
  16. package/dist/index.d.ts +3 -1
  17. package/dist/index.js +4 -1
  18. package/dist/spec-bridge/SpecBridge.d.ts +26 -0
  19. package/dist/spec-bridge/SpecBridge.js +62 -0
  20. package/dist/spec-bridge/bridges/dashboard.d.ts +81 -0
  21. package/dist/spec-bridge/bridges/dashboard.js +121 -0
  22. package/dist/spec-bridge/bridges/form-view.d.ts +44 -0
  23. package/dist/spec-bridge/bridges/form-view.js +77 -0
  24. package/dist/spec-bridge/bridges/list-view.d.ts +82 -0
  25. package/dist/spec-bridge/bridges/list-view.js +154 -0
  26. package/dist/spec-bridge/bridges/page.d.ts +68 -0
  27. package/dist/spec-bridge/bridges/page.js +81 -0
  28. package/dist/spec-bridge/index.d.ts +13 -0
  29. package/dist/spec-bridge/index.js +12 -0
  30. package/dist/spec-bridge/types.d.ts +34 -0
  31. package/{src/index.test.ts → dist/spec-bridge/types.js} +1 -8
  32. package/package.json +7 -7
  33. package/src/SchemaRenderer.tsx +66 -0
  34. package/src/__tests__/LazyPluginLoader.test.tsx +1 -1
  35. package/src/__tests__/SchemaRenderer.debug.test.tsx +69 -0
  36. package/src/__tests__/SchemaRenderer.expressions.test.tsx +131 -0
  37. package/src/__tests__/useNavigationOverlay.test.ts +162 -0
  38. package/src/context/SchemaRendererContext.tsx +9 -4
  39. package/src/context/__tests__/useDataScope.test.tsx +91 -0
  40. package/src/hooks/__tests__/useActionEngine.test.ts +170 -0
  41. package/src/hooks/__tests__/useCrudShortcuts.test.ts +85 -0
  42. package/src/hooks/__tests__/useDebugMode.test.ts +83 -0
  43. package/src/hooks/__tests__/useFocusTrap.test.ts +247 -0
  44. package/src/hooks/__tests__/usePageTransition.test.ts +158 -0
  45. package/src/hooks/__tests__/useReducedMotion.test.ts +104 -0
  46. package/src/hooks/index.ts +3 -0
  47. package/src/hooks/useActionEngine.ts +84 -0
  48. package/src/hooks/useDebugMode.ts +95 -0
  49. package/src/hooks/useGlobalUndo.ts +98 -0
  50. package/src/hooks/useNavigationOverlay.ts +14 -5
  51. package/src/index.ts +6 -0
  52. package/src/spec-bridge/SpecBridge.ts +64 -0
  53. package/src/spec-bridge/__tests__/P1SpecBridge.test.ts +855 -0
  54. package/src/spec-bridge/__tests__/SpecBridge.test.ts +550 -0
  55. package/src/spec-bridge/bridges/dashboard.ts +180 -0
  56. package/src/spec-bridge/bridges/form-view.ts +109 -0
  57. package/src/spec-bridge/bridges/list-view.ts +187 -0
  58. package/src/spec-bridge/bridges/page.ts +136 -0
  59. package/src/spec-bridge/index.ts +14 -0
  60. package/src/spec-bridge/types.ts +38 -0
  61. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,550 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ import { describe, it, expect } from 'vitest';
10
+ import { SpecBridge } from '../SpecBridge';
11
+ import { bridgeListView } from '../bridges/list-view';
12
+ import { bridgeFormView } from '../bridges/form-view';
13
+ import { bridgePage } from '../bridges/page';
14
+ import { bridgeDashboard } from '../bridges/dashboard';
15
+ import type { BridgeFn } from '../types';
16
+
17
+ describe('SpecBridge', () => {
18
+ describe('class orchestration', () => {
19
+ it('registers built-in bridges on construction', () => {
20
+ const bridge = new SpecBridge();
21
+ // All four built-in types should work without error
22
+ expect(() => bridge.transform('list', {})).not.toThrow();
23
+ expect(() => bridge.transform('form', {})).not.toThrow();
24
+ expect(() => bridge.transform('page', {})).not.toThrow();
25
+ expect(() => bridge.transform('dashboard', {})).not.toThrow();
26
+ });
27
+
28
+ it('throws for unknown spec types', () => {
29
+ const bridge = new SpecBridge();
30
+ expect(() => bridge.transform('unknown', {})).toThrow(
31
+ 'No bridge registered for spec type: unknown',
32
+ );
33
+ });
34
+
35
+ it('allows custom bridge registration', () => {
36
+ const bridge = new SpecBridge();
37
+ const customBridge: BridgeFn = (spec) => ({
38
+ type: 'custom-widget',
39
+ id: spec.id,
40
+ });
41
+ bridge.register('custom', customBridge);
42
+
43
+ const node = bridge.transform('custom', { id: 'my-custom' });
44
+ expect(node.type).toBe('custom-widget');
45
+ expect(node.id).toBe('my-custom');
46
+ });
47
+
48
+ it('allows overriding built-in bridges', () => {
49
+ const bridge = new SpecBridge();
50
+ const override: BridgeFn = () => ({ type: 'my-list' });
51
+ bridge.register('list', override);
52
+
53
+ const node = bridge.transformListView({});
54
+ expect(node.type).toBe('my-list');
55
+ });
56
+
57
+ it('passes context to bridge functions', () => {
58
+ const bridge = new SpecBridge({
59
+ user: { role: 'admin' },
60
+ variables: { theme: 'dark' },
61
+ });
62
+ const spy: BridgeFn = (_spec, ctx) => ({
63
+ type: 'test',
64
+ user: ctx.user,
65
+ variables: ctx.variables,
66
+ });
67
+ bridge.register('test', spy);
68
+
69
+ const node = bridge.transform('test', {});
70
+ expect(node.user).toEqual({ role: 'admin' });
71
+ expect(node.variables).toEqual({ theme: 'dark' });
72
+ });
73
+
74
+ it('updates context via updateContext', () => {
75
+ const bridge = new SpecBridge({ user: { role: 'viewer' } });
76
+ bridge.updateContext({ user: { role: 'admin' } });
77
+
78
+ const spy: BridgeFn = (_spec, ctx) => ({
79
+ type: 'test',
80
+ role: ctx.user?.role,
81
+ });
82
+ bridge.register('ctx-test', spy);
83
+
84
+ const node = bridge.transform('ctx-test', {});
85
+ expect(node.role).toBe('admin');
86
+ });
87
+ });
88
+
89
+ describe('bridgeListView', () => {
90
+ it('transforms a basic list view spec', () => {
91
+ const spec = {
92
+ name: 'accounts_list',
93
+ label: 'All Accounts',
94
+ type: 'grid',
95
+ columns: [
96
+ { field: 'name', label: 'Account Name', width: 200, sortable: true },
97
+ { field: 'industry', label: 'Industry', width: 150 },
98
+ ],
99
+ data: { provider: 'object', object: 'Account' },
100
+ selection: { mode: 'multiple' },
101
+ pagination: { pageSize: 25 },
102
+ };
103
+
104
+ const bridge = new SpecBridge();
105
+ const node = bridge.transformListView(spec);
106
+
107
+ expect(node.type).toBe('object-grid');
108
+ expect(node.id).toBe('accounts_list');
109
+ expect(node.label).toBe('All Accounts');
110
+ expect(node.columns).toHaveLength(2);
111
+ expect(node.columns[0].accessorKey).toBe('name');
112
+ expect(node.columns[0].header).toBe('Account Name');
113
+ expect(node.columns[0].width).toBe(200);
114
+ expect(node.columns[0].sortable).toBe(true);
115
+ expect(node.columns[1].accessorKey).toBe('industry');
116
+ expect(node.data).toEqual({ provider: 'object', object: 'Account' });
117
+ expect(node.selection).toEqual({ mode: 'multiple' });
118
+ expect(node.pagination).toEqual({ pageSize: 25 });
119
+ });
120
+
121
+ it('uses field name as header fallback', () => {
122
+ const node = bridgeListView(
123
+ { columns: [{ field: 'email' }] },
124
+ {},
125
+ );
126
+ expect(node.columns[0].header).toBe('email');
127
+ });
128
+
129
+ it('maps column properties correctly', () => {
130
+ const node = bridgeListView(
131
+ {
132
+ columns: [
133
+ {
134
+ field: 'status',
135
+ label: 'Status',
136
+ align: 'center',
137
+ hidden: false,
138
+ resizable: true,
139
+ wrap: true,
140
+ type: 'badge',
141
+ pinned: 'left',
142
+ summary: { type: 'count' },
143
+ link: { href: '/status' },
144
+ action: { type: 'navigate' },
145
+ },
146
+ ],
147
+ },
148
+ {},
149
+ );
150
+
151
+ const col = node.columns[0];
152
+ expect(col.align).toBe('center');
153
+ expect(col.hidden).toBe(false);
154
+ expect(col.resizable).toBe(true);
155
+ expect(col.wrap).toBe(true);
156
+ expect(col.type).toBe('badge');
157
+ expect(col.pinned).toBe('left');
158
+ expect(col.summary).toEqual({ type: 'count' });
159
+ expect(col.link).toEqual({ href: '/status' });
160
+ expect(col.action).toEqual({ type: 'navigate' });
161
+ });
162
+
163
+ it('maps rowHeight to density', () => {
164
+ const compact = bridgeListView({ rowHeight: 'compact' }, {});
165
+ expect(compact.density).toBe('compact');
166
+
167
+ const comfortable = bridgeListView({ rowHeight: 'comfortable' }, {});
168
+ expect(comfortable.density).toBe('comfortable');
169
+
170
+ const spacious = bridgeListView({ rowHeight: 'spacious' }, {});
171
+ expect(spacious.density).toBe('spacious');
172
+
173
+ const small = bridgeListView({ rowHeight: 'small' }, {});
174
+ expect(small.density).toBe('compact');
175
+
176
+ const short = bridgeListView({ rowHeight: 'short' }, {});
177
+ expect(short.density).toBe('compact');
178
+
179
+ const extraTall = bridgeListView({ rowHeight: 'extra_tall' }, {});
180
+ expect(extraTall.density).toBe('spacious');
181
+ });
182
+
183
+ it('includes optional list properties', () => {
184
+ const node = bridgeListView(
185
+ {
186
+ sort: { field: 'name', direction: 'asc' },
187
+ filter: { status: 'active' },
188
+ grouping: { field: 'region' },
189
+ rowColor: { field: 'priority' },
190
+ searchableFields: ['name', 'email'],
191
+ quickFilters: [{ field: 'status', values: ['active'] }],
192
+ },
193
+ {},
194
+ );
195
+
196
+ expect(node.sort).toEqual({ field: 'name', direction: 'asc' });
197
+ expect(node.filter).toEqual({ status: 'active' });
198
+ expect(node.grouping).toEqual({ field: 'region' });
199
+ expect(node.rowColor).toEqual({ field: 'priority' });
200
+ expect(node.searchableFields).toEqual(['name', 'email']);
201
+ expect(node.quickFilters).toHaveLength(1);
202
+ });
203
+
204
+ it('handles empty spec gracefully', () => {
205
+ const node = bridgeListView({}, {});
206
+ expect(node.type).toBe('object-grid');
207
+ expect(node.columns).toEqual([]);
208
+ });
209
+ });
210
+
211
+ describe('bridgeFormView', () => {
212
+ it('transforms a basic form view spec', () => {
213
+ const spec = {
214
+ type: 'create',
215
+ data: { provider: 'object', object: 'Contact' },
216
+ sections: [
217
+ {
218
+ label: 'Basic Info',
219
+ columns: 2,
220
+ fields: [
221
+ { field: 'firstName', label: 'First Name', required: true },
222
+ { field: 'lastName', label: 'Last Name', required: true },
223
+ { field: 'email', label: 'Email', placeholder: 'you@example.com' },
224
+ ],
225
+ },
226
+ ],
227
+ };
228
+
229
+ const bridge = new SpecBridge();
230
+ const node = bridge.transformFormView(spec);
231
+
232
+ expect(node.type).toBe('object-form');
233
+ expect(node.id).toBe('form-create');
234
+ expect(node.data).toEqual({ provider: 'object', object: 'Contact' });
235
+ expect(node.sections).toHaveLength(1);
236
+ expect(node.sections[0].label).toBe('Basic Info');
237
+ expect(node.sections[0].columns).toBe(2);
238
+ expect(node.sections[0].fields).toHaveLength(3);
239
+ expect(node.sections[0].fields[0].name).toBe('firstName');
240
+ expect(node.sections[0].fields[0].required).toBe(true);
241
+ });
242
+
243
+ it('maps field properties correctly', () => {
244
+ const node = bridgeFormView(
245
+ {
246
+ sections: [
247
+ {
248
+ fields: [
249
+ {
250
+ field: 'notes',
251
+ label: 'Notes',
252
+ placeholder: 'Enter notes',
253
+ helpText: 'Keep it brief',
254
+ readonly: true,
255
+ hidden: false,
256
+ colSpan: 2,
257
+ widget: 'textarea',
258
+ dependsOn: ['status'],
259
+ visibleOn: '${status === "active"}',
260
+ },
261
+ ],
262
+ },
263
+ ],
264
+ },
265
+ {},
266
+ );
267
+
268
+ const field = node.sections[0].fields[0];
269
+ expect(field.name).toBe('notes');
270
+ expect(field.label).toBe('Notes');
271
+ expect(field.placeholder).toBe('Enter notes');
272
+ expect(field.helpText).toBe('Keep it brief');
273
+ expect(field.readonly).toBe(true);
274
+ expect(field.hidden).toBe(false);
275
+ expect(field.colSpan).toBe(2);
276
+ expect(field.widget).toBe('textarea');
277
+ expect(field.dependsOn).toEqual(['status']);
278
+ expect(field.visibleOn).toBe('${status === "active"}');
279
+ });
280
+
281
+ it('maps section properties correctly', () => {
282
+ const node = bridgeFormView(
283
+ {
284
+ sections: [
285
+ {
286
+ label: 'Advanced',
287
+ collapsible: true,
288
+ collapsed: true,
289
+ columns: 3,
290
+ fields: [],
291
+ },
292
+ ],
293
+ },
294
+ {},
295
+ );
296
+
297
+ const section = node.sections[0];
298
+ expect(section.label).toBe('Advanced');
299
+ expect(section.collapsible).toBe(true);
300
+ expect(section.collapsed).toBe(true);
301
+ expect(section.columns).toBe(3);
302
+ });
303
+
304
+ it('uses default id when type is not specified', () => {
305
+ const node = bridgeFormView({}, {});
306
+ expect(node.id).toBe('form-default');
307
+ });
308
+
309
+ it('uses field name as label fallback', () => {
310
+ const node = bridgeFormView(
311
+ { sections: [{ fields: [{ field: 'age' }] }] },
312
+ {},
313
+ );
314
+ expect(node.sections[0].fields[0].label).toBe('age');
315
+ });
316
+
317
+ it('passes groups through', () => {
318
+ const node = bridgeFormView(
319
+ { groups: [{ name: 'g1', label: 'Group 1' }] },
320
+ {},
321
+ );
322
+ expect(node.groups).toEqual([{ name: 'g1', label: 'Group 1' }]);
323
+ });
324
+ });
325
+
326
+ describe('bridgePage', () => {
327
+ it('transforms a basic page spec', () => {
328
+ const spec = {
329
+ name: 'account_detail',
330
+ label: 'Account Detail',
331
+ type: 'detail',
332
+ variables: { accountId: '123' },
333
+ regions: [
334
+ {
335
+ name: 'main',
336
+ components: [
337
+ {
338
+ type: 'ui.heading',
339
+ id: 'title',
340
+ label: 'Account Details',
341
+ properties: { level: 2 },
342
+ },
343
+ {
344
+ type: 'ui.card',
345
+ id: 'info-card',
346
+ className: 'mt-4',
347
+ },
348
+ ],
349
+ },
350
+ ],
351
+ };
352
+
353
+ const bridge = new SpecBridge();
354
+ const node = bridge.transformPage(spec);
355
+
356
+ expect(node.type).toBe('page');
357
+ expect(node.id).toBe('account_detail');
358
+ expect(node.label).toBe('Account Detail');
359
+ expect(node.pageType).toBe('detail');
360
+ expect(node.variables).toEqual({ accountId: '123' });
361
+ expect(Array.isArray(node.body)).toBe(true);
362
+
363
+ const regions = node.body as any[];
364
+ expect(regions).toHaveLength(1);
365
+ expect(regions[0].type).toBe('page-region');
366
+ expect(regions[0].id).toBe('main');
367
+
368
+ const components = regions[0].body as any[];
369
+ expect(components).toHaveLength(2);
370
+ expect(components[0].type).toBe('ui.heading');
371
+ expect(components[0].level).toBe(2);
372
+ expect(components[1].className).toBe('mt-4');
373
+ });
374
+
375
+ it('maps component properties and events', () => {
376
+ const node = bridgePage(
377
+ {
378
+ regions: [
379
+ {
380
+ components: [
381
+ {
382
+ type: 'ui.button',
383
+ id: 'save-btn',
384
+ label: 'Save',
385
+ events: { onClick: 'save' },
386
+ visibility: '${canEdit}',
387
+ dataSource: { provider: 'api', read: '/api/data' },
388
+ responsive: { sm: 'hidden' },
389
+ aria: { label: 'Save record' },
390
+ },
391
+ ],
392
+ },
393
+ ],
394
+ },
395
+ {},
396
+ );
397
+
398
+ const regions = node.body as any[];
399
+ const comp = regions[0].body[0];
400
+ expect(comp.type).toBe('ui.button');
401
+ expect(comp.label).toBe('Save');
402
+ expect(comp.events).toEqual({ onClick: 'save' });
403
+ expect(comp.visibility).toBe('${canEdit}');
404
+ expect(comp.dataSource).toEqual({ provider: 'api', read: '/api/data' });
405
+ expect(comp.responsive).toEqual({ sm: 'hidden' });
406
+ expect(comp.aria).toEqual({ label: 'Save record' });
407
+ });
408
+
409
+ it('includes template and object when present', () => {
410
+ const node = bridgePage(
411
+ { template: 'two-column', object: 'Account' },
412
+ {},
413
+ );
414
+ expect(node.template).toBe('two-column');
415
+ expect(node.object).toBe('Account');
416
+ });
417
+
418
+ it('handles empty regions gracefully', () => {
419
+ const node = bridgePage({}, {});
420
+ expect(node.type).toBe('page');
421
+ expect(node.body).toEqual([]);
422
+ });
423
+ });
424
+
425
+ describe('bridgeDashboard', () => {
426
+ it('transforms a basic dashboard spec', () => {
427
+ const spec = {
428
+ name: 'sales_dashboard',
429
+ label: 'Sales Dashboard',
430
+ widgets: [
431
+ {
432
+ title: 'Revenue',
433
+ type: 'bar-chart',
434
+ object: 'Opportunity',
435
+ categoryField: 'stage',
436
+ valueField: 'amount',
437
+ aggregate: 'sum',
438
+ },
439
+ {
440
+ title: 'Pipeline',
441
+ type: 'metric',
442
+ object: 'Opportunity',
443
+ valueField: 'count',
444
+ },
445
+ ],
446
+ header: { title: 'Sales Overview' },
447
+ globalFilters: [
448
+ { field: 'region', type: 'select' },
449
+ ],
450
+ };
451
+
452
+ const bridge = new SpecBridge();
453
+ const node = bridge.transformDashboard(spec);
454
+
455
+ expect(node.type).toBe('dashboard');
456
+ expect(node.id).toBe('sales_dashboard');
457
+ expect(node.label).toBe('Sales Dashboard');
458
+
459
+ const body = node.body as any[];
460
+ // header + filter-bar + widget-grid
461
+ expect(body).toHaveLength(3);
462
+ expect(body[0].type).toBe('dashboard-header');
463
+ expect(body[0].title).toBe('Sales Overview');
464
+ expect(body[1].type).toBe('filter-bar');
465
+ expect(body[1].filters).toHaveLength(1);
466
+
467
+ const widgetGrid = body[2];
468
+ expect(widgetGrid.type).toBe('widget-grid');
469
+ const widgets = widgetGrid.body as any[];
470
+ expect(widgets).toHaveLength(2);
471
+ expect(widgets[0].type).toBe('bar-chart');
472
+ expect(widgets[0].title).toBe('Revenue');
473
+ expect(widgets[0].categoryField).toBe('stage');
474
+ expect(widgets[0].valueField).toBe('amount');
475
+ expect(widgets[0].aggregate).toBe('sum');
476
+ expect(widgets[1].type).toBe('metric');
477
+ });
478
+
479
+ it('maps widget properties correctly', () => {
480
+ const node = bridgeDashboard(
481
+ {
482
+ widgets: [
483
+ {
484
+ title: 'Chart',
485
+ description: 'A chart',
486
+ type: 'line-chart',
487
+ chartConfig: { smooth: true },
488
+ colorVariant: 'blue',
489
+ object: 'Deal',
490
+ filter: { status: 'won' },
491
+ categoryField: 'month',
492
+ valueField: 'revenue',
493
+ aggregate: 'avg',
494
+ measures: [{ field: 'revenue', agg: 'sum' }],
495
+ layout: { x: 0, y: 0, w: 6, h: 4 },
496
+ },
497
+ ],
498
+ },
499
+ {},
500
+ );
501
+
502
+ const widgetGrid = (node.body as any[])[0];
503
+ const widget = widgetGrid.body[0];
504
+ expect(widget.title).toBe('Chart');
505
+ expect(widget.description).toBe('A chart');
506
+ expect(widget.chartConfig).toEqual({ smooth: true });
507
+ expect(widget.colorVariant).toBe('blue');
508
+ expect(widget.object).toBe('Deal');
509
+ expect(widget.filter).toEqual({ status: 'won' });
510
+ expect(widget.measures).toHaveLength(1);
511
+ expect(widget.layout).toEqual({ x: 0, y: 0, w: 6, h: 4 });
512
+ });
513
+
514
+ it('omits header and filters when not present', () => {
515
+ const node = bridgeDashboard({ widgets: [] }, {});
516
+ const body = node.body as any[];
517
+ // Only widget-grid
518
+ expect(body).toHaveLength(1);
519
+ expect(body[0].type).toBe('widget-grid');
520
+ });
521
+
522
+ it('assigns sequential widget ids', () => {
523
+ const node = bridgeDashboard(
524
+ {
525
+ widgets: [
526
+ { title: 'A', type: 'metric' },
527
+ { title: 'B', type: 'metric' },
528
+ { title: 'C', type: 'metric' },
529
+ ],
530
+ },
531
+ {},
532
+ );
533
+
534
+ const widgetGrid = (node.body as any[])[0];
535
+ const widgets = widgetGrid.body as any[];
536
+ expect(widgets[0].id).toBe('widget-0');
537
+ expect(widgets[1].id).toBe('widget-1');
538
+ expect(widgets[2].id).toBe('widget-2');
539
+ });
540
+
541
+ it('defaults widget type to chart', () => {
542
+ const node = bridgeDashboard(
543
+ { widgets: [{ title: 'Untitled' }] },
544
+ {},
545
+ );
546
+ const widgetGrid = (node.body as any[])[0];
547
+ expect(widgetGrid.body[0].type).toBe('chart');
548
+ });
549
+ });
550
+ });