Skip to content

Commit 598a378

Browse files
committed
Merge branch 'dmdimitrov/chat-ai-component' of https://github.com/IgniteUI/igniteui-webcomponents into dmdimitrov/chat-ai-component
2 parents fbfa82e + e92820c commit 598a378

File tree

3 files changed

+33
-42
lines changed

3 files changed

+33
-42
lines changed

src/components/chat/chat-message-list.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { state } from 'lit/decorators.js';
44
import { repeat } from 'lit/directives/repeat.js';
55
import { chatContext } from '../common/context.js';
66
import {
7+
addKeybindings,
78
arrowDown,
89
arrowUp,
910
endKey,
@@ -62,6 +63,15 @@ export default class IgcChatMessageListComponent extends LitElement {
6263
registerComponent(IgcChatMessageListComponent, IgcChatMessageComponent);
6364
}
6465

66+
constructor() {
67+
super();
68+
addKeybindings(this)
69+
.set(homeKey, this.navigateToMessage)
70+
.set(endKey, this.navigateToMessage)
71+
.set(arrowUp, this.navigateToMessage)
72+
.set(arrowDown, this.navigateToMessage);
73+
}
74+
6575
/**
6676
* Formats a date to a human-readable label.
6777
* Returns 'Today', 'Yesterday', or localized date string.
@@ -167,7 +177,7 @@ export default class IgcChatMessageListComponent extends LitElement {
167177
* @param e KeyboardEvent from user input
168178
* @private
169179
*/
170-
private handleKeyDown(e: KeyboardEvent) {
180+
private navigateToMessage(e: KeyboardEvent) {
171181
if (!this._chatState?.messages || this._chatState.messages.length === 0) {
172182
return;
173183
}
@@ -178,21 +188,21 @@ export default class IgcChatMessageListComponent extends LitElement {
178188
let activeMessageId = '';
179189

180190
switch (e.key) {
181-
case homeKey:
191+
case 'home':
182192
activeMessageId = this._chatState.sortedMessagesIds[0];
183193
break;
184-
case endKey:
194+
case 'end':
185195
activeMessageId =
186196
this._chatState.sortedMessagesIds[
187197
this._chatState.sortedMessagesIds.length - 1
188198
];
189199
break;
190-
case arrowUp:
200+
case 'arrowup':
191201
if (currentIndex > 0) {
192202
activeMessageId = this._chatState.sortedMessagesIds[currentIndex - 1];
193203
}
194204
break;
195-
case arrowDown:
205+
case 'arrowdown':
196206
if (currentIndex < this._chatState?.messages.length - 1) {
197207
activeMessageId = this._chatState.sortedMessagesIds[currentIndex + 1];
198208
}
@@ -258,8 +268,7 @@ export default class IgcChatMessageListComponent extends LitElement {
258268
tabindex="0"
259269
@focusin=${this.handleFocusIn}
260270
@focusout=${this.handleFocusOut}
261-
@keydown=${this.handleKeyDown}
262-
>
271+
></div>
263272
<div part="message-list">
264273
${repeat(
265274
groupedMessages,
@@ -285,9 +294,11 @@ export default class IgcChatMessageListComponent extends LitElement {
285294
)}
286295
`
287296
)}
288-
${this._chatState?.options?.isComposing
289-
? this.renderLoadingTemplate()
290-
: nothing}
297+
${
298+
this._chatState?.options?.isComposing
299+
? this.renderLoadingTemplate()
300+
: nothing
301+
}
291302
</div>
292303
</div>
293304
`;

src/components/chat/chat.spec.ts

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ describe('Chat', () => {
202202
</slot>
203203
</div>
204204
<div aria-label="Suggestions" part="suggestions-container" role="list">
205+
<slot name="suggestions-header"> </slot>
205206
<slot name="suggestions" part="suggestions">
206207
</slot>
207208
</div>
@@ -432,6 +433,7 @@ describe('Chat', () => {
432433

433434
expect(suggestionsContainer).dom.to.equal(
434435
`<div aria-label="Suggestions" part="suggestions-container" role="list">
436+
<slot name="suggestions-header"> </slot>
435437
<slot name="suggestions" part="suggestions">
436438
<slot name="suggestion" part="suggestion" role="listitem">
437439
<igc-chip>
@@ -711,6 +713,7 @@ describe('Chat', () => {
711713

712714
expect(suggestionsContainer).dom.to.equal(
713715
`<div aria-label="Suggestions" part="suggestions-container" role="list">
716+
<slot name="suggestions-header"> </slot>
714717
<slot name="suggestions" part="suggestions">
715718
<slot name="suggestion" part="suggestion" role="listitem">
716719
<igc-chip>
@@ -1232,27 +1235,15 @@ describe('Chat', () => {
12321235
await nextFrame();
12331236

12341237
// Activates the previous message on `ArrowUp`
1235-
messageContainer.dispatchEvent(
1236-
new KeyboardEvent('keydown', {
1237-
key: arrowUp,
1238-
bubbles: true,
1239-
cancelable: true,
1240-
})
1241-
);
1242-
await nextFrame();
1238+
simulateKeyboard(messageContainer, arrowUp);
1239+
await elementUpdated(chat);
12431240
expect(messageContainer.getAttribute('aria-activedescendant')).to.equal(
12441241
'message-3'
12451242
);
12461243

12471244
// Activates the next message on `ArrowDown`
1248-
messageContainer.dispatchEvent(
1249-
new KeyboardEvent('keydown', {
1250-
key: arrowDown,
1251-
bubbles: true,
1252-
cancelable: true,
1253-
})
1254-
);
1255-
await nextFrame();
1245+
simulateKeyboard(messageContainer, arrowDown);
1246+
await elementUpdated(chat);
12561247
expect(messageContainer.getAttribute('aria-activedescendant')).to.equal(
12571248
'message-4'
12581249
);
@@ -1274,27 +1265,15 @@ describe('Chat', () => {
12741265
await nextFrame();
12751266

12761267
// Activates the first message on `Home`
1277-
messageContainer.dispatchEvent(
1278-
new KeyboardEvent('keydown', {
1279-
key: homeKey,
1280-
bubbles: true,
1281-
cancelable: true,
1282-
})
1283-
);
1284-
await nextFrame();
1268+
simulateKeyboard(messageContainer, homeKey);
1269+
await elementUpdated(chat);
12851270
expect(messageContainer.getAttribute('aria-activedescendant')).to.equal(
12861271
'message-1'
12871272
);
12881273

12891274
// Activates the last message on `End`
1290-
messageContainer.dispatchEvent(
1291-
new KeyboardEvent('keydown', {
1292-
key: endKey,
1293-
bubbles: true,
1294-
cancelable: true,
1295-
})
1296-
);
1297-
await nextFrame();
1275+
simulateKeyboard(messageContainer, endKey);
1276+
await elementUpdated(chat);
12981277
expect(messageContainer.getAttribute('aria-activedescendant')).to.equal(
12991278
'message-4'
13001279
);

src/components/chat/chat.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ export default class IgcChatComponent extends EventEmitterMixin<
239239
role="list"
240240
aria-label="Suggestions"
241241
>
242+
<slot name="suggestions-header"> </slot>
242243
<slot name="suggestions" part="suggestions">
243244
${this._chatState.options?.suggestions?.map(
244245
(suggestion) => html`

0 commit comments

Comments
 (0)