Skip to content

Commit 40ca8c8

Browse files
committed
fix(chat): elaborate the bidirectionality of the messages array
1 parent 3e93b94 commit 40ca8c8

2 files changed

Lines changed: 65 additions & 9 deletions

File tree

  • docs
    • angular/src/content/en/components
    • xplat/src/content/en/components/interactivity

docs/angular/src/content/en/components/chat.mdx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,27 @@ The Chat component exposes several key properties that let you control its state
8585

8686
| Name | Description |
8787
| ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
88-
| `messages` | Array of messages (`IgcChatMessage[]`) displayed in the chat. You can bind to this to control which messages are shown. |
88+
| `messages` | Bidirectional array of messages (`IgcChatMessage[]`) displayed in the chat. The Chat updates the supplied collection when the user sends a message. |
8989
| `draftMessage` | The current unsent message, represented as an object containing `text` and optional `attachments`. This is useful for saving or restoring message drafts. |
9090
| `options` | Chat configuration (<ApiLink type="ChatOptions" />) such as current user ID, input placeholders, accepted file types, quick reply suggestions and typing behavior. |
9191
| `templates` | Custom Angular templates (<ApiLink type="ChatTemplates" />) for message content, input, attachments, and other parts of the chat UI. |
9292

9393
These properties make it straightforward to synchronize the Chat’s UI with your application’s state and backend.
9494

95+
#### Bidirectional Messages Collection
96+
97+
The `messages` collection is bidirectional: the application provides the messages to display, and the Chat updates the original collection as the conversation continues. After the user sends a message, code holding a reference to the collection can access the newly created message. This is an in-place update to the collection rather than Angular two-way binding with `[(messages)]`.
98+
99+
The `messageCreated` event is a notification for persistence or other side effects. Do not append the created message to the collection again.
100+
101+
If the original collection must remain unchanged, pass the Chat a shallow copy:
102+
103+
```ts
104+
public messages = [...this.originalMessages];
105+
```
106+
107+
Existing message objects are still shared. Create a new instance of each message when building the copied collection if those objects must also remain independent.
108+
95109
### Attachments
96110
Modern conversations are rarely limited to text alone. The Chat component includes built-in support for file attachments, allowing users to share images, documents, and other files.
97111
By default, the input area includes an attachment button. You can control which file types are allowed by setting the `acceptedFiles` property:

docs/xplat/src/content/en/components/interactivity/chat.mdx

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,12 @@ const ChatExample = () => {
234234
}
235235
```
236236

237-
You can then sync messages coming from the client, by hooking to the `messageCreated` event and adding the created messages to the collection:
237+
The `Messages` collection is updated automatically when the user sends a message. You can handle the `MessageCreated` event to persist the message or perform other side effects; do not append the event detail to `Messages` again:
238238

239239
```cs
240240
public void OnMessageCreated(IgbChatMessageEventArgs e)
241241
{
242-
Messages = Messages.Append(e.Detail).ToArray();
242+
Console.WriteLine($"Message created: {e.Detail.Text}");
243243
}
244244
```
245245

@@ -250,15 +250,57 @@ This approach makes it easy to plug the Chat into your own data source, such as
250250
### Properties
251251
The <ApiLink type="Chat" /> component exposes several key properties that let you control its state and configuration:
252252

253-
| Name | Description |
254-
| ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
255-
| `messages` | Array of messages (<ApiLink type="ChatMessage" />[]) displayed in the chat. You can bind to this to control which messages are shown. |
256-
| `draftMessage` | The current unsent message, represented as an object containing `text` and optional `attachments`. This is useful for saving or restoring message drafts. |
257-
| `options` | Chat configuration (<ApiLink type="ChatOptions" />) such as current user ID, input placeholders, accepted file types, quick reply suggestions, typing delay, and custom renderers. |
258-
| `resourceStrings` | Localized resource strings for labels, headers, and system text. Use this property to adapt the component for different languages. |
253+
| Name | Description |
254+
| --------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
255+
| <PlatformBlock for="Blazor">`Messages`</PlatformBlock><PlatformBlock for="WebComponents,React">`messages`</PlatformBlock> | Bidirectional array of messages (<ApiLink type="ChatMessage" />[]) displayed in the chat. The Chat updates the supplied collection when the user sends a message. |
256+
| <PlatformBlock for="Blazor">`DraftMessage`</PlatformBlock><PlatformBlock for="WebComponents,React">`draftMessage`</PlatformBlock> | The current unsent message, represented as an object containing `text` and optional `attachments`. This is useful for saving or restoring message drafts. |
257+
| <PlatformBlock for="Blazor">`Options`</PlatformBlock><PlatformBlock for="WebComponents,React">`options`</PlatformBlock> | Chat configuration (<ApiLink type="ChatOptions" />) such as current user ID, input placeholders, accepted file types, quick reply suggestions, typing delay, and custom renderers. |
258+
| <PlatformBlock for="Blazor">`ResourceStrings`</PlatformBlock><PlatformBlock for="WebComponents,React">`resourceStrings`</PlatformBlock> | Localized resource strings for labels, headers, and system text. Use this property to adapt the component for different languages. |
259259

260260
These properties make it straightforward to synchronize the Chat’s UI with your application’s state and backend.
261261

262+
#### Bidirectional Messages Collection
263+
264+
The <PlatformBlock for="WebComponents,React">`messages`</PlatformBlock><PlatformBlock for="Blazor">`Messages`</PlatformBlock> collection is bidirectional: the application provides the messages to display, and the Chat updates the original collection as the conversation continues. After the user sends a message, code holding a reference to the collection can access the newly created message.
265+
266+
The <PlatformBlock for="WebComponents">`igcMessageCreated`</PlatformBlock><PlatformBlock for="React">`onMessageCreated`</PlatformBlock><PlatformBlock for="Blazor">`MessageCreated`</PlatformBlock> event is a notification for persistence or other side effects. Do not append the created message to the collection again.
267+
268+
If the original collection must remain unchanged, pass the Chat a shallow copy. Existing message objects are still shared; create a new instance of each message when building the copied collection if those objects must also remain independent.
269+
270+
<PlatformBlock for="WebComponents">
271+
272+
273+
```ts
274+
const chatMessages = [...originalMessages];
275+
chat.messages = chatMessages;
276+
```
277+
278+
</PlatformBlock>
279+
280+
<PlatformBlock for="React">
281+
282+
283+
```tsx
284+
const chatMessages = [...originalMessages];
285+
286+
return <IgrChat messages={chatMessages} options={options} />;
287+
```
288+
289+
</PlatformBlock>
290+
291+
<PlatformBlock for="Blazor">
292+
293+
294+
```cs
295+
IgbChatMessage[] chatMessages = originalMessages.ToArray();
296+
```
297+
298+
```razor
299+
<IgbChat Messages="chatMessages" Options="Options"></IgbChat>
300+
```
301+
302+
</PlatformBlock>
303+
262304
<PlatformBlock for="WebComponents, React">
263305

264306
### Attachments

0 commit comments

Comments
 (0)