@bbq-chat/widgets-angular 1.0.1 → 1.0.2
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/dist/README.md +252 -0
- package/dist/fesm2022/index.mjs +542 -0
- package/dist/fesm2022/index.mjs.map +1 -0
- package/dist/types/index.d.ts +335 -0
- package/package.json +26 -44
- package/.eslintrc.json +0 -23
- package/.prettierrc.json +0 -8
- package/EXAMPLES.md +0 -468
- package/ng-package.json +0 -10
- package/scripts/build.mjs +0 -63
- package/src/custom-widget-renderer.types.ts +0 -120
- package/src/index.ts +0 -75
- package/src/public_api.ts +0 -75
- package/src/widget-di.tokens.ts +0 -63
- package/src/widget-registry.service.ts +0 -128
- package/src/widget-renderer.component.ts +0 -400
- package/tsconfig.json +0 -34
package/dist/README.md
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
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
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @bbq-chat/widgets-angular @bbq-chat/widgets
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Peer Dependencies
|
|
12
|
+
|
|
13
|
+
This package requires:
|
|
14
|
+
- `@angular/common` >= 17.0.0
|
|
15
|
+
- `@angular/core` >= 17.0.0
|
|
16
|
+
- `@bbq-chat/widgets` ^1.0.0
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
### 1. Import the component
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { Component } from '@angular/core';
|
|
24
|
+
import { WidgetRendererComponent } from '@bbq-chat/widgets-angular';
|
|
25
|
+
import type { ChatWidget } from '@bbq-chat/widgets-angular';
|
|
26
|
+
|
|
27
|
+
@Component({
|
|
28
|
+
selector: 'app-chat',
|
|
29
|
+
standalone: true,
|
|
30
|
+
imports: [WidgetRendererComponent],
|
|
31
|
+
template: `
|
|
32
|
+
<bbq-widget-renderer
|
|
33
|
+
[widgets]="widgets"
|
|
34
|
+
(widgetAction)="handleWidgetAction($event)">
|
|
35
|
+
</bbq-widget-renderer>
|
|
36
|
+
`
|
|
37
|
+
})
|
|
38
|
+
export class ChatComponent {
|
|
39
|
+
widgets: ChatWidget[] = [];
|
|
40
|
+
|
|
41
|
+
handleWidgetAction(event: { actionName: string; payload: any }) {
|
|
42
|
+
console.log('Widget action:', event.actionName, event.payload);
|
|
43
|
+
// Handle the action (e.g., send to backend)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### 2. Import styles
|
|
49
|
+
|
|
50
|
+
In your `styles.css` or `styles.scss`:
|
|
51
|
+
|
|
52
|
+
```css
|
|
53
|
+
@import '@bbq-chat/widgets/styles';
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Or import a specific theme:
|
|
57
|
+
|
|
58
|
+
```css
|
|
59
|
+
@import '@bbq-chat/widgets/styles/light';
|
|
60
|
+
@import '@bbq-chat/widgets/styles/dark';
|
|
61
|
+
@import '@bbq-chat/widgets/styles/corporate';
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Components
|
|
65
|
+
|
|
66
|
+
### WidgetRendererComponent
|
|
67
|
+
|
|
68
|
+
The main component for rendering chat widgets.
|
|
69
|
+
|
|
70
|
+
**Inputs:**
|
|
71
|
+
- `widgets: ChatWidget[] | null | undefined` - Array of widgets to render
|
|
72
|
+
|
|
73
|
+
**Outputs:**
|
|
74
|
+
- `widgetAction: EventEmitter<{ actionName: string; payload: any }>` - Emits when a widget action is triggered
|
|
75
|
+
|
|
76
|
+
**Example:**
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
<bbq-widget-renderer
|
|
80
|
+
[widgets]="messageWidgets"
|
|
81
|
+
(widgetAction)="onWidgetAction($event)">
|
|
82
|
+
</bbq-widget-renderer>
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Services
|
|
86
|
+
|
|
87
|
+
### WidgetRegistryService
|
|
88
|
+
|
|
89
|
+
Service for registering custom widget types.
|
|
90
|
+
|
|
91
|
+
**Example:**
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { Component, OnInit } from '@angular/core';
|
|
95
|
+
import { WidgetRegistryService } from '@bbq-chat/widgets-angular';
|
|
96
|
+
|
|
97
|
+
export class MyCustomWidget {
|
|
98
|
+
type = 'myCustomWidget';
|
|
99
|
+
constructor(public label: string, public action: string) {}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@Component({
|
|
103
|
+
selector: 'app-root',
|
|
104
|
+
template: '...'
|
|
105
|
+
})
|
|
106
|
+
export class AppComponent implements OnInit {
|
|
107
|
+
constructor(private widgetRegistry: WidgetRegistryService) {}
|
|
108
|
+
|
|
109
|
+
ngOnInit() {
|
|
110
|
+
// Register custom widget factory
|
|
111
|
+
this.widgetRegistry.registerFactory('myCustomWidget', (obj) => {
|
|
112
|
+
if (obj.type === 'myCustomWidget') {
|
|
113
|
+
return new MyCustomWidget(obj.label, obj.action);
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Widget Types
|
|
122
|
+
|
|
123
|
+
All standard widget types from `@bbq-chat/widgets` are supported:
|
|
124
|
+
|
|
125
|
+
- `ButtonWidget` - Clickable buttons
|
|
126
|
+
- `CardWidget` - Information cards
|
|
127
|
+
- `FormWidget` - Form containers
|
|
128
|
+
- `InputWidget` - Text input fields
|
|
129
|
+
- `TextAreaWidget` - Multi-line text input
|
|
130
|
+
- `DropdownWidget` - Dropdown selects
|
|
131
|
+
- `SliderWidget` - Range sliders
|
|
132
|
+
- `ToggleWidget` - Toggle switches
|
|
133
|
+
- `FileUploadWidget` - File upload controls
|
|
134
|
+
- `DatePickerWidget` - Date pickers
|
|
135
|
+
- `MultiSelectWidget` - Multi-select dropdowns
|
|
136
|
+
- `ThemeSwitcherWidget` - Theme switcher controls
|
|
137
|
+
- `ProgressBarWidget` - Progress indicators
|
|
138
|
+
- `ImageWidget` - Image displays
|
|
139
|
+
- `ImageCollectionWidget` - Image galleries
|
|
140
|
+
|
|
141
|
+
## Advanced Usage
|
|
142
|
+
|
|
143
|
+
### Handling Widget Actions
|
|
144
|
+
|
|
145
|
+
Widget actions are emitted when users interact with widgets. Handle these actions in your component:
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
async handleWidgetAction(event: { actionName: string; payload: any }) {
|
|
149
|
+
const { actionName, payload } = event;
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
const response = await fetch('/api/chat/action', {
|
|
153
|
+
method: 'POST',
|
|
154
|
+
headers: { 'Content-Type': 'application/json' },
|
|
155
|
+
body: JSON.stringify({ action: actionName, payload })
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const data = await response.json();
|
|
159
|
+
// Update widgets with response
|
|
160
|
+
this.widgets = data.widgets;
|
|
161
|
+
} catch (error) {
|
|
162
|
+
console.error('Failed to handle widget action:', error);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Custom Widget Renderers
|
|
168
|
+
|
|
169
|
+
The library now supports three types of custom widget renderers for enhanced flexibility:
|
|
170
|
+
|
|
171
|
+
#### 1. HTML Function Renderer
|
|
172
|
+
|
|
173
|
+
Simple function that returns HTML strings:
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
this.widgetRegistry.registerRenderer('myWidget', (widget) => {
|
|
177
|
+
return `<div class="my-widget">${widget.label}</div>`;
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
#### 2. Angular Component Renderer (Recommended)
|
|
182
|
+
|
|
183
|
+
Use Angular components for full framework features including data binding, change detection, and dependency injection:
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
import { Component, Input } from '@angular/core';
|
|
187
|
+
import { CustomWidgetComponent } from '@bbq-chat/widgets-angular';
|
|
188
|
+
|
|
189
|
+
@Component({
|
|
190
|
+
selector: 'app-my-widget',
|
|
191
|
+
standalone: true,
|
|
192
|
+
template: `
|
|
193
|
+
<div class="my-widget">
|
|
194
|
+
<h3>{{ myWidget.title }}</h3>
|
|
195
|
+
<button (click)="onClick()">Action</button>
|
|
196
|
+
</div>
|
|
197
|
+
`
|
|
198
|
+
})
|
|
199
|
+
export class MyWidgetComponent implements CustomWidgetComponent {
|
|
200
|
+
@Input() widget!: ChatWidget;
|
|
201
|
+
widgetAction?: (actionName: string, payload: unknown) => void;
|
|
202
|
+
|
|
203
|
+
get myWidget(): MyCustomWidget {
|
|
204
|
+
return this.widget as MyCustomWidget;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
onClick() {
|
|
208
|
+
this.widgetAction?.('my_action', { data: 'example' });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Register the component
|
|
213
|
+
this.widgetRegistry.registerRenderer('myWidget', MyWidgetComponent);
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
#### 3. Angular Template Renderer
|
|
217
|
+
|
|
218
|
+
Use inline templates with full Angular template syntax:
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
import { WidgetTemplateContext } from '@bbq-chat/widgets-angular';
|
|
222
|
+
|
|
223
|
+
@Component({
|
|
224
|
+
template: `
|
|
225
|
+
<ng-template #myTemplate let-widget let-emitAction="emitAction">
|
|
226
|
+
<div class="my-widget">
|
|
227
|
+
<h3>{{ widget.title }}</h3>
|
|
228
|
+
<button (click)="emitAction('my_action', { data: 'example' })">
|
|
229
|
+
Action
|
|
230
|
+
</button>
|
|
231
|
+
</div>
|
|
232
|
+
</ng-template>
|
|
233
|
+
`
|
|
234
|
+
})
|
|
235
|
+
export class AppComponent implements OnInit {
|
|
236
|
+
@ViewChild('myTemplate', { static: true }) myTemplate!: TemplateRef<WidgetTemplateContext>;
|
|
237
|
+
|
|
238
|
+
ngOnInit() {
|
|
239
|
+
this.widgetRegistry.registerRenderer('myWidget', this.myTemplate);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
See [EXAMPLES.md](./EXAMPLES.md) for detailed examples and best practices.
|
|
245
|
+
|
|
246
|
+
## License
|
|
247
|
+
|
|
248
|
+
MIT
|
|
249
|
+
|
|
250
|
+
## Repository
|
|
251
|
+
|
|
252
|
+
https://github.com/JeanMarcMbouma/BbQ.ChatWidgets
|