Skip to content

Conversation

@zombieJ
Copy link
Member

@zombieJ zombieJ commented Nov 26, 2025

close ant-design/ant-design#55924

Summary by CodeRabbit

发布说明

  • Bug 修复

    • 改进了输入框组件的点击事件处理,确保在输入框内部元素上的点击不会触发意外的行为。
  • 测试

    • 添加了新的测试用例以验证输入框内部的事件处理逻辑。

✏️ Tip: You can customize this high-level summary in your review settings.

@vercel
Copy link

vercel bot commented Nov 26, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
select Ready Ready Preview Comment Nov 26, 2025 8:43am

@gemini-code-assist
Copy link

Summary of Changes

Hello @zombieJ, 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 SelectInput component where preventDefault was inadvertently called during mouseDown events on custom input elements, particularly when the interaction was with a child element nested within the custom input. The updated event handling logic now accurately determines if the click originated from within the custom input's structure, ensuring correct behavior and preventing unintended side effects.

Highlights

  • Bug Fix for Custom Input Interaction: Addresses an issue where preventDefault was incorrectly triggered on mouseDown events within custom input elements, specifically when interacting with child elements.
  • Improved Event Handling Logic: The onInternalMouseDown handler in SelectInput now checks if the event target is the input DOM element itself or any of its descendants, preventing preventDefault from being called unnecessarily in such cases.
  • New Test Case: A dedicated test has been added to confirm that preventDefault is not invoked when a mouseDown event occurs on a child element within a customized input, ensuring the fix works as expected.
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 Nov 26, 2025

Walkthrough

修改 SelectInput 的 onInternalMouseDown 事件处理逻辑,增强 preventDefault 的条件判断。现在仅在点击目标位于输入框外时才阻止默认行为,而对输入框内部(包括子元素如 textarea)的点击不再触发 preventDefault。

Changes

Cohort / File(s) 变更摘要
事件处理逻辑修复
src/SelectInput/index.tsx
强化 onInternalMouseDown 中的条件判断,将 preventDefault 限制在点击目标位于输入框 DOM 外部的情况,允许输入框内部的子元素(如自定义 textarea)正常接收事件和焦点
测试覆盖
tests/components.test.tsx
新增测试用例验证 textarea 上的 mouseDown 事件不触发 preventDefault;补充导入 createEvent 和 fireEvent

Sequence Diagram(s)

sequenceDiagram
    participant User as 用户
    participant Input as 输入框<br/>(含自定义元素)
    participant Handler as onInternalMouseDown
    participant Event as 事件默认行为

    rect rgb(220, 245, 220)
    Note over User,Event: 修复前:点击 textarea 被阻止
    User->>Input: 点击 textarea
    Input->>Handler: 触发 mouseDown
    Handler->>Event: preventDefault()<br/>(无条件)
    Event-->>Input: 焦点获取被阻止 ❌
    end

    rect rgb(245, 220, 220)
    Note over User,Event: 修复后:点击 textarea 正常工作
    User->>Input: 点击 textarea
    Input->>Handler: 触发 mouseDown
    Handler->>Handler: 检查点击目标<br/>是否在输入框内?
    alt 在输入框内
        Handler-->>Event: 不调用 preventDefault ✓
    else 在输入框外
        Handler->>Event: preventDefault()
    end
    Event-->>Input: 焦点正常获取 ✓
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • 需要重点关注的区域:
    • onInternalMouseDown 中条件判断的逻辑变化,确保 contains 检查能正确识别所有情况
    • 新增测试用例验证自定义输入元素(textarea)的焦点获取能力
    • 确认此修改不会影响其他依赖 preventDefault 行为的场景

Possibly related PRs

Suggested reviewers

  • afc163

Poem

