[Angular] Adding keyboard events to our control value accessor component
One of the most important thing when building custom form component is adding accessbility support.
The component should be focusable. we can achieve this by adding 'tabindex="0"':
<div
tabindex="0"
>
Add some css class for foucs:
<div
[class.focus]="focused"
tabindex="0"
(focus)="onFocus($event)"
(blur)="onBlur($event)"
>
.stock-counter {
& .focus {
box-shadow: 0 1px 1px rgba(0, 0, 0, .6);
} ...
}
onFocus() {
this.focused = true;
this.onTouch();
} onBlur() {
this.focused = false;
this.onTouch();
}
Handle keydwon event with code:
<div
[class.focus]="focused"
tabindex="0"
(keydown)="onKeyDown($event)"
(focus)="onFocus($event)"
(blur)="onBlur($event)"
>
onKeyDown(event: KeyboardEvent) { const handler = {
ArrowDown: () => this.decrement(),
ArrowUp: () => this.increment()
}; if(handler[event.code]) {
event.preventDefault();
event.stopPropagation();
handler[event.code]();
}
}
[Angular] Adding keyboard events to our control value accessor component的更多相关文章
- jQuery.Hotkeys - lets you watch for keyboard events anywhere in your code supporting almost any key combination
About jQuery Hotkeys is a plug-in that lets you easily add and remove handlers for keyboard events a ...
- [Angular] Implement a custom form component by using control value accessor
We have a form component: <label> <h3>Type</h3> <workout-type formControlName=& ...
- [Angular] Angular Global Keyboard Handling With EventManager
If we want to add global event handler, we can use 'EventManager' from '@angular/platform-broswer'. ...
- [Angular] Router outlet events
For example, we have a component which just simply render router-outlet: import { Component } from ' ...
- [Angular 2] Using events and refs
This lesson shows you how set listen for click events using the (click) syntax. It also covers getti ...
- [AngularFire] Angular File Uploads to Firebase Storage with Angular control value accessor
The upload class will be used in the service layer. Notice it has a constructor for file attribute, ...
- [Angular] Progress HTTP Events with 'HttpRequest'
New use case that is supported by the HTTP client is Progress events. To receive these events, we cr ...
- [Angular2 Form] Create custom form component using Control Value Accessor
//switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, ...
- Javascript Madness: Mouse Events
http://unixpapa.com/js/mouse.html Javascript Madness: Mouse Events Jan WolterAug 12, 2011 Note: I ha ...
随机推荐
- Java开源电商项目比較
这里比較的都是国外的开源项目,备选项目有: Smilehouse Workspace.Pulse.Shopizer.ofbiz.bigfish.broadleaf 1.Smilehouse Works ...
- Delphi部份函数,命令,属性中文说明
Abort 函数 引起放弃的意外处理 Abs 函数 绝对值函数 AddExitProc 函数 将一过程添加到运行时库的结束过程表中 Addr 函数 返回指定对象的地址 AdjustLineBreaks ...
- ImportError: No module named tornado.ioloop 记录过程
ImportError: No module named tornado.ioloop 记录过程 安装 pycurl pip install pycurl 报错 'curl-config' no ...
- 洛谷 P1599 结算日
洛谷 P1599 结算日 题目描述 “不放债不借债”,贝西多么希望自己可以遵循这个忠告.她已经和她的N(1 <= N <= 100,000)个朋友有了债务关系,或者借债了,或者放债了.她的 ...
- 【例题 7-9 UVA-1601】The Morning after Halloween
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 对于没有出现的,当成0节点就好. 所以总是认为有3个人需要走到各自的终点. 将平面图转成点边图.这样比较好枚举. (二维变成一维,模 ...
- Vue里父子组间的通讯
父组件代码 <template> <div> <child @child-say="listenToBoy" :mes=count></c ...
- Day2:模块初识
一.模块(库) 别人写好的一堆功能,封装起来,你直接导入就可以用,就不用自己再写一次,使用import方法 二.分类:标准库与第三方库 标准库:免安装,导入即可使用,最常用的一些功能 第三方库:需要下 ...
- [Angular] The Select DOM Event and Enabling Text Copy
When we "Tab" into a input field, we want to select all the content, if we start typing, i ...
- Qt开发程序在Windows 10应用须要管理员执行的解决思路
Qt开发程序在Windows 10应用须要管理员执行的解决思路 过了非常长的时间没有公布博客了.可是我依旧努力地开发Qt程序.眼下呢.我发现开发Qt程序在Windows 10上有一个怪现象--有些程序 ...
- js闭包(函数内部嵌套一个匿名函数:这个匿名函数可将所在函数的局部变量常驻内存)
js闭包(函数内部嵌套一个匿名函数:这个匿名函数可将所在函数的局部变量常驻内存) 一.总结 1.闭包:就是在一个函数内部嵌套一个匿名函数,这个匿名函数可以访问这个函数的变量. 二.要点 闭包 闭包的相 ...