[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 ...
随机推荐
- 解决sublime text3配置Python3编译环境:运行代码时提示“NO Build System”
只需要在路径中把单杠换成双杠,重启sublime即可.
- STM32 之ADC单次转换模式和连续转换模式
一.背景 在STM32中的AD的单通道采样中可以设置成单次转换模式和连续转换模式,如何理解这两个转换模式的区别,通过程序又是怎样实现的? 二.正文 首先理解单次转换模式,即ADC进行单次转换(单样本) ...
- http的原理
工作原理:客户机与服务器建立连接之后,发送一个请求给服务器,请求格式为统一资源标识符.协议版本号.(请求行.请求头.请求体),服务器接收请求后给予相应,包括相应行,响应头,响应体. ...
- amazeui学习笔记二(进阶开发5)--Web 组件开发规范Rules
amazeui学习笔记二(进阶开发5)--Web 组件开发规范Rules 一.总结 1.见名知意:见那些class名字知意,见函数名知意,见文件名知意 例如(HISTORY.md Web 组件更新历史 ...
- ios系统和某些移动端background-attachment:fixed不兼容性
固定背景不动:background-attachment:fixed; ios系统和某些移动端background-attachment:fixed不兼容性,没有任何效果,但可以hack一下就可以了, ...
- iOS开发RunLoop学习:一:RunLoop简单介绍
一:RunLoop的简单介绍 #import "ViewController.h" @interface ViewController () @end @implementatio ...
- Java核心技术 卷Ⅰ 基础知识(5)
第11章 异常.断言.日志和调试 处理错误 异常分类 声明已检查异常 如何抛出异常 创建异常类 捕获异常 捕获多个异常 再次抛出异常与异常链 finally子句 带资源的try语句 分析堆栈跟踪元素 ...
- oracle函数大全-字符串处理函数
字符函数——返回字符值 这些函数全都接收的是字符族类型的参数(CHR 除外)并且返回字符值.除了特别说明的之外,这些函数大部分返回VARCHAR2类型的数值.字符函数的返回类型所受的限制和基本数据库类 ...
- clear属性来取消浮动
在非IE浏览器(如Firefox)下,当容器的高度为auto,且容器的内容中有浮动(float为left或right)的元素,在这种情况下,容器的高度不能自动伸长以适应内容的高度,使得内容溢出到容器外 ...
- Openstack nova(二)——架构(一)
架构源自需求 需求分析 软件架构大部分都来自于需求.能够说.有什么样的需求,就会有什么样的架构, 尽管不同一时候期,不同的人来实现,可能不全然一样.可是整体来说, 架构不会相差太远. 如今假设假设须要 ...