feat(combo-box): migrate ComboBox with input-owned focus and filtering
Port the fixed-baseline ComboBox onto the B02a collection protocol: the input keeps real DOM focus, the list only tracks an active row, and filtering reuses the owner filter instead of a second collection kernel. - Five parts: root (field text control, value/input/open models, form value, owner wiring), InputGroup (composes the caller's Input into the combobox control and anchors the popover), Trigger, Value, Popover. - Closed-state metadata is planned from the slot tree, so filtering, the initial value label and form-value="text" work before the list has mounted. - Selection text syncs on selection changes only, so allows-custom-value typing is not overwritten; read-only and disabled are injected onto the composed input because it reads the text-field protocol, not the field context. - 24 baseline previews, the Chinese doc page, navigation, 19 behaviour tests and 6 demo/document contracts. Async demo uses a local async source with abort, loading and incremental append (documented adaptations). Not included: independent tarball consumption (B02d).
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { mount } from "@vue/test-utils";
|
||||
import { nextTick, type Component } from "vue";
|
||||
import { readFileSync } from "node:fs";
|
||||
|
||||
const settle = () => new Promise((resolve) => setTimeout(resolve, 4));
|
||||
const mounted: { unmount: () => void }[] = [];
|
||||
|
||||
const previews = [
|
||||
"default",
|
||||
"full-width",
|
||||
"with-description",
|
||||
"required",
|
||||
"disabled",
|
||||
"with-disabled-options",
|
||||
"with-sections",
|
||||
"controlled",
|
||||
"controlled-input-value",
|
||||
"asynchronous-loading",
|
||||
"default-selected-key",
|
||||
"allows-custom-value",
|
||||
"custom-indicator",
|
||||
"custom-value",
|
||||
"custom-filtering",
|
||||
"render-function",
|
||||
"menu-trigger",
|
||||
"form-value",
|
||||
"validation-behavior",
|
||||
"custom-validation",
|
||||
"read-only",
|
||||
"multiple-selection",
|
||||
"on-surface",
|
||||
"custom-styles",
|
||||
];
|
||||
|
||||
afterEach(async () => {
|
||||
while (mounted.length) mounted.pop()?.unmount();
|
||||
await nextTick();
|
||||
await settle();
|
||||
vi.restoreAllMocks();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
describe("ComboBox docs and demos", () => {
|
||||
it("keeps the pinned preview order and one demo per preview", () => {
|
||||
const document = readFileSync("apps/docs/components/combo-box.md", "utf8");
|
||||
const names = [
|
||||
...document.matchAll(/<ComponentPreview name="combo-box-([^"]+)"/g),
|
||||
].map((match) => match[1]);
|
||||
|
||||
expect(names).toEqual(previews);
|
||||
for (const name of previews) {
|
||||
const source = readFileSync(
|
||||
`apps/docs/demos/combo-box-${name}.vue`,
|
||||
"utf8",
|
||||
);
|
||||
|
||||
expect(source, name).toContain("<template>");
|
||||
expect(source, name).not.toContain("className");
|
||||
expect(source, name).not.toContain("@gravity-ui/icons");
|
||||
expect(source, name).not.toContain("<path");
|
||||
}
|
||||
});
|
||||
|
||||
it("documents Vue props and events instead of React ones", () => {
|
||||
const document = readFileSync("apps/docs/components/combo-box.md", "utf8");
|
||||
|
||||
expect(document).not.toMatch(/\|\s*`className`\s*\|/);
|
||||
expect(document).toContain("`class`");
|
||||
expect(document).toContain("Vue class 绑定");
|
||||
expect(document).toContain("### ComboBoxInputGroup");
|
||||
expect(document).toContain("### ComboBoxValue");
|
||||
expect(document).toContain("### ComboBoxTrigger");
|
||||
expect(document).toContain("### ComboBoxPopover");
|
||||
expect(document).not.toContain("[!toc]");
|
||||
expect(document).not.toContain("ComboBox.List");
|
||||
});
|
||||
|
||||
it("mounts every demo with a real combobox control", async () => {
|
||||
const modules = import.meta.glob("../apps/docs/demos/combo-box-*.vue");
|
||||
expect(Object.keys(modules)).toHaveLength(previews.length);
|
||||
|
||||
for (const [path, load] of Object.entries(modules)) {
|
||||
vi.useFakeTimers();
|
||||
const demo = (await load()) as { default: Component };
|
||||
const wrapper = mount(demo.default, { attachTo: document.body });
|
||||
|
||||
mounted.push(wrapper);
|
||||
await nextTick();
|
||||
const inputs = (wrapper.element as HTMLElement).querySelectorAll(
|
||||
'[role="combobox"]',
|
||||
);
|
||||
|
||||
expect(inputs.length, path).toBeGreaterThan(0);
|
||||
// Every combobox must be a real text input with a label association.
|
||||
for (const input of inputs) {
|
||||
expect(input.tagName, path).toBe("INPUT");
|
||||
expect(input.getAttribute("aria-expanded"), path).toBe("false");
|
||||
expect(input.id, path).not.toBe("");
|
||||
}
|
||||
wrapper.unmount();
|
||||
mounted.pop();
|
||||
vi.useRealTimers();
|
||||
await settle();
|
||||
document.body.innerHTML = "";
|
||||
}
|
||||
});
|
||||
|
||||
it("opens, filters and selects inside representative demos", async () => {
|
||||
const load = (name: string) =>
|
||||
import(`../apps/docs/demos/combo-box-${name}.vue`);
|
||||
|
||||
for (const name of ["default", "with-sections", "custom-styles"]) {
|
||||
const demo = (await load(name)) as { default: Component };
|
||||
const wrapper = mount(demo.default, { attachTo: document.body });
|
||||
mounted.push(wrapper);
|
||||
await nextTick();
|
||||
await settle();
|
||||
const input = (
|
||||
wrapper.element as HTMLElement
|
||||
).querySelector<HTMLInputElement>('[role="combobox"]')!;
|
||||
input.focus();
|
||||
input.dispatchEvent(
|
||||
new KeyboardEvent("keydown", {
|
||||
key: "ArrowDown",
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
}),
|
||||
);
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(input.getAttribute("aria-expanded"), name).toBe("true");
|
||||
const id = input.getAttribute("aria-activedescendant");
|
||||
expect(id, name).toBeTruthy();
|
||||
expect(document.getElementById(id!)?.getAttribute("role"), name).toBe(
|
||||
"option",
|
||||
);
|
||||
wrapper.unmount();
|
||||
mounted.pop();
|
||||
await settle();
|
||||
document.body.innerHTML = "";
|
||||
}
|
||||
});
|
||||
|
||||
it("shows a preselected item's text in the input", async () => {
|
||||
for (const [name, expected] of [
|
||||
["default-selected-key", "猫"],
|
||||
["disabled", "猫"],
|
||||
["read-only", "猫"],
|
||||
] as const) {
|
||||
const demo = (await import(
|
||||
`../apps/docs/demos/combo-box-${name}.vue`
|
||||
)) as { default: Component };
|
||||
const wrapper = mount(demo.default, { attachTo: document.body });
|
||||
mounted.push(wrapper);
|
||||
await nextTick();
|
||||
await settle();
|
||||
const input = (
|
||||
wrapper.element as HTMLElement
|
||||
).querySelector<HTMLInputElement>('[role="combobox"]')!;
|
||||
expect(input.value, name).toBe(expected);
|
||||
wrapper.unmount();
|
||||
mounted.pop();
|
||||
await settle();
|
||||
document.body.innerHTML = "";
|
||||
}
|
||||
});
|
||||
|
||||
it("submits form-value demos through hidden inputs", async () => {
|
||||
const demo =
|
||||
(await import("../apps/docs/demos/combo-box-form-value.vue")) as {
|
||||
default: Component;
|
||||
};
|
||||
const wrapper = mount(demo.default, { attachTo: document.body });
|
||||
mounted.push(wrapper);
|
||||
await nextTick();
|
||||
await settle();
|
||||
const hidden = [
|
||||
...(wrapper.element as HTMLElement).querySelectorAll<HTMLInputElement>(
|
||||
'input[type="hidden"][name]',
|
||||
),
|
||||
];
|
||||
expect(hidden.map((input) => input.name).sort()).toEqual([
|
||||
"animal",
|
||||
"animal-text",
|
||||
]);
|
||||
wrapper.unmount();
|
||||
mounted.pop();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,657 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { mount } from "@vue/test-utils";
|
||||
import { defineComponent, h, nextTick, ref } from "vue";
|
||||
import {
|
||||
ComboBox,
|
||||
ComboBoxInputGroup,
|
||||
ComboBoxPopover,
|
||||
ComboBoxTrigger,
|
||||
ComboBoxValue,
|
||||
Description,
|
||||
FieldError,
|
||||
Form,
|
||||
Input,
|
||||
Label,
|
||||
ListBox,
|
||||
ListBoxItem,
|
||||
} from "../packages/vue/src";
|
||||
|
||||
const settle = () => new Promise((resolve) => setTimeout(resolve, 3));
|
||||
const mounted: { unmount: () => void; element: Element }[] = [];
|
||||
|
||||
type Item = { id: string | number; text: string; disabled?: boolean };
|
||||
const animals: Item[] = [
|
||||
{ id: "cat", text: "猫" },
|
||||
{ id: "dog", text: "狗" },
|
||||
{ id: "bird", text: "鸟" },
|
||||
{ id: "fish", text: "鱼" },
|
||||
];
|
||||
|
||||
function mountCombo(
|
||||
props: Record<string, unknown> = {},
|
||||
options: { items?: Item[]; value?: boolean } = {},
|
||||
) {
|
||||
const items = options.items ?? animals;
|
||||
const wrapper = mount(
|
||||
defineComponent({
|
||||
setup() {
|
||||
return () =>
|
||||
h(
|
||||
ComboBox,
|
||||
{ "aria-label": "动物", ...props },
|
||||
{
|
||||
default: () => [
|
||||
h(Label, () => "最喜欢的动物"),
|
||||
h(ComboBoxInputGroup, null, {
|
||||
default: () => [
|
||||
h(Input, { placeholder: "搜索动物…" }),
|
||||
h(ComboBoxTrigger),
|
||||
],
|
||||
}),
|
||||
options.value
|
||||
? h(ComboBoxValue, { placeholder: "未选择" })
|
||||
: null,
|
||||
h(ComboBoxPopover, null, {
|
||||
default: () =>
|
||||
h(ListBox, null, {
|
||||
default: () =>
|
||||
items.map((item) =>
|
||||
h(
|
||||
ListBoxItem,
|
||||
{
|
||||
id: item.id,
|
||||
key: item.id,
|
||||
textValue: item.text,
|
||||
isDisabled: item.disabled,
|
||||
},
|
||||
() => item.text,
|
||||
),
|
||||
),
|
||||
empty: () =>
|
||||
h("p", { "data-test": "empty" }, "没有匹配项"),
|
||||
}),
|
||||
}),
|
||||
],
|
||||
},
|
||||
);
|
||||
},
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
mounted.push(wrapper);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
const input = () =>
|
||||
document.querySelector<HTMLInputElement>('[data-slot="input"]')!;
|
||||
const rowTexts = () =>
|
||||
[...document.querySelectorAll('[role="option"]')]
|
||||
.filter((row) => !row.hasAttribute("data-filtered"))
|
||||
.map((row) => row.textContent?.trim());
|
||||
|
||||
async function type(value: string) {
|
||||
const element = input();
|
||||
element.value = value;
|
||||
element.dispatchEvent(new Event("input", { bubbles: true }));
|
||||
await nextTick();
|
||||
await settle();
|
||||
}
|
||||
async function press(key: string, init: KeyboardEventInit = {}) {
|
||||
input().dispatchEvent(
|
||||
new KeyboardEvent("keydown", {
|
||||
key,
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
...init,
|
||||
}),
|
||||
);
|
||||
await nextTick();
|
||||
await settle();
|
||||
}
|
||||
|
||||
afterEach(async () => {
|
||||
while (mounted.length) mounted.pop()?.unmount();
|
||||
await nextTick();
|
||||
await settle();
|
||||
vi.restoreAllMocks();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
describe("ComboBox input semantics", () => {
|
||||
it("exposes one combobox control with label and ARIA wiring", async () => {
|
||||
mountCombo();
|
||||
await nextTick();
|
||||
const element = input();
|
||||
const label = document.querySelector("label")!;
|
||||
|
||||
expect(element.getAttribute("role")).toBe("combobox");
|
||||
expect(element.getAttribute("aria-expanded")).toBe("false");
|
||||
expect(element.getAttribute("aria-autocomplete")).toBe("list");
|
||||
expect(element.getAttribute("aria-haspopup")).toBe("listbox");
|
||||
expect(element.id).toBeTruthy();
|
||||
expect(label.getAttribute("for")).toBe(element.id);
|
||||
expect(
|
||||
document
|
||||
.querySelector('[data-slot="combo-box-trigger"]')!
|
||||
.getAttribute("tabindex"),
|
||||
).toBe("-1");
|
||||
});
|
||||
|
||||
it("opens on focus, filters with the input and keeps focus in the input", async () => {
|
||||
mountCombo();
|
||||
await nextTick();
|
||||
input().focus();
|
||||
input().dispatchEvent(new FocusEvent("focus"));
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(input().getAttribute("aria-expanded")).toBe("true");
|
||||
|
||||
await type("狗");
|
||||
expect(rowTexts()).toEqual(["狗"]);
|
||||
expect(document.activeElement).toBe(input());
|
||||
|
||||
const id = input().getAttribute("aria-activedescendant");
|
||||
expect(id).toBeTruthy();
|
||||
const option = document.getElementById(id!);
|
||||
expect(option?.getAttribute("role")).toBe("option");
|
||||
expect(option?.textContent?.trim()).toBe("狗");
|
||||
});
|
||||
|
||||
it("confirms with Enter, writes the text back and closes", async () => {
|
||||
const onChange = vi.fn();
|
||||
mountCombo({ onSelectionChange: onChange });
|
||||
await nextTick();
|
||||
input().focus();
|
||||
await press("ArrowDown");
|
||||
await press("ArrowDown");
|
||||
await press("Enter");
|
||||
|
||||
expect(onChange).toHaveBeenLastCalledWith("dog");
|
||||
expect(input().value).toBe("狗");
|
||||
expect(input().getAttribute("aria-expanded")).toBe("false");
|
||||
expect(input().getAttribute("aria-activedescendant")).toBeNull();
|
||||
});
|
||||
|
||||
it("closes on Escape without changing the value", async () => {
|
||||
const onChange = vi.fn();
|
||||
mountCombo({ onSelectionChange: onChange });
|
||||
await nextTick();
|
||||
input().focus();
|
||||
await press("ArrowDown");
|
||||
await press("Escape");
|
||||
expect(input().getAttribute("aria-expanded")).toBe("false");
|
||||
expect(onChange).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not confirm while an IME composition is active", async () => {
|
||||
const onChange = vi.fn();
|
||||
mountCombo({ onSelectionChange: onChange });
|
||||
await nextTick();
|
||||
input().focus();
|
||||
await press("ArrowDown");
|
||||
await press("Enter", { isComposing: true });
|
||||
expect(onChange).not.toHaveBeenCalled();
|
||||
await press("Enter", { keyCode: 229 });
|
||||
expect(onChange).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("clears the selection when the text stops matching it", async () => {
|
||||
const onChange = vi.fn();
|
||||
mountCombo({ onSelectionChange: onChange });
|
||||
await nextTick();
|
||||
input().focus();
|
||||
await press("ArrowDown");
|
||||
await press("Enter");
|
||||
expect(onChange).toHaveBeenLastCalledWith("cat");
|
||||
|
||||
await type("狗");
|
||||
expect(onChange).toHaveBeenLastCalledWith(null);
|
||||
});
|
||||
|
||||
it("keeps a typed custom value when allowsCustomValue is set", async () => {
|
||||
const onChange = vi.fn();
|
||||
mountCombo({ allowsCustomValue: true, onSelectionChange: onChange });
|
||||
await nextTick();
|
||||
input().focus();
|
||||
await press("ArrowDown");
|
||||
await press("Enter");
|
||||
await type("独角兽");
|
||||
expect(onChange).not.toHaveBeenLastCalledWith(null);
|
||||
expect(input().value).toBe("独角兽");
|
||||
});
|
||||
});
|
||||
|
||||
describe("ComboBox selection state", () => {
|
||||
it("keeps the accepted value when the parent rejects the proposal", async () => {
|
||||
mountCombo({ selectedKey: null, onSelectionChange: () => {} });
|
||||
await nextTick();
|
||||
input().focus();
|
||||
await press("ArrowDown");
|
||||
await press("Enter");
|
||||
await settle();
|
||||
// The rejected value never becomes accepted: nothing is selected.
|
||||
expect(document.querySelector('[aria-selected="true"]')).toBeNull();
|
||||
// And the proposal can be made again once the parent accepts the same key.
|
||||
await press("ArrowDown");
|
||||
await press("Enter");
|
||||
expect(document.querySelector('[aria-selected="true"]')).toBeNull();
|
||||
});
|
||||
|
||||
it("reflects an externally changed selected key", async () => {
|
||||
const selected = ref<string | number | null>("dog");
|
||||
const wrapper = mount(
|
||||
defineComponent({
|
||||
setup() {
|
||||
return () =>
|
||||
h(
|
||||
ComboBox,
|
||||
{
|
||||
"aria-label": "动物",
|
||||
selectedKey: selected.value,
|
||||
onSelectionChange: (key: string | number | null) =>
|
||||
(selected.value = key),
|
||||
},
|
||||
{
|
||||
default: () => [
|
||||
h(ComboBoxInputGroup, null, {
|
||||
default: () => [h(Input, {}), h(ComboBoxTrigger)],
|
||||
}),
|
||||
h(ComboBoxPopover, null, {
|
||||
default: () =>
|
||||
h(ListBox, null, {
|
||||
default: () =>
|
||||
animals.map((item) =>
|
||||
h(
|
||||
ListBoxItem,
|
||||
{
|
||||
id: item.id,
|
||||
key: item.id,
|
||||
textValue: item.text,
|
||||
},
|
||||
() => item.text,
|
||||
),
|
||||
),
|
||||
}),
|
||||
}),
|
||||
],
|
||||
},
|
||||
);
|
||||
},
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
mounted.push(wrapper);
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(input().value).toBe("狗");
|
||||
|
||||
selected.value = "cat";
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(input().value).toBe("猫");
|
||||
});
|
||||
|
||||
it("skips disabled keys and does not select them", async () => {
|
||||
const onChange = vi.fn();
|
||||
mountCombo(
|
||||
{ disabledKeys: ["cat"], onSelectionChange: onChange },
|
||||
{ items: animals },
|
||||
);
|
||||
await nextTick();
|
||||
input().focus();
|
||||
await press("ArrowDown");
|
||||
expect(
|
||||
document.querySelector('[role="option"]')!.getAttribute("aria-disabled"),
|
||||
).toBe("true");
|
||||
await press("Enter");
|
||||
expect(onChange).toHaveBeenLastCalledWith("dog");
|
||||
});
|
||||
|
||||
it("does not open or select while disabled", async () => {
|
||||
const onChange = vi.fn();
|
||||
mountCombo({ isDisabled: true, onSelectionChange: onChange });
|
||||
await nextTick();
|
||||
input().focus();
|
||||
await press("ArrowDown");
|
||||
expect(input().getAttribute("aria-expanded")).toBe("false");
|
||||
expect(onChange).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps the input read-only without selecting", async () => {
|
||||
const onChange = vi.fn();
|
||||
mountCombo({ isReadOnly: true, onSelectionChange: onChange });
|
||||
await nextTick();
|
||||
expect(input().readOnly).toBe(true);
|
||||
input().focus();
|
||||
await press("ArrowDown");
|
||||
expect(input().getAttribute("aria-expanded")).toBe("false");
|
||||
});
|
||||
|
||||
it("shows multiple values and keeps accepted keys when filtered out", async () => {
|
||||
const selected = ref<(string | number)[]>(["cat", "dog"]);
|
||||
const wrapper = mount(
|
||||
defineComponent({
|
||||
setup() {
|
||||
return () =>
|
||||
h(
|
||||
ComboBox,
|
||||
{
|
||||
"aria-label": "动物",
|
||||
selectionMode: "multiple",
|
||||
modelValue: selected.value,
|
||||
"onUpdate:modelValue": (value: unknown) =>
|
||||
(selected.value = (value ?? []) as (string | number)[]),
|
||||
},
|
||||
{
|
||||
default: () => [
|
||||
h(ComboBoxInputGroup, null, {
|
||||
default: () => [h(Input, {}), h(ComboBoxTrigger)],
|
||||
}),
|
||||
h(ComboBoxValue, { placeholder: "未选择" }),
|
||||
h(ComboBoxPopover, null, {
|
||||
default: () =>
|
||||
h(
|
||||
ListBox,
|
||||
{ selectionMode: "multiple" },
|
||||
{
|
||||
default: () =>
|
||||
animals.map((item) =>
|
||||
h(
|
||||
ListBoxItem,
|
||||
{
|
||||
id: item.id,
|
||||
key: item.id,
|
||||
textValue: item.text,
|
||||
},
|
||||
() => item.text,
|
||||
),
|
||||
),
|
||||
},
|
||||
),
|
||||
}),
|
||||
],
|
||||
},
|
||||
);
|
||||
},
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
mounted.push(wrapper);
|
||||
await nextTick();
|
||||
await settle();
|
||||
const value = () =>
|
||||
document
|
||||
.querySelector('[data-slot="combo-box-value"]')!
|
||||
.textContent?.trim();
|
||||
expect(value()).toBe("猫, 狗");
|
||||
|
||||
input().focus();
|
||||
await type("鸟");
|
||||
expect(value()).toBe("猫, 狗");
|
||||
expect(selected.value).toEqual(["cat", "dog"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("ComboBox form and validation", () => {
|
||||
it("submits the key or the text through a hidden input", async () => {
|
||||
const wrapper = mount(
|
||||
defineComponent({
|
||||
setup() {
|
||||
const submitted = ref("");
|
||||
return () =>
|
||||
h(
|
||||
Form,
|
||||
{
|
||||
onSubmit: (event: Event) => {
|
||||
event.preventDefault();
|
||||
const data = new FormData(
|
||||
event.currentTarget as HTMLFormElement,
|
||||
);
|
||||
submitted.value = `${data.get("animal")}|${data.get("animal-text")}`;
|
||||
},
|
||||
},
|
||||
{
|
||||
default: () => [
|
||||
h(
|
||||
ComboBox,
|
||||
{
|
||||
"aria-label": "key",
|
||||
name: "animal",
|
||||
formValue: "key",
|
||||
defaultSelectedKey: "cat",
|
||||
},
|
||||
{
|
||||
default: () => [
|
||||
h(ComboBoxInputGroup, null, {
|
||||
default: () => [h(Input, {}), h(ComboBoxTrigger)],
|
||||
}),
|
||||
h(ComboBoxPopover, null, {
|
||||
default: () =>
|
||||
h(ListBox, null, {
|
||||
default: () =>
|
||||
animals.map((item) =>
|
||||
h(
|
||||
ListBoxItem,
|
||||
{
|
||||
id: item.id,
|
||||
key: item.id,
|
||||
textValue: item.text,
|
||||
},
|
||||
() => item.text,
|
||||
),
|
||||
),
|
||||
}),
|
||||
}),
|
||||
],
|
||||
},
|
||||
),
|
||||
h(
|
||||
ComboBox,
|
||||
{
|
||||
"aria-label": "text",
|
||||
name: "animal-text",
|
||||
formValue: "text",
|
||||
defaultSelectedKey: "dog",
|
||||
},
|
||||
{
|
||||
default: () => [
|
||||
h(ComboBoxInputGroup, null, {
|
||||
default: () => [h(Input, {}), h(ComboBoxTrigger)],
|
||||
}),
|
||||
h(ComboBoxPopover, null, {
|
||||
default: () =>
|
||||
h(ListBox, null, {
|
||||
default: () =>
|
||||
animals.map((item) =>
|
||||
h(
|
||||
ListBoxItem,
|
||||
{
|
||||
id: item.id,
|
||||
key: item.id,
|
||||
textValue: item.text,
|
||||
},
|
||||
() => item.text,
|
||||
),
|
||||
),
|
||||
}),
|
||||
}),
|
||||
],
|
||||
},
|
||||
),
|
||||
h(
|
||||
"button",
|
||||
{ type: "submit", "data-test": "submit" },
|
||||
"提交",
|
||||
),
|
||||
h("p", { "data-test": "result" }, submitted.value),
|
||||
],
|
||||
},
|
||||
);
|
||||
},
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
mounted.push(wrapper);
|
||||
await nextTick();
|
||||
await settle();
|
||||
(
|
||||
document.querySelector('[data-test="submit"]') as HTMLButtonElement
|
||||
).click();
|
||||
await nextTick();
|
||||
expect(document.querySelector('[data-test="result"]')!.textContent).toBe(
|
||||
"cat|狗",
|
||||
);
|
||||
});
|
||||
|
||||
it("blocks native submit when required and empty", async () => {
|
||||
const wrapper = mount(
|
||||
defineComponent({
|
||||
setup() {
|
||||
const submitted = ref(0);
|
||||
return () =>
|
||||
h(
|
||||
Form,
|
||||
{
|
||||
onSubmit: (event: Event) => {
|
||||
event.preventDefault();
|
||||
submitted.value += 1;
|
||||
},
|
||||
},
|
||||
{
|
||||
default: () => [
|
||||
h(
|
||||
ComboBox,
|
||||
{ "aria-label": "动物", name: "animal", isRequired: true },
|
||||
{
|
||||
default: () => [
|
||||
h(Label, () => "动物"),
|
||||
h(ComboBoxInputGroup, null, {
|
||||
default: () => [h(Input, {}), h(ComboBoxTrigger)],
|
||||
}),
|
||||
h(ComboBoxPopover, null, {
|
||||
default: () =>
|
||||
h(ListBox, null, {
|
||||
default: () =>
|
||||
animals.map((item) =>
|
||||
h(
|
||||
ListBoxItem,
|
||||
{
|
||||
id: item.id,
|
||||
key: item.id,
|
||||
textValue: item.text,
|
||||
},
|
||||
() => item.text,
|
||||
),
|
||||
),
|
||||
}),
|
||||
}),
|
||||
h(FieldError, () => "请选择动物"),
|
||||
],
|
||||
},
|
||||
),
|
||||
h(
|
||||
"button",
|
||||
{ type: "submit", "data-test": "submit" },
|
||||
"提交",
|
||||
),
|
||||
h("p", { "data-test": "submitted" }, String(submitted.value)),
|
||||
],
|
||||
},
|
||||
);
|
||||
},
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
mounted.push(wrapper);
|
||||
await nextTick();
|
||||
await settle();
|
||||
const form = document.querySelector("form")!;
|
||||
const blocked = !form.checkValidity();
|
||||
expect(blocked).toBe(true);
|
||||
expect(input().required).toBe(true);
|
||||
});
|
||||
|
||||
it("surfaces a Description and an ErrorMessage through aria-describedby", async () => {
|
||||
mount(
|
||||
defineComponent({
|
||||
setup() {
|
||||
return () =>
|
||||
h(
|
||||
ComboBox,
|
||||
{ "aria-label": "动物", isInvalid: true },
|
||||
{
|
||||
default: () => [
|
||||
h(ComboBoxInputGroup, null, {
|
||||
default: () => [h(Input, {}), h(ComboBoxTrigger)],
|
||||
}),
|
||||
h(Description, () => "选择一种动物"),
|
||||
h(FieldError, () => "请选择动物"),
|
||||
h(ComboBoxPopover, null, {
|
||||
default: () =>
|
||||
h(ListBox, null, {
|
||||
default: () =>
|
||||
animals.map((item) =>
|
||||
h(
|
||||
ListBoxItem,
|
||||
{
|
||||
id: item.id,
|
||||
key: item.id,
|
||||
textValue: item.text,
|
||||
},
|
||||
() => item.text,
|
||||
),
|
||||
),
|
||||
}),
|
||||
}),
|
||||
],
|
||||
},
|
||||
);
|
||||
},
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await settle();
|
||||
const described = input().getAttribute("aria-describedby") ?? "";
|
||||
const description = document.querySelector('[data-slot="description"]')!;
|
||||
const error = document.querySelector('[data-slot="field-error"]')!;
|
||||
expect(described).toContain(description.id);
|
||||
expect(described).toContain(error.id);
|
||||
});
|
||||
});
|
||||
|
||||
describe("ComboBox menu trigger", () => {
|
||||
it("opens on input only for menu-trigger=input", async () => {
|
||||
mountCombo({ menuTrigger: "input" });
|
||||
await nextTick();
|
||||
input().focus();
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(input().getAttribute("aria-expanded")).toBe("false");
|
||||
await type("猫");
|
||||
expect(input().getAttribute("aria-expanded")).toBe("true");
|
||||
});
|
||||
|
||||
it("stays closed on focus for menu-trigger=manual and opens with ArrowDown", async () => {
|
||||
mountCombo({ menuTrigger: "manual" });
|
||||
await nextTick();
|
||||
input().focus();
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(input().getAttribute("aria-expanded")).toBe("false");
|
||||
await press("ArrowDown");
|
||||
expect(input().getAttribute("aria-expanded")).toBe("true");
|
||||
});
|
||||
|
||||
it("toggles from the trigger button", async () => {
|
||||
mountCombo();
|
||||
await nextTick();
|
||||
const trigger = document.querySelector<HTMLButtonElement>(
|
||||
'[data-slot="combo-box-trigger"]',
|
||||
)!;
|
||||
trigger.dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(input().getAttribute("aria-expanded")).toBe("true");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user