fix: complete TagGroup semantics event ownership and removal focus

This commit is contained in:
maoyingjie
2026-10-25 14:34:07 +08:00
parent af6bfda3ee
commit 20d34d7169
12 changed files with 528 additions and 53 deletions
+292
View File
@@ -987,6 +987,7 @@ describe("Tag demos and documentation", () => {
...document.matchAll(/<ComponentPreview name="tag-group-([^"]+)"/g),
].map((match) => match[1]);
expect(document).not.toContain("[!toc]");
expect(names).toEqual(previews);
for (const name of previews) {
const source = readFileSync(
@@ -1008,6 +1009,7 @@ describe("Tag demos and documentation", () => {
),
].map((match) => match[1]);
expect(errorDocument).not.toContain("[!toc]");
expect(errorNames).toEqual(["basic", "custom-styles"]);
for (const name of errorNames) {
const source = readFileSync(
@@ -1084,3 +1086,293 @@ describe("Tag demos and documentation", () => {
}
});
});
describe("B03 review regressions", () => {
it("preserves root/list styles and list-local accessible names", async () => {
await mountGroup({
props: { style: { marginTop: "7px" } },
groupProps: {
style: { paddingTop: "9px" },
"aria-label": "Local tags",
"aria-describedby": "external-help",
},
});
expect(queryOne('[data-slot="tag-group"]')!.style.marginTop).toBe("7px");
const list = queryOne('[role="grid"]')!;
expect(list.style.paddingTop).toBe("9px");
expect(list.getAttribute("aria-label")).toBe("Local tags");
expect(list.getAttribute("aria-describedby")).toContain("external-help");
});
it("does not select or navigate from an embedded input or remove button", async () => {
const update = vi.fn();
const remove = vi.fn();
await mountGroup({
props: { "onUpdate:modelValue": update, onRemove: remove },
content: (item) => [item.label, h("input", { "aria-label": "Edit" })],
});
const input = queryOne("input")!;
input.focus();
for (const key of ["ArrowRight", "Home", "Delete", "Enter", " "]) {
input.dispatchEvent(
new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true }),
);
input.dispatchEvent(
new KeyboardEvent("keyup", { key, bubbles: true, cancelable: true }),
);
}
await nextTick();
expect(document.activeElement).toBe(input);
expect(update).not.toHaveBeenCalled();
expect(remove).not.toHaveBeenCalled();
const button = queryOne('[data-slot="tag-remove-button"]')!;
button.focus();
button.dispatchEvent(
new KeyboardEvent("keydown", {
key: "Enter",
bubbles: true,
cancelable: true,
}),
);
await nextTick();
expect(update).not.toHaveBeenCalled();
});
it("disables the remove button of an individually disabled tag", async () => {
await mountGroup({
props: { onRemove: vi.fn() },
tags: [{ id: "a", label: "A", disabled: true }],
});
expect(queryOne<HTMLButtonElement>("button")!.disabled).toBe(true);
});
it("clears a pointer press released outside the row", async () => {
await mountGroup();
const tag = row("资讯")!;
tag.dispatchEvent(
new MouseEvent("pointerdown", { button: 0, bubbles: true }),
);
await nextTick();
expect(tag.hasAttribute("data-pressed")).toBe(true);
document.dispatchEvent(new Event("pointerup", { bubbles: true }));
await nextTick();
expect(tag.hasAttribute("data-pressed")).toBe(false);
});
it("provides gridcell children for every grid row", async () => {
await mountGroup();
for (const tag of queryAll('[role="row"]'))
expect(tag.querySelector('[role="gridcell"]')).not.toBeNull();
});
it("does not steal external focus when a previously focused item is removed", async () => {
const tags = ref([
{ id: "a", label: "A" },
{ id: "b", label: "B" },
]);
const wrapper = mount(
defineComponent({
setup: () => () =>
h("div", [
h("input", { "aria-label": "Outside" }),
h(
TagGroup,
{ "aria-label": "Tags" },
{
default: () =>
h(TagGroupList, null, {
default: () =>
tags.value.map((item) =>
h(Tag, { id: item.id, key: item.id }, () => item.label),
),
}),
},
),
]),
}),
{ attachTo: document.body },
);
wrappers.push(wrapper);
await nextTick();
await settle();
queryOne('[role="row"]')!.focus();
await nextTick();
const outside = queryOne("input")!;
outside.focus();
await nextTick();
tags.value = tags.value.slice(1);
await nextTick();
await nextTick();
await nextTick();
expect(document.activeElement).toBe(outside);
});
});
describe("B03 composition and removal", () => {
it.each([false, true])(
"keeps the custom row root and grid cell with asChild (component=%s)",
async (component) => {
const Custom = defineComponent({
setup:
(_, { slots }) =>
() =>
h("article", { "data-custom": "row" }, slots.default?.()),
});
const wrapper = mount(TagGroup, {
props: { onRemove: vi.fn(), "aria-label": "Custom" },
slots: {
default: () =>
h(TagGroupList, null, {
default: () =>
h(Tag, { id: "custom", asChild: true }, () =>
component
? h(
Custom,
{ "data-custom": "row" },
{ default: () => "Custom" },
)
: h("article", { "data-custom": "row" }, "Custom"),
),
}),
},
attachTo: document.body,
});
wrappers.push(wrapper);
await nextTick();
await settle();
const tag = queryOne('[data-slot="tag"]')!;
expect(tag.tagName).toBe("ARTICLE");
expect(tag.getAttribute("data-custom")).toBe("row");
expect(tag.getAttribute("role")).toBe("row");
expect(tag.querySelector('[role="gridcell"]')!.textContent).toContain(
"Custom",
);
expect(tag.querySelectorAll("button")).toHaveLength(1);
},
);
it("uses actual neighbour order when disabled rows precede a removed row", async () => {
const items = ref([
{ id: "d", label: "Disabled", disabled: true },
{ id: "a", label: "A" },
{ id: "b", label: "B" },
{ id: "c", label: "C" },
]);
const wrapper = mount(
defineComponent({
setup: () => () =>
h(
TagGroup,
{
"aria-label": "Tags",
onRemove: (keys: (string | number)[]) => {
items.value = items.value.filter(
(item) => !keys.includes(item.id),
);
},
},
{
default: () =>
h(TagGroupList, null, {
default: () =>
items.value.map((item) =>
h(
Tag,
{
id: item.id,
key: item.id,
isDisabled: item.disabled,
},
() => item.label,
),
),
}),
},
),
}),
{ attachTo: document.body },
);
wrappers.push(wrapper);
await nextTick();
await settle();
const tag = row("A")!;
tag.focus();
await nextTick();
tag.dispatchEvent(
new KeyboardEvent("keydown", {
key: "Delete",
bubbles: true,
cancelable: true,
}),
);
await nextTick();
await nextTick();
await nextTick();
expect(document.activeElement).toBe(row("B"));
expect(queryOne('[role="status"]')!.textContent).toContain("A");
});
it("does not announce removal rejected by the parent", async () => {
await mountGroup({ props: { onRemove: vi.fn() } });
row("资讯")!.focus();
await nextTick();
row("资讯")!.dispatchEvent(
new KeyboardEvent("keydown", { key: "Delete", bubbles: true }),
);
await nextTick();
expect(queryOne('[role="status"]')!.textContent).toBe("");
expect(document.activeElement).toBe(row("资讯"));
});
});
describe("B03 native focus regressions", () => {
it("restores after native removal clears focus before the registry updates", async () => {
const items = ref(["A", "B"]);
const wrapper = mount(
defineComponent({
setup: () => () =>
h(
TagGroup,
{
"aria-label": "Native",
onRemove: (keys: (string | number)[]) => {
(document.activeElement as HTMLElement).blur();
items.value = items.value.filter((key) => !keys.includes(key));
},
},
{
default: () =>
h(TagGroupList, null, {
default: () =>
items.value.map((key) =>
h(Tag, { id: key, key }, () => key),
),
}),
},
),
}),
{ attachTo: document.body },
);
wrappers.push(wrapper);
await nextTick();
await settle();
const button = row("A")!.querySelector("button")!;
button.focus();
await nextTick();
button.click();
await nextTick();
await nextTick();
await nextTick();
expect(document.activeElement).toBe(row("B"));
});
it("keeps one default Tab stop in a removable collection", async () => {
await mountGroup({ props: { onRemove: vi.fn() } });
expect(
queryAll('[role="row"]').filter((el) => el.tabIndex === 0),
).toHaveLength(1);
expect(
queryAll<HTMLButtonElement>("button").every((el) => el.tabIndex === -1),
).toBe(true);
});
});