Skip to content

Commit

Permalink
fix(dropdown): prevent selecting disabled option by keyboard (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
vt-allianz authored and GitHub Enterprise committed Oct 14, 2022
1 parent 6c1a884 commit f17389e
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions projects/ng-aquila/src/dropdown/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,16 +946,30 @@ export class NxDropdownComponent implements NxDropdownControl, ControlValueAcces

private setNextItemActive() {
const options = this._isLazy ? this.options : this.dropdownItems.toArray();
const next = Math.min(options.length - 1, options.indexOf(this._selectionModel.selected[0] as NxDropdownItemComponent) + 1);
this._selectionModel.select(options[next]);
this._propagateChanges();
let curIndex = options.indexOf(this._selectionModel.selected[0] as NxDropdownItemComponent);
for (curIndex++; curIndex < options.length; curIndex++) {
if (this._isSelectable(options[curIndex] as NxDropdownItemComponent, this._isLazy)) {
this._selectionModel.select(options[curIndex]);
this._propagateChanges();
return;
}
}
}

private setPreviousItemActive() {
const options = this._isLazy ? this.options : this.dropdownItems.toArray();
const prev = Math.max(0, options.indexOf(this._selectionModel.selected[0] as NxDropdownItemComponent) - 1);
this._selectionModel.select(options[prev]);
this._propagateChanges();
let curIndex = options.indexOf(this._selectionModel.selected[0] as NxDropdownItemComponent);
for (curIndex--; curIndex >= 0; curIndex--) {
if (this._isSelectable(options[curIndex] as NxDropdownItemComponent, this._isLazy)) {
this._selectionModel.select(options[curIndex]);
this._propagateChanges();
return;
}
}
}

private _isSelectable(option: NxDropdownItemComponent, isLazy?: boolean) {
return isLazy || (option && !option?.disabled && !option.selected);
}

private _handleClosedKeydown(event: KeyboardEvent) {
Expand Down

0 comments on commit f17389e

Please sign in to comment.