🐰 用户点击 textarea,焦点却无处可寻,
调整防御条件句,区分内外有妙技,
自定义输入组件,今日终于能输入,
一行逻辑显威力,问题迎刃而解矣!✨

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题'fix: not prevent when child'简洁清晰地反映了主要改动,即修复在子元素上不阻止默认行为的问题。
Linked Issues check ✅ Passed 代码变更修复了AutoComplete自定义输入组件中输入框无法获焦和输入的问题,完全符合#55924的需求。
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 docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-prevent

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

@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

This pull request addresses a bug where preventDefault was being called on child elements within a custom select input, which blocked user interaction. The fix in src/SelectInput/index.tsx correctly checks if the event target is a descendant of the input element before preventing the default behavior. A corresponding test has been added in tests/components.test.tsx to validate this change. The implementation is solid, though I have one suggestion to enhance type safety.

if (!disabled) {
if (event.target !== getDOM(inputRef.current)) {
const inputDOM = getDOM(inputRef.current);
if (inputDOM && event.target !== inputDOM && !inputDOM.contains(event.target as Node)) {

Choose a reason for hiding this comment

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

medium

For better type safety and to avoid an explicit type cast, it's preferable to use a type guard like instanceof Node to verify that event.target is a DOM node before using DOM-specific methods such as contains.

Suggested change
if (inputDOM && event.target !== inputDOM && !inputDOM.contains(event.target as Node)) {
if (inputDOM && event.target instanceof Node && event.target !== inputDOM && !inputDOM.contains(event.target)) {

@codecov
Copy link

codecov bot commented Nov 26, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.41%. Comparing base (7b9b2aa) to head (e23d947).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1174   +/-   ##
=======================================
  Coverage   99.41%   99.41%           
=======================================
  Files          31       31           
  Lines        1195     1196    +1     
  Branches      425      423    -2     
=======================================
+ Hits         1188     1189    +1     
  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 0ee6350 into master Nov 26, 2025
11 of 12 checks passed
@zombieJ zombieJ deleted the fix-prevent branch November 26, 2025 08:44
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: 0

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

26-47: 用例正确覆盖回归场景,可小幅增强健壮性

这个用例很好地锁定了「自定义输入包裹 textarea 时,不应调用 preventDefault」的行为变化,事件模拟方式(重写原生 event.preventDefaultfireEvent 触发)也能可靠捕获 onInternalMouseDown 中的调用,逻辑清晰。

可以考虑一个小优化:在创建事件前显式断言 textarea 存在,避免未来结构变动时代码在 createEvent 处直接抛错,而不是给出更直观的断言失败信息,例如:

-    const textareaEle = container.querySelector('textarea');
+    const textareaEle = container.querySelector('textarea');
+    expect(textareaEle).not.toBeNull();

整体上用例设计是对本次修复非常有价值的补充。

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 055a591 and e23d947.

📒 Files selected for processing (2)
  • src/SelectInput/index.tsx (1 hunks)
  • tests/components.test.tsx (2 hunks)
🔇 Additional comments (2)
tests/components.test.tsx (1)

1-1: 测试库导入与新用例匹配良好

这里补充导入 createEventfireEvent,与后面新增的用例需求完全对齐,没有多余依赖,看起来是合理且必要的调整。

src/SelectInput/index.tsx (1)

172-177: 有针对性地缩小 preventDefault 作用范围,修复自定义子输入无法获得焦点的问题

这里通过 inputDOM + contains 判断,仅在点击目标既不是输入根节点、也不在其 DOM 子树内时才调用 event.preventDefault(),从而允许像 <div><textarea /></div> 这类包裹结构内的实际输入元素获得浏览器默认的 focus 与光标行为,逻辑上与新增测试以及关联问题描述是一致的。

需要留意的一点是:当 inputRef.current 尚未挂载时,现在会跳过 preventDefault(旧逻辑在这种瞬间也会阻止默认行为)。这个行为差异理论上只会出现在极短的初始阶段,通常影响很小,但建议在实际交互回归中顺带确认是否有意料之外的闪动或聚焦差异。

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.

AutoComplete自定义输入组件中的Input无法输入

2 participants