fix(dropdown): preserve grouped root attrs and events without breaking seams

This commit is contained in:
maoyingjie
2026-10-28 12:35:35 +08:00
parent 8b1505438e
commit f8de4529f6
12 changed files with 305 additions and 23 deletions
+103 -7
View File
@@ -606,7 +606,7 @@ describe("ButtonGroup fullWidth inheritance", () => {
});
describe("ButtonGroup dropdown composition", () => {
it("keeps the dropdown trigger positional for the shared seam styles", () => {
it("marks the positional dropdown wrapper without leaking the private prop", () => {
const w = mount(
defineComponent({
render: () =>
@@ -626,13 +626,12 @@ describe("ButtonGroup dropdown composition", () => {
);
const group = w.get('[data-slot="button-group"]');
const trigger = w.get('button[aria-label="更多选项"]');
// The group seam CSS keys off positional children and upstream Dropdown
// has no DOM root: the trigger must sit directly in the group, with no
// wrapper in between and no internal marker leaked onto the DOM.
expect(trigger.element.parentElement).toBe(group.element);
expect(trigger.element.matches(":last-child")).toBe(true);
const root = w.get('[data-slot="dropdown-root"]');
expect(root.element.parentElement).toBe(group.element);
expect(root.element.matches(":last-child")).toBe(true);
expect(root.attributes("data-button-group-child")).toBe("true");
expect(trigger.element.parentElement).toBe(root.element);
expect(w.html()).not.toContain("__button_group_child");
expect(w.find('[data-slot="dropdown-root"]').exists()).toBe(false);
w.unmount();
});
@@ -646,6 +645,103 @@ describe("ButtonGroup dropdown composition", () => {
const root = w.get('[data-slot="dropdown-root"]');
expect(root.classes()).toContain("custom-root");
expect(root.attributes("data-trigger")).toBe("press");
expect(root.attributes("data-button-group-child")).toBeUndefined();
w.unmount();
});
it("preserves reactive root styles, identity and accessible attributes in a group", async () => {
const visible = ref(false);
const w = mount(
defineComponent({
render: () =>
h(ButtonGroup, () => [
h(
DropdownRoot,
{
id: "consumer-menu",
class: visible.value ? "visible-root" : "hidden-root",
style: { display: visible.value ? "flex" : "none" },
"data-consumer-root": visible.value ? "shown" : "hidden",
"aria-label": "菜单操作区",
},
() => h(DropdownTrigger, null, () => h(Button, () => "操作")),
),
]),
}),
);
const root = w.get("#consumer-menu");
expect(root.classes()).toContain("hidden-root");
expect((root.element as HTMLElement).style.display).toBe("none");
expect(root.attributes("data-consumer-root")).toBe("hidden");
expect(root.attributes("aria-label")).toBe("菜单操作区");
visible.value = true;
await nextTick();
expect(root.classes()).toContain("visible-root");
expect(root.classes()).not.toContain("hidden-root");
expect((root.element as HTMLElement).style.display).toBe("flex");
expect(root.attributes("data-consumer-root")).toBe("shown");
w.unmount();
});
it("keeps root capture cancellation for click, pointer and keyboard activation", async () => {
const cancel = ref(true);
const click = vi.fn((event: MouseEvent) => {
if (cancel.value) event.preventDefault();
});
const pointer = vi.fn((event: PointerEvent) => event.preventDefault());
const key = vi.fn((event: KeyboardEvent) => event.preventDefault());
const open = vi.fn();
const w = mount(
defineComponent({
render: () =>
h(ButtonGroup, () => [
h(
DropdownRoot,
{
onClickCapture: click,
onPointerdownCapture: pointer,
onKeydownCapture: key,
"onUpdate:open": open,
},
() => h(DropdownTrigger, null, () => h(Button, () => "操作")),
),
]),
}),
);
const button = w.get("button");
button.element.dispatchEvent(
new MouseEvent("click", { bubbles: true, cancelable: true }),
);
button.element.dispatchEvent(
new MouseEvent("pointerdown", {
bubbles: true,
cancelable: true,
button: 0,
ctrlKey: false,
}),
);
button.element.dispatchEvent(
new KeyboardEvent("keydown", {
bubbles: true,
cancelable: true,
key: "Enter",
}),
);
await nextTick();
expect(click).toHaveBeenCalledTimes(1);
expect(pointer).toHaveBeenCalledTimes(1);
expect(key).toHaveBeenCalledTimes(1);
expect(open).not.toHaveBeenCalled();
expect(button.attributes("aria-expanded")).toBe("false");
cancel.value = false;
button.element.dispatchEvent(
new MouseEvent("click", { bubbles: true, cancelable: true }),
);
await nextTick();
expect(click).toHaveBeenCalledTimes(2);
expect(open).toHaveBeenCalledTimes(1);
expect(open).toHaveBeenCalledWith(true);
expect(button.attributes("aria-expanded")).toBe("true");
w.unmount();
});
});