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的更多相关文章

  1. 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 ...

  2. [Angular] Implement a custom form component by using control value accessor

    We have a form component: <label> <h3>Type</h3> <workout-type formControlName=& ...

  3. [Angular] Angular Global Keyboard Handling With EventManager

    If we want to add global event handler, we can use 'EventManager' from '@angular/platform-broswer'. ...

  4. [Angular] Router outlet events

    For example, we have a component which just simply render router-outlet: import { Component } from ' ...

  5. [Angular 2] Using events and refs

    This lesson shows you how set listen for click events using the (click) syntax. It also covers getti ...

  6. [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, ...

  7. [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 ...

  8. [Angular2 Form] Create custom form component using Control Value Accessor

    //switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, ...

  9. Javascript Madness: Mouse Events

    http://unixpapa.com/js/mouse.html Javascript Madness: Mouse Events Jan WolterAug 12, 2011 Note: I ha ...

随机推荐

  1. JS怎么判断数组类型?

    1.判断对象的constructor是否指向Array,接着判断特殊的属性length,splice等.[应用的是constructor的定义:返回对象所对应的构造函数.] eg: [].constr ...

  2. vue.js代码开发最常见的功能集合

    1:点击新增按钮跳出新页面 <span class="inquire" @click="addNew">新增</span> 在方法中,添 ...

  3. MySQL集群搭建详解

    概述 MySQL Cluster 是MySQL 适合于分布式计算环境的高实用.可拓展.高性能.高冗余版本,其研发设计的初衷就是要满足许多行业里的最严酷应用要求,这些应用中经常要求数据库运行的可靠性要达 ...

  4. [React] Render Elements Outside the Current React Tree using Portals in React 16

    By default the React Component Tree directly maps to the DOM Tree. In some cases when you have UI el ...

  5. [Vue + TS] Use Properties in Vue Components Using @Prop Decorator with TypeScript

    With properties we can follow a one-way parent→child flow communication between components. This les ...

  6. 文件上传流式处理commons-fileupload

    1. 从请求中获取MultipartFile @RequestMapping(value="/upload", method=RequestMethod.POST) public ...

  7. jdk目录详解及其使用方法

    jdk目录详解 jdk目录详解 JDK(Java Development Kit,Java开发包,Java开发工具)是一个写Java的applet和应用程序的程序开发环境.它由一个处于操作系统层之上的 ...

  8. COGS——C 908. 校园网 || 洛谷——P 2746 [USACO5.3]校园网Network of Schools

    http://www.cogs.pro/cogs/problem/problem.php?pid=908   ||  https://www.luogu.org/problem/show?pid=27 ...

  9. android开发设计辅助工具整理

    1.Button设计工具button设计

  10. 如何把传统写法改成框架形式 es6

    每天思考的问题: 1.什么是组件 2.什么是插件 3.如何把传统写法改成框架形式 4.前端为什么要使用框架,使用框架的好处是什么? Image.png http://www.zhihu.com/que ...