Skip to content

Commit f684e3d

Browse files
committed
feat(chat): add template for message author
1 parent 7802431 commit f684e3d

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

src/components/chat/chat-state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export class ChatState {
8080
this.renderDefaultAttachmentHeader(att, msg),
8181
attachmentContentTemplate: (att: IgcMessageAttachment) =>
8282
this.renderDefaultAttachmentContent(att),
83+
messageAuthorTemplate: () => {},
8384
messageTemplate: (
8485
m: IgcMessage,
8586
ctx: { textContent: unknown; templates: Partial<IgcChatTemplates> }
@@ -226,6 +227,7 @@ export class ChatState {
226227
) => {
227228
const templates = { ...this._defaultTemplates, ...ctx.templates };
228229
return html`
230+
${templates.messageAuthorTemplate(m) ?? nothing}
229231
${ctx?.textContent ?? m.text}
230232
${m.attachments?.length
231233
? html`<igc-message-attachments .message=${m}>

src/components/chat/chat.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ describe('Chat', () => {
3636

3737
const createChatComponent = () => html`<igc-chat></igc-chat>`;
3838

39+
const messageAuthorTemplate = (msg: any) => {
40+
return msg.sender !== 'user' ? html`<span>AI Assistant</span>` : html``;
41+
};
42+
3943
const messageTemplate = (msg: any) => {
4044
return html`<div>
4145
<h5>${msg.sender === 'user' ? 'You' : 'Bot'}:</h5>
@@ -1123,6 +1127,39 @@ describe('Chat', () => {
11231127
draftMessage.text
11241128
);
11251129
});
1130+
1131+
it.only('should render messageAuthorTemplate', async () => {
1132+
chat.options = {
1133+
templates: {
1134+
messageAuthorTemplate: messageAuthorTemplate,
1135+
},
1136+
};
1137+
await elementUpdated(chat);
1138+
await aTimeout(500);
1139+
1140+
const messageElements = chat.shadowRoot
1141+
?.querySelector('igc-chat-message-list')
1142+
?.shadowRoot?.querySelectorAll('igc-chat-message');
1143+
expect(messageElements).not.to.be.undefined;
1144+
messageElements?.forEach((messageElement, index) => {
1145+
const isCurrentUser = chat.messages[index].sender === 'user';
1146+
const messsageContainer = messageElement.shadowRoot?.querySelector(
1147+
`div[part='message-container${isCurrentUser ? ' sent' : ''}']`
1148+
);
1149+
expect(messsageContainer).not.to.be.undefined;
1150+
expect(messsageContainer).dom.to.equal(
1151+
`<div part="message-container${isCurrentUser ? ' sent' : ''}">
1152+
${!isCurrentUser ? '<span>AI Assistant</span>' : ''}
1153+
<pre part="plain-text">
1154+
${chat.messages[index].text}
1155+
</pre>
1156+
<igc-message-attachments>
1157+
</igc-message-attachments>
1158+
${!isCurrentUser ? messageReactions : ''}
1159+
</div>`
1160+
);
1161+
});
1162+
});
11261163
});
11271164

11281165
describe('Interactions', () => {

src/components/chat/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ export type IgcChatTemplates = {
188188
*/
189189
attachmentContentTemplate?: (attachment: IgcMessageAttachment) => unknown;
190190

191+
/**
192+
* Template for rendering an information about the author of a single chat message.
193+
* Use this to customize message layout, formatting, or metadata.
194+
*/
195+
messageAuthorTemplate?: (message: IgcMessage) => unknown;
196+
191197
/**
192198
* Template for rendering a single chat message.
193199
* Use this to customize message layout, formatting, or metadata.

stories/chat.stories.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ const userMessages: any[] = [];
102102

103103
let isResponseSent: boolean;
104104

105+
const _messageAuthorTemplate = (msg: any) => {
106+
return msg.sender !== 'user'
107+
? html`
108+
<div>
109+
<igc-avatar
110+
shape="circle"
111+
src="https://www.infragistics.com/angular-demos/assets/images/men/1.jpg"
112+
style="position: relative;"
113+
>
114+
</igc-avatar>
115+
<span
116+
style="color: #c00000; font-weight: bold; position: absolute; margin: 8px"
117+
>AI Assistant</span
118+
>
119+
</div>
120+
`
121+
: html``;
122+
};
105123
const _messageActionsTemplate = (msg: any) => {
106124
return msg.sender !== 'user' && msg.text.trim()
107125
? isResponseSent !== false
@@ -521,6 +539,7 @@ export const Chat_Templates: Story = {
521539
inputPlaceholder: 'Type your message here...',
522540
suggestions: ['Hello', 'Hi', 'Generate an image!'],
523541
templates: {
542+
messageAuthorTemplate: _messageAuthorTemplate,
524543
messageActionsTemplate: _messageActionsTemplate,
525544
textAreaAttachmentsTemplate: _textAreaAttachmentsTemplate,
526545
textAreaActionsTemplate: actionsTemplate,

0 commit comments

Comments
 (0)