Skip to content

Support onClear callback in picker#976

Open
QDyanbing wants to merge 1 commit into
react-component:masterfrom
QDyanbing:support-onclear-event
Open

Support onClear callback in picker#976
QDyanbing wants to merge 1 commit into
react-component:masterfrom
QDyanbing:support-onclear-event

Conversation

@QDyanbing

@QDyanbing QDyanbing commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

变更内容

  • 为 Picker 和 RangePicker 增加公开的 onClear 回调类型。
  • 在点击清空按钮时触发 onClear,并保持现有清空与关闭弹层逻辑不变。
  • 补充单选和范围选择器的回归测试。
  • 更新 README API 表格说明。

背景

Ant Design 反馈 DatePicker / TimePicker 等支持清空的输入组件缺少清空回调。rc-picker 已有内部清空链路,但没有向外暴露对应 API。本次实现对齐 rc-input、rc-select 等组件的无参 onClear 设计。

关联 ant-design/ant-design#58349

验证

  • npm run lint:tsc
  • npm test -- --runTestsByPath tests/range.spec.tsx
  • npm test -- --runTestsByPath tests/picker.spec.tsx
  • npm run lint 无 error,仅有项目既有 warning
  • git diff --check

Summary by CodeRabbit

发布说明

  • 新增功能

    • 选择器组件现已支持 onClear 回调属性,在清除已选值时自动触发执行。
  • 文档

    • 补充了选择器 API 文档中关于 onClear 属性的详细说明。
  • 测试

    • 新增了对 onClear 回调功能的测试用例。

@vercel

vercel Bot commented Jun 13, 2026

Copy link
Copy Markdown

@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

coderabbitai Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@QDyanbing, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 16 minutes and 30 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a38a988b-d53d-4e31-b30d-f8073b6bd80e

📥 Commits

Reviewing files that changed from the base of the PR and between 32a7bef and 0ad2956.

📒 Files selected for processing (6)
  • README.md
  • src/PickerInput/RangePicker.tsx
  • src/PickerInput/SinglePicker.tsx
  • src/interface.tsx
  • tests/picker.spec.tsx
  • tests/range.spec.tsx

Walkthrough

为 Picker 组件新增 onClear 回调属性。通过接口定义、两个选择器实现、属性透传控制、测试用例和文档说明的完整流程,支持在清除操作时触发用户自定义的回调函数。

Changes

onClear 回调特性

Layer / File(s) Summary
SharedPickerProps 接口扩展
src/interface.tsx
在 SharedPickerProps 中新增可选属性 onClear?: VoidFunction,声明清除操作回调的公开合约。
SinglePicker 中的 onClear 实现
src/PickerInput/SinglePicker.tsx
从 filledProps 解构 onClear,在 onSelectorClear 流程中调用 onClear?.(),并在 panelProps 中通过 omit 排除 onClear 避免下传给 Popup。
RangePicker 中的 onClear 实现
src/PickerInput/RangePicker.tsx
从 filledProps 解构 onClear,在 onSelectorClear 流程中调用 onClear?.(),并在 panelProps 中通过 omit 排除 onClear 避免下传给 Popup。
测试覆盖
tests/picker.spec.tsx, tests/range.spec.tsx
为单选和范围选择器新增测试用例,验证 allowClear 场景下 clearValue() 触发 onClear 回调一次,且输入框值被清空。
文档更新
README.md
在 Picker API 属性表中新增 onClear 项,标注为清除按钮点击时的回调函数。

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • react-component/picker#971: 主 PR 在 SinglePicker/RangePicker 的清除流程中接入 onClear 回调,相关 PR 重构清除控制为基于按钮的 ClearIcon 实现并在点击时调用 onClear,两者都直接涉及相同的清除动作 onClear 处理。

Suggested reviewers

  • zombieJ
  • afc163

Poem

🐰 一个清除的回调,
值消失时轻声呼唤,
SinglePicker、RangePicker 齐声应答,
测试验证每一次触发,
文档轻轻记下这段故事。

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题"Support onClear callback in picker"准确简洁地概括了主要变更。
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.

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

✨ Finishing Touches
🧪 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 and usage tips.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces an onClear callback prop to both single and range pickers, which is triggered when the clear button is clicked, and includes corresponding unit tests and documentation. The reviewer pointed out that onClear is destructured but not omitted from panelProps in both RangePicker.tsx and SinglePicker.tsx. This omission could cause the prop to be passed down to the Popup component, potentially triggering React unrecognized prop warnings in the console. It is recommended to add 'onClear' to the omit list in both files.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/PickerInput/RangePicker.tsx
Comment thread src/PickerInput/SinglePicker.tsx
@QDyanbing QDyanbing force-pushed the support-onclear-event branch from 670ac71 to b9d7286 Compare June 13, 2026 11:49
@QDyanbing QDyanbing marked this pull request as ready for review June 13, 2026 11:50
@codecov

