fix: disable link navigation and align breadcrumb actions and date focus

This commit is contained in:
maoyingjie
2026-10-29 09:03:52 +08:00
parent 680a592e7b
commit 68d45c66f9
27 changed files with 983 additions and 44 deletions
+32
View File
@@ -118,3 +118,35 @@ describe("DatePicker documentation", () => {
expect(document.querySelectorAll('[name="time"]')).toHaveLength(1);
});
});
it.each(["date-picker", "date-range-picker"])(
"%s trigger uses actual focus-visible state",
async (name) => {
const Demo =
name === "date-picker"
? (await import("../apps/docs/demos/date-picker-basic.vue")).default
: (await import("../apps/docs/demos/date-range-picker-basic.vue"))
.default;
const w = mount(Demo, { attachTo: document.body });
wrappers.push(w);
const trigger = w.get(`[data-slot="${name}-trigger"]`);
let visible = false;
const original = trigger.element.matches.bind(trigger.element);
const spy = vi
.spyOn(trigger.element, "matches")
.mockImplementation((selector) =>
selector === ":focus-visible" ? visible : original(selector),
);
try {
await trigger.trigger("focus");
expect(trigger.attributes("data-focus-visible")).toBeUndefined();
visible = true;
await trigger.trigger("focus");
expect(trigger.attributes("data-focus-visible")).toBe("true");
await trigger.trigger("blur");
expect(trigger.attributes("data-focus-visible")).toBeUndefined();
} finally {
spy.mockRestore();
}
},
);
+66
View File
@@ -0,0 +1,66 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Link,
Breadcrumbs,
BreadcrumbsItem,
ConfigProvider,
} from "@heroui-vue/components";
import SinglePicker from "../../apps/docs/demos/date-picker-basic.vue";
import RangePicker from "../../apps/docs/demos/date-range-picker-basic.vue";
const disabled = ref(true),
locale = ref("zh-CN");
const nativeLink = ref<{ focus: () => void }>();
const actions = ref<(string | number)[]>([]);
const clicks = ref(0),
childClicks = ref(0);
</script>
<template>
<main style="padding: 24px; display: grid; gap: 24px">
<label>
<input v-model="disabled" type="checkbox" />
禁用链接
</label>
<label>
语言
<select v-model="locale">
<option>zh-CN</option>
<option>en-US</option>
<option>fr-FR</option>
</select>
</label>
<Link
ref="nativeLink"
id="native-link"
href="#link-destination"
target="_blank"
download="example.txt"
:is-disabled="disabled"
@click="clicks++"
>
原生链接
</Link>
<Link id="child-link" as-child :is-disabled="disabled" @click="clicks++">
<a
href="#child-destination"
target="_blank"
download="child.txt"
@click="childClicks++"
>
子链接
</a>
</Link>
<button @click="nativeLink?.focus()">聚焦原生链接</button>
<output id="link-counts">{{ clicks }}/{{ childClicks }}</output>
<ConfigProvider :locale="locale">
<Breadcrumbs :is-disabled="disabled" @action="actions.push($event)">
<BreadcrumbsItem :key="0">数字零</BreadcrumbsItem>
<BreadcrumbsItem key="">空标识</BreadcrumbsItem>
<BreadcrumbsItem>当前页</BreadcrumbsItem>
</Breadcrumbs>
</ConfigProvider>
<output id="actions">{{ JSON.stringify(actions) }}</output>
<section id="single"><SinglePicker /></section>
<section id="range"><RangePicker /></section>
</main>
</template>
+15
View File
@@ -856,5 +856,20 @@
["Typography", "TypographyRoot"]
],
"css": []
},
"link-focus": {
"exports": [
"Link",
"Breadcrumbs",
"BreadcrumbsItem",
"DatePickerTrigger",
"DateRangePickerTrigger"
],
"css": [
".hv-link",
".hv-breadcrumbs",
".hv-date-picker__trigger",
".hv-date-range-picker__trigger"
]
}
}
+26
View File
@@ -0,0 +1,26 @@
import type {
BreadcrumbsProps,
BreadcrumbsItemProps,
LinkProps,
} from "@heroui-vue/components";
const root: BreadcrumbsProps = {
onAction: (key: string | number) => {
void key;
},
};
const item: BreadcrumbsItemProps = { id: 0 };
const link: LinkProps = {
href: "/files",
asChild: true,
isDisabled: true,
download: "file.txt",
};
// @ts-expect-error keys include numeric identities
const wrongAction: BreadcrumbsProps = { onAction: (_key: boolean) => {} };
// @ts-expect-error internal collection identity injection is not a public item prop
const privateKey: BreadcrumbsItemProps = { itemKey: 0 };
void item;
void root;
void link;
void wrongAction;
void privateKey;
+116 -3
View File
@@ -1,6 +1,6 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi } from "vitest";
import { mount } from "@vue/test-utils";
import { h, inject, defineComponent, nextTick } from "vue";
import { h, inject, defineComponent, nextTick, ref } from "vue";
import {
Surface,
surfaceContextKey,
@@ -96,7 +96,7 @@ describe("Link", () => {
});
const a = w.get("a");
expect(a.attributes("aria-disabled")).toBe("true");
expect(a.attributes("href")).toBe("#");
expect(a.attributes("href")).toBeUndefined();
await a.trigger("click");
expect(clicks).toBe(0);
expect(captureClicks).toBe(0);
@@ -559,3 +559,116 @@ it("omits the implicit same-window target so host routers can intercept navigati
expect(explicit.attributes("target")).toBe("_self");
explicit.unmount();
});
it.each([false, true])(
"removes disabled navigation attributes and restores them dynamically (asChild=%s)",
async (asChild) => {
const click = vi.fn();
const childCapture = vi.fn();
const childRef = ref<HTMLElement>();
const w = mount(Link, {
props: {
asChild,
href: "#parent",
target: "_blank",
download: "parent.txt",
isDisabled: true,
},
attrs: { class: "consumer", onClick: click, "data-test": "kept" },
slots: {
default: () =>
asChild
? h(
"a",
{
href: "#child",
target: "_self",
download: "child.txt",
ref: childRef,
tabIndex: 0,
onClickCapture: childCapture,
},
"link",
)
: "link",
},
attachTo: document.body,
});
try {
const root = w.get("a");
expect(root.attributes("href")).toBeUndefined();
expect(root.attributes("target")).toBeUndefined();
expect(root.attributes("download")).toBeUndefined();
expect(root.attributes("aria-disabled")).toBe("true");
expect(root.attributes("tabindex")).toBe("-1");
expect(root.classes()).toContain("consumer");
expect(root.attributes("data-test")).toBe("kept");
if (asChild) expect(childRef.value).toBe(root.element);
const event = new MouseEvent("click", {
bubbles: true,
cancelable: true,
});
root.element.dispatchEvent(event);
expect(event.defaultPrevented).toBe(true);
const middle = new MouseEvent("auxclick", {
bubbles: true,
cancelable: true,
button: 1,
});
root.element.dispatchEvent(middle);
expect(middle.defaultPrevented).toBe(true);
expect(click).not.toHaveBeenCalled();
expect(childCapture).not.toHaveBeenCalled();
await w.setProps({ isDisabled: false });
expect(root.attributes("href")).toBe(asChild ? "#child" : "#parent");
expect(root.attributes("download")).toBe(
asChild ? "child.txt" : "parent.txt",
);
await root.trigger("click");
expect(click).toHaveBeenCalledTimes(1);
if (asChild) expect(childCapture).toHaveBeenCalledTimes(1);
await w.setProps({ isDisabled: true });
expect(root.attributes("href")).toBeUndefined();
} finally {
w.unmount();
}
},
);
it("preserves forwarding child refs and handlers while respecting cancellation", async () => {
const child = vi.fn(),
parent = vi.fn();
const forwardedRef = ref();
const Forwarded = defineComponent({
inheritAttrs: false,
setup(_, { attrs }) {
return () => h("a", attrs, "forwarded");
},
});
const w = mount(Link, {
props: { asChild: true, isDisabled: true },
attrs: {
onClick: parent,
onClickCapture: (e: Event) => e.preventDefault(),
},
slots: {
default: () =>
h(Forwarded, { href: "#forwarded", ref: forwardedRef, onClick: child }),
},
attachTo: document.body,
});
try {
expect(w.get("a").attributes("href")).toBeUndefined();
await w.get("a").trigger("click");
expect(child).not.toHaveBeenCalled();
await w.setProps({ isDisabled: false });
expect(forwardedRef.value.$el).toBe(w.get("a").element);
const event = new MouseEvent("click", { bubbles: true, cancelable: true });
w.get("a").element.dispatchEvent(event);
expect(event.defaultPrevented).toBe(true);
expect(parent).toHaveBeenCalledTimes(1);
expect(child).toHaveBeenCalledTimes(1);
} finally {
w.unmount();
}
});
@@ -2,6 +2,7 @@ import { describe, it, expect, vi, afterEach } from "vitest";
import { mount } from "@vue/test-utils";
import { defineComponent, h, nextTick, ref } from "vue";
import {
ConfigProvider,
Breadcrumbs,
BreadcrumbsItem,
EmptyState,
@@ -799,3 +800,84 @@ describe("Breadcrumbs", () => {
w.unmount();
});
});
it("localizes breadcrumb labels dynamically while explicit names take precedence", async () => {
const locale = ref("en-US"),
label = ref<string>();
const w = mount(
defineComponent({
render: () =>
h(ConfigProvider, { locale: locale.value }, () =>
h(Breadcrumbs, { "aria-label": label.value }, () =>
h(BreadcrumbsItem, {}, () => "current"),
),
),
}),
);
try {
expect(w.get("ol").attributes("aria-label")).toBe("Breadcrumbs");
locale.value = "zh-CN";
await nextTick();
expect(w.get("ol").attributes("aria-label")).toBe("导航栏");
label.value = "自定义路径";
await nextTick();
locale.value = "fr-FR";
await nextTick();
expect(w.get("ol").attributes("aria-label")).toBe("自定义路径");
label.value = "";
await nextTick();
expect(w.get("ol").attributes("aria-label")).toBe("Chemin de navigation");
} finally {
w.unmount();
}
});
it("emits one breadcrumb action preserving zero/empty identities, respecting cancellation and disabled/current items", async () => {
const action = vi.fn(),
disabled = ref(false),
cancel = ref(false);
const w = mount(
defineComponent({
render: () =>
h(Breadcrumbs, { isDisabled: disabled.value, onAction: action }, () => [
h(
BreadcrumbsItem,
{
key: 0,
onClick: (e: Event) => {
if (cancel.value) e.preventDefault();
},
},
() => "zero",
),
h(BreadcrumbsItem, { key: "" }, () => "empty"),
h(
BreadcrumbsItem,
{ key: "string-key", id: "0" },
() => "string zero",
),
h(BreadcrumbsItem, { key: "current" }, () => "current"),
]),
}),
{ attachTo: document.body },
);
try {
const links = () => w.findAll('[data-slot="link"]');
await links()[0].trigger("click");
expect(action).toHaveBeenLastCalledWith(0);
await links()[1].trigger("keydown", { key: "Enter" });
expect(action).toHaveBeenLastCalledWith("");
await links()[2].trigger("click");
expect(action).toHaveBeenLastCalledWith("0");
expect(action).toHaveBeenCalledTimes(3);
cancel.value = true;
await nextTick();
await links()[0].trigger("click");
await links()[3].trigger("click");
disabled.value = true;
await nextTick();
await links()[1].trigger("click");
expect(action).toHaveBeenCalledTimes(3);
} finally {
w.unmount();
}
});