Skip to content

fix: prevent dropdown from closing when mousedown on input in multiple mode#1214

Merged
zombieJ merged 3 commits intoreact-component:masterfrom
QDyanbing:fix/multiple-input-mousedown-close
Feb 26, 2026
Merged

fix: prevent dropdown from closing when mousedown on input in multiple mode#1214
zombieJ merged 3 commits intoreact-component:masterfrom
QDyanbing:fix/multiple-input-mousedown-close

Conversation

@QDyanbing
Copy link
Contributor

@QDyanbing QDyanbing commented Feb 25, 2026

🤔 本次改动解决了什么问题?

修复 Selectmultiple 模式下,当下拉框已打开时,
在 input 内部进行鼠标操作(例如选中文字或移动光标),
会错误触发 toggleOpen(),导致下拉框被意外关闭的问题。


🔍 复现步骤

  1. 使用 Select,设置 mode="multiple"showSearch
  2. 打开下拉框
  3. 在 input 内拖拽选择文本或点击移动光标
  4. 下拉框被错误收起

✅ 修复思路

当满足以下条件时,不再触发 toggleOpen()

  • 当前为 multiple 模式
  • 下拉框已处于打开状态
  • mousedown 事件来源于 input 元素内部

该修改使 input 内部交互(如文本选择)不会触发关闭逻辑,
行为与旧版本实现保持一致,同时不影响其他交互场景。


🧪 测试

新增回归测试,确保:

  • multiple 模式下,input 内 mousedown 不会导致下拉框关闭

🔗 相关 Issue

fix ant-design/ant-design#57056

Summary by CodeRabbit

发布说明

  • Bug 修复

    • 改进了下拉关闭行为:在多选模式且下拉已打开时,点击输入框不会意外关闭下拉,同时保留单选/组合框模式下的既有关闭行为。
  • 测试

    • 添加了覆盖多选模式输入框交互的测试,验证在输入框上按下鼠标时下拉保持打开,确保该场景稳定不回归。

@vercel
Copy link

vercel bot commented Feb 25, 2026

@QDyanbing is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 61d253b and 9485e8e.

📒 Files selected for processing (1)
  • src/SelectInput/index.tsx

Walkthrough

在 SelectInput 中新增对事件目标是否位于输入框的检测,拆分并调整防止下拉关闭的判断以区分单选/组合框与多选场景,修改 mousedown 处理以在多选且点击输入框时保持下拉打开;并新增对应的多选交互测试。

Changes

Cohort / File(s) Summary
SelectInput 点击处理改进
src/SelectInput/index.tsx
新增 isClickOnInput 判断;将原单一的防止关闭条件拆为 shouldPreventCloseOnSingle(open 且非 multiple 且 combobox 或 showSearch)与 shouldPreventCloseOnMultipleInput(open 且 multiple 且点击在输入框上);最终防闭合逻辑为两者或运算;调整 mousedown 的 preventDefault 仅在点击非输入框时执行。
多选模式测试覆盖
tests/Multiple.test.tsx
新增测试:在 multiple + showSearch 场景下对输入框执行 mouseDown(模拟文本选取)不应导致下拉关闭,验证交互行为。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Input
    participant SelectInput
    participant Dropdown
    User->>Input: mousedown / click
    Input->>SelectInput: 传递事件(包含 target 信息)
    alt 点击在输入框上
        SelectInput->>Dropdown: 保持打开(阻止关闭的条件触发)
    else 点击在输入框外
        SelectInput->>Dropdown: 根据原有规则决定是否关闭
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Suggested reviewers

  • zombieJ

Poem

🐰 在框里轻点与拖拽,
文字安然不被掠,
多选静候手中指,
下拉不走也不急,
兔子修补献胡萝卜 🥕

🚥 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 标题准确地总结了主要变更:在多选模式下防止鼠标按下输入框时下拉菜单关闭,与代码变更完全相关。
Linked Issues check ✅ Passed 代码变更完全满足链接问题的需求:修复了多选模式下鼠标在输入框内的文本选择交互不再关闭下拉菜单的问题。
Out of Scope Changes check ✅ Passed 所有代码变更都在范围内,仅涉及修复多选模式下输入框鼠标交互问题及其测试覆盖。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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 and usage tips.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @QDyanbing, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a bug in the Select component when used in multiple mode with showSearch. The dropdown was closing unexpectedly when users interacted with the input field. The fix prevents this behavior by checking if the mousedown event originated from within the input element and preventing the toggleOpen() function from being triggered. A new test case was added to ensure this fix does not regress in the future.

Highlights

  • Bug Fix: Addresses an issue where the Select dropdown in multiple mode was unintentionally closing when users interacted with the input field (e.g., selecting text or moving the cursor).
  • Preventing Unintended Closures: The fix ensures that mousedown events within the input element of a multiple select do not trigger toggleOpen(), thus preventing the dropdown from closing unexpectedly.
  • Regression Test: A new regression test was added to verify that mousedown events inside the input field do not cause the dropdown to close in multiple mode.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/SelectInput/index.tsx
    • Modified to prevent dropdown from closing on mousedown inside input in multiple mode.
  • tests/Multiple.test.tsx
    • Added a regression test to ensure the dropdown does not close on mousedown inside the input in multiple mode.
