[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]();
    }
  }
时间: 2024-10-12 01:22:47

[Angular] Adding keyboard events to our control value accessor component的相关文章

[Angular] Angular Global Keyboard Handling With EventManager

If we want to add global event handler, we can use 'EventManager' from '@angular/platform-broswer'. Now we have a modal component, we want to click 'Esc' key to close the modal. <au-modal class="auth-modal" *auModalOpenOnClick="[loginBut

[Angular] Router outlet events

For example, we have a component which just simply render router-outlet: import { Component } from '@angular/core'; @Component({ selector: 'mail-app', styleUrls: ['mail-app.component.scss'], template: ` <div class="mail"> <router-outlet

[Angular 2] Using events and refs

This lesson shows you how set listen for click events using the (click) syntax. It also covers getting values off of an input using the #ref syntax then passing that value into the onClick event handler.

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

//switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS, Validators} from '@angular/forms'; @Component({ selector: 'switch-control', templateUrl: './switch-control.componen

Document Object Model (DOM) Level 3 Events Specification

Document Object Model (DOM) Level 3 Events Specification W3C Working Draft 25 September 2014 This version: http://www.w3.org/TR/2014/WD-DOM-Level-3-Events-20140925/ Latest published version: http://www.w3.org/TR/DOM-Level-3-Events/ Latest editor's dr

All About Angular 2.0

angular All About Angular 2.0 Posted by Rob Eisenberg on  November 6th, 2014. Have questions about the strategy for Angular 2.0? This is the place. In the following article I'll explain the major feature areas of Angular 2.0 along with the motivation

Adding a Timepicker to jQuery UI Datepicker

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="

Mousetrap - a simple library for handling keyboard shortcuts in Javascript

Mousetrap is a simple library for handling keyboard shortcuts in Javascript. It is around 2kb minified and gzipped and 4.5kb minified, has no external dependencies, and has been tested in the following browsers: Internet Explorer 6+ Safari Firefox Ch

listview control messages

CTreeCtrl是在OnNMCustomdraw中自绘的,不要设定其他属性 CListCtrl是在DrawItem中自绘的,要设定属性LVS_OWNERDRAWFIXED|LVS_REPORT CButton是在DrawItem中自绘的,要设定属性BS_OWNERDRAW CMenu是在DrawItem中自绘的,要把每一项都设定属性MF_OWNERDRAW,用到递归方法 单选按钮是基于CButton在DrawItem中自绘的,要重写Create设定属性为 dwStyle &= ~(0xF);