@decaf-ts/ui-decorators 0.5.8 → 0.5.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.
Files changed (58) hide show
  1. package/README.md +422 -12
  2. package/dist/ui-decorators.cjs +612 -77
  3. package/dist/ui-decorators.esm.cjs +611 -78
  4. package/lib/esm/index.d.ts +7 -3
  5. package/lib/esm/index.js +8 -4
  6. package/lib/esm/model/Renderable.d.ts +15 -0
  7. package/lib/esm/model/Renderable.js +1 -1
  8. package/lib/esm/model/decorators.d.ts +105 -7
  9. package/lib/esm/model/decorators.js +117 -8
  10. package/lib/esm/model/index.d.ts +0 -4
  11. package/lib/esm/model/index.js +1 -5
  12. package/lib/esm/model/model.d.ts +156 -50
  13. package/lib/esm/model/model.js +1 -1
  14. package/lib/esm/model/overrides.d.ts +8 -0
  15. package/lib/esm/model/overrides.js +19 -1
  16. package/lib/esm/ui/Rendering.js +89 -46
  17. package/lib/esm/ui/constants.d.ts +149 -4
  18. package/lib/esm/ui/constants.js +150 -5
  19. package/lib/esm/ui/decorators.d.ts +192 -15
  20. package/lib/esm/ui/decorators.js +203 -18
  21. package/lib/esm/ui/errors.d.ts +28 -0
  22. package/lib/esm/ui/errors.js +29 -1
  23. package/lib/esm/ui/index.d.ts +0 -4
  24. package/lib/esm/ui/index.js +1 -5
  25. package/lib/esm/ui/interfaces.d.ts +25 -0
  26. package/lib/esm/ui/interfaces.js +9 -1
  27. package/lib/esm/ui/types.d.ts +85 -2
  28. package/lib/esm/ui/types.js +9 -1
  29. package/lib/esm/ui/utils.d.ts +1 -1
  30. package/lib/esm/ui/utils.js +2 -2
  31. package/lib/index.cjs +8 -4
  32. package/lib/index.d.ts +7 -3
  33. package/lib/model/Renderable.cjs +1 -1
  34. package/lib/model/Renderable.d.ts +15 -0
  35. package/lib/model/decorators.cjs +118 -8
  36. package/lib/model/decorators.d.ts +105 -7
  37. package/lib/model/index.cjs +1 -5
  38. package/lib/model/index.d.ts +0 -4
  39. package/lib/model/model.cjs +1 -1
  40. package/lib/model/model.d.ts +156 -50
  41. package/lib/model/overrides.cjs +19 -1
  42. package/lib/model/overrides.d.ts +8 -0
  43. package/lib/ui/Rendering.cjs +89 -46
  44. package/lib/ui/constants.cjs +150 -5
  45. package/lib/ui/constants.d.ts +149 -4
  46. package/lib/ui/decorators.cjs +204 -18
  47. package/lib/ui/decorators.d.ts +192 -15
  48. package/lib/ui/errors.cjs +29 -1
  49. package/lib/ui/errors.d.ts +28 -0
  50. package/lib/ui/index.cjs +1 -5
  51. package/lib/ui/index.d.ts +0 -4
  52. package/lib/ui/interfaces.cjs +9 -1
  53. package/lib/ui/interfaces.d.ts +25 -0
  54. package/lib/ui/types.cjs +9 -1
  55. package/lib/ui/types.d.ts +85 -2
  56. package/lib/ui/utils.cjs +2 -2
  57. package/lib/ui/utils.d.ts +1 -1
  58. package/package.json +1 -1
package/README.md CHANGED
@@ -2,8 +2,9 @@
2
2
 
3
3
  ## User Interface Decorators
4
4
 
5
- Introduces a declarative way to render UI forms
5
+ A TypeScript library that provides a declarative approach to UI rendering through model decorators. It extends the functionality of `@decaf-ts/decorator-validation` by adding UI rendering capabilities to models, allowing them to be automatically rendered as UI components with proper validation and styling.
6
6
 
7
+ The library offers a flexible rendering engine architecture that can be extended to support different UI frameworks (React, Angular, HTML5, etc.) while maintaining a consistent model-driven approach to UI development. It bridges the gap between data models and their visual representation, enabling developers to define UI behavior directly on their domain models.
7
8
 
8
9
 
