@@ -9,12 +9,14 @@ import {
99 Input ,
1010 OnDestroy ,
1111 OnInit ,
12+ AfterViewInit ,
1213 ViewChild ,
1314} from '@angular/core' ;
1415import { ControlValueAccessor , FormsModule , NG_VALUE_ACCESSOR } from '@angular/forms' ;
1516import { IconMagnifierComponent } from '../icons/icon-magnifier/icon-magnifier.component' ;
1617import { debounceTime , noop , Subject } from 'rxjs' ;
1718import { NgClass } from '@angular/common' ;
19+ import { FocusOnKeyUtil } from '../utils/focus-on-key.util' ;
1820
1921export const SEARCH_CONTROL_VALUE_ACCESSOR = {
2022 provide : NG_VALUE_ACCESSOR ,
@@ -31,9 +33,10 @@ export const SEARCH_CONTROL_VALUE_ACCESSOR = {
3133 changeDetection : ChangeDetectionStrategy . OnPush ,
3234 providers : [ SEARCH_CONTROL_VALUE_ACCESSOR ] ,
3335} )
34- export class SearchComponent implements OnInit , OnDestroy , ControlValueAccessor {
36+ export class SearchComponent implements OnInit , AfterViewInit , OnDestroy , ControlValueAccessor {
3537 @Input ( ) placeholder : string = 'Search...' ;
3638 @Input ( ) focusKey : string = '/' ;
39+ @Input ( { transform : booleanAttribute } ) forceFocus : boolean = false ;
3740 @Input ( { transform : booleanAttribute } ) focusKeyEnabled : boolean = true ;
3841 @Input ( { transform : booleanAttribute } ) slim : boolean = false ;
3942 @ViewChild ( 'input' ) _inputElement ! : ElementRef < HTMLInputElement > ;
@@ -43,6 +46,12 @@ export class SearchComponent implements OnInit, OnDestroy, ControlValueAccessor
4346
4447 private _value : string = '' ;
4548 private _disabled : boolean = false ;
49+ private focusKeyUtil = new FocusOnKeyUtil ( {
50+ key : '/' ,
51+ ctrl : false ,
52+ shift : false ,
53+ force : false ,
54+ } ) ;
4655
4756 @Input ( )
4857 get value ( ) : string {
@@ -70,17 +79,24 @@ export class SearchComponent implements OnInit, OnDestroy, ControlValueAccessor
7079 this . value = value ;
7180 this . _onChange ( value ) ;
7281 } ) ;
82+ this . focusKeyUtil . updateConfig ( {
83+ key : this . focusKey ,
84+ force : this . forceFocus ,
85+ } ) ;
86+ }
7387
88+ ngAfterViewInit ( ) : void {
7489 if ( this . focusKeyEnabled ) {
75- window . addEventListener ( 'keyup' , this . _onKeyEvent . bind ( this ) , true ) ;
90+ this . focusKeyUtil . setFocusElement ( this . _inputElement . nativeElement ) ;
91+ this . focusKeyUtil . enable ( ) ;
7692 }
7793 }
7894
7995 ngOnDestroy ( ) : void {
8096 this . searchSubject . complete ( ) ;
8197
8298 if ( this . focusKeyEnabled ) {
83- window . removeEventListener ( 'keyup' , this . _onKeyEvent , true ) ;
99+ this . focusKeyUtil ?. disable ( ) ;
84100 }
85101 }
86102
@@ -114,11 +130,4 @@ export class SearchComponent implements OnInit, OnDestroy, ControlValueAccessor
114130 protected _onInteractionEvent ( event : Event ) : void {
115131 event . stopPropagation ( ) ;
116132 }
117-
118- protected _onKeyEvent ( event : KeyboardEvent ) : void {
119- if ( event . key === this . focusKey ) {
120- this . _inputElement . nativeElement . focus ( ) ;
121- event . preventDefault ( ) ;
122- }
123- }
124133}
0 commit comments