@@ -664,6 +664,88 @@ describe('mention', () => {
664664 } ) ;
665665 } ) ) ;
666666 } ) ;
667+
668+ describe ( 'clear button' , ( ) => {
669+ let fixture : ComponentFixture < NzTestClearMentionComponent > ;
670+ let textarea : HTMLTextAreaElement ;
671+
672+ beforeEach ( fakeAsync ( ( ) => {
673+ fixture = TestBed . createComponent ( NzTestClearMentionComponent ) ;
674+ fixture . detectChanges ( ) ;
675+ textarea = fixture . debugElement . query ( By . css ( 'textarea' ) ) . nativeElement ;
676+ tick ( ) ;
677+ } ) ) ;
678+
679+ it ( 'should not show clear button when nzAllowClear is false' , fakeAsync ( ( ) => {
680+ fixture . componentInstance . allowClear = false ;
681+ fixture . detectChanges ( ) ;
682+ typeInElement ( 'test value' , textarea ) ;
683+ fixture . detectChanges ( ) ;
684+ tick ( ) ;
685+ expect ( fixture . debugElement . query ( By . css ( '.ant-mentions-clear-icon' ) ) ) . toBeNull ( ) ;
686+ } ) ) ;
687+
688+ it ( 'should show clear button when nzAllowClear is true and has value' , fakeAsync ( ( ) => {
689+ fixture . componentInstance . allowClear = true ;
690+ fixture . detectChanges ( ) ;
691+ typeInElement ( 'test value' , textarea ) ;
692+ fixture . detectChanges ( ) ;
693+ tick ( ) ;
694+ expect ( fixture . debugElement . query ( By . css ( '.ant-mentions-clear-icon' ) ) ) . toBeTruthy ( ) ;
695+ } ) ) ;
696+
697+ it ( 'should not show clear button when nzAllowClear is true but has no value' , fakeAsync ( ( ) => {
698+ fixture . componentInstance . allowClear = true ;
699+ fixture . detectChanges ( ) ;
700+ typeInElement ( '' , textarea ) ;
701+ fixture . detectChanges ( ) ;
702+ tick ( ) ;
703+ expect ( fixture . debugElement . query ( By . css ( '.ant-mentions-clear-icon' ) ) ) . toBeNull ( ) ;
704+ } ) ) ;
705+
706+ it ( 'should clear input value when clear button is clicked' , fakeAsync ( ( ) => {
707+ fixture . componentInstance . allowClear = true ;
708+ fixture . detectChanges ( ) ;
709+ typeInElement ( 'test value' , textarea ) ;
710+ fixture . detectChanges ( ) ;
711+ tick ( ) ;
712+
713+ const clearButton = fixture . debugElement . query ( By . css ( '.ant-mentions-clear-icon' ) ) . nativeElement ;
714+ clearButton . click ( ) ;
715+ fixture . detectChanges ( ) ;
716+ tick ( ) ;
717+
718+ expect ( textarea . value ) . toBe ( '' ) ;
719+ expect ( fixture . componentInstance . inputValue ) . toBe ( '' ) ;
720+ } ) ) ;
721+
722+ it ( 'should emit nzOnClear when clear button is clicked' , fakeAsync ( ( ) => {
723+ const spy = spyOn ( fixture . componentInstance , 'onClear' ) ;
724+ fixture . componentInstance . allowClear = true ;
725+ fixture . detectChanges ( ) ;
726+ typeInElement ( 'test value' , textarea ) ;
727+ fixture . detectChanges ( ) ;
728+ tick ( ) ;
729+
730+ const clearButton = fixture . debugElement . query ( By . css ( '.ant-mentions-clear-icon' ) ) . nativeElement ;
731+ clearButton . click ( ) ;
732+ fixture . detectChanges ( ) ;
733+
734+ expect ( spy ) . toHaveBeenCalled ( ) ;
735+ } ) ) ;
736+
737+ it ( 'should use custom clear icon when provided' , fakeAsync ( ( ) => {
738+ fixture . componentInstance . allowClear = true ;
739+ fixture . componentInstance . useCustomClearIcon = true ;
740+ fixture . detectChanges ( ) ;
741+ typeInElement ( 'test value' , textarea ) ;
742+ fixture . detectChanges ( ) ;
743+ tick ( ) ;
744+
745+ const clearIcon = fixture . debugElement . query ( By . css ( '.custom-clear-icon' ) ) ;
746+ expect ( clearIcon ) . toBeTruthy ( ) ;
747+ } ) ) ;
748+ } ) ;
667749} ) ;
668750
669751@Component ( {
@@ -806,6 +888,33 @@ class NzTestVariantMentionComponent {
806888 @ViewChild ( NzMentionComponent , { static : false } ) mention ! : NzMentionComponent ;
807889}
808890
891+ @Component ( {
892+ imports : [ FormsModule , NzInputModule , NzMentionModule ] ,
893+ template : `
894+ <nz-mention
895+ [nzSuggestions]="suggestions"
896+ [nzAllowClear]="allowClear"
897+ [nzClearIcon]="useCustomClearIcon ? clearIconTemplate : null"
898+ (nzOnClear)="onClear()"
899+ >
900+ <textarea nz-input [nzAutosize]="{ minRows: 4, maxRows: 4 }" [(ngModel)]="inputValue" nzMentionTrigger></textarea>
901+ <ng-template #clearIconTemplate>
902+ <span class="custom-clear-icon">×</span>
903+ </ng-template>
904+ </nz-mention>
905+ `
906+ } )
907+ class NzTestClearMentionComponent {
908+ inputValue = '' ;
909+ suggestions = [ 'angular' , 'ant-design' , 'mention' ] ;
910+ allowClear = false ;
911+ useCustomClearIcon = false ;
912+
913+ @ViewChild ( NzMentionComponent , { static : false } ) mention ! : NzMentionComponent ;
914+
915+ onClear ( ) : void { }
916+ }
917+
809918class MockDirectionality {
810919 value = 'ltr' ;
811920 change = new Subject ( ) ;
0 commit comments