9
10
  ![Licence](https://img.shields.io/github/license/decaf-ts/ui-decorators.svg?style=plastic)
@@ -31,17 +32,52 @@ Documentation available [here](https://decaf-ts.github.io/ui-decorators/)
31
32
 
32
33
  ### Description
33
34
 
34
- Extension of `db-decorators`, exposes a simple implementation to handle automatic model rendering:
35
- - decorate classes and attributes as UI elements or UI element properties;
36
- - provides the base objects to implement `RenderingEngine` specific to each tech (Ionic, Angular, React, HTML5, etc);
37
- - automatic CRUD view rendering;
38
- - automatic UI validation according to `decorator-validation`'s decorators;
39
- - enables automatic custom validation (not HTML standard);
35
+ The UI Decorators library is an extension of `@decaf-ts/decorator-validation` and `@decaf-ts/db-decorators` that provides a comprehensive framework for automatic model rendering in user interfaces. It enables a declarative approach to UI development by allowing developers to define how their data models should be rendered directly on the model classes and properties.
40
36
 
41
- Adds a new Decorator ```uimodel``` to add UI metadata to the model, that can be later interpreted by different rendering strategies
42
- Adds a new Decorator ```uiprop``` to add UI metadata to the model's properties, that can later be converted into properties for the `uimodel`
43
- Adds a new Decorator ```uielement``` to add UI metadata to the model's properties, that can later be converted into Graphical elements
44
- Adds a new Decorator ```hideOn``` to add UI metadata to the model's properties, so heir visibility can be controlled depending on the CRUD operation
37
+ #### Core Functionality
38
+
39
+ - **Model Rendering**: Extends the Model class with the ability to render itself as a UI component
40
+ - **Flexible Rendering Engine**: Provides an abstract RenderingEngine class that can be implemented for different UI frameworks
41
+ - **Validation Integration**: Automatically applies validation rules from `@decaf-ts/decorator-validation` to UI elements
42
+ - **CRUD Operation Support**: Controls element visibility and behavior based on the current CRUD operation (Create, Read, Update, Delete)
43
+ - **List Rendering**: Special support for rendering collections of models as lists with customizable item templates
44
+
45
+ #### Class Decorators
46
+
47
+ - **`@uimodel(tag?, props?)`**: Marks a class as a UI model and specifies how it should be rendered, including the HTML tag to use and additional properties
48
+ - **`@renderedBy(engine)`**: Specifies which rendering engine implementation should be used for a particular model
49
+ - **`@uilistitem(tag?, props?)`**: Defines how a model should be rendered when it appears as an item in a list
50
+
51
+ #### Property Decorators
52
+
53
+ - **`@uiprop(propName?, stringify?)`**: Maps a model property to a UI component property, optionally with a different name or stringified
54
+ - **`@uielement(tag, props?, serialize?)`**: Maps a model property to a specific UI element with custom properties
55
+ - **`@uilistprop(propName?, props?)`**: Maps a model property containing a list to a list container component
56
+ - **`@hideOn(...operations)`**: Hides a property during specific CRUD operations
57
+ - **`@hidden()`**: Completely hides a property in all UI operations
58
+
59
+ #### Rendering Engine
60
+
61
+ The abstract `RenderingEngine` class provides the foundation for implementing rendering strategies for different UI frameworks:
62
+
63
+ - **Type Translation**: Converts between model types and HTML input types
64
+ - **Validation Handling**: Applies validation rules from the model to UI elements
65
+ - **Field Definition Generation**: Converts model metadata into UI field definitions
66
+ - **Engine Management**: Registers and retrieves rendering engine implementations
67
+ - **Extensibility**: Can be extended to support any UI framework or rendering strategy
68
+
69
+ #### Integration with Validation
70
+
71
+ The library seamlessly integrates with the validation system from `@decaf-ts/decorator-validation`, automatically applying validation rules to UI elements:
72
+
73
+ - Required fields
74
+ - Minimum and maximum values
75
+ - Minimum and maximum length
76
+ - Pattern matching
77
+ - Type-specific validation (email, URL, date, password)
78
+ - Custom validation rules
79
+
80
+ This integration ensures that UI components not only display data correctly but also enforce the same validation rules defined in the model.
45
81
 
46
82
 
47
83
  ### How to Use
@@ -49,7 +85,381 @@ Adds a new Decorator ```hideOn``` to add UI metadata to the model's properties,
49
85
  - [Initial Setup](../../workdocs/tutorials/For%20Developers.md#_initial-setup_)
50
86
  - [Installation](../../workdocs/tutorials/For%20Developers.md#installation)
51
87
 
52
-
88
+ ## Basic Usage
89
+
90
+ ### Creating a UI Model
91
+
92
+ The most basic usage involves decorating a model class with `@uimodel` to make it renderable:
93
+
94
+ ```typescript
95
+ import { Model, attribute } from '@decaf-ts/decorator-validation';
96
+ import { uimodel, uielement } from '@decaf-ts/ui-decorators';
97
+
98
+ @uimodel()
99
+ class UserProfile extends Model {
100
+ @attribute()
101
+ @uielement('input', { type: 'text', placeholder: 'Enter your name' })
102
+ name: string;
103
+
104
+ @attribute()
105
+ @uielement('input', { type: 'email', placeholder: 'Enter your email' })
106
+ email: string;
107
+ }
108
+
109
+ // Create an instance
110
+ const user = new UserProfile();
111
+ user.name = 'John Doe';
112
+ user.email = 'john@example.com';
113
+
114
+ // Render the model (the actual rendering depends on the registered rendering engine)
115
+ const renderedUI = user.render();
116
+ ```
117
+
118
+ ### Customizing UI Model Rendering
119
+
120
+ You can customize how a model is rendered by providing a tag and properties to the `@uimodel` decorator:
121
+
122
+ ```typescript
123
+ import { Model, attribute } from '@decaf-ts/decorator-validation';
124
+ import { uimodel, uielement } from '@decaf-ts/ui-decorators';
125
+
126
+ @uimodel('div', { class: 'user-card', style: 'border: 1px solid #ccc; padding: 16px;' })
127
+ class UserCard extends Model {
128
+ @attribute()
129
+ @uielement('h2', { class: 'user-name' })
130
+ name: string;
131
+
132
+ @attribute()
133
+ @uielement('p', { class: 'user-bio' })
134
+ bio: string;
135
+ }
136
+ ```
137
+
138
+ ### Specifying a Rendering Engine
139
+
140
+ You can specify which rendering engine to use for a particular model:
141
+
142
+ ```typescript
143
+ import { Model, attribute } from '@decaf-ts/decorator-validation';
144
+ import { uimodel, renderedBy, uielement } from '@decaf-ts/ui-decorators';
145
+
146
+ @uimodel()
147
+ @renderedBy('react')
148
+ class ReactComponent extends Model {
149
+ @attribute()
150
+ @uielement('input', { type: 'text' })
151
+ title: string;
152
+ }
153
+ ```
154
+
155
+ ### Mapping Properties to UI Elements
156
+
157
+ The `@uielement` decorator maps a model property to a specific UI element:
158
+
159
+ ```typescript
160
+ import { Model, attribute, required, minLength, maxLength } from '@decaf-ts/decorator-validation';
161
+ import { uimodel, uielement } from '@decaf-ts/ui-decorators';
162
+
163
+ @uimodel('form')
164
+ class LoginForm extends Model {
165
+ @attribute()
166
+ @required()
167
+ @minLength(3)
168
+ @maxLength(50)
169
+ @uielement('input', {
170
+ type: 'text',
171
+ placeholder: 'Username',
172
+ class: 'form-control'
173
+ })
174
+ username: string;
175
+
176
+ @attribute()
177
+ @required()
178
+ @minLength(8)
179
+ @uielement('input', {
180
+ type: 'password',
181
+ placeholder: 'Password',
182
+ class: 'form-control'
183
+ })
184
+ password: string;
185
+
186
+ @attribute()
187
+ @uielement('button', {
188
+ type: 'submit',
189
+ class: 'btn btn-primary'
190
+ })
191
+ submitButton: string = 'Login';
192
+ }
193
+ ```
194
+
195
+ ### Mapping Properties to Component Properties
196
+
197
+ The `@uiprop` decorator maps a model property to a UI component property:
198
+
199
+ ```typescript
200
+ import { Model, attribute } from '@decaf-ts/decorator-validation';
201
+ import { uimodel, uiprop } from '@decaf-ts/ui-decorators';
202
+
203
+ @uimodel('user-profile-component')
204
+ class UserProfile extends Model {
205
+ @attribute()
206
+ @uiprop() // Will be passed as 'fullName' to the component
207
+ fullName: string;
208
+
209
+ @attribute()
210
+ @uiprop('userEmail') // Will be passed as 'userEmail' to the component
211
+ email: string;
212
+
213
+ @attribute()
214
+ @uiprop('userData', true) // Will be passed as stringified JSON
215
+ userData: Record<string, any>;
216
+ }
217
+ ```
218
+
219
+ ### Controlling Property Visibility
220
+
221
+ You can control when properties are visible using the `@hideOn` and `@hidden` decorators:
222
+
223
+ ```typescript
224
+ import { Model } from '@decaf-ts/decorator-validation';
225
+ import { uimodel, uielement, hideOn, hidden } from '@decaf-ts/ui-decorators';
226
+ import { OperationKeys } from '@decaf-ts/db-decorators';
227
+
228
+ @uimodel()
229
+ class User extends Model {
230
+ @uielement('input', { type: 'text' })
231
+ username: string;
232
+
233
+ @uielement('input', { type: 'password' })
234
+ @hideOn(OperationKeys.READ) // Hide during READ operations
235
+ password: string;
236
+
237
+ @uielement('input', { type: 'text' })
238
+ @hidden() // Completely hidden in all operations
239
+ internalId: string;
240
+ }
241
+ ```
242
+
243
+ ### Rendering Lists of Models
244
+
245
+ You can render lists of models using the `@uilistitem` and `@uilistprop` decorators:
246
+
247
+ ```typescript
248
+ import { Model, list } from '@decaf-ts/decorator-validation';
249
+ import { uimodel, uilistitem, uilistprop, uielement } from '@decaf-ts/ui-decorators';
250
+
251
+ // Define a list item model
252
+ @uimodel()
253
+ @uilistitem('li', { class: 'todo-item' })
254
+ class TodoItem extends Model {
255
+ @uielement('span', { class: 'todo-text' })
256
+ text: string;
257
+
258
+ @uielement('input', { type: 'checkbox' })
259
+ completed: boolean;
260
+ }
261
+
262
+ // Define a list container model
263
+ @uimodel('div', { class: 'todo-app' })
264
+ class TodoList extends Model {
265
+ @uielement('h1')
266
+ title: string = 'My Todo List';
267
+
268
+ @list(TodoItem)
269
+ @uilistprop('items', { class: 'todo-items-container' })
270
+ items: TodoItem[];
271
+ }
272
+
273
+ // Usage
274
+ const todoList = new TodoList();
275
+ todoList.items = [
276
+ new TodoItem({ text: 'Learn TypeScript', completed: true }),
277
+ new TodoItem({ text: 'Build a UI with decorators', completed: false })
278
+ ];
279
+
280
+ const renderedList = todoList.render();
281
+ ```
282
+
283
+ ## Creating a Custom Rendering Engine
284
+
285
+ To implement a custom rendering engine for a specific UI framework, you need to extend the `RenderingEngine` abstract class:
286
+
287
+ ```typescript
288
+ import { Model } from '@decaf-ts/decorator-validation';
289
+ import { RenderingEngine, FieldDefinition } from '@decaf-ts/ui-decorators';
290
+
291
+ // Define the output type for your rendering engine
292
+ type ReactElement = any; // Replace with actual React element type
293
+
294
+ // Create a custom rendering engine for React
295
+ class ReactRenderingEngine extends RenderingEngine<ReactElement> {
296
+ constructor() {
297
+ super('react'); // Specify the engine flavor
298
+ }
299
+
300
+ // Initialize the engine (required abstract method)
301
+ async initialize(...args: any[]): Promise<void> {
302
+ // Import React or perform any other initialization
303
+ this.initialized = true;
304
+ }
305
+
306
+ // Implement the render method (required abstract method)
307
+ render<M extends Model>(
308
+ model: M,
309
+ globalProps: Record<string, unknown> = {},
310
+ ...args: any[]
311
+ ): ReactElement {
312
+ // Convert the model to a field definition
313
+ const fieldDef = this.toFieldDefinition(model, globalProps);
314
+
315
+ // Convert the field definition to a React element
316
+ return this.createReactElement(fieldDef);
317
+ }
318
+
319
+ // Helper method to create React elements
320
+ private createReactElement(fieldDef: FieldDefinition<ReactElement>): ReactElement {
321
+ // Implementation would use React.createElement or JSX
322
+ // This is just a placeholder
323
+ return {
324
+ type: fieldDef.tag,
325
+ props: {
326
+ ...fieldDef.props,
327
+ children: fieldDef.children?.map(child => this.createReactElement(child))
328
+ }
329
+ };
330
+ }
331
+ }
332
+
333
+ // Register the custom rendering engine
334
+ new ReactRenderingEngine();
335
+
336
+ // Now models can specify to use this engine
337
+ @uimodel()
338
+ @renderedBy('react')
339
+ class ReactComponent extends Model {
340
+ // ...
341
+ }
342
+ ```
343
+
344
+ ## Integration with Validation
345
+
346
+ The UI decorators library automatically integrates with the validation system from `@decaf-ts/decorator-validation`:
347
+
348
+ ```typescript
349
+ import { Model, required, email, minLength, pattern } from '@decaf-ts/decorator-validation';
350
+ import { uimodel, uielement } from '@decaf-ts/ui-decorators';
351
+
352
+ @uimodel('form', { class: 'registration-form' })
353
+ class RegistrationForm extends Model {
354
+ @required()
355
+ @minLength(3)
356
+ @uielement('input', { type: 'text', placeholder: 'Username' })
357
+ username: string;
358
+
359
+ @required()
360
+ @email()
361
+ @uielement('input', { type: 'email', placeholder: 'Email' })
362
+ email: string;
363
+
364
+ @required()
365
+ @minLength(8)
366
+ @pattern(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).*$/) // Requires lowercase, uppercase, and digit
367
+ @uielement('input', { type: 'password', placeholder: 'Password' })
368
+ password: string;
369
+
370
+ // Validation will be automatically applied to the rendered UI elements
371
+ }
372
+ ```
373
+
374
+ ## Complete Example
375
+
376
+ Here's a complete example showing how to use the UI decorators library to create a user registration form:
377
+
378
+ ```typescript
379
+ import { Model, attribute, required, email, minLength, maxLength, pattern } from '@decaf-ts/decorator-validation';
380
+ import { uimodel, uielement, renderedBy } from '@decaf-ts/ui-decorators';
381
+
382
+ @uimodel('form', { class: 'registration-form', id: 'user-registration' })
383
+ @renderedBy('html5') // Use the HTML5 rendering engine
384
+ class UserRegistration extends Model {
385
+ @required()
386
+ @minLength(2)
387
+ @maxLength(50)
388
+ @uielement('input', {
389
+ type: 'text',
390
+ placeholder: 'First Name',
391
+ class: 'form-control'
392
+ })
393
+ firstName: string;
394
+
395
+ @required()
396
+ @minLength(2)
397
+ @maxLength(50)
398
+ @uielement('input', {
399
+ type: 'text',
400
+ placeholder: 'Last Name',
401
+ class: 'form-control'
402
+ })
403
+ lastName: string;
404
+
405
+ @required()
406
+ @email()
407
+ @uielement('input', {
408
+ type: 'email',
409
+ placeholder: 'Email Address',
410
+ class: 'form-control'
411
+ })
412
+ email: string;
413
+
414
+ @required()
415
+ @minLength(8)
416
+ @pattern(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/)
417
+ @uielement('input', {
418
+ type: 'password',
419
+ placeholder: 'Password (min 8 chars, include uppercase, lowercase, number, and special char)',
420
+ class: 'form-control'
421
+ })
422
+ password: string;
423
+
424
+ @required()
425
+ @uielement('select', { class: 'form-control' })
426
+ country: string;
427
+
428
+ @attribute()
429
+ @uielement('textarea', {
430
+ placeholder: 'Tell us about yourself',
431
+ class: 'form-control',
432
+ rows: 4
433
+ })
434
+ bio: string;
435
+
436
+ @uielement('input', {
437
+ type: 'checkbox',
438
+ class: 'form-check-input'
439
+ })
440
+ acceptTerms: boolean = false;
441
+
442
+ @uielement('button', {
443
+ type: 'submit',
444
+ class: 'btn btn-primary'
445
+ })
446
+ submitButton: string = 'Register';
447
+ }
448
+
449
+ // Create an instance
450
+ const registration = new UserRegistration();
451
+
452
+ // Render the form
453
+ const form = registration.render();
454
+
455
+ // Check for validation errors
456
+ const errors = registration.hasErrors();
457
+ if (errors) {
458
+ console.error('Validation errors:', errors);
459
+ }
460
+ ```
461
+
462
+ This example demonstrates how to create a complete registration form with various input types and validation rules, all defined declaratively using decorators.
53
463
 
54
464
 
55
465
  ### Related