fix(pickers): rework B02 against the public review
Restore native clearing, unify the form/field protocol with the other choice families (per-key entries, disabled and removed keys omitted, shared form owner), synchronise ComboBox text by key identity, keep filtering out of open composition sessions, and stop stealing focus when a controlled close is rejected. Also merge caller refs/ARIA/disabled, implement asChild, autoFocus and ComboBox variant, use a locale-aware base matcher, and put the indicator class and slot on the icon element the ported CSS selects.
This commit is contained in:
@@ -713,3 +713,186 @@ describe("ComboBox ported style hooks", () => {
|
||||
).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("ComboBox filtering and composition", () => {
|
||||
it("inherits an accent-insensitive base matcher", async () => {
|
||||
mountCombo({}, { items: [{ id: "cafe", text: "Café" }] });
|
||||
await nextTick();
|
||||
input().focus();
|
||||
await nextTick();
|
||||
await settle();
|
||||
|
||||
await type("cafe");
|
||||
expect(rowTexts()).toEqual(["Café"]);
|
||||
await type("CAFÉ");
|
||||
expect(rowTexts()).toEqual(["Café"]);
|
||||
// Decomposed input still matches the precomposed label.
|
||||
await type("cafe\u0301");
|
||||
expect(rowTexts()).toEqual(["Café"]);
|
||||
});
|
||||
|
||||
it("lets a custom defaultFilter win over the built-in matcher", async () => {
|
||||
const defaultFilter = vi.fn(
|
||||
(text: string, query: string) => !query || text === query,
|
||||
);
|
||||
mountCombo({ defaultFilter });
|
||||
await nextTick();
|
||||
input().focus();
|
||||
await nextTick();
|
||||
await settle();
|
||||
|
||||
await type("猫");
|
||||
expect(defaultFilter).toHaveBeenCalled();
|
||||
expect(rowTexts()).toEqual(["猫"]);
|
||||
// The built-in contains() would match "猫" inside "熊猫"; the custom one does not.
|
||||
await type("熊");
|
||||
expect(rowTexts()).toEqual([]);
|
||||
});
|
||||
|
||||
it("filters only after an open composition session ends", async () => {
|
||||
mountCombo();
|
||||
await nextTick();
|
||||
input().focus();
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(rowTexts()).toHaveLength(4);
|
||||
|
||||
input().dispatchEvent(new CompositionEvent("compositionstart"));
|
||||
await nextTick();
|
||||
await type("狗");
|
||||
expect(rowTexts()).toHaveLength(4);
|
||||
|
||||
input().dispatchEvent(new CompositionEvent("compositionend"));
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(rowTexts()).toEqual(["狗"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("ComboBox field protocol", () => {
|
||||
it("lets the user clear a required value, then blocks the submit", async () => {
|
||||
const update = vi.fn();
|
||||
mountCombo({
|
||||
name: "animal",
|
||||
defaultValue: "cat",
|
||||
isRequired: true,
|
||||
"onUpdate:modelValue": update,
|
||||
});
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(input().value).toBe("猫");
|
||||
|
||||
await type("");
|
||||
expect(update).toHaveBeenLastCalledWith(null);
|
||||
expect(input().value).toBe("");
|
||||
// The emptied field is invalid for native validation, not un-editable.
|
||||
expect(input().checkValidity()).toBe(false);
|
||||
});
|
||||
|
||||
it("associates the input and its reset handling with an external form", async () => {
|
||||
const external = document.createElement("form");
|
||||
external.id = "external-form";
|
||||
document.body.appendChild(external);
|
||||
mountCombo({
|
||||
name: "animal",
|
||||
form: "external-form",
|
||||
defaultValue: "cat",
|
||||
});
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(input().form).toBe(external);
|
||||
expect(new FormData(external).get("animal")).toBe("cat");
|
||||
|
||||
// An accepted reset restores the initial value through the same owner.
|
||||
const reset = new Event("reset", { cancelable: true });
|
||||
external.dispatchEvent(reset);
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(new FormData(external).get("animal")).toBe("cat");
|
||||
|
||||
// A cancelled reset leaves the field untouched.
|
||||
const cancelled = new Event("reset", { cancelable: true });
|
||||
cancelled.preventDefault();
|
||||
external.dispatchEvent(cancelled);
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(input().value).toBe("猫");
|
||||
external.remove();
|
||||
});
|
||||
|
||||
it("submits no entry for a disabled field", async () => {
|
||||
mountCombo({ name: "animal", defaultValue: "cat", isDisabled: true });
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(
|
||||
document.querySelector('[data-slot="combo-box-form-value"]'),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("ComboBox asChild, variant and autoFocus", () => {
|
||||
it("keeps the caller's root element, handlers and ref", async () => {
|
||||
const onClick = vi.fn();
|
||||
const hostRef = ref<HTMLElement>();
|
||||
const wrapper = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(
|
||||
ComboBox,
|
||||
{ asChild: true },
|
||||
{
|
||||
default: () =>
|
||||
h(
|
||||
"section",
|
||||
{ ref: hostRef, "data-test": "host", onClick },
|
||||
"picker",
|
||||
),
|
||||
},
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
const host = wrapper.element as HTMLElement;
|
||||
expect(host.tagName).toBe("SECTION");
|
||||
expect(host.getAttribute("data-slot")).toBe("combo-box");
|
||||
expect(host.classList.contains("hv-combo-box")).toBe(true);
|
||||
expect(host.textContent).toContain("picker");
|
||||
expect(hostRef.value).toBe(host);
|
||||
host.dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
||||
expect(onClick).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("lets an explicit Input variant override the inherited one", async () => {
|
||||
mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(
|
||||
ComboBox,
|
||||
{ variant: "secondary" },
|
||||
{
|
||||
default: () => [
|
||||
h(ComboBoxInputGroup, null, {
|
||||
default: () => [
|
||||
h(Input, { variant: "primary" }),
|
||||
h(ComboBoxTrigger),
|
||||
],
|
||||
}),
|
||||
],
|
||||
},
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
expect(input().classList.contains("hv-input--primary")).toBe(true);
|
||||
expect(input().classList.contains("hv-input--secondary")).toBe(false);
|
||||
});
|
||||
|
||||
it("skips autoFocus when the field is disabled", async () => {
|
||||
mountCombo({ autoFocus: true, isDisabled: true });
|
||||
await nextTick();
|
||||
await settle();
|
||||
expect(document.activeElement).not.toBe(input());
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user