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();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user