@decaf-ts/ui-decorators 0.5.9 → 0.5.11

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 (63) hide show
  1. package/LICENSE.md +21 -157
  2. package/README.md +422 -12
  3. package/dist/ui-decorators.cjs +609 -55
  4. package/dist/ui-decorators.esm.cjs +607 -56
  5. package/lib/esm/index.d.ts +7 -3
  6. package/lib/esm/index.js +8 -4
  7. package/lib/esm/model/Renderable.d.ts +15 -0
  8. package/lib/esm/model/Renderable.js +1 -1
  9. package/lib/esm/model/decorators.d.ts +93 -14
  10. package/lib/esm/model/decorators.js +102 -16
  11. package/lib/esm/model/index.d.ts +0 -4
  12. package/lib/esm/model/index.js +1 -5
  13. package/lib/esm/model/model.d.ts +156 -50
  14. package/lib/esm/model/model.js +1 -1
  15. package/lib/esm/model/overrides.d.ts +8 -0
  16. package/lib/esm/model/overrides.js +19 -1
  17. package/lib/esm/ui/Rendering.js +43 -8
  18. package/lib/esm/ui/constants.d.ts +149 -4
  19. package/lib/esm/ui/constants.js +150 -5
  20. package/lib/esm/ui/decorators.d.ts +229 -21
  21. package/lib/esm/ui/decorators.js +242 -25
  22. package/lib/esm/ui/errors.d.ts +28 -0
  23. package/lib/esm/ui/errors.js +29 -1
  24. package/lib/esm/ui/handlers.d.ts +17 -0
  25. package/lib/esm/ui/handlers.js +18 -0
  26. package/lib/esm/ui/index.d.ts +1 -4
  27. package/lib/esm/ui/index.js +2 -5
  28. package/lib/esm/ui/interfaces.d.ts +25 -0
  29. package/lib/esm/ui/interfaces.js +9 -1
  30. package/lib/esm/ui/types.d.ts +63 -5
  31. package/lib/esm/ui/types.js +9 -1
  32. package/lib/esm/ui/utils.d.ts +1 -1
  33. package/lib/esm/ui/utils.js +2 -2
  34. package/lib/index.cjs +8 -4
  35. package/lib/index.d.ts +7 -3
  36. package/lib/model/Renderable.cjs +1 -1
  37. package/lib/model/Renderable.d.ts +15 -0
  38. package/lib/model/decorators.cjs +103 -16
  39. package/lib/model/decorators.d.ts +93 -14
  40. package/lib/model/index.cjs +1 -5
  41. package/lib/model/index.d.ts +0 -4
  42. package/lib/model/model.cjs +1 -1
  43. package/lib/model/model.d.ts +156 -50
  44. package/lib/model/overrides.cjs +19 -1
  45. package/lib/model/overrides.d.ts +8 -0
  46. package/lib/ui/Rendering.cjs +43 -8
  47. package/lib/ui/constants.cjs +150 -5
  48. package/lib/ui/constants.d.ts +149 -4
  49. package/lib/ui/decorators.cjs +243 -25
  50. package/lib/ui/decorators.d.ts +229 -21
  51. package/lib/ui/errors.cjs +29 -1
  52. package/lib/ui/errors.d.ts +28 -0
  53. package/lib/ui/handlers.cjs +22 -0
  54. package/lib/ui/handlers.d.ts +17 -0
  55. package/lib/ui/index.cjs +2 -5
  56. package/lib/ui/index.d.ts +1 -4
  57. package/lib/ui/interfaces.cjs +9 -1
  58. package/lib/ui/interfaces.d.ts +25 -0
  59. package/lib/ui/types.cjs +9 -1
  60. package/lib/ui/types.d.ts +63 -5
  61. package/lib/ui/utils.cjs +2 -2
  62. package/lib/ui/utils.d.ts +1 -1
  63. package/package.json +2 -2
