简体   繁体   English

keyup.enter/space 事件在“@fortawesome/angular-fontawesome”图标上多次触发 / Angular 6 项目

[英]keyup.enter/space event triggers multiple times on "@fortawesome/angular-fontawesome" icon / Angular 6 project

In my angular 6 application, there is fort awesome icon as below:在我的 angular 6 应用程序中,有一个很棒的图标,如下所示:

<div>
   <fa-icon [icon]="trashIcon" class="custom-trash-icon" tabindex="0"
     (click)="onDeleteIconClick()"  (keyup.enter)="onDeleteIconClick()"  
     (keyup.Space)="onDeleteIconClick">
  </fa-icon>
</div>

in.ts file, it opens a NgbModal modal window for deletion confirmation, and if a user presses 'yes' or 'no' button, it is aimed to delete the selected resource asynchronously, However since the focus from the trash icon is not moving it keeps popping up the modal window again and again on ' enter ' or ' space ' keys, however, it is working well on Click event through the mouse. in.ts 文件,它打开一个NgbModal模态 window 用于删除确认,如果用户按下“是”或“否”按钮,它的目的是异步删除所选资源,但是由于trash图标的焦点没有移动它不断在“ enter ”或“ space ”键上一次又一次地弹出模态 window,但是,它在通过鼠标的Click事件上运行良好。

.ts file code .ts文件代码

public onDeleteIconClick() {
        
        const modalRef = this._modalService.open(DeleteComponent, {
            backdrop: "static",
            windowClass: "custom-modal",
            keyboard: true,
            centered: true,
        });
        modalRef.componentInstance.confirmationText = `Do you want to delete the resource?`;
        modalRef.result.then(
            (result) => {
                if (result && result.toLowerCase() === "yes")
                 {
                         this.deleteResource.emit();
                   
                }
            },
            (reason) => { }
        );

    }

I believe that is because the focus is not moving from the thrash icon, did any encounter a similar issue?我相信那是因为焦点没有从 thrash 图标移开,有没有人遇到过类似的问题?

Please note that keyup.enter , keyup.enter events are targeted for accessibility purposes.请注意keyup.enterkeyup.enter事件是针对可访问性目的的。

Edit ***************编辑***************

Here is the stackblitz , wherein once u click on the trash icon and when it opens up the modal window, click on cancel using enter key of the keyboard, please notice that the modal window will keep appearing.这是stackblitz ,其中一旦你点击垃圾桶图标,当它打开模式 window 时,使用键盘的回车键点击取消,请注意模式 window 将继续出现。

The best solution (as suggested in Andy's comment) is to use a <button> and remove keybord bindings at all:最好的解决方案(如 Andy 的评论中所建议的)是使用<button>并完全删除键盘绑定:

<button type="button" (click)="onDeleteIconClick()">
   <fa-icon [icon]="trashIcon" class="custom-trash-icon" tabindex="0">
   </fa-icon>
</button>

If for some reason you want to stick with your current template, you could pass the event object to onDeleteIconClick() and remove the focus from within the function:如果出于某种原因您想坚持使用当前模板,您可以将事件 object 传递给onDeleteIconClick()并从 function 中移除焦点:

// HTML
<fa-icon [icon]="trashIcon" class="custom-trash-icon" tabindex="0"
     (click)="onDeleteIconClick($event)"  
     (keyup.enter)="onDeleteIconClick($event)"  
     (keyup.Space)="onDeleteIconClick($event)">
</fa-icon>

// TS
onDeleteIconClick(ev) {
   ev.currentTarget.blur();      
   // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM