11import { ContextProvider } from '@lit/context' ;
22import { html , LitElement , nothing } from 'lit' ;
33import { property } from 'lit/decorators.js' ;
4+ import { cache } from 'lit/directives/cache.js' ;
45import { repeat } from 'lit/directives/repeat.js' ;
56import { addThemingController } from '../../theming/theming-controller.js' ;
67import IgcButtonComponent from '../button/button.js' ;
@@ -11,6 +12,7 @@ import { registerComponent } from '../common/definitions/register.js';
1112import { IgcChatResourceStringEN } from '../common/i18n/chat.resources.js' ;
1213import type { Constructor } from '../common/mixins/constructor.js' ;
1314import { EventEmitterMixin } from '../common/mixins/event-emitter.js' ;
15+ import { isEmpty } from '../common/util.js' ;
1416import IgcIconComponent from '../icon/icon.js' ;
1517import IgcListComponent from '../list/list.js' ;
1618import IgcListHeaderComponent from '../list/list-header.js' ;
@@ -337,21 +339,14 @@ export default class IgcChatComponent extends EventEmitterMixin<
337339 const messages = this . _chatState ?. messages ?? [ ] ;
338340
339341 return html `
340- < div
341- part ="message-list "
342- aria-label ="Message list "
343- role ="group "
344- tabindex ="0 "
345- >
342+ < div part ="message-list " tabindex ="0 ">
346343 ${ repeat (
347344 messages ,
348345 ( message ) => message . id ,
349346 ( message ) => {
350- const messageId = `message-${ message . id } ` ;
351347 return html `
352348 < igc-chat-message
353- id =${ messageId }
354- role ="option"
349+ id =${ `message-${ message . id } ` }
355350 part ="message-item"
356351 .message=${ message }
357352 exportparts="message-container, message-text, message-attachments, message-actions,
@@ -374,49 +369,57 @@ export default class IgcChatComponent extends EventEmitterMixin<
374369 ` ;
375370 }
376371
377- private * renderLoadingTemplate ( ) {
378- yield html `< div part ="typing-indicator ">
379- < div part ="typing-dot "> </ div >
380- < div part ="typing-dot "> </ div >
381- < div part ="typing-dot "> </ div >
382- < div part ="typing-dot "> </ div >
383- </ div > ` ;
372+ private renderLoadingTemplate ( ) {
373+ return html `
374+ < div part ="typing-indicator ">
375+ < div part ="typing-dot "> </ div >
376+ < div part ="typing-dot "> </ div >
377+ < div part ="typing-dot "> </ div >
378+ < div part ="typing-dot "> </ div >
379+ </ div >
380+ ` ;
384381 }
385382
386383 private renderSuggestionPrefix ( ) {
387- return html `< span slot ="start ">
388- < igc-icon name ="star-icon " collection ="material "> </ igc-icon >
389- </ span > ` ;
384+ return html `
385+ < span slot ="start ">
386+ < igc-icon name ="star-icon " collection ="material "> </ igc-icon >
387+ </ span >
388+ ` ;
390389 }
391390
392391 private renderSuggestions ( ) {
393392 const hasContent = this . _slots . hasAssignedElements ( 'suggestions-header' ) ;
394- return html `< div part ="suggestions-container ">
395- < igc-list role ="list " aria-label ="Suggestions ">
396- < igc-list-header part ="suggestions-header ">
397- < span ?hidden =${ hasContent } >
398- ${ this . resourceStrings . suggestionsHeader }
399- </ span >
400- < slot name ="suggestions-header "> </ slot >
401- </ igc-list-header >
402- < slot name ="suggestions " part ="suggestions ">
403- ${ this . _chatState . options ?. suggestions ?. map (
404- ( suggestion ) => html `
405- < slot name ="suggestion " part ="suggestion " role ="listitem ">
406- < igc-list-item
407- @click =${ ( ) =>
408- this . _chatState ?. handleSuggestionClick ( suggestion ) }
409- >
410- ${ this . renderSuggestionPrefix ( ) }
411- < span slot ="title "> ${ suggestion } </ span >
412- </ igc-list-item >
413- </ slot >
414- `
415- ) }
416- </ slot >
417- < slot name ="suggestions-actions " part ="suggestions-actions "> </ slot >
418- </ igc-list >
419- </ div > ` ;
393+ const suggestions = this . _chatState . options ?. suggestions ?? [ ] ;
394+
395+ return html `
396+ < div part ="suggestions-container ">
397+ < igc-list >
398+ < igc-list-header part ="suggestions-header ">
399+ < span ?hidden =${ hasContent } >
400+ ${ this . resourceStrings . suggestionsHeader }
401+ </ span >
402+ < slot name ="suggestions-header "> </ slot >
403+ </ igc-list-header >
404+ < slot name ="suggestions " part ="suggestions ">
405+ ${ suggestions . map (
406+ ( suggestion ) => html `
407+ < slot name ="suggestion " part ="suggestion ">
408+ < igc-list-item
409+ @click =${ ( ) =>
410+ this . _chatState ?. handleSuggestionClick ( suggestion ) }
411+ >
412+ ${ this . renderSuggestionPrefix ( ) }
413+ < span slot ="title "> ${ suggestion } </ span >
414+ </ igc-list-item >
415+ </ slot >
416+ `
417+ ) }
418+ </ slot >
419+ < slot name ="suggestions-actions " part ="suggestions-actions "> </ slot >
420+ </ igc-list >
421+ </ div >
422+ ` ;
420423 }
421424
422425 /**
@@ -427,10 +430,20 @@ export default class IgcChatComponent extends EventEmitterMixin<
427430 this . _context . setValue ( this . _chatState , true ) ;
428431 }
429432
433+ private _renderEmptyState ( ) {
434+ return html `
435+ < div part ="empty-state ">
436+ < slot name ="empty-state "> </ slot >
437+ </ div >
438+ ` ;
439+ }
440+
430441 protected override render ( ) {
431- const hasSuggestions =
432- this . _chatState . options ?. suggestions &&
433- this . _chatState . options . suggestions . length > 0 ;
442+ const hasMessages = ! isEmpty ( this . messages ) ;
443+ const suggestions = isEmpty ( this . _chatState . options ?. suggestions ?? [ ] )
444+ ? nothing
445+ : this . renderSuggestions ( ) ;
446+
434447 return html `
435448 < div
436449 part ="chat-container "
@@ -466,17 +479,16 @@ export default class IgcChatComponent extends EventEmitterMixin<
466479 "
467480 >
468481 ${ this . renderHeader ( ) }
482+
469483 < div part ="chat-wrapper ">
470- ${ this . messages . length === 0
471- ? html `< div part ="empty-state ">
472- < slot name ="empty-state "> </ slot >
473- </ div > `
474- : this . renderMessages ( ) }
475- ${ hasSuggestions &&
476- this . _chatState . suggestionsPosition === 'below-messages'
477- ? this . renderSuggestions ( )
484+ ${ cache (
485+ hasMessages ? this . renderMessages ( ) : this . _renderEmptyState ( )
486+ ) }
487+ ${ this . _chatState . suggestionsPosition === 'below-messages'
488+ ? suggestions
478489 : nothing }
479490 </ div >
491+
480492 < igc-chat-input
481493 exportparts ="
482494 input-container,
@@ -490,9 +502,8 @@ export default class IgcChatComponent extends EventEmitterMixin<
490502 send-button "
491503 >
492504 </ igc-chat-input >
493- ${ hasSuggestions &&
494- this . _chatState . suggestionsPosition === 'below-input'
495- ? this . renderSuggestions ( )
505+ ${ this . _chatState . suggestionsPosition === 'below-input'
506+ ? suggestions
496507 : nothing }
497508 </ div >
498509 ` ;
0 commit comments