Skip to content

Commit

Permalink
🧹 Tidy up listbox bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmcnamara-eng committed Nov 8, 2021
1 parent d14513a commit 6f34df6
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions addon/components/listbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ const ACTIVATE_NONE = 0;
const ACTIVATE_FIRST = 1;
const ACTIVATE_LAST = 2;

const PREVENTED_KEYDOWN_EVENTS = new Set([
'ArrowUp',
'ArrowDown',
'ArrowLeft',
'ArrowRight',
'PageUp',
'PageDown',
'Home',
'End',
]);

export default class ListboxComponent extends Component {
@tracked activeOptionIndex;
activateBehaviour = ACTIVATE_NONE;
Expand Down Expand Up @@ -76,16 +87,7 @@ export default class ListboxComponent extends Component {

@action
handleKeyDown(event) {
if (
[
'ArrowUp',
'ArrowDown',
'ArrowLeft',
'ArrowRight',
'Home',
'End',
].indexOf(event.key) > -1
) {
if (PREVENTED_KEYDOWN_EVENTS.has(event.key)) {
event.preventDefault();
}
}
Expand Down Expand Up @@ -247,6 +249,11 @@ export default class ListboxComponent extends Component {
}

scrollIntoView(optionElement) {
// Cannot use optionElement.scrollIntoView() here because that function
// also scrolls the *window* by some amount. Here, we don't want to
// jerk the window, we just want to make the the option element visible
// inside its container.

optionElement.parentElement.scroll(
0,
optionElement.offsetTop - optionElement.parentElement.offsetTop
Expand Down

0 comments on commit 6f34df6

Please sign in to comment.