Skip to content

fix: prevent onSelect firing twice on Enter when dropdown is open#1219

Merged
zombieJ merged 3 commits intoreact-component:masterfrom
QDyanbing:fix/select-trgger-twice
Mar 16, 2026
Merged

fix: prevent onSelect firing twice on Enter when dropdown is open#1219
zombieJ merged 3 commits intoreact-component:masterfrom
QDyanbing:fix/select-trgger-twice

Conversation

@QDyanbing
Copy link
Contributor

@QDyanbing QDyanbing commented Mar 15, 2026

问题

mode="tags" 下,用户输入(如 "da")后按回车选择下拉中的选项(如 "Dacryoadenitis")时,onSelect 会触发两次:

  1. 一次把输入内容当成新 tag(输入值)
  2. 一次为选中的选项

关联缺陷

fix ant-design/ant-design#57285

原因

语义化重构(#1166)之后,SelectInput/Input.tsx 里在 tags 模式下按 Enter 会无条件调用 onSearchSubmit。旧版 Selector 只在菜单关闭时(!open)才调用,重构时漏掉了这个条件,造成回归。

修改

仅在下拉关闭!open)时调用 onSearchSubmit。下拉打开时由 OptionList 处理 Enter 选中当前高亮项(与 BaseSelect 注释中的设计一致)。

变更说明

  • src/SelectInput/Input.tsx:在 tags 模式下,按 Enter 时增加 !open 判断再调用 onSearchSubmit
  • tests/Tags.test.tsx:新增用例「should trigger onSelect once when pressing Enter to select option in tags mode (dropdown open)」,防止该问题再次出现。

验证

  • 去掉本次修改:新用例失败(onSelect 被调用两次)。
  • 保留本次修改:新用例通过(onSelect 仅调用一次)。
  • 下拉关闭时按 Enter 仍会把输入当新 tag 添加,行为不变。

Summary by CodeRabbit

发布说明

  • Bug Fixes

    • 修复标签模式下 Enter 键行为:仅在下拉关闭时才触发提交,避免在列表打开时误触发提交或选择。
  • Tests

    • 新增测试用例:验证在标签模式且列表打开时按 Enter 仅触发一次选中行为,确保交互稳定。

@vercel
Copy link

vercel bot commented Mar 15, 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.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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!

此拉取请求旨在解决 Select 组件在 tags 模式下,用户在下拉菜单打开时按 Enter 键选择选项时 onSelect 事件会意外触发两次的问题。通过调整 Enter 键的处理逻辑,确保了 onSelect 行为的正确性,避免了重复创建标签,从而提升了用户体验和组件的稳定性。

Highlights

  • 修复 onSelect 重复触发问题: 在 mode="tags" 模式下,当下拉菜单打开时,按 Enter 键选择选项不再导致 onSelect 事件触发两次。
  • 优化 Enter 键处理逻辑: 在 SelectInput/Input.tsx 中,为 Enter 键提交搜索的逻辑增加了 !open 条件判断,确保只有在下拉菜单关闭时才将输入内容作为新标签提交。
  • 新增回归测试: 添加了一个新的测试用例 (Tags.test.tsx),以验证在 tags 模式下,当下拉菜单打开时按 Enter 键选择选项时 onSelect 只触发一次,防止未来出现类似问题。

🧠 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/Input.tsx
    • tags 模式下,处理 Enter 键时增加了对下拉菜单是否关闭 (!open) 的判断。
  • tests/Tags.test.tsx
    • 新增了 keyUp 导入。
    • 添加了一个测试用例,验证在 tags 模式下,当下拉菜单打开时按 Enter 键选择选项时 onSelect 只触发一次。
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.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 15, 2026

Walkthrough

在标签模式(mode="tags")下,Enter 提交的额外前置条件:仅在下拉列表关闭时才会触发 onSearchSubmit;新增测试验证在下拉打开时按 Enter 选择选项只触发 onSelect 一次。

Changes

Cohort / File(s) Summary
输入框逻辑
src/SelectInput/Input.tsx
为标签模式下按 Enter 的处理增加了额外守卫:要求下拉框处于关闭状态才调用 onSearchSubmit(保留原有的 Enter、tags 模式、composition 状态与 onSearchSubmit 存在的判断)。
测试用例
tests/Tags.test.tsx
新增测试:在 mode="tags"、下拉打开且 showSearch 的情况下,模拟按 Enter 选择选项,断言 onSelect 被调用且只调用一次;同时扩展了对 utils/common 的导入以包含 keyUp 辅助。

Sequence Diagram(s)

(无)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • afc163
  • zombieJ

诗歌

🐰 回车轻敲心不慌,
下拉先合才放心,
标签跳跃选项稳,
测试一条守护真,
小兔欢跳庆更笃信!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed 标题准确概括了拉取请求的主要变更:通过添加!open条件防止在下拉列表打开时Enter键触发onSelect两次。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

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.

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 通过在 Input.tsxonKeyDown 事件处理器中增加 !open 条件,修复了在 tags 模式下下拉框展开时按回车键 onSelect 会触发两次的问题。这个修改逻辑正确,直接解决了所述问题。同时,PR 中也包含了 tests/Tags.test.tsx 的更新,增加了一个新的测试用例来覆盖这个场景,防止未来发生回归,这一点做得很好。我对测试代码提出了一点小建议,旨在提高其可读性和健壮性,具体请看文件中的评论。

@codecov
Copy link

codecov bot commented Mar 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.44%. Comparing base (fe73bbf) to head (1cc6349).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1219   +/-   ##
=======================================
  Coverage   99.44%   99.44%           
=======================================
  Files          31       31           
  Lines        1251     1256    +5     
  Branches      454      437   -17     
=======================================
+ Hits         1244     1249    +5     
  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.

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/Tags.test.tsx`:
- Around line 150-171: The test currently only asserts handleSelect call count
which can hide semantic failures; update the test case for the Select in tags
mode to also assert the onSelect payload to ensure the highlighted option was
actually selected. After the Enter key sequence (where handleSelect jest.fn() is
invoked), add an assertion checking handleSelect was called with the expected
selected value/option (e.g., matching value 'opt1' or the option object for the
first highlighted item) so the test verifies both occurrence and correctness for
onSelect.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 05edff92-9401-4ef5-9263-389b3c2d5486

📥 Commits

Reviewing files that changed from the base of the PR and between fe73bbf and 1daf881.

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


// Handle Enter key submission - referencing Selector implementation
if (key === 'Enter' && mode === 'tags' && !compositionStatusRef.current && onSearchSubmit) {
if (
Copy link
Member

Choose a reason for hiding this comment

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

是不是应该检测一下是否有内容再决定?按理说这个 Input 的优先级会更高,只有没有内容的时候再认为是 option 响应会比较好~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

其他地方应该有处理
image
这里只补充了!open

@zombieJ zombieJ merged commit 0c61753 into react-component:master Mar 16, 2026
9 of 10 checks passed
@QDyanbing QDyanbing deleted the fix/select-trgger-twice branch March 16, 2026 06:17
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 mode="tags" triggers onSelect twice (input value selected option)

2 participants