首页 > 代码库 > [Angular] Adding keyboard events to our control value accessor component
[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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。