Activity
  • QDyanbing authored the pull request.
  • The pull request title is: fix: prevent dropdown from closing when mousedown on input in multiple mode
  • The pull request description indicates that the changes fix an issue where the Select dropdown in multiple mode was closing unexpectedly when interacting with the input field.
  • A new test case was added to prevent regressions.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

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

Choose a reason for hiding this comment

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

Code Review

本次 PR 修复了在 Select 多选模式下,于输入框内进行鼠标操作(如选择文本)会导致下拉菜单意外关闭的问题。实现方式是在 mousedown 事件处理中增加了判断,当处于多选模式且下拉菜单已打开时,如果事件源是输入框内部,则阻止下拉菜单关闭。这个改动逻辑清晰,解决了描述的问题。同时,新增的测试用例也很好地覆盖了该场景,确保了修复的有效性。我有一个小的代码可读性建议,请见具体评论。

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
tests/Multiple.test.tsx (1)

716-731: 建议补一个“对照关闭”断言,避免防关闭范围未来被意外放大。

当前只验证了 input 内不关闭;可再补一次非 input 区域点击后应关闭,测试约束会更完整。

可选测试补充示例
   it('should not close dropdown when mousedown on input in multiple mode (text selection)', () => {
     const { container } = render(
       <Select mode="multiple" showSearch options={[{ value: 'light' }]} />,
     );

     const input = container.querySelector('input') as HTMLInputElement;

     // Open dropdown first
     toggleOpen(container);
     expectOpen(container, true);

     // Start interaction from input (simulate starting text selection / cursor move)
     fireEvent.mouseDown(input);

     expectOpen(container, true);
+
+    // Control: clicking non-input area should still close
+    fireEvent.mouseDown(container.querySelector('.rc-select-selector') as HTMLElement);
+    expectOpen(container, false);
   });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/Multiple.test.tsx` around lines 716 - 731, The test 'should not close
dropdown when mousedown on input in multiple mode (text selection)' only asserts
that a mousedown on the input doesn't close the dropdown; add a complementary
assertion that a mousedown on a non-input area closes it to prevent accidental
widening of the "do not close" scope—after the existing input mouseDown and
expectOpen(container, true) steps, simulate a mouseDown on a non-input element
(e.g., container or document body) using fireEvent.mouseDown and then call
expectOpen(container, false) to verify the dropdown closes; refer to the
existing helpers toggleOpen, expectOpen and the input variable used in this test
when adding this check.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@tests/Multiple.test.tsx`:
- Around line 716-731: The test 'should not close dropdown when mousedown on
input in multiple mode (text selection)' only asserts that a mousedown on the
input doesn't close the dropdown; add a complementary assertion that a mousedown
on a non-input area closes it to prevent accidental widening of the "do not
close" scope—after the existing input mouseDown and expectOpen(container, true)
steps, simulate a mouseDown on a non-input element (e.g., container or document
body) using fireEvent.mouseDown and then call expectOpen(container, false) to
verify the dropdown closes; refer to the existing helpers toggleOpen, expectOpen
and the input variable used in this test when adding this check.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 700c9f8 and 0708f78.

📒 Files selected for processing (2)
  • src/SelectInput/index.tsx
  • tests/Multiple.test.tsx

@zombieJ
Copy link
Member

zombieJ commented Feb 25, 2026

@gemini-code-assist pls help to CR again

Copy link
Contributor

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

Choose a reason for hiding this comment

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

Code Review

本次 PR 旨在修复 Select 组件在 multiple 模式下,当下拉框已打开时,在输入框内进行鼠标操作会意外导致下拉框关闭的问题。您的修改方案是正确的,通过在 onInternalMouseDown 事件处理器中增加一个条件判断,当处于 multiple 模式且点击事件源于输入框时,阻止下拉框的关闭。这个逻辑是清晰且有效的。此外,您添加的回归测试能够很好地覆盖这个场景,确保了修复的稳定性。整体而言,这是一个出色的修复。

@codecov
Copy link

codecov bot commented Feb 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.43%. Comparing base (700c9f8) to head (9485e8e).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1214   +/-   ##
=======================================
  Coverage   99.43%   99.43%           
=======================================
  Files          31       31           
  Lines        1239     1242    +3     
  Branches      427      452   +25     
=======================================
+ Hits         1232     1235    +3     
  Misses          7        7           

☔ View full report in Codecov by Sentry.
📢 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.

@zombieJ zombieJ merged commit dc0ffc0 into react-component:master Feb 26, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Select Multiple: cannot cursor-select the text inside the search box using mouse

2 participants