codecov Bot commented Jun 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.81%. Comparing base (fc61e3f) to head (0ad2956).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #976   +/-   ##
=======================================
  Coverage   98.81%   98.81%           
=======================================
  Files          66       66           
  Lines        2696     2698    +2     
  Branches      744      744           
=======================================
+ Hits         2664     2666    +2     
  Misses         29       29           
  Partials        3        3           

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

@QDyanbing QDyanbing force-pushed the support-onclear-event branch from b9d7286 to 32a7bef Compare June 13, 2026 11:54

@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

🧹 Nitpick comments (2)
tests/picker.spec.tsx (1)

948-956: ⚡ Quick win

建议显式验证 onClear 回调不接收任何参数。

当前测试仅检查了调用次数(toHaveBeenCalledTimes(1)),但根据 PR 描述和类型定义,onClear 应为 VoidFunction(无参数回调)。建议添加显式断言以验证回调签名:

  clearValue();
  expect(onClear).toHaveBeenCalledTimes(1);
+ expect(onClear).toHaveBeenCalledWith();
  expect(document.querySelector('input').value).toEqual('');

这样可以确保 API 契约得到遵守,防止未来意外引入参数导致的回归问题。

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

In `@tests/picker.spec.tsx` around lines 948 - 956, The test currently only
asserts onClear was called once; update the "trigger onClear when clear value"
test to also assert that onClear was invoked with no arguments by adding
expect(onClear).toHaveBeenCalledWith(); (keep existing render of DayPicker with
onClear and the clearValue() call unchanged) so the test verifies the callback
is a VoidFunction.
tests/range.spec.tsx (1)

1956-1970: ⚡ Quick win

建议显式验证 onClear 回调不接收任何参数。

当前测试仅检查了调用次数(toHaveBeenCalledTimes(1)),但根据 PR 描述和类型定义,onClear 应为 VoidFunction(无参数回调)。建议添加显式断言以验证回调签名:

  clearValue();
  expect(onClear).toHaveBeenCalledTimes(1);
+ expect(onClear).toHaveBeenCalledWith();
  expect(document.querySelector('input').value).toEqual('');

这样可以确保 API 契约得到遵守,防止未来意外引入参数导致的回归问题。

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

In `@tests/range.spec.tsx` around lines 1956 - 1970, Add an explicit assertion
that the onClear mock receives no arguments: after invoking clearValue() and the
existing expect(onClear).toHaveBeenCalledTimes(1), add
expect(onClear).toHaveBeenCalledWith() (or an equivalent zero-argument
assertion) to the test that renders DayRangePicker with allowClear; reference
the onClear mock, the DayRangePicker render, and the clearValue() helper so the
test verifies the callback is a VoidFunction.
🤖 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 `@README.md`:
- Line 54: 在 README 文档的 API 表中将 onClear 的类型从泛用 Function 明确为无参回调 (VoidFunction 或
() => void) 并在 RangePicker 的 API 表里新增一行同样声明 onClear: VoidFunction/() =>
void;在文中定位到 Picker 表格中对 onClear 的条目(标识符 onClear)替换类型文本,并在 RangePicker 表格(标识符
RangePicker)新增对应行,确保两处文档一致以免用户使用错误签名。

---

Nitpick comments:
In `@tests/picker.spec.tsx`:
- Around line 948-956: The test currently only asserts onClear was called once;
update the "trigger onClear when clear value" test to also assert that onClear
was invoked with no arguments by adding expect(onClear).toHaveBeenCalledWith();
(keep existing render of DayPicker with onClear and the clearValue() call
unchanged) so the test verifies the callback is a VoidFunction.

In `@tests/range.spec.tsx`:
- Around line 1956-1970: Add an explicit assertion that the onClear mock
receives no arguments: after invoking clearValue() and the existing
expect(onClear).toHaveBeenCalledTimes(1), add
expect(onClear).toHaveBeenCalledWith() (or an equivalent zero-argument
assertion) to the test that renders DayRangePicker with allowClear; reference
the onClear mock, the DayRangePicker render, and the clearValue() helper so the
test verifies the callback is a VoidFunction.
🪄 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: 1ec3ead7-297f-4e89-9d4f-d748cd24ea67

📥 Commits

Reviewing files that changed from the base of the PR and between fc61e3f and 32a7bef.

📒 Files selected for processing (6)
  • README.md
  • src/PickerInput/RangePicker.tsx
  • src/PickerInput/SinglePicker.tsx
  • src/interface.tsx
  • tests/picker.spec.tsx
  • tests/range.spec.tsx

Comment thread README.md Outdated
@QDyanbing QDyanbing force-pushed the support-onclear-event branch from 32a7bef to 0ad2956 Compare June 13, 2026 12:05
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.

1 participant