fix: preserve nested search names and add browser consumption fixture

This commit is contained in:
maoyingjie
2026-10-25 21:08:28 +08:00
parent 647c39035d
commit b129f79366
7 changed files with 253 additions and 6 deletions
@@ -68,10 +68,19 @@ export default defineComponent({
ctx.filterText.value = text(); ctx.filterText.value = text();
} }
function compose(nodes: VNodeChild[]): VNodeChild[] { function compose(
nodes: VNodeChild[],
inheritedAria: readonly string[] = [],
): VNodeChild[] {
return nodes.map((child) => { return nodes.map((child) => {
if (!isVNode(child)) return child; if (!isVNode(child)) return child;
const name = componentName(child); const name = componentName(child);
const protectedAria =
name === "SearchField"
? ["aria-label", "aria-labelledby", "aria-describedby"].filter(
(key) => child.props?.[key] !== undefined,
)
: inheritedAria;
if (name === "ListBox") return child; if (name === "ListBox") return child;
if (name === "SearchFieldInput") { if (name === "SearchFieldInput") {
const own = (child.props ?? {}) as Record<string, unknown>; const own = (child.props ?? {}) as Record<string, unknown>;
@@ -96,14 +105,18 @@ export default defineComponent({
"aria-labelledby", "aria-labelledby",
"aria-describedby", "aria-describedby",
]) { ]) {
if (own[key] !== undefined) delete injected[key]; if (own[key] !== undefined || protectedAria.includes(key))
delete injected[key];
} }
return cloneVNode(child, injected, true); return cloneVNode(child, injected, true);
} }
const children = child.children; const children = child.children;
if (Array.isArray(children)) { if (Array.isArray(children)) {
const clone = cloneVNode(child); const clone = cloneVNode(child);
clone.children = compose(children as VNodeChild[]) as never; clone.children = compose(
children as VNodeChild[],
protectedAria,
) as never;
return clone; return clone;
} }
if (children && typeof children === "object") { if (children && typeof children === "object") {
@@ -116,7 +129,10 @@ export default defineComponent({
clone.children = { clone.children = {
...withDefault, ...withDefault,
default: () => default: () =>
compose(([withDefault.default!()] as VNodeChild[]).flat()), compose(
([withDefault.default!()] as VNodeChild[]).flat(),
protectedAria,
),
} as never; } as never;
return clone; return clone;
} }
+2 -2
View File
@@ -644,13 +644,13 @@ describe("Autocomplete form and validation", () => {
}); });
describe("Autocomplete composition", () => { describe("Autocomplete composition", () => {
it("keeps a caller-provided aria-label on the trigger and search box", async () => { it("keeps separate caller-provided names on the trigger and inner SearchField", async () => {
track(shell({})); track(shell({}));
await nextTick(); await nextTick();
await settle(); await settle();
expect(trigger().getAttribute("aria-label")).toBe("州"); expect(trigger().getAttribute("aria-label")).toBe("州");
await open(); await open();
expect(search().getAttribute("aria-label")).toBe("州"); expect(search().getAttribute("aria-label")).toBe("搜索州名");
}); });
it("mounts as a reusable component surface", async () => { it("mounts as a reusable component surface", async () => {
+11
View File
@@ -67,6 +67,17 @@ describe("public picker integration", () => {
document.querySelector("input[role=combobox]"), document.querySelector("input[role=combobox]"),
); );
}); });
it("preserves an accessible name declared on the inner SearchField root", async () => {
view(
'<Autocomplete default-open aria-label="Picker"><AutocompleteTrigger>Choose</AutocompleteTrigger><AutocompletePopover><AutocompleteFilter><SearchField aria-label="Search options"><SearchFieldInput/></SearchField><ListBox><ListBoxItem id="a">Alpha</ListBoxItem></ListBox></AutocompleteFilter></AutocompletePopover></Autocomplete>',
);
await flushPromises();
expect(
document
.querySelector("input[role=combobox]")
?.getAttribute("aria-label"),
).toBe("Search options");
});
it("respects a caller-disabled Autocomplete trigger before opening", async () => { it("respects a caller-disabled Autocomplete trigger before opening", async () => {
const w = view( const w = view(
'<Autocomplete><AutocompleteTrigger disabled>Choose</AutocompleteTrigger><AutocompletePopover><ListBox><ListBoxItem id="a">Alpha</ListBoxItem></ListBox></AutocompletePopover></Autocomplete>', '<Autocomplete><AutocompleteTrigger disabled>Choose</AutocompleteTrigger><AutocompletePopover><ListBox><ListBoxItem id="a">Alpha</ListBoxItem></ListBox></AutocompletePopover></Autocomplete>',
+12
View File
@@ -0,0 +1,12 @@
# B02 独立浏览器消费
将本目录内容复制到工作区外的独立目录。安装最终 pnpm pack 产生的 components/styles 两份 tarball、Vue与Vite;pnpm-workspace.yaml的styles override指向该样式tarball。不能配置指向源码的alias。main.js只从包根/发布CSS导入,使用render函数不依赖Vue模板编译器。
运行 `pnpm exec vite --host 127.0.0.1 --port 5184 --strictPort`(先确认空闲;也可使用已安装的Vite二进制作为开发工具),打开实际返回URL。
1. 点ComboBox箭头,面板在视口内/锚点旁,鼠标点狗后关闭、文本狗、焦点留在输入。
2. 点Autocomplete,搜索Texas,鼠标选择后单选关闭;点清除后恢复占位,焦点与Tab/外部点击不反跳。
3. 长列表展开,按End,活动Row99实际在受限滚动区可见;不是仅DOM/aria-activedescendant存在。
4. 嵌套Modal内展开列表,两次Escape分别关闭列表/Modal,焦点恢复。快速关闭重开同样可用。
记录真实坐标、滚动、焦点与截图;不能因DOM已挂载或CSS文本存在就通过。构建产物还应执行 `vite build`,类型正反例使用上级b02-pickers-contract.ts两种解析模式。
+12
View File
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>B02 独立产物验收</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/main.js"></script>
</body>
</html>
+165
View File
@@ -0,0 +1,165 @@
import { createApp, defineComponent, h } from "vue";
import {
Autocomplete,
AutocompleteTrigger,
AutocompleteValue,
AutocompleteClearButton,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteFilter,
SearchField,
SearchFieldInput,
ComboBox,
ComboBoxInputGroup,
ComboBoxTrigger,
ComboBoxPopover,
Input,
ListBox,
ListBoxItem,
Button,
Modal,
ModalBackdrop,
ModalContainer,
ModalDialog,
ModalHeading,
} from "@heroui-vue/components";
import "@heroui-vue/styles/css";
import "./style.css";
const animals = [
{ id: "cat", textValue: "猫" },
{ id: "dog", textValue: "狗" },
{ id: "bird", textValue: "鸟" },
];
const states = [
{ id: "california", textValue: "California" },
{ id: "texas", textValue: "Texas" },
{ id: "florida", textValue: "Florida" },
];
const rows = (items) =>
h(
ListBox,
{ items },
{
default: ({ item }) =>
h(
ListBoxItem,
{ id: item.id, textValue: item.textValue, key: item.id },
() => item.textValue,
),
},
);
function combo(label, long = false) {
const items = long
? Array.from({ length: 100 }, (_, i) => ({ id: i, textValue: `Row ${i}` }))
: animals;
return h(
ComboBox,
{ "aria-label": label, items, style: { width: "280px" } },
{
default: () => [
h(ComboBoxInputGroup, null, {
default: () => [h(Input, { placeholder: label }), h(ComboBoxTrigger)],
}),
h(
ComboBoxPopover,
{ style: { maxHeight: "180px", overflowY: "auto" } },
{ default: () => rows(items) },
),
],
},
);
}
function autocomplete() {
return h(
Autocomplete,
{
"aria-label": "独立 Autocomplete",
name: "state",
items: states,
defaultValue: "california",
style: { width: "280px" },
},
{
default: () => [
h(AutocompleteTrigger, null, {
default: () => [
h(AutocompleteValue),
h(AutocompleteClearButton),
h(AutocompleteIndicator),
],
}),
h(AutocompletePopover, null, {
default: () =>
h(AutocompleteFilter, null, {
default: () => [
h(
SearchField,
{ "aria-label": "独立搜索" },
{
default: () =>
h(SearchFieldInput, { placeholder: "搜索州" }),
},
),
rows(states),
],
}),
}),
],
},
);
}
createApp(
defineComponent({
setup() {
return () =>
h("main", [
h("h1", "B02 独立产物验收"),
h(
"p",
"Vue 组件和 CSS 来自两份最终 tarball;没有指向工作区源码的 alias。",
),
h("section", [
h("h2", "ComboBox:点击、选择、关闭和焦点"),
combo("独立 ComboBox"),
]),
h("section", [
h("h2", "Autocomplete:搜索、单选和清空"),
autocomplete(),
]),
h("section", [
h("h2", "长列表:End 后活动项应可见"),
combo("长列表", true),
]),
h(Modal, null, {
default: () => [
h(Button, null, () => "打开嵌套验收"),
h(ModalBackdrop, null, {
default: () =>
h(ModalContainer, null, {
default: () =>
h(
ModalDialog,
{
"aria-label": "嵌套选择验收",
style: { width: "400px" },
},
{
default: () => [
h(ModalHeading, null, () => "嵌套选择验收"),
combo("模态框内 ComboBox"),
h(
"p",
"第一次 Escape 关闭列表,第二次关闭模态框。",
),
],
},
),
}),
}),
],
}),
h("button", { type: "button", id: "outside" }, "外部焦点目标"),
]);
},
}),
).mount("#app");
+31
View File
@@ -0,0 +1,31 @@
body {
margin: 0;
padding: 32px;
background: var(--background);
color: var(--foreground);
font-family: system-ui, sans-serif;
}
main {
max-width: 820px;
margin: auto;
}
h1 {
font-size: 24px;
font-weight: 650;
margin-bottom: 12px;
}
h2 {
font-size: 16px;
font-weight: 600;
margin-bottom: 16px;
}
section {
padding: 24px;
margin: 24px 0;
border: 1px solid var(--border);
border-radius: 20px;
}
#outside {
display: block;
margin-top: 24px;
}