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

Prevent keyboard navigation to hidden tab #6789

Merged
merged 6 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Prevent keyboard navigation to hidden tab",
"packageName": "@microsoft/fast-foundation",
"email": "7282195+m-akinc@users.noreply.github.com",
"dependentChangeType": "prerelease"
}
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,70 @@ test.describe("Tabs", () => {

await expect(element).toHaveJSProperty("activeid", secondTabId);
});

test("should not allow selecting hidden tab using arrow keys", async () => {
test.slow();

await root.evaluate(node => {
node.innerHTML = /* html */ `
<fast-tabs>
<fast-tab>Tab one</fast-tab>
<fast-tab hidden>Tab two</fast-tab>
<fast-tab>Tab three</fast-tab>
<fast-tab-panel>Tab panel one</fast-tab-panel>
<fast-tab-panel>Tab panel two</fast-tab-panel>
<fast-tab-panel>Tab panel three</fast-tab-panel>
</fast-tabs>
`;
});

const firstTab = tabs.nth(0);

const thirdTab = tabs.nth(2);

const firstTabId = (await firstTab.getAttribute("id")) ?? "";

const thirdTabId = (await thirdTab.getAttribute("id")) ?? "";

await element.evaluate((node: FASTTabs, firstTabId) => {
node.activeid = firstTabId;
}, firstTabId);

await firstTab.press("ArrowRight");

await expect(element).toHaveJSProperty("activeid", thirdTabId);
});

test("should not allow selecting hidden tab by pressing End", async () => {
test.slow();

await root.evaluate(node => {
node.innerHTML = /* html */ `
<fast-tabs>
<fast-tab>Tab one</fast-tab>
<fast-tab>Tab two</fast-tab>
<fast-tab hidden>Tab three</fast-tab>
<fast-tab-panel>Tab panel one</fast-tab-panel>
<fast-tab-panel>Tab panel two</fast-tab-panel>
<fast-tab-panel>Tab panel three</fast-tab-panel>
</fast-tabs>
`;
});

const firstTab = tabs.nth(0);

const secondTab = tabs.nth(1);

const firstTabId = (await firstTab.getAttribute("id")) ?? "";

const secondTabId = (await secondTab.getAttribute("id")) ?? "";

await element.evaluate((node: FASTTabs, firstTabId) => {
node.activeid = firstTabId;
}, firstTabId);

await firstTab.press("End");

await expect(element).toHaveJSProperty("activeid", secondTabId);
});
});
8 changes: 6 additions & 2 deletions packages/web-components/fast-foundation/src/tabs/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,12 @@ export class FASTTabs extends FASTElement {
return el.getAttribute("aria-disabled") === "true";
};

private isHiddenElement = (el: Element): el is HTMLElement => {
return el.hasAttribute("hidden");
};

private isFocusableElement = (el: Element): el is HTMLElement => {
return !this.isDisabledElement(el);
return !this.isDisabledElement(el) && !this.isHiddenElement(el);
};

private getActiveIndex(): number {
Expand Down Expand Up @@ -272,7 +276,7 @@ export class FASTTabs extends FASTElement {
* This method allows the active index to be adjusted by numerical increments
*/
public adjust(adjustment: number): void {
const focusableTabs = this.tabs.filter(t => !this.isDisabledElement(t));
const focusableTabs = this.tabs.filter(t => this.isFocusableElement(t));
const currentActiveTabIndex = focusableTabs.indexOf(this.activetab);

const nextTabIndex = limit(
Expand Down