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: accordion item should not collapse on unrelated change events #6158

Merged
merged 6 commits into from
Jul 1, 2022
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": "patch",
"comment": "accordion verifies change events",
"packageName": "@microsoft/fast-foundation",
"email": "stephcomeau@msn.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from "chai";
import { FASTAccordion, accordionTemplate, AccordionExpandMode } from "./index.js";
import { FASTAccordionItem, accordionItemTemplate } from "../accordion-item/index.js";
import { fixture, uniqueElementName } from "../testing/fixture.js";
import { FASTCheckbox, checkboxTemplate } from "../checkbox/index.js";
import { Updates } from "@microsoft/fast-element";

const Accordion = FASTAccordion.define({
Expand All @@ -14,6 +15,11 @@ const AccordionItem = FASTAccordionItem.define({
template: accordionItemTemplate()
});

const Checkbox = FASTCheckbox.define({
name: uniqueElementName("checkbox"),
template: checkboxTemplate()
});

async function setup() {
const { element, connect, disconnect } = await fixture(Accordion);

Expand Down Expand Up @@ -66,4 +72,93 @@ describe("Accordion", () => {

await disconnect();
});

it("should expand/collapse items when clicked in multi mode", async () => {
const { element, connect, disconnect, item1, item2, item3 } = await setup();

element.expandmode = AccordionExpandMode.multi;

await connect();
await Updates.next();

expect(item1.expanded).to.equal(false);
expect(item2.expanded).to.equal(false);
expect(item3.expanded).to.equal(false);

item1.expandbutton.click();
item2.expandbutton.click();
item3.expandbutton.click();

await Updates.next();

expect(item1.expanded).to.equal(true);
expect(item2.expanded).to.equal(true);
expect(item3.expanded).to.equal(true);

item1.expandbutton.click();
item2.expandbutton.click();
item3.expandbutton.click();

await Updates.next();

expect(item1.expanded).to.equal(false);
expect(item2.expanded).to.equal(false);
expect(item3.expanded).to.equal(false);

await disconnect();
});

it("should always be one item expanded in single mode", async () => {
const { element, connect, disconnect, item1, item2, item3 } = await setup();

element.expandmode = AccordionExpandMode.single;

await connect();
await Updates.next();

expect(item1.expanded).to.equal(true);
expect(item2.expanded).to.equal(false);
expect(item3.expanded).to.equal(false);

item2.expandbutton.click();

await Updates.next();

expect(item1.expanded).to.equal(false);
expect(item2.expanded).to.equal(true);
expect(item3.expanded).to.equal(false);

item2.expandbutton.click();

await Updates.next();

expect(item1.expanded).to.equal(false);
expect(item2.expanded).to.equal(true);
expect(item3.expanded).to.equal(false);

await disconnect();
});

it("should ignore 'change' events from components other than accordion items", async () => {
const { element, connect, disconnect, item1, item2, item3 } = await setup();

element.expandmode = AccordionExpandMode.multi;
const checkbox = new Checkbox();
item1.appendChild(checkbox);

await connect();
await Updates.next();
expect(item1.expanded).to.equal(false);

item1.expandbutton.click();
await Updates.next();
expect(item1.expanded).to.equal(true);

checkbox.click();
await Updates.next();
expect(item1.expanded).to.equal(true);

await disconnect();
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ export class FASTAccordion extends FASTElement {
};

private activeItemChange = (event: Event): void => {
if (event.defaultPrevented || event.target !== event.currentTarget) {
return;
}

event.preventDefault();
scomea marked this conversation as resolved.
Show resolved Hide resolved
const selectedItem = event.target as FASTAccordionItem;
this.activeid = selectedItem.getAttribute("id");
if (this.isSingleExpandMode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default {
</fast-accordion-item>
<fast-accordion-item>
<div slot="heading">Accordion Item 2 Heading</div>
Accordion Item 2 Content
<fast-checkbox>A checkbox as content</fast-checkbox
</fast-accordion-item>
`,
},
Expand Down