mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
110088: changes to keyboard navigation / selection
This commit is contained in:
@@ -26,7 +26,7 @@
|
|||||||
(keydown)="selectOnKeyDown($event, sdRef)">
|
(keydown)="selectOnKeyDown($event, sdRef)">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div ngbDropdownMenu
|
<div #dropdownMenu ngbDropdownMenu
|
||||||
class="dropdown-menu scrollable-dropdown-menu w-100"
|
class="dropdown-menu scrollable-dropdown-menu w-100"
|
||||||
aria-haspopup="true"
|
aria-haspopup="true"
|
||||||
aria-expanded="false"
|
aria-expanded="false"
|
||||||
@@ -42,7 +42,8 @@
|
|||||||
[scrollWindow]="false">
|
[scrollWindow]="false">
|
||||||
|
|
||||||
<button class="dropdown-item disabled" *ngIf="optionsList && optionsList.length == 0">{{'form.no-results' | translate}}</button>
|
<button class="dropdown-item disabled" *ngIf="optionsList && optionsList.length == 0">{{'form.no-results' | translate}}</button>
|
||||||
<button class="dropdown-item collection-item text-truncate" *ngFor="let listEntry of optionsList"
|
<button class="dropdown-item collection-item text-truncate" *ngFor="let listEntry of optionsList; let i = index"
|
||||||
|
[class.active]="i === selectedIndex"
|
||||||
(keydown.enter)="onSelect(listEntry); sdRef.close()" (mousedown)="onSelect(listEntry); sdRef.close()"
|
(keydown.enter)="onSelect(listEntry); sdRef.close()" (mousedown)="onSelect(listEntry); sdRef.close()"
|
||||||
title="{{ listEntry.display }}" role="option"
|
title="{{ listEntry.display }}" role="option"
|
||||||
[attr.id]="listEntry.display == (currentValue|async) ? ('combobox_' + id + '_selected') : null">
|
[attr.id]="listEntry.display == (currentValue|async) ? ('combobox_' + id + '_selected') : null">
|
||||||
|
@@ -1,4 +1,13 @@
|
|||||||
import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
|
import {
|
||||||
|
ChangeDetectorRef,
|
||||||
|
Component,
|
||||||
|
EventEmitter,
|
||||||
|
Input,
|
||||||
|
OnInit,
|
||||||
|
Output,
|
||||||
|
ViewChild,
|
||||||
|
ElementRef
|
||||||
|
} from '@angular/core';
|
||||||
import { UntypedFormGroup } from '@angular/forms';
|
import { UntypedFormGroup } from '@angular/forms';
|
||||||
|
|
||||||
import { Observable, of as observableOf } from 'rxjs';
|
import { Observable, of as observableOf } from 'rxjs';
|
||||||
@@ -28,6 +37,8 @@ import { FormFieldMetadataValueObject } from '../../../models/form-field-metadat
|
|||||||
templateUrl: './dynamic-scrollable-dropdown.component.html'
|
templateUrl: './dynamic-scrollable-dropdown.component.html'
|
||||||
})
|
})
|
||||||
export class DsDynamicScrollableDropdownComponent extends DsDynamicVocabularyComponent implements OnInit {
|
export class DsDynamicScrollableDropdownComponent extends DsDynamicVocabularyComponent implements OnInit {
|
||||||
|
@ViewChild('dropdownMenu', { read: ElementRef }) dropdownMenu: ElementRef;
|
||||||
|
|
||||||
@Input() bindId = true;
|
@Input() bindId = true;
|
||||||
@Input() group: UntypedFormGroup;
|
@Input() group: UntypedFormGroup;
|
||||||
@Input() model: DynamicScrollableDropdownModel;
|
@Input() model: DynamicScrollableDropdownModel;
|
||||||
@@ -41,6 +52,7 @@ export class DsDynamicScrollableDropdownComponent extends DsDynamicVocabularyCom
|
|||||||
public pageInfo: PageInfo;
|
public pageInfo: PageInfo;
|
||||||
public optionsList: any;
|
public optionsList: any;
|
||||||
public inputText: string = null;
|
public inputText: string = null;
|
||||||
|
public selectedIndex = 0;
|
||||||
public acceptableKeys = ['Space', 'NumpadMultiply', 'NumpadAdd', 'NumpadSubtract', 'NumpadDecimal', 'Semicolon', 'Equal', 'Comma', 'Minus', 'Period', 'Quote', 'Backquote'];
|
public acceptableKeys = ['Space', 'NumpadMultiply', 'NumpadAdd', 'NumpadSubtract', 'NumpadDecimal', 'Semicolon', 'Equal', 'Comma', 'Minus', 'Period', 'Quote', 'Backquote'];
|
||||||
|
|
||||||
constructor(protected vocabularyService: VocabularyService,
|
constructor(protected vocabularyService: VocabularyService,
|
||||||
@@ -73,6 +85,7 @@ export class DsDynamicScrollableDropdownComponent extends DsDynamicVocabularyCom
|
|||||||
list.pageInfo.totalElements,
|
list.pageInfo.totalElements,
|
||||||
list.pageInfo.totalPages
|
list.pageInfo.totalPages
|
||||||
);
|
);
|
||||||
|
this.selectedIndex = 0;
|
||||||
this.cdr.detectChanges();
|
this.cdr.detectChanges();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -96,6 +109,23 @@ export class DsDynamicScrollableDropdownComponent extends DsDynamicVocabularyCom
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
navigateDropdown(event: KeyboardEvent) {
|
||||||
|
if (event.key === 'ArrowDown') {
|
||||||
|
this.selectedIndex = Math.min(this.selectedIndex + 1, this.optionsList.length - 1);
|
||||||
|
} else if (event.key === 'ArrowUp') {
|
||||||
|
this.selectedIndex = Math.max(this.selectedIndex - 1, 0);
|
||||||
|
}
|
||||||
|
this.scrollToSelected();
|
||||||
|
}
|
||||||
|
|
||||||
|
scrollToSelected() {
|
||||||
|
const dropdownItems = this.dropdownMenu.nativeElement.querySelectorAll('.dropdown-item');
|
||||||
|
const selectedItem = dropdownItems[this.selectedIndex];
|
||||||
|
if (selectedItem) {
|
||||||
|
selectedItem.scrollIntoView({ block: 'nearest' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* KeyDown handler to allow toggling the dropdown via keyboard
|
* KeyDown handler to allow toggling the dropdown via keyboard
|
||||||
* @param event KeyboardEvent
|
* @param event KeyboardEvent
|
||||||
@@ -104,12 +134,19 @@ export class DsDynamicScrollableDropdownComponent extends DsDynamicVocabularyCom
|
|||||||
selectOnKeyDown(event: KeyboardEvent, sdRef: NgbDropdown) {
|
selectOnKeyDown(event: KeyboardEvent, sdRef: NgbDropdown) {
|
||||||
const keyName = event.key;
|
const keyName = event.key;
|
||||||
|
|
||||||
if (keyName === ' ' || keyName === 'Enter') {
|
if (keyName === 'Enter') {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
sdRef.toggle();
|
if (sdRef.isOpen()) {
|
||||||
|
this.onSelect(this.optionsList[this.selectedIndex]);
|
||||||
|
sdRef.close();
|
||||||
|
} else {
|
||||||
|
sdRef.open();
|
||||||
|
}
|
||||||
} else if (keyName === 'ArrowDown' || keyName === 'ArrowUp') {
|
} else if (keyName === 'ArrowDown' || keyName === 'ArrowUp') {
|
||||||
this.openDropdown(sdRef);
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
this.navigateDropdown(event);
|
||||||
} else if (keyName === 'Backspace') {
|
} else if (keyName === 'Backspace') {
|
||||||
this.removeKeyFromInput();
|
this.removeKeyFromInput();
|
||||||
} else if (this.isAcceptableKey(keyName)) {
|
} else if (this.isAcceptableKey(keyName)) {
|
||||||
|
Reference in New Issue
Block a user