feat: migrate Menu and Dropdown with selection and submenus
Port HeroUI menu family and Dropdown from ce90ea3: Reka DropdownMenu base, MenuKey[] selection with section owners, 17 official demos, dropdown docs and B01 tests. Long-press stays demo-level; submenu close is level-scoped.
This commit is contained in:
@@ -0,0 +1,257 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { defineComponent, h, ref, nextTick } from "vue";
|
||||
import {
|
||||
Button,
|
||||
DropdownRoot,
|
||||
DropdownPopover,
|
||||
DropdownMenu,
|
||||
DropdownItem,
|
||||
DropdownItemIndicator,
|
||||
DropdownSection,
|
||||
DropdownSubmenuTrigger,
|
||||
DropdownSubmenuIndicator,
|
||||
Label,
|
||||
Menu,
|
||||
MenuItem,
|
||||
MenuSection,
|
||||
} from "../packages/vue/src";
|
||||
|
||||
const cleanup: (() => void)[] = [];
|
||||
afterEach(() => {
|
||||
cleanup
|
||||
.splice(0)
|
||||
.reverse()
|
||||
.forEach((fn) => fn());
|
||||
vi.useRealTimers();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
function mountApp(setup: () => unknown, render?: () => unknown) {
|
||||
const w = mount(
|
||||
defineComponent(
|
||||
render
|
||||
? { setup: setup as never, render: render as never }
|
||||
: { setup: setup as never },
|
||||
),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
cleanup.push(() => w.unmount());
|
||||
return w;
|
||||
}
|
||||
|
||||
it("Dropdown opens and action item fires once", async () => {
|
||||
const actions: string[] = [];
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true }, () => [
|
||||
h(Button, () => "操作"),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () => [
|
||||
h(DropdownItem, { id: "new-file", textValue: "新建文件" }, () =>
|
||||
h(Label, () => "新建文件"),
|
||||
),
|
||||
h(
|
||||
DropdownItem,
|
||||
{ id: "delete-file", textValue: "删除文件", variant: "danger" },
|
||||
() => h(Label, () => "删除文件"),
|
||||
),
|
||||
]),
|
||||
),
|
||||
]),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const items = document.querySelectorAll('[data-slot="menu-item"]');
|
||||
expect(items.length).toBe(2);
|
||||
expect((items[1] as HTMLElement).className).toContain("hv-menu-item--danger");
|
||||
expect((items[0] as HTMLElement).getAttribute("role")).toBe("menuitem");
|
||||
|
||||
(items[0] as HTMLElement).click();
|
||||
await flushPromises();
|
||||
expect(actions).toEqual(["new-file"]);
|
||||
});
|
||||
|
||||
it("Menu multiple selection toggles keys", async () => {
|
||||
const selected = ref<string[]>(["apple"]);
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true }, () => [
|
||||
h(Button, () => "水果"),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(
|
||||
DropdownMenu,
|
||||
{
|
||||
selectionMode: "multiple",
|
||||
selectedKeys: selected.value,
|
||||
"onUpdate:selectedKeys": (k: (string | number)[]) => {
|
||||
selected.value = k as string[];
|
||||
},
|
||||
},
|
||||
() => [
|
||||
h(DropdownItem, { id: "apple", textValue: "苹果" }, () => [
|
||||
h(DropdownItemIndicator),
|
||||
h(Label, () => "苹果"),
|
||||
]),
|
||||
h(DropdownItem, { id: "banana", textValue: "香蕉" }, () => [
|
||||
h(DropdownItemIndicator),
|
||||
h(Label, () => "香蕉"),
|
||||
]),
|
||||
],
|
||||
),
|
||||
),
|
||||
]),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const items = document.querySelectorAll('[data-slot="menu-item"]');
|
||||
expect(items.length).toBe(2);
|
||||
(items[1] as HTMLElement).click();
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(selected.value).toContain("banana");
|
||||
expect(selected.value).toContain("apple");
|
||||
});
|
||||
|
||||
it("MenuSection can own section-level selection", async () => {
|
||||
const styles = ref<string[]>(["bold"]);
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true }, () => [
|
||||
h(Button, () => "样式"),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(Menu, {}, () =>
|
||||
h(
|
||||
MenuSection,
|
||||
{
|
||||
selectionMode: "multiple",
|
||||
selectedKeys: styles.value,
|
||||
"onUpdate:selectedKeys": (k: (string | number)[]) => {
|
||||
styles.value = k as string[];
|
||||
},
|
||||
},
|
||||
() => [
|
||||
h(MenuItem, { id: "bold", textValue: "粗体" }, () =>
|
||||
h(Label, () => "粗体"),
|
||||
),
|
||||
h(MenuItem, { id: "italic", textValue: "斜体" }, () =>
|
||||
h(Label, () => "斜体"),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
]),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const items = document.querySelectorAll('[data-slot="menu-item"]');
|
||||
expect(items.length).toBe(2);
|
||||
(items[1] as HTMLElement).click();
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(styles.value).toEqual(expect.arrayContaining(["bold", "italic"]));
|
||||
});
|
||||
|
||||
it("disabled item is marked disabled", async () => {
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true }, () => [
|
||||
h(Button, () => "操作"),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(Menu, { disabledKeys: ["x"] }, () =>
|
||||
h(MenuItem, { id: "x", textValue: "X" }, () => h(Label, () => "X")),
|
||||
),
|
||||
),
|
||||
]),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const item = document.querySelector('[data-slot="menu-item"]') as HTMLElement;
|
||||
expect(item).toBeTruthy();
|
||||
const disabled =
|
||||
item.hasAttribute("disabled") ||
|
||||
item.getAttribute("aria-disabled") === "true" ||
|
||||
item.getAttribute("data-disabled") === "true" ||
|
||||
(item as HTMLInputElement).disabled === true;
|
||||
expect(disabled).toBe(true);
|
||||
});
|
||||
|
||||
it("indicator uses checkmark svg and data-visible when selected", async () => {
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true }, () => [
|
||||
h(Button, () => "水果"),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(
|
||||
DropdownMenu,
|
||||
{ selectionMode: "multiple", selectedKeys: ["apple"] },
|
||||
() => [
|
||||
h(DropdownItem, { id: "apple", textValue: "苹果" }, () => [
|
||||
h(DropdownItemIndicator),
|
||||
h(Label, () => "苹果"),
|
||||
]),
|
||||
],
|
||||
),
|
||||
),
|
||||
]),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const ind = document.querySelector('[data-slot="menu-item-indicator"]');
|
||||
expect(ind).toBeTruthy();
|
||||
expect(ind!.getAttribute("data-type")).toBe("checkmark");
|
||||
expect(
|
||||
ind!.querySelector('[data-slot="menu-item-indicator--checkmark"]'),
|
||||
).not.toBeNull();
|
||||
});
|
||||
|
||||
it("submenu trigger opens nested items", async () => {
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true }, () => [
|
||||
h(Button, () => "分享"),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(DropdownMenu, {}, () => [
|
||||
h(DropdownItem, { id: "copy-link", textValue: "复制链接" }, () =>
|
||||
h(Label, () => "复制链接"),
|
||||
),
|
||||
h(DropdownSubmenuTrigger, {}, () => [
|
||||
h(DropdownItem, { id: "share", textValue: "分享" }, () => [
|
||||
h(Label, () => "其他"),
|
||||
h(DropdownSubmenuIndicator),
|
||||
]),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(DropdownMenu, {}, () =>
|
||||
h(
|
||||
DropdownItem,
|
||||
{ id: "whatsapp", textValue: "WhatsApp" },
|
||||
() => h(Label, () => "WhatsApp"),
|
||||
),
|
||||
),
|
||||
),
|
||||
]),
|
||||
]),
|
||||
),
|
||||
]),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const triggers = document.querySelectorAll(
|
||||
'[data-slot="dropdown-submenu-trigger"]',
|
||||
);
|
||||
expect(triggers.length).toBe(1);
|
||||
// Open submenu via pointer (Reka SubTrigger)
|
||||
(triggers[0] as HTMLElement).dispatchEvent(
|
||||
new MouseEvent("pointerenter", { bubbles: true }),
|
||||
);
|
||||
(triggers[0] as HTMLElement).click();
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const html = document.body.innerHTML;
|
||||
expect(html).toContain("WhatsApp");
|
||||
expect(html).toContain("其他");
|
||||
expect(
|
||||
document.querySelector('[data-slot="dropdown-submenu-trigger"]'),
|
||||
).toBeTruthy();
|
||||
});
|
||||
Reference in New Issue
Block a user