@object-ui/react 0.3.0 → 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.
Files changed (40) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/SchemaRenderer.d.ts +7 -0
  3. package/dist/SchemaRenderer.js +7 -0
  4. package/dist/components/form/FieldFactory.d.ts +43 -0
  5. package/dist/components/form/FieldFactory.js +157 -0
  6. package/dist/components/form/FormRenderer.d.ts +40 -0
  7. package/dist/components/form/FormRenderer.js +87 -0
  8. package/dist/components/form/FormRenderer.test.d.ts +8 -0
  9. package/dist/components/form/FormRenderer.test.js +534 -0
  10. package/dist/components/form/index.d.ts +11 -0
  11. package/dist/components/form/index.js +9 -0
  12. package/dist/context/SchemaRendererContext.d.ts +13 -0
  13. package/dist/context/SchemaRendererContext.js +21 -0
  14. package/dist/context/index.d.ts +8 -1
  15. package/dist/context/index.js +8 -1
  16. package/dist/hooks/index.d.ts +9 -1
  17. package/dist/hooks/index.js +9 -1
  18. package/dist/hooks/useActionRunner.d.ts +32 -0
  19. package/dist/hooks/useActionRunner.js +65 -0
  20. package/dist/hooks/useExpression.d.ts +27 -0
  21. package/dist/hooks/useExpression.js +41 -0
  22. package/dist/index.d.ts +8 -0
  23. package/dist/index.js +8 -0
  24. package/dist/index.test.d.ts +7 -0
  25. package/dist/index.test.js +7 -0
  26. package/package.json +16 -4
  27. package/src/SchemaRenderer.tsx +8 -0
  28. package/src/components/form/FieldFactory.tsx +504 -0
  29. package/src/components/form/FormRenderer.test.tsx +599 -0
  30. package/src/components/form/FormRenderer.tsx +209 -0
  31. package/src/components/form/README.md +352 -0
  32. package/src/components/form/index.ts +12 -0
  33. package/src/context/SchemaRendererContext.tsx +40 -0
  34. package/src/context/index.ts +9 -1
  35. package/src/hooks/index.ts +11 -1
  36. package/src/hooks/useActionRunner.ts +81 -0
  37. package/src/hooks/useExpression.ts +56 -0
  38. package/src/index.test.ts +8 -0
  39. package/src/index.ts +9 -0
  40. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,534 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ /**
3
+ * ObjectUI
4
+ * Copyright (c) 2024-present ObjectStack Inc.
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
9
+ import { describe, it, expect } from 'vitest';
10
+ import { render, screen } from '@testing-library/react';
11
+ import '@testing-library/jest-dom';
12
+ import { FormRenderer } from './FormRenderer';
13
+ describe('FormRenderer', () => {
14
+ it('should render a simple form with text fields', () => {
15
+ const schema = {
16
+ type: 'simple',
17
+ sections: [
18
+ {
19
+ label: 'Personal Information',
20
+ collapsible: false,
21
+ collapsed: false,
22
+ columns: 2,
23
+ fields: [
24
+ {
25
+ field: 'firstName',
26
+ label: 'First Name',
27
+ required: true,
28
+ widget: 'text',
29
+ },
30
+ {
31
+ field: 'lastName',
32
+ label: 'Last Name',
33
+ required: true,
34
+ widget: 'text',
35
+ },
36
+ ],
37
+ },
38
+ ],
39
+ };
40
+ const { container } = render(_jsx(FormRenderer, { schema: schema }));
41
+ expect(screen.getByText('Personal Information')).toBeInTheDocument();
42
+ expect(screen.getByLabelText(/First Name/)).toBeInTheDocument();
43
+ expect(screen.getByLabelText(/Last Name/)).toBeInTheDocument();
44
+ });
45
+ it('should render form with initial data', () => {
46
+ const schema = {
47
+ type: 'simple',
48
+ sections: [
49
+ {
50
+ label: 'User Details',
51
+ collapsible: false,
52
+ collapsed: false,
53
+ columns: 1,
54
+ fields: [
55
+ {
56
+ field: 'username',
57
+ label: 'Username',
58
+ widget: 'text',
59
+ },
60
+ ],
61
+ },
62
+ ],
63
+ };
64
+ const data = { username: 'john_doe' };
65
+ render(_jsx(FormRenderer, { schema: schema, data: data }));
66
+ const input = screen.getByLabelText('Username');
67
+ expect(input.value).toBe('john_doe');
68
+ });
69
+ it('should handle multi-column layout', () => {
70
+ const schema = {
71
+ type: 'simple',
72
+ sections: [
73
+ {
74
+ label: 'Contact',
75
+ collapsible: false,
76
+ collapsed: false,
77
+ columns: 3,
78
+ fields: [
79
+ {
80
+ field: 'email',
81
+ label: 'Email',
82
+ widget: 'email',
83
+ },
84
+ {
85
+ field: 'phone',
86
+ label: 'Phone',
87
+ widget: 'tel',
88
+ },
89
+ {
90
+ field: 'address',
91
+ label: 'Address',
92
+ widget: 'textarea',
93
+ colSpan: 3,
94
+ },
95
+ ],
96
+ },
97
+ ],
98
+ };
99
+ render(_jsx(FormRenderer, { schema: schema }));
100
+ expect(screen.getByLabelText('Email')).toBeInTheDocument();
101
+ expect(screen.getByLabelText('Phone')).toBeInTheDocument();
102
+ expect(screen.getByLabelText('Address')).toBeInTheDocument();
103
+ });
104
+ it('should hide fields marked as hidden', () => {
105
+ const schema = {
106
+ type: 'simple',
107
+ sections: [
108
+ {
109
+ label: 'Form',
110
+ collapsible: false,
111
+ collapsed: false,
112
+ columns: 1,
113
+ fields: [
114
+ {
115
+ field: 'visible',
116
+ label: 'Visible Field',
117
+ widget: 'text',
118
+ },
119
+ {
120
+ field: 'hidden',
121
+ label: 'Hidden Field',
122
+ widget: 'text',
123
+ hidden: true,
124
+ },
125
+ ],
126
+ },
127
+ ],
128
+ };
129
+ render(_jsx(FormRenderer, { schema: schema }));
130
+ expect(screen.getByLabelText('Visible Field')).toBeInTheDocument();
131
+ expect(screen.queryByLabelText('Hidden Field')).not.toBeInTheDocument();
132
+ });
133
+ it('should support legacy string field format', () => {
134
+ const schema = {
135
+ type: 'simple',
136
+ sections: [
137
+ {
138
+ label: 'Legacy Form',
139
+ collapsible: false,
140
+ collapsed: false,
141
+ columns: 1,
142
+ fields: ['username', 'email'],
143
+ },
144
+ ],
145
+ };
146
+ const { container } = render(_jsx(FormRenderer, { schema: schema }));
147
+ // String fields should still render as inputs
148
+ const inputs = container.querySelectorAll('input[type="text"]');
149
+ expect(inputs.length).toBeGreaterThan(0);
150
+ });
151
+ it('should handle number fields', () => {
152
+ const schema = {
153
+ type: 'simple',
154
+ sections: [
155
+ {
156
+ label: 'Numbers',
157
+ collapsible: false,
158
+ collapsed: false,
159
+ columns: 1,
160
+ fields: [
161
+ {
162
+ field: 'age',
163
+ label: 'Age',
164
+ widget: 'number',
165
+ required: true,
166
+ },
167
+ ],
168
+ },
169
+ ],
170
+ };
171
+ render(_jsx(FormRenderer, { schema: schema }));
172
+ const input = screen.getByLabelText(/Age/);
173
+ expect(input.type).toBe('number');
174
+ });
175
+ it('should handle checkbox fields', () => {
176
+ const schema = {
177
+ type: 'simple',
178
+ sections: [
179
+ {
180
+ label: 'Preferences',
181
+ collapsible: false,
182
+ collapsed: false,
183
+ columns: 1,
184
+ fields: [
185
+ {
186
+ field: 'subscribe',
187
+ label: 'Subscribe to newsletter',
188
+ widget: 'checkbox',
189
+ },
190
+ ],
191
+ },
192
+ ],
193
+ };
194
+ render(_jsx(FormRenderer, { schema: schema }));
195
+ const checkbox = screen.getByLabelText('Subscribe to newsletter');
196
+ expect(checkbox.type).toBe('checkbox');
197
+ });
198
+ it('should render help text when provided', () => {
199
+ const schema = {
200
+ type: 'simple',
201
+ sections: [
202
+ {
203
+ label: 'Help Text',
204
+ collapsible: false,
205
+ collapsed: false,
206
+ columns: 1,
207
+ fields: [
208
+ {
209
+ field: 'password',
210
+ label: 'Password',
211
+ widget: 'password',
212
+ helpText: 'Must be at least 8 characters',
213
+ },
214
+ ],
215
+ },
216
+ ],
217
+ };
218
+ render(_jsx(FormRenderer, { schema: schema }));
219
+ expect(screen.getByText('Must be at least 8 characters')).toBeInTheDocument();
220
+ });
221
+ it('should mark required fields with asterisk', () => {
222
+ const schema = {
223
+ type: 'simple',
224
+ sections: [
225
+ {
226
+ label: 'Required',
227
+ collapsible: false,
228
+ collapsed: false,
229
+ columns: 1,
230
+ fields: [
231
+ {
232
+ field: 'name',
233
+ label: 'Name',
234
+ widget: 'text',
235
+ required: true,
236
+ },
237
+ ],
238
+ },
239
+ ],
240
+ };
241
+ const { container } = render(_jsx(FormRenderer, { schema: schema }));
242
+ // Check for asterisk
243
+ const asterisk = container.querySelector('.text-red-500');
244
+ expect(asterisk).toBeInTheDocument();
245
+ expect(asterisk?.textContent).toContain('*');
246
+ });
247
+ // Test new field types
248
+ describe('Extended field types', () => {
249
+ it('should render currency field with dollar sign', () => {
250
+ const schema = {
251
+ type: 'simple',
252
+ sections: [
253
+ {
254
+ label: 'Pricing',
255
+ collapsible: false,
256
+ collapsed: false,
257
+ columns: 1,
258
+ fields: [
259
+ {
260
+ field: 'price',
261
+ label: 'Price',
262
+ widget: 'currency',
263
+ },
264
+ ],
265
+ },
266
+ ],
267
+ };
268
+ const { container } = render(_jsx(FormRenderer, { schema: schema }));
269
+ expect(screen.getByLabelText('Price')).toBeInTheDocument();
270
+ expect(container.querySelector('.absolute')?.textContent).toBe('$');
271
+ });
272
+ it('should render percent field with percent sign', () => {
273
+ const schema = {
274
+ type: 'simple',
275
+ sections: [
276
+ {
277
+ label: 'Metrics',
278
+ collapsible: false,
279
+ collapsed: false,
280
+ columns: 1,
281
+ fields: [
282
+ {
283
+ field: 'discount',
284
+ label: 'Discount',
285
+ widget: 'percent',
286
+ },
287
+ ],
288
+ },
289
+ ],
290
+ };
291
+ const { container } = render(_jsx(FormRenderer, { schema: schema }));
292
+ expect(screen.getByLabelText('Discount')).toBeInTheDocument();
293
+ expect(container.querySelector('.absolute')?.textContent).toBe('%');
294
+ });
295
+ it('should render phone field', () => {
296
+ const schema = {
297
+ type: 'simple',
298
+ sections: [
299
+ {
300
+ label: 'Contact',
301
+ collapsible: false,
302
+ collapsed: false,
303
+ columns: 1,
304
+ fields: [
305
+ {
306
+ field: 'phoneNumber',
307
+ label: 'Phone Number',
308
+ widget: 'phone',
309
+ },
310
+ ],
311
+ },
312
+ ],
313
+ };
314
+ render(_jsx(FormRenderer, { schema: schema }));
315
+ const input = screen.getByLabelText('Phone Number');
316
+ expect(input.type).toBe('tel');
317
+ });
318
+ it('should render markdown field as textarea', () => {
319
+ const schema = {
320
+ type: 'simple',
321
+ sections: [
322
+ {
323
+ label: 'Content',
324
+ collapsible: false,
325
+ collapsed: false,
326
+ columns: 1,
327
+ fields: [
328
+ {
329
+ field: 'content',
330
+ label: 'Content',
331
+ widget: 'markdown',
332
+ },
333
+ ],
334
+ },
335
+ ],
336
+ };
337
+ render(_jsx(FormRenderer, { schema: schema }));
338
+ const textarea = screen.getByLabelText('Content');
339
+ expect(textarea.tagName).toBe('TEXTAREA');
340
+ });
341
+ it('should render html field as textarea', () => {
342
+ const schema = {
343
+ type: 'simple',
344
+ sections: [
345
+ {
346
+ label: 'Content',
347
+ collapsible: false,
348
+ collapsed: false,
349
+ columns: 1,
350
+ fields: [
351
+ {
352
+ field: 'htmlContent',
353
+ label: 'HTML Content',
354
+ widget: 'html',
355
+ },
356
+ ],
357
+ },
358
+ ],
359
+ };
360
+ render(_jsx(FormRenderer, { schema: schema }));
361
+ const textarea = screen.getByLabelText('HTML Content');
362
+ expect(textarea.tagName).toBe('TEXTAREA');
363
+ });
364
+ it('should render file field', () => {
365
+ const schema = {
366
+ type: 'simple',
367
+ sections: [
368
+ {
369
+ label: 'Upload',
370
+ collapsible: false,
371
+ collapsed: false,
372
+ columns: 1,
373
+ fields: [
374
+ {
375
+ field: 'document',
376
+ label: 'Document',
377
+ widget: 'file',
378
+ },
379
+ ],
380
+ },
381
+ ],
382
+ };
383
+ render(_jsx(FormRenderer, { schema: schema }));
384
+ const input = screen.getByLabelText('Document');
385
+ expect(input.type).toBe('file');
386
+ });
387
+ it('should render image field', () => {
388
+ const schema = {
389
+ type: 'simple',
390
+ sections: [
391
+ {
392
+ label: 'Upload',
393
+ collapsible: false,
394
+ collapsed: false,
395
+ columns: 1,
396
+ fields: [
397
+ {
398
+ field: 'avatar',
399
+ label: 'Avatar',
400
+ widget: 'image',
401
+ },
402
+ ],
403
+ },
404
+ ],
405
+ };
406
+ render(_jsx(FormRenderer, { schema: schema }));
407
+ const input = screen.getByLabelText('Avatar');
408
+ expect(input.type).toBe('file');
409
+ });
410
+ it('should render location field with lat/lng inputs', () => {
411
+ const schema = {
412
+ type: 'simple',
413
+ sections: [
414
+ {
415
+ label: 'Location',
416
+ collapsible: false,
417
+ collapsed: false,
418
+ columns: 1,
419
+ fields: [
420
+ {
421
+ field: 'coordinates',
422
+ label: 'Coordinates',
423
+ widget: 'location',
424
+ },
425
+ ],
426
+ },
427
+ ],
428
+ };
429
+ const { container } = render(_jsx(FormRenderer, { schema: schema }));
430
+ const inputs = container.querySelectorAll('input[type="number"]');
431
+ expect(inputs.length).toBe(2);
432
+ });
433
+ it('should render lookup field as select', () => {
434
+ const schema = {
435
+ type: 'simple',
436
+ sections: [
437
+ {
438
+ label: 'Relations',
439
+ collapsible: false,
440
+ collapsed: false,
441
+ columns: '1',
442
+ fields: [
443
+ {
444
+ field: 'account',
445
+ label: 'Account',
446
+ widget: 'lookup',
447
+ options: [
448
+ { label: 'Account 1', value: '1' },
449
+ { label: 'Account 2', value: '2' },
450
+ ],
451
+ },
452
+ ],
453
+ },
454
+ ],
455
+ };
456
+ render(_jsx(FormRenderer, { schema: schema }));
457
+ const select = screen.getByLabelText('Account');
458
+ expect(select.tagName).toBe('SELECT');
459
+ });
460
+ it('should render user field as select', () => {
461
+ const schema = {
462
+ type: 'simple',
463
+ sections: [
464
+ {
465
+ label: 'Assignment',
466
+ collapsible: false,
467
+ collapsed: false,
468
+ columns: '1',
469
+ fields: [
470
+ {
471
+ field: 'assignedTo',
472
+ label: 'Assigned To',
473
+ widget: 'user',
474
+ options: [
475
+ { label: 'User 1', value: 'user1' },
476
+ { label: 'User 2', value: 'user2' },
477
+ ],
478
+ },
479
+ ],
480
+ },
481
+ ],
482
+ };
483
+ render(_jsx(FormRenderer, { schema: schema }));
484
+ const select = screen.getByLabelText('Assigned To');
485
+ expect(select.tagName).toBe('SELECT');
486
+ });
487
+ it('should render formula field as disabled input', () => {
488
+ const schema = {
489
+ type: 'simple',
490
+ sections: [
491
+ {
492
+ label: 'Computed',
493
+ collapsible: false,
494
+ collapsed: false,
495
+ columns: 1,
496
+ fields: [
497
+ {
498
+ field: 'total',
499
+ label: 'Total',
500
+ widget: 'formula',
501
+ },
502
+ ],
503
+ },
504
+ ],
505
+ };
506
+ render(_jsx(FormRenderer, { schema: schema }));
507
+ const input = screen.getByLabelText('Total');
508
+ expect(input.disabled).toBe(true);
509
+ });
510
+ it('should render object field as JSON textarea', () => {
511
+ const schema = {
512
+ type: 'simple',
513
+ sections: [
514
+ {
515
+ label: 'Data',
516
+ collapsible: false,
517
+ collapsed: false,
518
+ columns: 1,
519
+ fields: [
520
+ {
521
+ field: 'metadata',
522
+ label: 'Metadata',
523
+ widget: 'object',
524
+ },
525
+ ],
526
+ },
527
+ ],
528
+ };
529
+ render(_jsx(FormRenderer, { schema: schema }));
530
+ const textarea = screen.getByLabelText('Metadata');
531
+ expect(textarea.tagName).toBe('TEXTAREA');
532
+ });
533
+ });
534
+ });
@@ -0,0 +1,11 @@
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
+ export { FormRenderer } from './FormRenderer';
9
+ export type { FormRendererProps } from './FormRenderer';
10
+ export { FieldFactory } from './FieldFactory';
11
+ export type { FieldFactoryProps, ExtendedFormField } from './FieldFactory';
@@ -0,0 +1,9 @@
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
+ export { FormRenderer } from './FormRenderer';
9
+ export { FieldFactory } from './FieldFactory';
@@ -0,0 +1,13 @@
1
+ import React from 'react';
2
+ interface SchemaRendererContextType {
3
+ dataSource: any;
4
+ debug?: boolean;
5
+ }
6
+ export declare const SchemaRendererProvider: ({ children, dataSource, debug }: {
7
+ children: React.ReactNode;
8
+ dataSource: any;
9
+ debug?: boolean;
10
+ }) => import("react/jsx-runtime").JSX.Element;
11
+ export declare const useSchemaContext: () => SchemaRendererContextType;
12
+ export declare const useDataScope: (path?: string) => any;
13
+ export {};
@@ -0,0 +1,21 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { createContext, useContext, useMemo } from 'react';
3
+ const SchemaRendererContext = createContext(null);
4
+ export const SchemaRendererProvider = ({ children, dataSource, debug }) => {
5
+ const value = useMemo(() => ({ dataSource, debug }), [dataSource, debug]);
6
+ return (_jsx(SchemaRendererContext.Provider, { value: value, children: children }));
7
+ };
8
+ export const useSchemaContext = () => {
9
+ const context = useContext(SchemaRendererContext);
10
+ if (!context) {
11
+ throw new Error('useSchemaContext must be used within a SchemaRendererProvider');
12
+ }
13
+ return context;
14
+ };
15
+ export const useDataScope = (path) => {
16
+ const { dataSource } = useSchemaContext();
17
+ if (!path)
18
+ return dataSource;
19
+ // Simple path resolution for now. In real app might be more complex
20
+ return path.split('.').reduce((acc, part) => acc && acc[part], dataSource);
21
+ };
@@ -1 +1,8 @@
1
- export {};
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
+ export * from './SchemaRendererContext';
@@ -1 +1,8 @@
1
- export {};
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
+ export * from './SchemaRendererContext';
@@ -1 +1,9 @@
1
- export {};
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
+ export * from './useExpression';
9
+ export * from './useActionRunner';
@@ -1 +1,9 @@
1
- export {};
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
+ export * from './useExpression';
9
+ export * from './useActionRunner';
@@ -0,0 +1,32 @@
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
+ import { ActionRunner, type ActionContext, type ActionResult } from '@object-ui/core';
9
+ /**
10
+ * Hook for executing actions with loading state
11
+ *
12
+ * @example
13
+ * ```tsx
14
+ * const { execute, loading, error, result } = useActionRunner({ data: formData });
15
+ *
16
+ * const handleClick = () => {
17
+ * execute({
18
+ * type: 'action',
19
+ * api: '/api/save',
20
+ * successMessage: 'Saved successfully!'
21
+ * });
22
+ * };
23
+ * ```
24
+ */
25
+ export declare function useActionRunner(context?: ActionContext): {
26
+ execute: (action: any) => Promise<ActionResult>;
27
+ loading: boolean;
28
+ error: string | null;
29
+ result: ActionResult | null;
30
+ updateContext: (newContext: Partial<ActionContext>) => void;
31
+ runner: ActionRunner;
32
+ };