DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
fix(ui): Chip 转发 ref,修复英雄区「选择工作区」菜单停在测量态 (#783) (#786)
Mao Mr4987f236f0f0
diff --git a/packages/dsh-tauri-ui/src/client/components/chip.test.tsx b/packages/dsh-tauri-ui/src/client/components/chip.test.tsxnew file mode 100644index 00000000..9201b6ca--- /dev/null+++ b/packages/dsh-tauri-ui/src/client/components/chip.test.tsx@@ -0,0 +1,27 @@+import { cleanup, render } from '@testing-library/react'+// @vitest-environment jsdom+import { createRef } from 'react'+import { afterEach, describe, expect, it, vi } from 'vitest'+import { Chip } from './chip'++vi.mock('dsh-tauri/client', async () => {+ const mod = await import('tailwind-variants')+ return { tv: mod.tv }+})++afterEach(cleanup)++describe('chip', () => {+ it('把调用方 ref 交给真实 button,供 portal 菜单测量锚点', () => {+ const ref = createRef<HTMLButtonElement>()+ const view = render(<Chip ref={ref} variant="seat" aria-label="选择工作区">选择工作区</Chip>)++ expect(ref.current, 'Chip 必须转发 ref,否则 getAnchorRect 永远拿不到锚点').toBeInstanceOf(HTMLButtonElement)+ expect(ref.current).toBe(view.container.querySelector('button'))+ })++ // React 19 把 ref 当普通 prop,渲染断言区分不了「转发」与「当 prop 收下」;部署端 React 18 会丢弃 ref。+ it('对外声明为可转发 ref 的组件', () => {+ expect((Chip as unknown as { $$typeof?: symbol }).$$typeof).toBe(Symbol.for('react.forward_ref'))+ })+})diff --git a/packages/dsh-tauri-ui/src/client/components/chip.tsx b/packages/dsh-tauri-ui/src/client/components/chip.tsxindex 7b1b2706..4cd764e4 100644--- a/packages/dsh-tauri-ui/src/client/components/chip.tsx+++ b/packages/dsh-tauri-ui/src/client/components/chip.tsx@@ -2,8 +2,9 @@import type { VariantProps } from 'dsh-tauri/client'import type { ComponentProps, ReactElement, ReactNode } from 'react'import { tv } from 'dsh-tauri/client'+import { forwardRef } from 'react'-export interface ChipProps extends Omit<ComponentProps<'button'>, 'children'> {+export interface ChipProps extends Omit<ComponentProps<'button'>, 'children' | 'ref'> {variant: ChipVarianticon?: ReactNodebadge?: ReactNode@@ -53,12 +54,16 @@ const chip = tv({export type ChipVariant = NonNullable<VariantProps<typeof chip>['variant']>-export function Chip({ variant, icon, badge, chevron, open, className, children, ...rest }: ChipProps): ReactElement {+// eslint-disable-next-line react/no-forward-ref -- 部署端前端内置 React 18(issue #783):普通函数组件会静默丢弃 ref+export const Chip = forwardRef<HTMLButtonElement, ChipProps>((+ { variant, icon, badge, chevron, open, className, children, ...rest },+ ref,+): ReactElement => {// 官方 PermissionRow.selector 不包 icon/label/badge,其余两个 variant 按 PermissionSelect/AgentPresetSeat 包裹。const wraps = variant !== 'selector'const styles = chip({ variant, hasIcon: wraps && icon != null, className })return (- <button type="button" className={styles.base()} {...rest}>+ <button type="button" className={styles.base()} {...rest} ref={ref}>{wraps && icon != null ? <span className={styles.icon()} aria-hidden>{icon}</span> : icon}{wraps && children != null ? <span className={styles.label()}>{children}</span> : children}{wraps && badge != null ? <span className={styles.badge()} aria-hidden>{badge}</span> : badge}@@ -67,4 +72,4 @@ export function Chip({ variant, icon, badge, chevron, open, className, children,: <span className={styles.chevron()} aria-hidden data-open={open === true ? 'true' : undefined}>{chevron}</span>}</button>)-}+})diff --git a/test/e2e/plugins/dsh-tauri-ui.e2e.ts b/test/e2e/plugins/dsh-tauri-ui.e2e.tsindex 7990ec24..0e6e7485 100644--- a/test/e2e/plugins/dsh-tauri-ui.e2e.ts+++ b/test/e2e/plugins/dsh-tauri-ui.e2e.ts@@ -283,16 +283,37 @@ describe('L2 客户端', () => {const menus = Array.from(document.querySelectorAll('[role="menu"]')).filter(menu => menu.getClientRects().length > 0)const items = Array.from(menus[0]?.querySelectorAll('button[role="menuitem"]') ?? []) as HTMLButtonElement[]+ const anchor = chip?.getBoundingClientRect() ?? null+ const rect = menus[0]?.getBoundingClientRect() ?? nullreturn {expanded: chip?.getAttribute('aria-expanded') ?? null,visibleMenus: menus.length,count: items.length,labels: items.map(item => item.textContent?.trim() ?? ''),firstDisabled: items[0]?.disabled ?? null,+ visibility: menus[0] === undefined ? null : getComputedStyle(menus[0]).visibility,+ left: rect === null ? null : Math.round(rect.left),+ top: rect === null ? null : Math.round(rect.top),+ width: rect === null ? null : Math.round(rect.width),+ gapBelowChip: rect === null || anchor === null ? null : Math.round(rect.top - anchor.bottom),+ leftOffsetFromChip: rect === null || anchor === null ? null : Math.round(rect.left - anchor.left),}})expect(menu.expanded, '菜单打开时 chip 必须回报 aria-expanded').toBe('true')+ // 定位断言:chip 未把 ref 转交给 button 时 `getAnchorRect()` 恒返回 null,+ // 官方 Menu 会静默跳过定位,菜单留在测量态(内联 `visibility: hidden` + `left/top: 0`)。+ // 只断言「有可见菜单」抓不到这个故障——测量态菜单仍参与布局,同样满足 `getClientRects().length > 0`。+ expect(menu.visibility, '菜单必须完成定位,不得停在测量态').not.toBe('hidden')+ expect(menu.left, '菜单不得停在左上角原点(测量态特征)').toBeGreaterThan(0)+ expect(menu.top, '菜单不得停在左上角原点(测量态特征)').toBeGreaterThan(0)+ expect(menu.width, '菜单必须有真实宽度,而非零尺寸测量态').toBeGreaterThan(0)+ // 几何契约取自官方 primitives 的 portal 定位:`side === 'bottom'` 时 `y = anchor.bottom + 4`,+ // `align === 'start'` 时 `x = anchor.left`,随后各自按 12px 视口边距夹紧。+ expect(menu.gapBelowChip, '菜单必须紧贴 chip 下方').toBeGreaterThanOrEqual(0)+ expect(menu.gapBelowChip, '菜单必须紧贴 chip 下方').toBeLessThanOrEqual(16)+ expect(menu.leftOffsetFromChip, '菜单左缘必须对齐 chip 左缘').toBeGreaterThanOrEqual(0)+ expect(menu.leftOffsetFromChip, '菜单左缘必须对齐 chip 左缘').toBeLessThanOrEqual(12)expect(menu.visibleMenus, '打开后必须只有本选择器这一个可见菜单(否则断言对象不确定)').toBe(1)expect(menu.count, '打开后必须渲染菜单条目').toBeGreaterThan(0)expect(menu.labels[0], '「未分组」必须是第一个选项').toBe('未分组')