Skip to content
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: disabled menu items should be focusable #4154

Merged
merged 3 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ <h4>Default</h4>
<fast-menu>
<fast-menu-item>Menu item 1</fast-menu-item>
<fast-menu-item>Menu item 2</fast-menu-item>
<fast-menu-item>Menu item 3</fast-menu-item>
<fast-menu-item disabled="true">Menu item 3</fast-menu-item>
<fast-menu-item>Menu item 4</fast-menu-item>
</fast-menu>

<h4>With seperator</h4>
Expand Down
5 changes: 2 additions & 3 deletions packages/web-components/fast-foundation/src/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe("Menu", () => {
it("should set tabindex of the first focusable menu item to 0", async () => {
scomea marked this conversation as resolved.
Show resolved Hide resolved
const { element, connect, disconnect } = await fixture(html<FASTMenu>`
<fast-menu>
<div>I put a div in my menu</div>
<fast-menu-item disabled id="id1">Foo</fast-menu-item>
<fast-menu-item id="id2">Bar</fast-menu-item>
<fast-menu-item>Baz</fast-menu-item>
Expand All @@ -39,9 +40,7 @@ describe("Menu", () => {

await connect();

expect(
element.querySelectorAll("fast-menu-item")[1].getAttribute("tabindex")
).to.equal("0");
expect(document.getElementById("id1")?.getAttribute("tabindex")).to.equal("0");

await disconnect();
});
Expand Down
14 changes: 1 addition & 13 deletions packages/web-components/fast-foundation/src/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,29 +163,17 @@ export class Menu extends FASTElement {
);
};

/**
* check if the item is disabled
*/
private isDisabledElement = (el: Element): el is HTMLElement => {
return this.isMenuItemElement(el) && el.getAttribute("aria-disabled") === "true";
};

/**
* check if the item is focusable
*/
private isFocusableElement = (el: Element): el is HTMLElement => {
return this.isMenuItemElement(el) && !this.isDisabledElement(el);
return this.isMenuItemElement(el);
};

private handleMenuItemFocus = (e: KeyboardEvent): void => {
const target = e.currentTarget as Element;
const focusIndex: number = this.menuItems.indexOf(target);

if (this.isDisabledElement(target)) {
target.blur();
return;
}

if (focusIndex !== this.focusIndex && focusIndex !== -1) {
this.setFocus(focusIndex, focusIndex > this.focusIndex ? 1 : -1);
}
Expand Down