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:
maoyingjie
2026-10-25 20:40:24 +08:00
parent 3e9be13b19
commit e34b9debdf
24 changed files with 1808 additions and 236 deletions
+206 -1
View File
@@ -683,9 +683,11 @@ describe("Autocomplete ported style hooks", () => {
await nextTick();
await settle();
// The class, the `data-slot` and `data-open` must share one element, or the
// ported `.hv-autocomplete__indicator[...]` rules never match.
expect(
document.querySelector(
'[data-slot="autocomplete-indicator"] [data-slot="autocomplete-default-indicator"]',
'.hv-autocomplete__indicator[data-slot="autocomplete-default-indicator"]',
),
).not.toBeNull();
expect(
@@ -693,3 +695,206 @@ describe("Autocomplete ported style hooks", () => {
).not.toBeNull();
});
});
describe("Autocomplete filter protocol", () => {
it("inherits an accent-insensitive base matcher", async () => {
track(
shell(
{},
{ states: [{ id: "cafe", name: "Café" }] as unknown as State[] },
),
);
await nextTick();
await open();
await type("cafe");
expect(rowTexts()).toEqual(["Café"]);
await type("CAFÉ");
expect(rowTexts()).toEqual(["Café"]);
await type("cafe\u0301");
expect(rowTexts()).toEqual(["Café"]);
});
it("filters only after an open composition session ends", async () => {
track(shell({}));
await nextTick();
await open();
expect(rowTexts()).toHaveLength(3);
search().dispatchEvent(new CompositionEvent("compositionstart"));
await nextTick();
await type("Texas");
expect(rowTexts()).toHaveLength(3);
search().dispatchEvent(new CompositionEvent("compositionend"));
await nextTick();
await settle();
expect(rowTexts()).toEqual(["Texas"]);
});
});
describe("Autocomplete native layer", () => {
it("keeps the caller's cancel on the clear button", async () => {
const change = vi.fn();
const wrapper = mount(
defineComponent({
setup: () => () =>
h(
Autocomplete,
{ defaultValue: "a", onChange: change },
{
default: () => [
h(AutocompleteTrigger, null, {
default: () => [
h(AutocompleteValue),
h(AutocompleteClearButton, {
onClick: (event: MouseEvent) => event.preventDefault(),
}),
],
}),
h(AutocompletePopover, null, {
default: () =>
h(AutocompleteFilter, null, {
default: () =>
h(ListBox, null, {
default: () =>
states.map((state) =>
h(
ListBoxItem,
{
id: state.id,
key: state.id,
textValue: state.name,
},
() => state.name,
),
),
}),
}),
}),
],
},
),
}),
{ attachTo: document.body },
);
track(wrapper as never);
await nextTick();
await settle();
document
.querySelector<HTMLButtonElement>(
'[data-slot="autocomplete-clear-button"]',
)!
.click();
await settle();
expect(change).not.toHaveBeenCalled();
});
it("submits one entry per key and none for a disabled field", async () => {
const multi = mount(
defineComponent({
setup: () => () =>
h("form", null, [
h(Autocomplete, {
name: "choice",
selectionMode: "multiple",
defaultValue: ["california", "texas"],
}),
]),
}),
{ attachTo: document.body },
);
track(multi as never);
await nextTick();
await settle();
const form = multi.element as HTMLFormElement;
expect(new FormData(form).getAll("choice")).toEqual([
"california",
"texas",
]);
multi.unmount();
mounted.pop();
const disabled = mount(
defineComponent({
setup: () => () =>
h("form", null, [
h(Autocomplete, {
name: "choice",
defaultValue: "california",
isDisabled: true,
}),
]),
}),
{ attachTo: document.body },
);
track(disabled as never);
await nextTick();
await settle();
expect(
new FormData(disabled.element as HTMLFormElement).has("choice"),
).toBe(false);
});
it("maps the source items prop onto the closed-state text", async () => {
track(
shell({
items: [
{ id: "california", textValue: "California" },
{ id: "texas", textValue: "Texas" },
],
defaultValue: "texas",
}),
);
await nextTick();
await settle();
expect(valueText()).toBe("Texas");
});
it("tags a custom indicator with the baseline slot contract", async () => {
track(
mount(
defineComponent({
setup: () => () =>
h(Autocomplete, null, {
default: () => [
h(AutocompleteTrigger, null, {
default: () => [
h(AutocompleteValue),
h(AutocompleteIndicator, null, {
default: () => h("i", { "data-test": "icon" }),
}),
],
}),
h(AutocompletePopover, null, {
default: () =>
h(AutocompleteFilter, null, {
default: () =>
h(ListBox, null, {
default: () =>
states.map((state) =>
h(
ListBoxItem,
{
id: state.id,
key: state.id,
textValue: state.name,
},
() => state.name,
),
),
}),
}),
}),
],
}),
}),
{ attachTo: document.body },
),
);
await nextTick();
await settle();
const custom = document.querySelector('[data-test="icon"]')!;
expect(custom.getAttribute("data-slot")).toBe("autocomplete-indicator");
expect(custom.classList.contains("hv-autocomplete__indicator")).toBe(true);
});
});