fix: allow clearing the value via keyboard - #1247
Conversation
|
@Pareder is attempting to deploy a commit to the React Component Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
Walkthrough清除按钮新增 Enter 和 Space 键盘事件处理。处理器阻止事件冒泡,并保留按钮原生激活流程。测试覆盖两种按键,并确认下拉菜单不会打开。 Changes清除按钮键盘交互
Estimated code review effort: 2 (简单) | ~10 分钟 Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
src/SelectInput/index.tsxtests/shared/allowClearTest.tsx
yoyo837
left a comment
There was a problem hiding this comment.
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
stopPropagationrather than modifying the parent handler to checkevent.target, which is cleaner and avoids leaking implementation details upward - The test properly verifies both that
defaultPreventedisfalse(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.
|
@yoyo837 Done, adjusted the test case to check both negative and positive cases. |
yoyo837
left a comment
There was a problem hiding this comment.
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. 👍
|
The plan is to merge them after the release of antd@6.6.0. |
The clear button sits inside the select root, whose
onKeyDowncallspreventDefault()on Enter/Space to open the dropdown. That canceled the button's native activation, so noclickfired 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