@bbq-chat/widgets-angular 1.0.3 → 1.0.5

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
@@ -2,6 +2,14 @@
2
2
 
3
3
  Angular components and services for BbQ ChatWidgets. This package provides Angular-native wrappers for the core [@bbq-chat/widgets](https://www.npmjs.com/package/@bbq-chat/widgets) library.
4
4
 
5
+ ## Features
6
+
7
+ - ✨ **Native Angular Components** - All widgets render as Angular components with full framework support
8
+ - 🎨 **Type-Safe** - Full TypeScript support with comprehensive type definitions
9
+ - 🔄 **Two-Way Data Binding** - Leverage Angular's powerful data binding
10
+ - 🎯 **Automatic Rendering** - The `WidgetRendererComponent` automatically uses `AngularWidgetRenderer` for all built-in widgets
11
+ - 🔧 **Extensible** - Easy custom widget registration with full Angular component support
12
+
5
13
  ## Installation
6
14
 
7
15
  ```bash
@@ -65,7 +73,12 @@ Or import a specific theme:
65
73
 
66
74
  ### WidgetRendererComponent
67
75
 
68
- The main component for rendering chat widgets.
76
+ The main component for rendering chat widgets. It automatically uses the `AngularWidgetRenderer` to render all built-in widgets as native Angular components.
77
+
78
+ **Rendering Modes:**
79
+ - Built-in widgets (button, card, form, etc.) are rendered as Angular components via `AngularWidgetRenderer`
80
+ - Custom widgets can use HTML functions, Angular components, or Angular templates
81
+ - Falls back to `SsrWidgetRenderer` (HTML string rendering) if needed for compatibility
69
82
 
70
83
  **Inputs:**
71
84
  - `widgets: ChatWidget[] | null | undefined` - Array of widgets to render
@@ -120,11 +133,11 @@ export class AppComponent implements OnInit {
120
133
 
121
134
  ## Widget Types
122
135
 
123
- All standard widget types from `@bbq-chat/widgets` are supported:
136
+ All standard widget types from `@bbq-chat/widgets` are supported and rendered as native Angular components:
124
137
 
125
138
  - `ButtonWidget` - Clickable buttons
126
139
  - `CardWidget` - Information cards
127
- - `FormWidget` - Form containers
140
+ - `FormWidget` - Form containers with validation
128
141
  - `InputWidget` - Text input fields
129
142
  - `TextAreaWidget` - Multi-line text input
130
143
  - `DropdownWidget` - Dropdown selects
@@ -138,6 +151,82 @@ All standard widget types from `@bbq-chat/widgets` are supported:
138
151
  - `ImageWidget` - Image displays
139
152
  - `ImageCollectionWidget` - Image galleries
140
153
 
154
+ ## AngularWidgetRenderer
155
+
156
+ The `AngularWidgetRenderer` is automatically used by `WidgetRendererComponent` to render all built-in widgets as native Angular components instead of HTML strings. This provides several advantages:
157
+
158
+ ### Benefits
159
+
160
+ - **Native Angular Components** - Full Angular lifecycle hooks, change detection, and dependency injection
161
+ - **Type Safety** - Strong typing throughout the rendering pipeline
162
+ - **Better Performance** - No need for `innerHTML` or manual DOM manipulation
163
+ - **Reactive Updates** - Leverage Angular's reactive forms and data binding
164
+ - **Testability** - Easier to test with Angular TestBed
165
+ - **Easy Component Swapping** - Simple API to replace any widget component with your own
166
+
167
+ ### How It Works
168
+
169
+ The `WidgetRendererComponent` automatically detects and uses `AngularWidgetRenderer` for all built-in widget types:
170
+
171
+ ```typescript
172
+ // The WidgetRendererComponent automatically uses AngularWidgetRenderer
173
+ // No configuration needed - it just works!
174
+ <bbq-widget-renderer
175
+ [widgets]="widgets"
176
+ (widgetAction)="handleWidgetAction($event)">
177
+ </bbq-widget-renderer>
178
+ ```
179
+
180
+ ### Swapping Widget Components
181
+
182
+ You can easily replace any built-in widget component with your own custom implementation:
183
+
184
+ ```typescript
185
+ import { Component, OnInit, Inject } from '@angular/core';
186
+ import { ANGULAR_WIDGET_RENDERER, AngularWidgetRenderer } from '@bbq-chat/widgets-angular';
187
+ import { MyCustomButtonComponent } from './my-custom-button.component';
188
+
189
+ @Component({
190
+ selector: 'app-root',
191
+ template: '...'
192
+ })
193
+ export class AppComponent implements OnInit {
194
+ constructor(@Inject(ANGULAR_WIDGET_RENDERER) private renderer: AngularWidgetRenderer) {}
195
+
196
+ ngOnInit() {
197
+ // Replace the built-in button component with your custom one
198
+ this.renderer.registerComponent('button', MyCustomButtonComponent);
199
+
200
+ // Or register multiple components at once
201
+ this.renderer.registerComponents({
202
+ button: MyCustomButtonComponent,
203
+ input: MyCustomInputComponent,
204
+ card: MyCustomCardComponent
205
+ });
206
+ }
207
+ }
208
+ ```
209
+
210
+ ### Built-in Widget Components
211
+
212
+ All built-in widgets are available as standalone Angular components:
213
+
214
+ ```typescript
215
+ import {
216
+ ButtonWidgetComponent,
217
+ CardWidgetComponent,
218
+ FormWidgetComponent,
219
+ InputWidgetComponent,
220
+ // ... and more
221
+ } from '@bbq-chat/widgets-angular';
222
+ ```
223
+
224
+ These components can be used directly in your templates if needed, though typically you'll use them through `WidgetRendererComponent`.
225
+
226
+ ### Form Widget Dynamic Rendering
227
+
228
+ The `FormWidgetComponent` uses dynamic component rendering to render form fields. Instead of inline HTML, it dynamically instantiates the appropriate widget component (InputWidget, SliderWidget, etc.) for each field. This ensures consistency across all widgets and makes the form behavior automatically inherit any custom widget component replacements.
229
+
141
230
  ## Advanced Usage
142
231
 
143
232
  ### Handling Widget Actions
package/dist/README.md ADDED
@@ -0,0 +1,341 @@
1
+ # @bbq-chat/widgets-angular
2
+
3
+ Angular components and services for BbQ ChatWidgets. This package provides Angular-native wrappers for the core [@bbq-chat/widgets](https://www.npmjs.com/package/@bbq-chat/widgets) library.
4
+
5
+ ## Features
6
+
7
+ - ✨ **Native Angular Components** - All widgets render as Angular components with full framework support
8
+ - 🎨 **Type-Safe** - Full TypeScript support with comprehensive type definitions
9
+ - 🔄 **Two-Way Data Binding** - Leverage Angular's powerful data binding
10
+ - 🎯 **Automatic Rendering** - The `WidgetRendererComponent` automatically uses `AngularWidgetRenderer` for all built-in widgets
11
+ - 🔧 **Extensible** - Easy custom widget registration with full Angular component support
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install @bbq-chat/widgets-angular @bbq-chat/widgets
17
+ ```
18
+
19
+ ## Peer Dependencies
20
+
21
+ This package requires:
22
+ - `@angular/common` >= 17.0.0
23
+ - `@angular/core` >= 17.0.0
24
+ - `@bbq-chat/widgets` ^1.0.0
25
+
26
+ ## Quick Start
27
+
28
+ ### 1. Import the component
29
+
30
+ ```typescript
31
+ import { Component } from '@angular/core';
32
+ import { WidgetRendererComponent } from '@bbq-chat/widgets-angular';
33
+ import type { ChatWidget } from '@bbq-chat/widgets-angular';
34
+
35
+ @Component({
36
+ selector: 'app-chat',
37
+ standalone: true,
38
+ imports: [WidgetRendererComponent],
39
+ template: `
40
+ <bbq-widget-renderer
41
+ [widgets]="widgets"
42
+ (widgetAction)="handleWidgetAction($event)">
43
+ </bbq-widget-renderer>
44
+ `
45
+ })
46
+ export class ChatComponent {
47
+ widgets: ChatWidget[] = [];
48
+
49
+ handleWidgetAction(event: { actionName: string; payload: any }) {
50
+ console.log('Widget action:', event.actionName, event.payload);
51
+ // Handle the action (e.g., send to backend)
52
+ }
53
+ }
54
+ ```
55
+
56
+ ### 2. Import styles
57
+
58
+ In your `styles.css` or `styles.scss`:
59
+
60
+ ```css
61
+ @import '@bbq-chat/widgets/styles';
62
+ ```
63
+
64
+ Or import a specific theme:
65
+
66
+ ```css
67
+ @import '@bbq-chat/widgets/styles/light';
68
+ @import '@bbq-chat/widgets/styles/dark';
69
+ @import '@bbq-chat/widgets/styles/corporate';
70
+ ```
71
+
72
+ ## Components
73
+
74
+ ### WidgetRendererComponent
75
+
76
+ The main component for rendering chat widgets. It automatically uses the `AngularWidgetRenderer` to render all built-in widgets as native Angular components.
77
+
78
+ **Rendering Modes:**
79
+ - Built-in widgets (button, card, form, etc.) are rendered as Angular components via `AngularWidgetRenderer`
80
+ - Custom widgets can use HTML functions, Angular components, or Angular templates
81
+ - Falls back to `SsrWidgetRenderer` (HTML string rendering) if needed for compatibility
82
+
83
+ **Inputs:**
84
+ - `widgets: ChatWidget[] | null | undefined` - Array of widgets to render
85
+
86
+ **Outputs:**
87
+ - `widgetAction: EventEmitter<{ actionName: string; payload: any }>` - Emits when a widget action is triggered
88
+
89
+ **Example:**
90
+
91
+ ```typescript
92
+ <bbq-widget-renderer
93
+ [widgets]="messageWidgets"
94
+ (widgetAction)="onWidgetAction($event)">
95
+ </bbq-widget-renderer>
96
+ ```
97
+
98
+ ## Services
99
+
100
+ ### WidgetRegistryService
101
+
102
+ Service for registering custom widget types.
103
+
104
+ **Example:**
105
+
106
+ ```typescript
107
+ import { Component, OnInit } from '@angular/core';
108
+ import { WidgetRegistryService } from '@bbq-chat/widgets-angular';
109
+
110
+ export class MyCustomWidget {
111
+ type = 'myCustomWidget';
112
+ constructor(public label: string, public action: string) {}
113
+ }
114
+
115
+ @Component({
116
+ selector: 'app-root',
117
+ template: '...'
118
+ })
119
+ export class AppComponent implements OnInit {
120
+ constructor(private widgetRegistry: WidgetRegistryService) {}
121
+
122
+ ngOnInit() {
123
+ // Register custom widget factory
124
+ this.widgetRegistry.registerFactory('myCustomWidget', (obj) => {
125
+ if (obj.type === 'myCustomWidget') {
126
+ return new MyCustomWidget(obj.label, obj.action);
127
+ }
128
+ return null;
129
+ });
130
+ }
131
+ }
132
+ ```
133
+
134
+ ## Widget Types
135
+
136
+ All standard widget types from `@bbq-chat/widgets` are supported and rendered as native Angular components:
137
+
138
+ - `ButtonWidget` - Clickable buttons
139
+ - `CardWidget` - Information cards
140
+ - `FormWidget` - Form containers with validation
141
+ - `InputWidget` - Text input fields
142
+ - `TextAreaWidget` - Multi-line text input
143
+ - `DropdownWidget` - Dropdown selects
144
+ - `SliderWidget` - Range sliders
145
+ - `ToggleWidget` - Toggle switches
146
+ - `FileUploadWidget` - File upload controls
147
+ - `DatePickerWidget` - Date pickers
148
+ - `MultiSelectWidget` - Multi-select dropdowns
149
+ - `ThemeSwitcherWidget` - Theme switcher controls
150
+ - `ProgressBarWidget` - Progress indicators
151
+ - `ImageWidget` - Image displays
152
+ - `ImageCollectionWidget` - Image galleries
153
+
154
+ ## AngularWidgetRenderer
155
+
156
+ The `AngularWidgetRenderer` is automatically used by `WidgetRendererComponent` to render all built-in widgets as native Angular components instead of HTML strings. This provides several advantages:
157
+
158
+ ### Benefits
159
+
160
+ - **Native Angular Components** - Full Angular lifecycle hooks, change detection, and dependency injection
161
+ - **Type Safety** - Strong typing throughout the rendering pipeline
162
+ - **Better Performance** - No need for `innerHTML` or manual DOM manipulation
163
+ - **Reactive Updates** - Leverage Angular's reactive forms and data binding
164
+ - **Testability** - Easier to test with Angular TestBed
165
+ - **Easy Component Swapping** - Simple API to replace any widget component with your own
166
+
167
+ ### How It Works
168
+
169
+ The `WidgetRendererComponent` automatically detects and uses `AngularWidgetRenderer` for all built-in widget types:
170
+
171
+ ```typescript
172
+ // The WidgetRendererComponent automatically uses AngularWidgetRenderer
173
+ // No configuration needed - it just works!
174
+ <bbq-widget-renderer
175
+ [widgets]="widgets"
176
+ (widgetAction)="handleWidgetAction($event)">
177
+ </bbq-widget-renderer>
178
+ ```
179
+
180
+ ### Swapping Widget Components
181
+
182
+ You can easily replace any built-in widget component with your own custom implementation:
183
+
184
+ ```typescript
185
+ import { Component, OnInit, Inject } from '@angular/core';
186
+ import { ANGULAR_WIDGET_RENDERER, AngularWidgetRenderer } from '@bbq-chat/widgets-angular';
187
+ import { MyCustomButtonComponent } from './my-custom-button.component';
188
+
189
+ @Component({
190
+ selector: 'app-root',
191
+ template: '...'
192
+ })
193
+ export class AppComponent implements OnInit {
194
+ constructor(@Inject(ANGULAR_WIDGET_RENDERER) private renderer: AngularWidgetRenderer) {}
195
+
196
+ ngOnInit() {
197
+ // Replace the built-in button component with your custom one
198
+ this.renderer.registerComponent('button', MyCustomButtonComponent);
199
+
200
+ // Or register multiple components at once
201
+ this.renderer.registerComponents({
202
+ button: MyCustomButtonComponent,
203
+ input: MyCustomInputComponent,
204
+ card: MyCustomCardComponent
205
+ });
206
+ }
207
+ }
208
+ ```
209
+
210
+ ### Built-in Widget Components
211
+
212
+ All built-in widgets are available as standalone Angular components:
213
+
214
+ ```typescript
215
+ import {
216
+ ButtonWidgetComponent,
217
+ CardWidgetComponent,
218
+ FormWidgetComponent,
219
+ InputWidgetComponent,
220
+ // ... and more
221
+ } from '@bbq-chat/widgets-angular';
222
+ ```
223
+
224
+ These components can be used directly in your templates if needed, though typically you'll use them through `WidgetRendererComponent`.
225
+
226
+ ### Form Widget Dynamic Rendering
227
+
228
+ The `FormWidgetComponent` uses dynamic component rendering to render form fields. Instead of inline HTML, it dynamically instantiates the appropriate widget component (InputWidget, SliderWidget, etc.) for each field. This ensures consistency across all widgets and makes the form behavior automatically inherit any custom widget component replacements.
229
+
230
+ ## Advanced Usage
231
+
232
+ ### Handling Widget Actions
233
+
234
+ Widget actions are emitted when users interact with widgets. Handle these actions in your component:
235
+
236
+ ```typescript
237
+ async handleWidgetAction(event: { actionName: string; payload: any }) {
238
+ const { actionName, payload } = event;
239
+
240
+ try {
241
+ const response = await fetch('/api/chat/action', {
242
+ method: 'POST',
243
+ headers: { 'Content-Type': 'application/json' },
244
+ body: JSON.stringify({ action: actionName, payload })
245
+ });
246
+
247
+ const data = await response.json();
248
+ // Update widgets with response
249
+ this.widgets = data.widgets;
250
+ } catch (error) {
251
+ console.error('Failed to handle widget action:', error);
252
+ }
253
+ }
254
+ ```
255
+
256
+ ### Custom Widget Renderers
257
+
258
+ The library now supports three types of custom widget renderers for enhanced flexibility:
259
+
260
+ #### 1. HTML Function Renderer
261
+
262
+ Simple function that returns HTML strings:
263
+
264
+ ```typescript
265
+ this.widgetRegistry.registerRenderer('myWidget', (widget) => {
266
+ return `<div class="my-widget">${widget.label}</div>`;
267
+ });
268
+ ```
269
+
270
+ #### 2. Angular Component Renderer (Recommended)
271
+
272
+ Use Angular components for full framework features including data binding, change detection, and dependency injection:
273
+
274
+ ```typescript
275
+ import { Component, Input } from '@angular/core';
276
+ import { CustomWidgetComponent } from '@bbq-chat/widgets-angular';
277
+
278
+ @Component({
279
+ selector: 'app-my-widget',
280
+ standalone: true,
281
+ template: `
282
+ <div class="my-widget">
283
+ <h3>{{ myWidget.title }}</h3>
284
+ <button (click)="onClick()">Action</button>
285
+ </div>
286
+ `
287
+ })
288
+ export class MyWidgetComponent implements CustomWidgetComponent {
289
+ @Input() widget!: ChatWidget;
290
+ widgetAction?: (actionName: string, payload: unknown) => void;
291
+
292
+ get myWidget(): MyCustomWidget {
293
+ return this.widget as MyCustomWidget;
294
+ }
295
+
296
+ onClick() {
297
+ this.widgetAction?.('my_action', { data: 'example' });
298
+ }
299
+ }
300
+
301
+ // Register the component
302
+ this.widgetRegistry.registerRenderer('myWidget', MyWidgetComponent);
303
+ ```
304
+
305
+ #### 3. Angular Template Renderer
306
+
307
+ Use inline templates with full Angular template syntax:
308
+
309
+ ```typescript
310
+ import { WidgetTemplateContext } from '@bbq-chat/widgets-angular';
311
+
312
+ @Component({
313
+ template: `
314
+ <ng-template #myTemplate let-widget let-emitAction="emitAction">
315
+ <div class="my-widget">
316
+ <h3>{{ widget.title }}</h3>
317
+ <button (click)="emitAction('my_action', { data: 'example' })">
318
+ Action
319
+ </button>
320
+ </div>
321
+ </ng-template>
322
+ `
323
+ })
324
+ export class AppComponent implements OnInit {
325
+ @ViewChild('myTemplate', { static: true }) myTemplate!: TemplateRef<WidgetTemplateContext>;
326
+
327
+ ngOnInit() {
328
+ this.widgetRegistry.registerRenderer('myWidget', this.myTemplate);
329
+ }
330
+ }
331
+ ```
332
+
333
+ See [EXAMPLES.md](./EXAMPLES.md) for detailed examples and best practices.
334
+
335
+ ## License
336
+
337
+ MIT
338
+
339
+ ## Repository
340
+
341
+ https://github.com/JeanMarcMbouma/BbQ.ChatWidgets