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 bug in toolbar click handler when a slotted element has child elements #6830

Merged
merged 6 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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": "Fix bug in toolbar click handler when a slotted element has child elements",
"packageName": "@microsoft/fast-foundation",
"email": "[email protected]",
"dependentChangeType": "patch"
chrisdholt marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,73 @@ test.describe("Toolbar", () => {

await expect(element).toHaveJSProperty("activeIndex", 0);
});

test("should update activeIndex when an element within the toolbar is clicked", async () => {
await root.evaluate(node => {
node.innerHTML = /* html */ `
<fast-toolbar>
<button slot="start">Start Slot Button</button>
<button>Button 1</button>
<button>Button 2</button>
<button>Button 3</button>
<button slot="end">End Slot Button</button>
</fast-toolbar>
`;
});

const button2 = element.locator("button", { hasText: "Button 2" });

const startSlotButton = element.locator("button", {
hasText: "Start Slot Button",
});

await element.focus();

await expect(startSlotButton).toBeFocused();

await expect(element).toHaveJSProperty("activeIndex", 0);

await button2.click();

await expect(button2).toBeFocused();

await expect(element).toHaveJSProperty("activeIndex", 2);
});

test("should update activeIndex when a nested element within the toolbar is clicked", async () => {
await root.evaluate(node => {
node.innerHTML = /* html */ `
<fast-toolbar>
<button slot="start">Start Slot Button</button>
<button>Button 1</button>
<button>
Button 2
<div>more button content</div>
</button>
<button>Button 3</button>
<button slot="end">End Slot Button</button>
</fast-toolbar>
`;
});

const button2 = element.locator("button", { hasText: "Button 2" });

const button2InnerContent = button2.locator("div");

const startSlotButton = element.locator("button", {
hasText: "Start Slot Button",
});

await element.focus();

await expect(startSlotButton).toBeFocused();

await expect(element).toHaveJSProperty("activeIndex", 0);

await button2InnerContent.click();

await expect(button2).toBeFocused();

await expect(element).toHaveJSProperty("activeIndex", 2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ export class FASTToolbar extends FASTElement {
* @internal
*/
public clickHandler(e: MouseEvent): boolean | void {
const activeIndex = this.focusableElements?.indexOf(e.target as HTMLElement);
const activeIndex = this.focusableElements?.findIndex(x =>
x.contains(e.target as HTMLElement)
);
if (activeIndex > -1 && this.activeIndex !== activeIndex) {
this.setFocusedElement(activeIndex);
}
Expand Down