Last active
April 9, 2018 19:13
-
-
Save AhsanAyaz/14f28545b5fe87d138671fe10a5e5457 to your computer and use it in GitHub Desktop.
AppComponent after adding (keyup) handler on the search input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
import { ListKeyManager } from '@angular/cdk/a11y'; | |
import { ListItemComponent } from './core/components/list-item/list-item.component'; | |
import { UP_ARROW, DOWN_ARROW, ENTER } from '@angular/cdk/keycodes'; | |
... | |
export class AppComponent implements OnInit { | |
... | |
keyboardEventsManager: ListKeyManager<any>; | |
searchQuery: string; | |
@ViewChildren(ListItemComponent) listItems: QueryList<ListItemComponent>; | |
constructor(private usersService: UsersService) { | |
} | |
... | |
/** | |
* @author Ahsan Ayaz | |
* @desc Triggered when a key is pressed while the input is focused | |
*/ | |
handleKeyUp(event: KeyboardEvent) { | |
event.stopImmediatePropagation(); | |
if (this.keyboardEventsManager) { | |
if (event.keyCode === DOWN_ARROW || event.keyCode === UP_ARROW) { | |
// passing the event to key manager so we get a change fired | |
this.keyboardEventsManager.onKeydown(event); | |
return false; | |
} else if (event.keyCode === ENTER) { | |
// when we hit enter, the keyboardManager should call the selectItem method of the `ListItemComponent` | |
this.keyboardEventsManager.activeItem.selectItem(); | |
return false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment