[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 ...
随机推荐
- 【Codeforces Round #455 (Div. 2) A】Generate Login
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举两个串的前缀长度就好. 组出来. 排序. 取字典序最小的那个. [代码] #include <bits/stdc++.h& ...
- Lightoj 1043 - Triangle Partitioning【二分】
题目链接:http://lightoj.com/volume_showproblem.php? problem=1043 题意:一个三角形ABC,DE//BC.已知三角形ADE和四边形BDEC的面积的 ...
- 逆波兰法(计算器)程序<无括号版>
涉及队列.栈的运用. Java中队列可以用: Queue<String> q = new LinkedList(); 来声明,其主要的方法有: poll(),peak(),offer(), ...
- 动态规划例子:Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- python2与python3的不同
1.源码: python2的源码混乱.冗余.重复,非常不规范 python3的源码经过整合,优美.清晰.简单 2.编码: python2的默认编码是ASCII码,不能识别中文,需要在行首添加# -*- ...
- php高并发秒杀解决方案
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/super_runman/article/details/53037151 在秒杀.抢火车票等地方,我 ...
- 算法中的优化问题(optimization problem)
和多数算法不同的是,有些问题的答案不只一个,而是需要在多个答案中,按照一定标准选出"最佳"答案,这类问题就统称为"优化问题"(optimization prob ...
- 【原创】面向对象版本地CPU资源占用监控脚本
前期准备: 1.python2.7环境 2.相关第三方库下载安装 脚本工作过程: 1.根据输入的进程名判断进程是否存在,如果不存在则进行等待,直到检测到进程PID,中途进程退出抛出异常,键入enter ...
- ACdream 1127 Base Station (离线查询+树状数组)
题目链接: http://acdream.info/problem?pid=1127 题目: 移动通信系统中,通信网的建立主要通过基站来完成. 基站可以分为主基站和子基站.子基站和各个移动用户进行连接 ...
- Android 用SQLite 使用 CursorLoader 中的数据填充列表视图
我做了简单的测试应用程序基于此示例.有一个按钮,插入到数据库和列表视图的数据.都是在 MainActivity 中.在原来的代码是restartLoader() 仅从调用 onResume() ,但它 ...