package/LICENSE.md CHANGED
@@ -1,157 +1,21 @@
1
- # GNU LESSER GENERAL PUBLIC LICENSE
2
-
3
- Version 3, 29 June 2007
4
-
5
- Copyright (C) 2025 Tiago Venceslau
6
- <https:/github.com/TiagoVenceslau>
7
-
8
- Everyone is permitted to copy and distribute verbatim copies of this
9
- license document, but changing it is not allowed.
10
-
11
- This version of the GNU Lesser General Public License incorporates the
12
- terms and conditions of version 3 of the GNU General Public License,
13
- supplemented by the additional permissions listed below.
14
-
15
- ## 0. Additional Definitions.
16
-
17
- As used herein, "this License" refers to version 3 of the GNU Lesser
18
- General Public License, and the "GNU GPL" refers to version 3 of the
19
- GNU General Public License.
20
-
21
- "The Library" refers to a covered work governed by this License, other
22
- than an Application or a Combined Work as defined below.
23
-
24
- An "Application" is any work that makes use of an interface provided
25
- by the Library, but which is not otherwise based on the Library.
26
- Defining a subclass of a class defined by the Library is deemed a mode
27
- of using an interface provided by the Library.
28
-
29
- A "Combined Work" is a work produced by combining or linking an
30
- Application with the Library. The particular version of the Library
31
- with which the Combined Work was made is also called the "Linked
32
- Version".
33
-
34
- The "Minimal Corresponding Source" for a Combined Work means the
35
- Corresponding Source for the Combined Work, excluding any source code
36
- for portions of the Combined Work that, considered in isolation, are
37
- based on the Application, and not on the Linked Version.
38
-
39
- The "Corresponding Application Code" for a Combined Work means the
40
- object code and/or source code for the Application, including any data
41
- and utility programs needed for reproducing the Combined Work from the
42
- Application, but excluding the System Libraries of the Combined Work.
43
-
44
- ## 1. Exception to Section 3 of the GNU GPL.
45
-
46
- You may convey a covered work under sections 3 and 4 of this License
47
- without being bound by section 3 of the GNU GPL.
48
-
49
- ## 2. Conveying Modified Versions.
50
-
51
- If you modify a copy of the Library, and, in your modifications, a
52
- facility refers to a function or data to be supplied by an Application
53
- that uses the facility (other than as an argument passed when the
54
- facility is invoked), then you may convey a copy of the modified
55
- version:
56
-
57
- - a) under this License, provided that you make a good faith effort
58
- to ensure that, in the event an Application does not supply the
59
- function or data, the facility still operates, and performs
60
- whatever part of its purpose remains meaningful, or
61
- - b) under the GNU GPL, with none of the additional permissions of
62
- this License applicable to that copy.
63
-
64
- ## 3. Object Code Incorporating Material from Library Header Files.
65
-
66
- The object code form of an Application may incorporate material from a
67
- header file that is part of the Library. You may convey such object
68
- code under terms of your choice, provided that, if the incorporated
69
- material is not limited to numerical parameters, data structure
70
- layouts and accessors, or small macros, inline functions and templates
71
- (ten or fewer lines in length), you do both of the following:
72
-
73
- - a) Give prominent notice with each copy of the object code that
74
- the Library is used in it and that the Library and its use are
75
- covered by this License.
76
- - b) Accompany the object code with a copy of the GNU GPL and this
77
- license document.
78
-
79
- ## 4. Combined Works.
80
-
81
- You may convey a Combined Work under terms of your choice that, taken
82
- together, effectively do not restrict modification of the portions of
83
- the Library contained in the Combined Work and reverse engineering for
84
- debugging such modifications, if you also do each of the following:
85
-
86
- - a) Give prominent notice with each copy of the Combined Work that
87
- the Library is used in it and that the Library and its use are
88
- covered by this License.
89
- - b) Accompany the Combined Work with a copy of the GNU GPL and this
90
- license document.
91
- - c) For a Combined Work that displays copyright notices during
92
- execution, include the copyright notice for the Library among
93
- these notices, as well as a reference directing the user to the
94
- copies of the GNU GPL and this license document.
95
- - d) Do one of the following:
96
- - 0) Convey the Minimal Corresponding Source under the terms of
97
- this License, and the Corresponding Application Code in a form
98
- suitable for, and under terms that permit, the user to
99
- recombine or relink the Application with a modified version of
100
- the Linked Version to produce a modified Combined Work, in the
101
- manner specified by section 6 of the GNU GPL for conveying
102
- Corresponding Source.
103
- - 1) Use a suitable shared library mechanism for linking with
104
- the Library. A suitable mechanism is one that (a) uses at run
105
- time a copy of the Library already present on the user's
106
- computer system, and (b) will operate properly with a modified
107
- version of the Library that is interface-compatible with the
108
- Linked Version.
109
- - e) Provide Installation Information, but only if you would
110
- otherwise be required to provide such information under section 6
111
- of the GNU GPL, and only to the extent that such information is
112
- necessary to install and execute a modified version of the
113
- Combined Work produced by recombining or relinking the Application
114
- with a modified version of the Linked Version. (If you use option
115
- 4d0, the Installation Information must accompany the Minimal
116
- Corresponding Source and Corresponding Application Code. If you
117
- use option 4d1, you must provide the Installation Information in
118
- the manner specified by section 6 of the GNU GPL for conveying
119
- Corresponding Source.)
120
-
121
- ## 5. Combined Libraries.
122
-
123
- You may place library facilities that are a work based on the Library
124
- side by side in a single library together with other library
125
- facilities that are not Applications and are not covered by this
126
- License, and convey such a combined library under terms of your
127
- choice, if you do both of the following:
128
-
129
- - a) Accompany the combined library with a copy of the same work
130
- based on the Library, uncombined with any other library
131
- facilities, conveyed under the terms of this License.
132
- - b) Give prominent notice with the combined library that part of it
133
- is a work based on the Library, and explaining where to find the
134
- accompanying uncombined form of the same work.
135
-
136
- ## 6. Revised Versions of the GNU Lesser General Public License.
137
-
138
- The Free Software Foundation may publish revised and/or new versions
139
- of the GNU Lesser General Public License from time to time. Such new
140
- versions will be similar in spirit to the present version, but may
141
- differ in detail to address new problems or concerns.
142
-
143
- Each version is given a distinguishing version number. If the Library
144
- as you received it specifies that a certain numbered version of the
145
- GNU Lesser General Public License "or any later version" applies to
146
- it, you have the option of following the terms and conditions either
147
- of that published version or of any later version published by the
148
- Free Software Foundation. If the Library as you received it does not
149
- specify a version number of the GNU Lesser General Public License, you
150
- may choose any version of the GNU Lesser General Public License ever
151
- published by the Free Software Foundation.
152
-
153
- If the Library as you received it specifies that a proxy can decide
154
- whether future versions of the GNU Lesser General Public License shall
155
- apply, that proxy's public statement of acceptance of any version is
156
- permanent authorization for you to choose that version for the
157
- Library.
1
+ # MIT License
2
+
3
+ Copyright (c) 2025 Tiago Venceslau
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
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