Skip to content

Commit 64e8bd2

Browse files
committed
add keyboard handling
1 parent 682caf7 commit 64e8bd2

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

apps/daffio/src/app/docs/search/components/search-modal/search-modal.component.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ import { DaffioDocsSearchFooterComponent } from '../search-footer/search-footer.
3838
templateUrl: './search-modal.component.html',
3939
styleUrl: './search-modal.component.scss',
4040
changeDetection: ChangeDetectionStrategy.OnPush,
41+
host: {
42+
'(keydown)': 'onKeydown($event)',
43+
},
4144
imports: [
4245
DaffioDocsSearchFieldComponent,
4346
DaffioDocsSearchResultItemDirective,
@@ -78,5 +81,26 @@ export class DaffioDocsSearchModalComponent implements AfterViewInit, OnInit {
7881

7982
ngAfterViewInit() {
8083
this.keyManager = new ActiveDescendantKeyManager(this.items).withWrap();
84+
85+
this.items.changes.subscribe(() => {
86+
if (this.items.length > 0) {
87+
this.keyManager.setFirstItemActive();
88+
}
89+
});
90+
91+
if (this.items.length > 0) {
92+
this.keyManager.setFirstItemActive();
93+
}
94+
}
95+
96+
onKeydown(event: KeyboardEvent) {
97+
if (event.key === 'Enter') {
98+
const activeItem = this.keyManager.activeItem;
99+
if (activeItem) {
100+
activeItem.click();
101+
}
102+
} else {
103+
this.keyManager.onKeydown(event);
104+
}
81105
}
82106
}

apps/daffio/src/app/docs/search/directives/search-result-item/search-result-item.directive.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Highlightable } from '@angular/cdk/a11y';
22
import {
33
Directive,
4+
ElementRef,
45
HostBinding,
56
} from '@angular/core';
67

@@ -17,11 +18,20 @@ export class DaffioDocsSearchResultItemDirective implements Highlightable {
1718
return this._isActive;
1819
};
1920

21+
constructor(private elementRef: ElementRef<HTMLElement>) {}
22+
2023
setActiveStyles() {
2124
this._isActive = true;
2225
};
2326

2427
setInactiveStyles() {
2528
this._isActive = false;
2629
}
30+
31+
click() {
32+
const link = this.elementRef.nativeElement.querySelector('a');
33+
if (link) {
34+
link.click();
35+
}
36+
}
2737
}

0 commit comments

Comments
 (0)