test: cover keyboard item activation and cancellation for menu items
This commit is contained in:
@@ -72,7 +72,7 @@ default、with-icons、with-descriptions、with-disabled-items、with-sections
|
||||
1. **长按的触发权归真实触发器**:`DropdownRoot` 不再包裹整段内容,只提供 `triggerMode`/`longPressDuration`/`open`/`setOpen` 上下文;`DropdownTrigger` 在 `longPress` 模式自行渲染一层 `display: contents` 守卫,用 **capture 阶段**接管 pointer/click/keydown,并以 `event.preventDefault()` 触发 Reka 自带的 `if (event.defaultPrevented) return` 守卫取消其打开/切换。短点击不开、release 后的 click 不回关、Enter/Space 只切换一次都由真实 `DropdownTrigger > Button` 上的事件决定,不依赖晚到的冒泡拦截。回归覆盖阈值前后、移动阈值、`pointercancel`、非主键/非主指针、卸载清理。
|
||||
2. **点击链交回 Vue DOM 事件层**:`MenuItem` 不再手工遍历用户处理器数组。用户的 `onClick`(函数或数组)与内部激活动作合并成一个 `onClick` 数组下发给同一元素,Reka 的 `handleSelect` 由 `onSelect` 里的 `preventDefault` 中和,因此 `preventDefault`、capture、`stopImmediatePropagation` 都是 Vue/浏览器原生语义;`stopImmediatePropagation` 后第二个处理器与激活都不再执行。
|
||||
3. **Esc 逐层关闭**:`DropdownSubmenuTrigger` 自己持有子菜单 open,在子内容与子触发器上拦截 Escape(`preventDefault` + `stopPropagation`,只作用于本层,不改共享 overlay 内核,也不加全局 document 监听),关闭当前层并把握焦还给子触发器;父层保留,第二次 Escape 才关闭根菜单。焦点在子菜单项内(键盘)与焦点在子触发器上(指针打开)两条路径都有回归,浏览器实测一致。
|
||||
4. **回归与产物**:原 `b01-menu-dropdown` 增至 **16/16**(两条旧长按测试原先只在 Root 包裹层派发事件,已改为操作真实触发器),两文件合计 **23/23**;完整 **71 文件 / 885 项**;typecheck、build:docs、Prettier、`git diff --check` 通过。独立 tarball 重新打包后 ESM+CSS 与 bundler/NodeNext 类型契约仍通过。
|
||||
4. **回归与产物**:原 `b01-menu-dropdown` 增至 **18/18**(两条旧长按测试原先只在 Root 包裹层派发事件,已改为操作真实触发器;另加键盘激活与键盘取消两项),两文件合计 **25/25**;完整 **71 文件 / 888 项**;typecheck、build:docs、Prettier、`git diff --check` 通过。独立 tarball 重新打包后 ESM+CSS 与 bundler/NodeNext 类型契约仍通过。
|
||||
5. **浏览器(5181,真实点击/按键)**:长按示例真实短点击后不打开;真实 Enter 打开且不被二次切换关闭;Space 一次开一次关;Esc 关闭后焦点回触发器。`with-submenus` 焦点在子菜单项上按 Esc → 只关子菜单、根菜单保持、焦点回「其他」,再按 Esc → 关根菜单、焦点回「分享」,console warn/error 为 0。
|
||||
|
||||
**仍存限制(诚实记录)**:本运行时浏览器后端没有 press-and-hold 输入,“按住达到阈值才打开”的计时路径只有 jsdom + 假时钟证据;完整读屏、物理触屏、Nuxt、逐像素与自动视觉对比未验证。
|
||||
|
||||
@@ -920,8 +920,8 @@ Button/PopoverTrigger/Link/ToggleButtonGroup 源码及几何复核后,移除
|
||||
- `tests/b01-review.probe.test.ts` **7/7**:上一轮 4 项保持通过,新增 3 项(真实 `DropdownTrigger > Button` 短点击不开、长按 Enter 只打开一次、`onClick` 数组中 `stopImmediatePropagation` 后第二个处理器与激活都不执行)由失败转通过。
|
||||
- 点击链改由 Vue DOM 事件层执行:用户处理器与激活处理器作为同一个 `onClick` 数组下发,Reka 的 `handleSelect` 在 `onSelect` 中被 `preventDefault` 中和,因此 `preventDefault`、capture、`stopImmediatePropagation` 都是浏览器原生语义,不再手工遍历处理器。
|
||||
- 长按模式移交真实触发器:`DropdownTrigger` 在 `longPress` 模式下以 capture 阶段接管 pointer/click/keydown,通过 Reka 自身的 `event.defaultPrevented` 守卫取消其打开,`DropdownRoot` 不再包裹整段内容。回归覆盖短点击不开、达到阈值打开、release/click 不回关、移动阈值取消、`pointercancel`、非主键/非主指针忽略、Enter 与 Space 各只切换一次、卸载清理(`vi.getTimerCount()` 归零)。
|
||||
- Esc 逐层:`DropdownSubmenuTrigger` 自持子菜单 open,在子内容与子触发器上拦截 Escape(`preventDefault` + `stopPropagation`),只关闭当前层并把焦点交还子触发器,父层与根层保持;第二层 Escape 才关闭根菜单。焦点在子菜单项内(键盘路径)与焦点在子触发器上(指针打开路径)各有 jsdom 回归。
|
||||
- 原 `tests/b01-menu-dropdown.test.ts` 增至 **16/16**(两条旧长按测试原先只在 Root 包裹层派发事件,已改为操作真实 `DropdownTrigger > Button`);两文件合计 **23/23**;完整 **71 文件 / 885 项**;typecheck、build:docs、Prettier、diff 检查通过(仅既存 chunk 大小提示)。
|
||||
- Esc 逐层:`DropdownSubmenuTrigger` 自持子菜单 open,在子内容与子触发器上拦截 Escape(`preventDefault` + `stopPropagation`),只关闭当前层并把焦点交还子触发器,父层与根层保持;第二层 Escape 才关闭根菜单。焦点在子菜单项内(键盘路径)与焦点在子触发器上(指针打开路径)各有 jsdom 回归;条目的键盘激活保留单次动作与取消边界。
|
||||
- 原 `tests/b01-menu-dropdown.test.ts` 增至 **18/18**(两条旧长按测试原先只在 Root 包裹层派发事件,已改为操作真实 `DropdownTrigger > Button`);两文件合计 **25/25**;完整 **71 文件 / 888 项**;typecheck、build:docs、Prettier、diff 检查通过(仅既存 chunk 大小提示)。
|
||||
- 独立 tarball `/tmp/heroui-b01-consumer` 重新打包安装后:ESM 命名导出 + `@heroui-vue/styles/css` 通过;`b01-menu-dropdown-contract.ts` 在 bundler 与 NodeNext 下正反例均通过(`@ts-expect-error` 仍生效)。
|
||||
- 浏览器 5181 实测(真实点击/真实按键):长按示例真实短点击后 `aria-expanded=false`、无 popover;真实 Enter 打开并保持、`aria-expanded=true`;Space 一次开一次关;Esc 关闭后焦点回到「长按」触发器。`with-submenus` 中焦点在子菜单项 WhatsApp 上按 Esc:子菜单卸载、根菜单仍开(`aria-expanded=true`)、焦点回「其他」;再按 Esc 关根菜单、焦点回「分享」。指针打开子菜单、焦点留在「其他」时同样只关一层。期间 console warn/error 为 0。
|
||||
- 本运行时浏览器后端不提供 press-and-hold 输入,因此“按住达到阈值才打开”的计时路径只有 jsdom + 假时钟证据;真实鼠标长按与物理触屏仍未验证。完整读屏、Nuxt、逐像素、自动视觉对比也未验证。
|
||||
|
||||
@@ -580,3 +580,73 @@ it("Escape on a pointer-opened submenu trigger closes only that level", async ()
|
||||
await settle();
|
||||
expect(document.querySelector('[data-slot="dropdown-popover"]')).toBeNull();
|
||||
});
|
||||
|
||||
it("keyboard activation on a focused item fires the action once", async () => {
|
||||
const actions: string[] = [];
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true }, () => [
|
||||
h(DropdownTrigger, {}, () => h(Button, () => "菜单")),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () =>
|
||||
h(DropdownItem, { id: "new-file", textValue: "新建文件" }, () =>
|
||||
h(Label, () => "新建文件"),
|
||||
),
|
||||
),
|
||||
),
|
||||
]),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const item = document.querySelector('[data-slot="menu-item"]') as HTMLElement;
|
||||
item.focus();
|
||||
expect(document.activeElement).toBe(item);
|
||||
item.dispatchEvent(
|
||||
new KeyboardEvent("keydown", {
|
||||
key: "Enter",
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
}),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(actions).toEqual(["new-file"]);
|
||||
});
|
||||
|
||||
it("keyboard activation is cancelled when the item click is prevented", async () => {
|
||||
const actions: string[] = [];
|
||||
mountApp(
|
||||
() => () =>
|
||||
h(DropdownRoot, { defaultOpen: true }, () => [
|
||||
h(DropdownTrigger, {}, () => h(Button, () => "菜单")),
|
||||
h(DropdownPopover, {}, () =>
|
||||
h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () =>
|
||||
h(
|
||||
DropdownItem,
|
||||
{
|
||||
id: "new-file",
|
||||
textValue: "新建文件",
|
||||
onClick: (e: MouseEvent) => e.preventDefault(),
|
||||
},
|
||||
() => h(Label, () => "新建文件"),
|
||||
),
|
||||
),
|
||||
),
|
||||
]),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const item = document.querySelector('[data-slot="menu-item"]') as HTMLElement;
|
||||
item.focus();
|
||||
item.dispatchEvent(
|
||||
new KeyboardEvent("keydown", {
|
||||
key: "Enter",
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
}),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(actions).toEqual([]);
|
||||
expect(document.querySelector('[data-slot="menu-item"]')).not.toBeNull();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user