fix(dropdown): preserve grouped root attrs and events without breaking seams
This commit is contained in:
+103
-7
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
<script setup lang="ts">
|
||||
import { reactive, ref } from "vue";
|
||||
import {
|
||||
Button,
|
||||
ButtonGroup,
|
||||
DropdownRoot,
|
||||
DropdownTrigger,
|
||||
DropdownPopover,
|
||||
DropdownMenu,
|
||||
DropdownItem,
|
||||
} from "@heroui-vue/components";
|
||||
|
||||
const cancel = ref(true);
|
||||
const hidden = ref(false);
|
||||
const counts = reactive({ click: 0, pointerdown: 0, keydown: 0 });
|
||||
const cases = [
|
||||
{ orientation: "horizontal" as const, dir: "ltr" as const },
|
||||
{ orientation: "horizontal" as const, dir: "rtl" as const },
|
||||
{ orientation: "vertical" as const, dir: "ltr" as const },
|
||||
];
|
||||
const positions = ["first", "middle", "last"];
|
||||
function capture(event: Event) {
|
||||
counts[event.type as keyof typeof counts]++;
|
||||
if (cancel.value) event.preventDefault();
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<main class="p-6 space-y-6">
|
||||
<h1>ButtonGroup / Dropdown 根属性与接缝</h1>
|
||||
<label>
|
||||
<input v-model="cancel" type="checkbox" />
|
||||
取消打开
|
||||
</label>
|
||||
<label>
|
||||
<input v-model="hidden" type="checkbox" />
|
||||
隐藏根容器
|
||||
</label>
|
||||
<output aria-label="捕获事件次数">{{ JSON.stringify(counts) }}</output>
|
||||
<section v-for="item in cases" :key="item.orientation + item.dir">
|
||||
<h2>{{ item.orientation }} / {{ item.dir }}</h2>
|
||||
<ButtonGroup
|
||||
:orientation="item.orientation"
|
||||
:dir="item.dir"
|
||||
:data-case="item.orientation + '-' + item.dir"
|
||||
>
|
||||
<DropdownRoot
|
||||
v-for="position in positions"
|
||||
:id="item.orientation + '-' + item.dir + '-' + position"
|
||||
:key="position"
|
||||
class="consumer-root"
|
||||
:data-position="position"
|
||||
:style="{
|
||||
display: hidden ? 'none' : 'flex',
|
||||
'--consumer-marker': 'kept',
|
||||
}"
|
||||
:aria-label="position + '菜单容器'"
|
||||
@click.capture="capture"
|
||||
@pointerdown.capture="capture"
|
||||
@keydown.capture="capture"
|
||||
>
|
||||
<DropdownTrigger>
|
||||
<Button variant="outline">{{ position }}</Button>
|
||||
</DropdownTrigger>
|
||||
<DropdownPopover>
|
||||
<DropdownMenu>
|
||||
<DropdownItem id="action">执行操作</DropdownItem>
|
||||
</DropdownMenu>
|
||||
</DropdownPopover>
|
||||
</DropdownRoot>
|
||||
</ButtonGroup>
|
||||
</section>
|
||||
</main>
|
||||
</template>
|
||||
@@ -36,6 +36,7 @@
|
||||
"MenuSection"
|
||||
],
|
||||
"css": [
|
||||
"data-button-group-child",
|
||||
"hv-menu-item",
|
||||
"hv-menu-item__indicator--submenu",
|
||||
"hv-dropdown__popover",
|
||||
@@ -570,7 +571,15 @@
|
||||
]
|
||||
},
|
||||
"form": {
|
||||
"exports": ["Form", "FormRoot", "TextField", "Input", "Label", "FieldError", "Button"],
|
||||
"exports": [
|
||||
"Form",
|
||||
"FormRoot",
|
||||
"TextField",
|
||||
"Input",
|
||||
"Label",
|
||||
"FieldError",
|
||||
"Button"
|
||||
],
|
||||
"css": [".hv-textfield", ".hv-input", ".hv-field-error", ".hv-button"]
|
||||
},
|
||||
"api-root-aliases": {
|
||||
|
||||
Reference in New Issue
Block a user