Skip to content

fix: allow clearing the value via keyboard - #1247

Merged
yoyo837 merged 2 commits into
react-component:masterfrom
Pareder:fix/clear-button-keyboard-press
Aug 11, 2026
Merged

fix: allow clearing the value via keyboard#1247
yoyo837 merged 2 commits into
react-component:masterfrom
Pareder:fix/clear-button-keyboard-press

Conversation

@Pareder

@Pareder Pareder commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

The clear button sits inside the select root, whose onKeyDown calls preventDefault() on Enter/Space to open the dropdown. That canceled the button's native activation, so no click fired and the value was never cleared — the dropdown just opened instead. The button now stops propagation of Enter/Space so they only activate the button.

Summary by CodeRabbit

  • Bug 修复
    • 修复选择器清除按钮的键盘操作,按下 Enter 或空格键时可正常执行清除。
    • 防止键盘操作意外打开下拉菜单或触发其他选择器行为。

@vercel

vercel Bot commented Aug 7, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

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 Plus

Run ID: eedd7daf-4fa3-446c-8702-7084a3d4e434

📥 Commits

Reviewing files that changed from the base of the PR and between 4d0641e and 464f05e.

📒 Files selected for processing (1)
  • tests/shared/allowClearTest.tsx

Walkthrough

清除按钮新增 Enter 和 Space 键盘事件处理。处理器阻止事件冒泡,并保留按钮原生激活流程。测试覆盖两种按键,并确认下拉菜单不会打开。

Changes

清除按钮键盘交互

Layer / File(s) Summary
键盘事件处理与验证
src/SelectInput/index.tsx, tests/shared/allowClearTest.tsx
清除按钮在 Enter 和 Space 按键时阻止事件冒泡。测试验证默认行为未被阻止、下拉菜单不会打开,并确认单值和数组值可以清除。

Estimated code review effort: 2 (简单) | ~10 分钟

Poem

我是小兔,轻敲清除键,
Enter、Space 都听见。
冒泡停下不添乱,
原生点击照常现。
下拉菜单安安眠。

🚥 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 标题准确概括了通过键盘操作清除 Select 值这一主要变更,内容清晰且具体。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 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.

@codecov

codecov Bot commented Aug 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.45%. Comparing base (dcd39c2) to head (464f05e).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1247   +/-   ##
=======================================
  Coverage   99.44%   99.45%           
=======================================
  Files          31       31           
  Lines        1271     1274    +3     
  Branches      466      445   -21     
=======================================
+ Hits         1264     1267    +3     
  Misses          7        7           

☔ 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.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@tests/shared/allowClearTest.tsx`:
- Around line 26-40: 增强“keeps Enter/Space on the clear button local to it”测试:分别为
Enter 和 Space 执行完整键盘激活流程,而非仅触发 keydown;为 Select 提供 onClear 断言,并验证触发后单选值为
undefined、多选值为空数组、Input 值已清空,同时保留下拉未打开的断言。
🪄 Autofix

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 Plus

Run ID: c46284d6-ab09-4c3c-b382-f8ee508f964f

📥 Commits

Reviewing files that changed from the base of the PR and between dcd39c2 and 4d0641e.

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

Comment thread tests/shared/allowClearTest.tsx Outdated
@Pareder

Pareder commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

@afc163 @zombieJ @yoyo837 Could you please review?

@yoyo837 yoyo837 left a comment

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.

Thanks for this fix, @Pareder! This is a clean and well-targeted change.

The problem: When the clear button is focused and the user presses Enter/Space, the keydown event bubbles up to a parent handler that calls preventDefault() (to open the dropdown), which cancels the native button activation and prevents click from firing. The fix — stopPropagation() on Enter/Space scoped to the button — correctly isolates the clear button keyboard interaction from the dropdown toggle logic.

What I like:

  • The fix uses stopPropagation rather than modifying the parent handler to check event.target, which is cleaner and avoids leaking implementation details upward
  • The test properly verifies both that defaultPrevented is false (root handler did not interfere) and that the dropdown did not open
  • Coverage check passes — all lines are covered

One small suggestion (non-blocking): The test currently verifies the negative case (dropdown did not open, no preventDefault). It would be even stronger to also verify the positive case — that Enter/Space on the clear button actually triggers a clear through the full keyboard activation flow (keydown → click → value cleared). This would catch regressions where the button renders but its click handler is disconnected for some reason. Something like:

fireEvent.keyDown(clear, { key: "Enter" });
// The native button activation should fire a click
fireEvent.click(clear);
expect(onChange).toHaveBeenCalledWith(undefined, undefined);

That said, the existing "clears value" test already covers the click path, so this is more of a nice-to-have for keyboard-specific coverage. Not blocking.

@Pareder

Pareder commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

@yoyo837 Done, adjusted the test case to check both negative and positive cases.

@yoyo837 yoyo837 left a comment

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.

The new keyboard test looks great — it covers exactly the full activation flow (keyDown → click → value cleared) for both Enter and Space. The assertions check that the root handler did not interfere (defaultPrevented: false, no dropdown open) and that the clear actually happened (onChange, onClear, input cleared).

Still looks good to merge. 👍

@yoyo837

yoyo837 commented Aug 10, 2026

Copy link
Copy Markdown
Member

The plan is to merge them after the release of antd@6.6.0.

@yoyo837
yoyo837 merged commit 2c17b60 into react-component:master Aug 11, 2026
11 of 12 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.

2 participants