-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(dropdown): open dropdown on ArrowDown
& ArrowUp
keys
#10264
Changes from 6 commits
d3370e1
8940e89
95a46f1
520fa62
6150a6d
a45b1dc
a5c193c
de0c3eb
6b08f1d
59836d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -238,7 +238,7 @@ export class Dropdown | |
<div | ||
class="calcite-trigger-container" | ||
id={`${guid}-menubutton`} | ||
onClick={this.openCalciteDropdown} | ||
onClick={this.clickHandler} | ||
onKeyDown={this.keyDownHandler} | ||
ref={this.setReferenceEl} | ||
> | ||
|
@@ -354,7 +354,7 @@ export class Dropdown | |
return; | ||
} | ||
|
||
this.openCalciteDropdown(); | ||
this.clickHandler(); | ||
} | ||
|
||
@Listen("pointerleave") | ||
|
@@ -445,6 +445,8 @@ export class Dropdown | |
|
||
guid = `calcite-dropdown-${guid()}`; | ||
|
||
private focusLastDropdownItem = false; | ||
|
||
//-------------------------------------------------------------------------- | ||
// | ||
// Private Methods | ||
|
@@ -541,6 +543,7 @@ export class Dropdown | |
|
||
onOpen(): void { | ||
this.calciteDropdownOpen.emit(); | ||
this.focusOnFirstActiveOrDefaultItem(); | ||
} | ||
|
||
onBeforeClose(): void { | ||
|
@@ -573,24 +576,24 @@ export class Dropdown | |
return; | ||
} | ||
|
||
if (this.open) { | ||
if (key === "Escape") { | ||
this.closeCalciteDropdown(); | ||
event.preventDefault(); | ||
return; | ||
} else if (event.shiftKey && key === "Tab") { | ||
this.closeCalciteDropdown(); | ||
event.preventDefault(); | ||
return; | ||
} | ||
if (key === "Escape") { | ||
this.closeCalciteDropdown(); | ||
event.preventDefault(); | ||
return; | ||
} | ||
|
||
if (isActivationKey(key)) { | ||
this.openCalciteDropdown(); | ||
event.preventDefault(); | ||
} else if (key === "Escape") { | ||
if (this.open && event.shiftKey && key === "Tab") { | ||
this.closeCalciteDropdown(); | ||
event.preventDefault(); | ||
return; | ||
} | ||
|
||
if (isActivationKey(key)) { | ||
this.clickHandler(); | ||
event.preventDefault(); | ||
} else if (key === "ArrowDown" || key === "ArrowUp") { | ||
this.focusLastDropdownItem = key === "ArrowUp"; | ||
this.open = true; | ||
} | ||
}; | ||
|
||
|
@@ -634,30 +637,23 @@ export class Dropdown | |
} | ||
} | ||
|
||
private focusOnFirstActiveOrFirstItem = (): void => { | ||
this.getFocusableElement( | ||
this.getTraversableItems().find((item) => item.selected) || this.items[0], | ||
); | ||
}; | ||
private focusOnFirstActiveOrDefaultItem = (): void => { | ||
const selectedItem = this.getTraversableItems().find((item) => item.selected); | ||
const target: HTMLCalciteDropdownItemElement = | ||
selectedItem || | ||
(this.focusLastDropdownItem ? this.items[this.items.length - 1] : this.items[0]); | ||
|
||
private getFocusableElement(item: HTMLCalciteDropdownItemElement): void { | ||
if (!item) { | ||
this.focusLastDropdownItem = false; | ||
|
||
if (!target) { | ||
return; | ||
} | ||
|
||
focusElement(item); | ||
} | ||
|
||
private toggleOpenEnd = (): void => { | ||
this.focusOnFirstActiveOrFirstItem(); | ||
this.el.removeEventListener("calciteDropdownOpen", this.toggleOpenEnd); | ||
focusElement(target); | ||
}; | ||
|
||
private openCalciteDropdown = () => { | ||
private clickHandler = (): void => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we keep this dedicated click handler and restore the previous There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think |
||
this.open = !this.open; | ||
if (this.open) { | ||
this.el.addEventListener("calciteDropdownOpen", this.toggleOpenEnd); | ||
} | ||
}; | ||
|
||
private updateTabIndexOfItems(target: HTMLCalciteDropdownItemElement): void { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should cancel the event here too since it's handled.