Skip to content

fix(popover): 修复点击外部触发 ref.show() 时弹窗关闭问题#7073

Open
Passing-of-A-Dream wants to merge 3 commits into
ant-design:masterfrom
Passing-of-A-Dream:fix/popover-ref-show
Open

fix(popover): 修复点击外部触发 ref.show() 时弹窗关闭问题#7073
Passing-of-A-Dream wants to merge 3 commits into
ant-design:masterfrom
Passing-of-A-Dream:fix/popover-ref-show

Conversation

@Passing-of-A-Dream

@Passing-of-A-Dream Passing-of-A-Dream commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

fixed #6731 修复 ref.show() 调用时弹窗会关闭

Summary by CodeRabbit

  • Bug Fixes
    • 优化弹出层(Popover)显示与关闭交互:在通过 show() 的短时间触发链路内更稳定,避免同一点击导致的意外立即关闭。
    • 外部点击后仍可按预期关闭;从关闭状态再次显示时也能正常打开并展示内容。
  • Tests
    • 增补了与 PopoverRef.show() 行为相关的测试用例,覆盖点击触发可见状态下的防抖关闭、外部点击关闭以及再次打开展示内容等场景。

- 新增 showingRef 标记防止 show() 触发链上的 click-away
- 添加测试用例验证修复效果
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. bug labels Jul 6, 2026
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 17f82da4-bfc4-427e-a09c-bace08d054f7

📥 Commits

Reviewing files that changed from the base of the PR and between 1a5425b and b5d26c3.

📒 Files selected for processing (1)
  • src/components/popover/popover.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/components/popover/popover.tsx

📝 Walkthrough

Walkthrough

Popover 的 show() 增加了短暂保护窗口,避免 ref 触发展示后被同一点击链路立即关闭;同时补充了对应的可见性与点击外部关闭测试。

Changes

Popover show() 修复

Layer / File(s) Summary
show() 触发窗口与点击外部保护逻辑
src/components/popover/popover.tsx
show() 先标记 showingRef 再设置可见,并在短时间后复位;useClickAway 回调在该窗口内跳过关闭。
show() 行为测试用例
src/components/popover/tests/popover.test.tsx
新增 act 并补充 3 个测试,覆盖 ref.show() 打开、已可见状态再次调用、以及点击外部关闭的行为。

Estimated code review effort: 2 (Simple) | ~15 minutes

Poem

小兔轻点 ref 一下,
弹层乖乖亮起来呀,
showingRef 先守一瞬,
外部点击别急关它,
🐇 三个测试来护航,
show() 稳稳不慌张。

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了通过 ref.show() 打开 Popover 时立即关闭的问题。
Linked Issues check ✅ Passed 修改通过延迟重置和点击外部保护,解决了 #6731 所述 ref 打开无响应/被立刻关闭的问题。
Out of Scope Changes check ✅ Passed 新增内容主要是对应行为修复与测试覆盖,没有明显无关改动。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a showingRef to track when the Popover is being shown via ref.show(), preventing the click-away handler from immediately closing it. It also adds corresponding unit tests to verify this behavior. The review feedback suggests managing the setTimeout in show() with a ref and clearing it on unmount or consecutive calls to prevent potential memory leaks, race conditions, or Jest open handle warnings.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +79 to +90
const showingRef = useRef(false)

useImperativeHandle(
ref,
() => ({
show: () => setVisible(true),
show: () => {
showingRef.current = true
setVisible(true)
setTimeout(() => {
showingRef.current = false
})
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

show 方法中使用了 setTimeout 来异步重置 showingRef.current。然而,如果组件在定时器触发前被卸载,或者连续多次调用 show,可能会导致以下问题:\n1. 测试环境中的 Open Handles 警告:在测试用例中,如果测试在定时器执行前结束并卸载了组件,未清理的定时器会导致 Jest 报 "open handles" 警告,甚至可能影响后续测试。\n2. 竞态条件:连续多次调用 show 时,多个定时器交错执行可能会提前将 showingRef.current 重置为 false。\n\n建议使用 timeoutRef 保存定时器 ID,并在组件卸载时以及下一次调用 show 前进行清理。

  const showingRef = useRef(false)\n  const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)\n\n  useEffect(() => {\n    return () => {\n      if (timeoutRef.current) {\n        clearTimeout(timeoutRef.current)\n      }\n    }\n  }, [])\n\n  useImperativeHandle(\n    ref,\n    () => ({\n      show: () => {\n        showingRef.current = true\n        setVisible(true)\n        if (timeoutRef.current) {\n          clearTimeout(timeoutRef.current)\n        }\n        timeoutRef.current = setTimeout(() => {\n          showingRef.current = false\n          timeoutRef.current = null\n        })\n      },

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/components/popover/popover.tsx (1)

79-95: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

建议为该时序技巧补充注释,防止后续被误删。

show() 中的 setTimeout 用于制造一个短暂窗口,让触发 show() 的同一次点击事件先冒泡完成(此时 useClickAway 才能读取到 showingRef.current === true 而跳过关闭)。这个依赖关系并不直观,建议加一行注释说明用途,避免未来有人误以为 setTimeout 是多余代码而将其移除,导致 #6731 问题回归。

📝 建议补充注释
       show: () => {
+        // 标记进入 show() 触发链,避免同一次点击事件冒泡到 document 时被 useClickAway 立即关闭
         showingRef.current = true
         setVisible(true)
         setTimeout(() => {
           showingRef.current = false
         })
       },
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/components/popover/popover.tsx` around lines 79 - 95, The show/hide
timing in popover.tsx is intentionally using setTimeout inside
useImperativeHandle’s show method to keep showingRef.current true through the
same click event so useClickAway won’t immediately close it. Add a short comment
near show() and the timeout explaining this event-bubbling window and the
dependency on showingRef.current, so future refactors don’t remove it from the
Popover component and reintroduce the close-on-open issue.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/components/popover/popover.tsx`:
- Around line 79-95: The show/hide timing in popover.tsx is intentionally using
setTimeout inside useImperativeHandle’s show method to keep showingRef.current
true through the same click event so useClickAway won’t immediately close it.
Add a short comment near show() and the timeout explaining this event-bubbling
window and the dependency on showingRef.current, so future refactors don’t
remove it from the Popover component and reintroduce the close-on-open issue.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8c0ff733-a5e5-4086-9cf5-9555784d9739

📥 Commits

Reviewing files that changed from the base of the PR and between 6823d80 and 034c2cc.

📒 Files selected for processing (2)
  • src/components/popover/popover.tsx
  • src/components/popover/tests/popover.test.tsx

- 使用 ref 存储定时器引用
- 在组件卸载时清理定时器
- 防止内存泄漏
@pkg-pr-new

pkg-pr-new Bot commented Jul 6, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/antd-mobile@7073

commit: b5d26c3

@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.39%. Comparing base (6823d80) to head (b5d26c3).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7073      +/-   ##
==========================================
+ Coverage   93.34%   93.39%   +0.05%     
==========================================
  Files         337      337              
  Lines        7418     7430      +12     
  Branches     1860     1901      +41     
==========================================
+ Hits         6924     6939      +15     
+ Misses        486      459      -27     
- Partials        8       32      +24     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

ref,
() => ({
show: () => setVisible(true),
show: () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉可以封装个 hook,现在这么 inline 写看起来很脏。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Popover 使用ref打开失败

2 participants