Skip to content

Commit

Permalink
fix: acccordion expand mode changes should update the UI as expected (#…
Browse files Browse the repository at this point in the history
…6611)

* fixes an issue where accordion expand-mode changes were not reflected in the component

* Change files

* update api report

* go back to aria-disabled as it removes the item from the tabindex, duh

* fixup accordion tests
  • Loading branch information
chrisdholt authored Jan 28, 2023
1 parent 4a616b4 commit 2a2fe95
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "fixes an issue where accordion expand-mode changes were not reflected in the component",
"packageName": "@microsoft/fast-foundation",
"email": "[email protected]",
"dependentChangeType": "prerelease"
}
2 changes: 2 additions & 0 deletions packages/web-components/fast-foundation/docs/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ export class FASTAccordion extends FASTElement {
// (undocumented)
protected accordionItems: Element[];
expandmode: AccordionExpandMode;
// (undocumented)
expandmodeChanged(prev: AccordionExpandMode, next: AccordionExpandMode): void;
// @internal (undocumented)
handleChange(source: any, propertyName: string): void;
// @internal (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ export const myAccordionItem = AccordionItem.compose<AccordionItemOptions>({
| `expandmode` | public | `AccordionExpandMode` | | Controls the expand mode of the Accordion, either allowing single or multiple item expansion. | |
| `accordionItems` | protected | `Element[]` | | | |

#### Methods

| Name | Privacy | Description | Parameters | Return | Inherited From |
| ------------------- | ------- | ----------- | ------------------------------------------------------ | ------ | -------------- |
| `expandmodeChanged` | public | | `prev: AccordionExpandMode, next: AccordionExpandMode` | | |

#### Events

| Name | Type | Description | Inherited From |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Locator, Page } from "@playwright/test";
import { expect, test } from "@playwright/test";
import { fixtureURL } from "../__test__/helpers.js";
import type { FASTAccordionItem } from "../accordion-item/index.js";
import { AccordionExpandMode } from "./accordion.options.js";
import type { FASTAccordion } from "./accordion.js";

test.describe("Accordion", () => {
let page: Page;
Expand Down Expand Up @@ -150,6 +150,94 @@ test.describe("Accordion", () => {
await expect(secondItem).toHaveBooleanAttribute("expanded");
});

test("should set the expanded items' button to aria-disabled when in single expand mode", async () => {
await root.evaluate(node => {
node.innerHTML = /* html */ `
<fast-accordion expand-mode="single">
<fast-accordion-item>
<span slot="heading">Heading 1</span>
<div>Content 1</div>
</fast-accordion-item>
<fast-accordion-item>
<span slot="heading">Heading 2</span>
<div>Content 2</div>
</fast-accordion-item>
</fast-accordion>
`;
});

const items = element.locator("fast-accordion-item");

const firstItem = items.nth(0);

const secondItem = items.nth(1);

await firstItem.click();

await expect(firstItem).toHaveBooleanAttribute("expanded");

await expect(firstItem.locator("button")).toHaveAttribute(
"aria-disabled",
"true"
);

await secondItem.click();

await expect(firstItem).not.toHaveBooleanAttribute("expanded");

await expect(firstItem.locator("button")).not.toHaveAttribute(
"aria-disabled",
"true"
);
await expect(firstItem.locator("button")).not.toHaveAttribute(
"aria-disabled",
"false"
);

await expect(secondItem).toHaveBooleanAttribute("expanded");

await expect(secondItem.locator("button")).toHaveAttribute(
"aria-disabled",
"true"
);
});

test("should remove an expanded items' expandbutton aria-disabled attribute when expand mode changes from single to multi", async () => {
await root.evaluate(node => {
node.innerHTML = /* html */ `
<fast-accordion expand-mode="single">
<fast-accordion-item>
<span slot="heading">Heading 1</span>
<div>Content 1</div>
</fast-accordion-item>
<fast-accordion-item>
<span slot="heading">Heading 2</span>
<div>Content 2</div>
</fast-accordion-item>
</fast-accordion>
`;
});

const items = element.locator("fast-accordion-item");

const firstItem = items.nth(0);

await firstItem.click();

await expect(firstItem).toHaveBooleanAttribute("expanded");

await expect(firstItem.locator("button")).toHaveAttribute(
"aria-disabled",
"true"
);

await element.evaluate(node => {
node.setAttribute("expand-mode", "multi");
});

await expect(firstItem.locator("button")).not.hasAttribute("aria-disabled");
});

test("should set the first item as expanded if no child is expanded by default in single mode", async () => {
await root.evaluate(node => {
node.innerHTML = /* html */ `
Expand Down
23 changes: 21 additions & 2 deletions packages/web-components/fast-foundation/src/accordion/accordion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ export class FASTAccordion extends FASTElement {
*/
@attr({ attribute: "expand-mode" })
public expandmode: AccordionExpandMode = AccordionExpandMode.multi;
public expandmodeChanged(prev: AccordionExpandMode, next: AccordionExpandMode) {
if (!this.$fastController.isConnected) {
return;
}

const expandedItem = this.findExpandedItem();

if (!expandedItem) {
return;
}

if (next !== AccordionExpandMode.single) {
(expandedItem as FASTAccordionItem)?.expandbutton.removeAttribute(
"aria-disabled"
);
} else {
this.setSingleExpandMode(expandedItem);
}
}

/**
* @internal
Expand Down Expand Up @@ -131,12 +150,12 @@ export class FASTAccordion extends FASTElement {
currentItems.forEach((item: FASTAccordionItem, index: number) => {
if (this.activeItemIndex === index) {
item.expanded = true;
item.expandbutton.setAttribute("disabled", "");
item.expandbutton.setAttribute("aria-disabled", "true");
} else {
item.expanded = false;

if (!item.hasAttribute("disabled")) {
item.expandbutton.removeAttribute("disabled");
item.expandbutton.removeAttribute("aria-disabled");
}
}
});
Expand Down

0 comments on commit 2a2fe95

Please sign in to comment.