Skip to content

Commit

Permalink
Prevent keyboard navigation to hidden tab (#6789)
Browse files Browse the repository at this point in the history
* Prevent keyboard navigation to hidden tab

* Change files

* Fix issue with Home/End

* use hasAttribute

* Update change/@microsoft-fast-foundation-7d8d0430-523b-473a-af77-e2bcf0bc6e38.json

* Update change/@microsoft-fast-foundation-7d8d0430-523b-473a-af77-e2bcf0bc6e38.json

---------

Co-authored-by: Nicholas Rice <3213292+nicholasrice@users.noreply.github.com>
  • Loading branch information
m-akinc and nicholasrice authored Aug 2, 2023
1 parent 9f6451f commit 50dba9c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
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"
}
66 changes: 66 additions & 0 deletions packages/web-components/fast-foundation/src/tabs/tabs.pw.spec.ts
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

0 comments on commit 50dba9c

Please sign in to comment.