fix: anchor public pickers and preserve native Vue refs

This commit is contained in:
maoyingjie
2026-10-25 20:54:57 +08:00
parent 05c2f501a0
commit ff480c5fc3
8 changed files with 161 additions and 59 deletions
+14
View File
@@ -0,0 +1,14 @@
# B02 公开组合接手修复
2026-10-25,起点05c2f50,分支fix/b02-public-integration。当前用户反馈点击没有下拉框,集成人直接接手,不再把同一问题交回另一AI反复返修。
## 已定位与修复
- 原生点击ComboBox触发器后aria-expanded=true、6个option已挂载,但面板top=-496/bottom=-248,停留在Floating首次测量位置。两族只写PopoverRoot.triggerElement(焦点恢复),未注册Popper定位Anchor。
- ComboBoxInputGroup与AutocompleteTrigger使用已有Reka PopoverAnchor asChild,不添加DOM包装、不自建定位。修复后ComboBox实际面板left488/top163/width256,对齐输入left488/top419;空间不足向上翻转。真实点击“狗”成功、关闭、输入显示狗且焦点留在输入。
- Autocomplete默认例是多选,点击California/Texas后应保持展开,并非关闭失败;真实搜索Texas后点击成功、触发器显示Texas,焦点仍为搜索combobox。单选关闭路径单独验证。
- 删除手动解构Vue内部ref持有者的mergedRef,改用cloneVNode第三参数mergeRef=true。新增真实字符串模板ref、Teleport搜索ref与卸载清理回归,保留原有Ref对象用例。
- AutocompleteTrigger显式disabled参与实际激活守卫与ARIA/tabindex。
- 国际化过滤改为Intl.Collator(search/base),支持土耳其语İ/i、I/ı及德语ß/ss,不再先去除组合符而破坏locale含义;自定义filter优先级不变。
新增8项集成/locale回归,原30项复核探针和两族行为套件合计93项通过。最终集成/产物/浏览器结果由本批最终验收记录补充。之前“floating选项not actionable”的记录保留为历史,不再归因工具环境;只验挂载/aria-expanded或CSS文本不足以证明下拉框可用。
+1 -1
View File
@@ -15,7 +15,7 @@
"build:docs": "pnpm build && pnpm --filter @heroui-vue/docs build",
"typecheck": "vue-tsc --noEmit",
"test": "vitest run",
"test:unit": "vitest run tests/choice-demos.test.ts tests/choice-fields.test.ts tests/fieldset-search-field.test.ts tests/components.test.ts tests/composition.test.ts tests/docs-search.test.ts tests/button-demos.test.ts tests/aligned-components.test.ts tests/surface-link-spinner.test.ts tests/close-button-avatar-skeleton.test.ts tests/separator-kbd-chip.test.ts tests/badge-alert.test.ts tests/input-group.test.ts tests/field-docs-textarea.test.ts tests/field-foundation.test.ts tests/typography-empty-breadcrumbs.test.ts tests/progress-meter.test.ts tests/disclosure-accordion.test.ts tests/overlay-foundation.test.ts tests/modal.test.ts tests/popover-tooltip.test.ts tests/overlay-demos.test.ts tests/scroll-tabs.test.ts tests/button-toggle.test.ts tests/m26-review.probe.test.ts tests/toolbar.test.ts tests/m29-review.probe.test.ts tests/m30-m32-review.probe.test.ts tests/pagination.test.ts tests/header.test.ts tests/input-otp.test.ts tests/number-field-foundation.test.ts tests/number-field.test.ts tests/slider-foundation.test.ts tests/slider.test.ts tests/collection-state.test.ts tests/collection-foundation.test.ts tests/list-box.test.ts tests/alert-dialog.test.ts tests/drawer.test.ts tests/m33-m34-review.probe.test.ts tests/m33-m34-lifecycle-review.probe.test.ts tests/m33-m34-coexist.test.ts tests/select.test.ts tests/date-value.test.ts tests/date-model.test.ts tests/date-foundation.test.ts tests/date-field.test.ts tests/date-field-demos.test.ts tests/toast.test.ts tests/table.test.ts tests/m35-m36-takeover.test.ts tests/m35-m36-review.probe.test.ts tests/m35-m36-review-2.probe.test.ts tests/m35-m36-review-3.probe.test.ts tests/m35-m36-review-4.probe.test.ts tests/m35-m36-review-5.probe.test.ts tests/m35-m36-hydration.test.ts tests/docs-shell.test.ts tests/b01-menu-dropdown.test.ts tests/b01-review.probe.test.ts tests/b01-complete-review.probe.test.ts tests/cssom.test.ts tests/tag-error-message.test.ts tests/collection-filter-virtual.test.ts tests/b02a-review.probe.test.ts tests/b02a-round2-review.probe.test.ts tests/b02-review.probe.test.ts tests/combo-box.test.ts tests/combo-box-demos.test.ts tests/autocomplete.test.ts tests/autocomplete-demos.test.ts",
"test:unit": "vitest run tests/choice-demos.test.ts tests/choice-fields.test.ts tests/fieldset-search-field.test.ts tests/components.test.ts tests/composition.test.ts tests/docs-search.test.ts tests/button-demos.test.ts tests/aligned-components.test.ts tests/surface-link-spinner.test.ts tests/close-button-avatar-skeleton.test.ts tests/separator-kbd-chip.test.ts tests/badge-alert.test.ts tests/input-group.test.ts tests/field-docs-textarea.test.ts tests/field-foundation.test.ts tests/typography-empty-breadcrumbs.test.ts tests/progress-meter.test.ts tests/disclosure-accordion.test.ts tests/overlay-foundation.test.ts tests/modal.test.ts tests/popover-tooltip.test.ts tests/overlay-demos.test.ts tests/scroll-tabs.test.ts tests/button-toggle.test.ts tests/m26-review.probe.test.ts tests/toolbar.test.ts tests/m29-review.probe.test.ts tests/m30-m32-review.probe.test.ts tests/pagination.test.ts tests/header.test.ts tests/input-otp.test.ts tests/number-field-foundation.test.ts tests/number-field.test.ts tests/slider-foundation.test.ts tests/slider.test.ts tests/collection-state.test.ts tests/collection-foundation.test.ts tests/list-box.test.ts tests/alert-dialog.test.ts tests/drawer.test.ts tests/m33-m34-review.probe.test.ts tests/m33-m34-lifecycle-review.probe.test.ts tests/m33-m34-coexist.test.ts tests/select.test.ts tests/date-value.test.ts tests/date-model.test.ts tests/date-foundation.test.ts tests/date-field.test.ts tests/date-field-demos.test.ts tests/toast.test.ts tests/table.test.ts tests/m35-m36-takeover.test.ts tests/m35-m36-review.probe.test.ts tests/m35-m36-review-2.probe.test.ts tests/m35-m36-review-3.probe.test.ts tests/m35-m36-review-4.probe.test.ts tests/m35-m36-review-5.probe.test.ts tests/m35-m36-hydration.test.ts tests/docs-shell.test.ts tests/b01-menu-dropdown.test.ts tests/b01-review.probe.test.ts tests/b01-complete-review.probe.test.ts tests/cssom.test.ts tests/tag-error-message.test.ts tests/collection-filter-virtual.test.ts tests/b02a-review.probe.test.ts tests/b02a-round2-review.probe.test.ts tests/b02-review.probe.test.ts tests/combo-box.test.ts tests/combo-box-demos.test.ts tests/autocomplete.test.ts tests/autocomplete-demos.test.ts tests/b02-public-integration.test.ts",
"test:ssr": "vitest run tests/ssr.test.ts tests/input-otp-ssr.test.ts tests/slider-foundation-ssr.test.ts tests/slider-ssr.test.ts tests/collection-foundation-ssr.test.ts tests/list-box-ssr.test.ts tests/select-ssr.test.ts tests/date-foundation-ssr.test.ts tests/date-field-ssr.test.ts tests/m35-m36-ssr.test.ts",
"format": "prettier --check .",
"format:fix": "prettier --write .",
@@ -19,7 +19,7 @@ import {
} from "vue";
import { useChoiceValue } from "../choice/shared";
import { useAutocomplete } from "./context";
import { elementFromRef, mergedRef } from "../combo-box/dom";
import { elementFromRef } from "../combo-box/dom";
function componentName(node: VNode): string | undefined {
const type = node.type as Record<string, unknown> | undefined;
@@ -83,11 +83,11 @@ export default defineComponent({
composing.value = true;
},
onCompositionend: () => commitComposition(),
// cloneVNode only honours a ref passed through its extra props.
ref: mergedRef(child.ref, (instance: unknown) => {
// Let Vue preserve the caller's normalized template ref.
ref: (instance: unknown) => {
ctx.filterInput.value = elementFromRef(instance) as
HTMLInputElement | undefined;
}),
},
};
// The search input's own accessible name and description win over the
// picker root's: the trigger name is not a name for this control.
@@ -98,7 +98,7 @@ export default defineComponent({
]) {
if (own[key] !== undefined) delete injected[key];
}
return cloneVNode(child, injected);
return cloneVNode(child, injected, true);
}
const children = child.children;
if (Array.isArray(children)) {
@@ -1,7 +1,7 @@
<script lang="ts">
/** Clickable value surface: opens the popover and is the single Tab entry. */
import { defineComponent, h, mergeProps, ref, watch } from "vue";
import { injectPopoverRootContext } from "reka-ui";
import { PopoverAnchor, injectPopoverRootContext } from "reka-ui";
import { useAutocomplete } from "./context";
import { elementFromRef } from "../combo-box/dom";
@@ -13,7 +13,10 @@ export default defineComponent({
const ctx = useAutocomplete();
const element = ref<HTMLElement>();
const popover = injectPopoverRootContext();
const disabled = () => props.isDisabled || ctx.disabled.value;
const disabled = () =>
props.isDisabled ||
ctx.disabled.value ||
(attrs.disabled !== undefined && attrs.disabled !== false);
watch(
element,
@@ -79,7 +82,14 @@ export default defineComponent({
injected["data-disabled"] = true;
injected["aria-disabled"] = true;
}
return h("div", mergeProps(attrs, injected), slots.default?.());
return h(
PopoverAnchor,
{ asChild: true },
{
default: () =>
h("div", mergeProps(attrs, injected), slots.default?.()),
},
);
};
},
});
+16 -10
View File
@@ -1,15 +1,21 @@
/**
* Case- and accent-insensitive substring match, the equivalent of ICU
* "base" sensitivity for search: `Café` matches `cafe`, `STRASSE` matches
* `strasse` after compatibility normalisation.
*/
const fold = (value: string) => value.normalize("NFKD").replace(/\p{Mn}/gu, "");
/** Locale-aware base-sensitivity substring matching; no hand-written accent folding. */
export function createBaseContains(locale?: string) {
const collator = new Intl.Collator(locale, {
usage: "search",
sensitivity: "base",
});
return (text: string, query: string) => {
if (!query) return true;
return fold(text)
.toLocaleLowerCase(locale)
.includes(fold(query).toLocaleLowerCase(locale));
const characters = Array.from(text.normalize("NFC"));
const needle = query.normalize("NFC");
// Collation expansions (ß/ss, ligatures) need not have equal code-point lengths.
for (let start = 0; start < characters.length; start++) {
let candidate = "";
for (let end = start; end < characters.length; end++) {
candidate += characters[end];
if (collator.compare(candidate, needle) === 0) return true;
}
}
return false;
};
}
@@ -18,9 +18,9 @@ import {
type VNode,
type VNodeChild,
} from "vue";
import { injectPopoverRootContext } from "reka-ui";
import { PopoverAnchor, injectPopoverRootContext } from "reka-ui";
import { useComboBox } from "./context";
import { elementFromRef, mergedRef } from "./dom";
import { elementFromRef } from "./dom";
function componentName(node: VNode): string | undefined {
const type = node.type as Record<string, unknown> | undefined;
@@ -67,16 +67,15 @@ export default defineComponent({
"onUpdate:modelValue": (next: string) => ctx.setInputValue(next),
// The root variant is inherited; an explicit Input variant still wins.
variant: own.variant ?? ctx.variant(),
// cloneVNode only honours a ref passed through its extra props, so the
// caller's template ref and ours are merged here.
ref: mergedRef(node.ref, (instance: unknown) => {
// Vue merges normalized refs, including string/useTemplateRef/ref_for.
ref: (instance: unknown) => {
ctx.inputElement.value = elementFromRef(instance) as
HTMLInputElement | undefined;
}),
},
};
for (const key of Object.keys(injected))
if (injected[key] === undefined) delete injected[key];
return cloneVNode(node, injected);
return cloneVNode(node, injected, true);
}
return () => {
@@ -85,14 +84,21 @@ export default defineComponent({
Array.isArray(rendered) ? rendered : [rendered]
) as VNodeChild[];
return h(
"div",
mergeProps(attrs, {
ref: root,
class: ctx.slots.value.inputGroup(),
"data-slot": "combo-box-input-group",
"data-disabled": ctx.disabled.value || undefined,
}),
children.map(compose),
PopoverAnchor,
{ asChild: true },
{
default: () =>
h(
"div",
mergeProps(attrs, {
ref: root,
class: ctx.slots.value.inputGroup(),
"data-slot": "combo-box-input-group",
"data-disabled": ctx.disabled.value || undefined,
}),
children.map(compose),
),
},
);
};
},
@@ -6,31 +6,6 @@ export function elementFromRef(value: unknown): HTMLElement | undefined {
return element instanceof Element ? (element as HTMLElement) : undefined;
}
/**
* Keep both the caller's template ref and our internal one. Vue replaces a
* cloned vnode's ref, so the merge has to happen here; Vue 3.5 also stores
* component refs inside a `{ i, r }` holder that must be unwrapped.
*/
export function mergedRef(...refs: unknown[]) {
const assign = (ref: unknown, instance: unknown) => {
if (!ref) return;
if (typeof ref === "function") {
ref(instance);
return;
}
if (typeof ref !== "object") return;
if ("value" in (ref as object)) {
(ref as { value: unknown }).value = instance;
return;
}
const inner = (ref as { r?: unknown }).r;
if (inner !== undefined) assign(inner, instance);
};
return (instance: unknown) => {
for (const ref of refs) assign(ref, instance);
};
}
/**
* Vue vnode shape bits (not exported by the package). `cloneVNode` copies
* `shapeFlag`, so replacing an element's children needs these to stay coherent.
+91
View File
@@ -0,0 +1,91 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent } from "vue";
import {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Input,
ListBox,
ListBoxItem,
Autocomplete,
AutocompleteTrigger,
AutocompletePopover,
AutocompleteFilter,
SearchField,
SearchFieldInput,
} from "../packages/vue/src";
import { createBaseContains } from "../packages/vue/src/components/collection/match";
const components = {
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
Input,
ListBox,
ListBoxItem,
Autocomplete,
AutocompleteTrigger,
AutocompletePopover,
AutocompleteFilter,
SearchField,
SearchFieldInput,
};
const roots: ReturnType<typeof mount>[] = [];
afterEach(() => {
roots.splice(0).forEach((w) => w.unmount());
document.body.innerHTML = "";
vi.restoreAllMocks();
});
function view(template: string) {
const w = mount(defineComponent({ components, template }), {
attachTo: document.body,
});
roots.push(w);
return w;
}
describe("public picker integration", () => {
it("retains Vue string template refs on the actual composed Input and clears them on removal", async () => {
const w = view(
'<ComboBox><ComboBoxInputGroup><Input ref="consumerInput"/><ComboBoxTrigger/></ComboBoxInputGroup><ComboBoxPopover><ListBox><ListBoxItem id="a">Alpha</ListBoxItem></ListBox></ComboBoxPopover></ComboBox>',
);
await flushPromises();
expect((w.vm.$refs.consumerInput as { $el: Element }).$el).toBe(
w.get("input[role=combobox]").element,
);
w.unmount();
roots.splice(roots.indexOf(w), 1);
expect(w.vm.$refs.consumerInput).toBeNull();
});
it("retains a string template ref on the teleported search input", async () => {
const w = view(
'<Autocomplete default-open><AutocompleteTrigger>Choose</AutocompleteTrigger><AutocompletePopover><AutocompleteFilter><SearchField><SearchFieldInput ref="consumerSearch"/></SearchField><ListBox><ListBoxItem id="a">Alpha</ListBoxItem></ListBox></AutocompleteFilter></AutocompletePopover></Autocomplete>',
);
await flushPromises();
expect((w.vm.$refs.consumerSearch as { $el: Element }).$el).toBe(
document.querySelector("input[role=combobox]"),
);
});
it("respects a caller-disabled Autocomplete trigger before opening", async () => {
const w = view(
'<Autocomplete><AutocompleteTrigger disabled>Choose</AutocompleteTrigger><AutocompletePopover><ListBox><ListBoxItem id="a">Alpha</ListBoxItem></ListBox></AutocompletePopover></Autocomplete>',
);
await w.get('[data-slot="autocomplete-trigger"]').trigger("click");
expect(
w.get('[data-slot="autocomplete-trigger"]').attributes("aria-expanded"),
).toBe("false");
expect(
w.get('[data-slot="autocomplete-trigger"]').attributes("aria-disabled"),
).toBe("true");
});
});
it.each([
["tr", "İstanbul", "istanbul", true],
["tr", "Isparta", "ısparta", true],
["tr", "Isparta", "isparta", false],
["de", "Fußgänger", "fuss", true],
["en", "Café", "cafe\u0301", true],
])("locale filter %s: %s / %s", (locale, text, query, expected) => {
expect(createBaseContains(locale)(text, query)).toBe(expected);
});