close autosuggestion with escape key

This commit is contained in:
Nicolas Constant 2019-07-24 18:15:56 -04:00
parent fdcdf9d813
commit 1eaafb12a9
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
1 changed files with 8 additions and 3 deletions

View File

@ -2,7 +2,7 @@ import { Component, OnInit, OnDestroy, Input, Output, EventEmitter, ElementRef,
import { HttpErrorResponse } from '@angular/common/http'; import { HttpErrorResponse } from '@angular/common/http';
import { Store } from '@ngxs/store'; import { Store } from '@ngxs/store';
import { Subscription, Observable } from 'rxjs'; import { Subscription, Observable } from 'rxjs';
import { UP_ARROW, DOWN_ARROW, ENTER } from '@angular/cdk/keycodes'; import { UP_ARROW, DOWN_ARROW, ENTER, ESCAPE } from '@angular/cdk/keycodes';
import { MastodonService, VisibilityEnum } from '../../services/mastodon.service'; import { MastodonService, VisibilityEnum } from '../../services/mastodon.service';
import { Status, Attachment } from '../../services/models/mastodon.interfaces'; import { Status, Attachment } from '../../services/models/mastodon.interfaces';
@ -484,12 +484,13 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
handleKeyDown(event: KeyboardEvent): boolean { handleKeyDown(event: KeyboardEvent): boolean {
if (this.hasSuggestions) { if (this.hasSuggestions) {
if (event.keyCode === DOWN_ARROW || event.keyCode === UP_ARROW || event.keyCode === ENTER) { let keycode = event.keyCode;
if (keycode === DOWN_ARROW || keycode === UP_ARROW || keycode === ENTER || keycode === ESCAPE) {
event.stopImmediatePropagation(); event.stopImmediatePropagation();
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
switch (event.keyCode) { switch (keycode) {
case DOWN_ARROW: case DOWN_ARROW:
this.autoSuggestUserActionsStream.next(AutosuggestUserActionEnum.MoveDown); this.autoSuggestUserActionsStream.next(AutosuggestUserActionEnum.MoveDown);
break; break;
@ -499,6 +500,10 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
case ENTER: case ENTER:
this.autoSuggestUserActionsStream.next(AutosuggestUserActionEnum.Validate); this.autoSuggestUserActionsStream.next(AutosuggestUserActionEnum.Validate);
break; break;
case ESCAPE:
this.autosuggestData = null;
this.hasSuggestions = false;
break;
} }
return false; return false;