Skip to content

fix: avoid submitting unconfirmed range values on blur#966

Open
QDyanbing wants to merge 4 commits intoreact-component:masterfrom
QDyanbing:codex/fix-range-picker-unconfirmed-blur
Open

fix: avoid submitting unconfirmed range values on blur#966
QDyanbing wants to merge 4 commits intoreact-component:masterfrom
QDyanbing:codex/fix-range-picker-unconfirmed-blur

Conversation

@QDyanbing
Copy link
Copy Markdown
Contributor

@QDyanbing QDyanbing commented Apr 20, 2026

Summary

  • avoid marking plain range input focus switches as input operations when needConfirm is enabled
  • keep the existing blur-submit behavior for !needConfirm flows
  • add a regression test for showTime + allowEmpty without pressing OK

Testing

  • npm test -- --runInBand tests/range.spec.tsx tests/new-range.spec.tsx
  • npm test -- --runInBand

Fixes ant-design/ant-design#57728

Summary by CodeRabbit

发布说明

  • New Features

    • 增加键盘/鼠标切换识别,区分通过 Tab 键的切换与其他焦点变化以改善确认行为。
  • Bug Fixes

    • 修复在切换/失焦流程中错误触发部分确认或完整确认的问题,确保未确认状态下不会意外提交或清空错误字段。
    • 优化鼠标按下切换时的确认逻辑,部分确认行为更符合预期。
  • Tests

    • 扩充用例,覆盖显示时间与允许为空场景下的切换、失焦与提交差异。

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 20, 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.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: da74b1cb-80df-4457-9d36-0c413ebd7ea7

📥 Commits

Reviewing files that changed from the base of the PR and between 5853032 and cc1730c.

📒 Files selected for processing (2)
  • src/PickerInput/RangePicker.tsx
  • tests/range.spec.tsx
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/range.spec.tsx
  • src/PickerInput/RangePicker.tsx

Walkthrough

在 RangePicker 中加入键盘/鼠标切换跟踪与相关焦点/鼠标处理,调整在 needConfirm(如 showTime)模式下的失焦与字段切换提交逻辑;新增测试覆盖这些场景以防止未确认时的部分提交或误触发 onChange。

Changes

Cohort / File(s) Summary
RangePicker 实现
src/PickerInput/RangePicker.tsx
新增用于区分键盘驱动与鼠标驱动切换的 refs(pendingKeyboardSwitchRefkeyboardSwitchInputRef)、增加 onSelectorMouseDown 及相关 focus/blur 处理、在字段切换/blur 时基于键盘切换标记调整 triggerOpen 与部分确认(triggerPartConfirm)的触发条件;同时对泛型 extends 语法做了格式化调整(无 API 签名变化)。
测试用例
tests/range.spec.tsx
新增多条测试,覆盖 showTime + allowEmpty 情况下:在未按 OK 的字段切换与 blur 场景不应触发 onChange 或提交部分值;并验证不同的键盘/鼠标切换路径下提交/清空行为的差异。

Sequence Diagram(s)

(未生成序列图 — 变更集中在组件内部事件与状态流,交互主体有限)

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • zombieJ
  • afc163

Poem

🐰 我在键盘与鼠标间跳跃,
轻嗅焦点,标记每次切换,
若未按下 OK,我把叶子收回,
风不带走未确认的梦,
等你敲下确认,我们再庆祝 🎉

🚥 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 标题准确总结了主要变化:避免在blur时提交未确认的range值,这与PR的核心目标和代码变化直接相关。
Linked Issues check ✅ Passed PR实现了#57728要求的所有功能:通过新增keyboard switch追踪来区分键盘切换和其他焦点变化,确保showTime+allowEmpty模式下blur时不会提交未确认的值。
Out of Scope Changes check ✅ Passed 所有代码变化都与#57728关联,包括RangePicker.tsx中的键盘/鼠标切换追踪逻辑和range.spec.tsx中的blur行为测试,没有发现超出范围的更改。

✏️ 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.

Copy link
Copy Markdown
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

This pull request introduces formatting updates to the range picker interfaces and adds a conditional check to prevent setting the last operation to 'input' when confirmation is required. It also includes a new test case to ensure unconfirmed values are not submitted on blur when 'allowEmpty' is enabled. Feedback suggests that the current fix may not be robust enough to handle cases where a user types into the input, recommending that the '!needConfirm' check be moved to the 'useLayoutEffect' responsible for field leave logic.

Comment thread src/PickerInput/RangePicker.tsx Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 20, 2026

Codecov Report

❌ Patch coverage is 95.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 98.78%. Comparing base (738bcac) to head (cc1730c).

Files with missing lines Patch % Lines
src/PickerInput/RangePicker.tsx 95.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #966      +/-   ##
==========================================
- Coverage   98.81%   98.78%   -0.03%     
==========================================
  Files          65       65              
  Lines        2695     2714      +19     
  Branches      724      754      +30     
==========================================
+ Hits         2663     2681      +18     
- Misses         29       30       +1     
  Partials        3        3              

☔ 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
Copy Markdown

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

🧹 Nitpick comments (1)
tests/range.spec.tsx (1)

898-919: 建议增加暂存值已产生的前置断言。

当前测试只验证最终未提交;如果 selectCell(11) 未来没有真正触发暂存选择,测试仍可能因为 onChange 未调用、输入为空而误通过。建议断言 onCalendarChange 已收到未确认的 start 值,再执行 blur。

🧪 建议增强回归测试
 it('should not submit unconfirmed values on blur when allowEmpty lets fields switch', () => {
   const onChange = jest.fn();
-  const { container } = render(<DayRangePicker showTime allowEmpty onChange={onChange} />);
+  const onCalendarChange = jest.fn();
+  const { container } = render(
+    <DayRangePicker
+      showTime
+      allowEmpty
+      onChange={onChange}
+      onCalendarChange={onCalendarChange}
+    />,
+  );
 
   openPicker(container, 0);
   selectCell(11);
+  expect(onCalendarChange).toHaveBeenCalledWith(
+    expect.anything(),
+    ['1990-09-11 00:00:00', ''],
+    { range: 'start' },
+  );
 
   openPicker(container, 1);
   openPicker(container, 0);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/range.spec.tsx` around lines 898 - 919, Add a pre-assert that the
picker emitted the transient selection by creating a jest.fn() for
onCalendarChange and passing it into the DayRangePicker, call selectCell(11),
then assert onCalendarChange was invoked with an interim value containing the
expected unconfirmed start (e.g. first element non-empty/expected date) before
proceeding to the blur and final expect; reference the onCalendarChange prop,
selectCell, and DayRangePicker to locate where to add the spy and assertion.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@tests/range.spec.tsx`:
- Around line 898-919: Add a pre-assert that the picker emitted the transient
selection by creating a jest.fn() for onCalendarChange and passing it into the
DayRangePicker, call selectCell(11), then assert onCalendarChange was invoked
with an interim value containing the expected unconfirmed start (e.g. first
element non-empty/expected date) before proceeding to the blur and final expect;
reference the onCalendarChange prop, selectCell, and DayRangePicker to locate
where to add the spy and assertion.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f57b3f14-9664-4bb3-99d2-e6100496c0e0

📥 Commits

Reviewing files that changed from the base of the PR and between 738bcac and 08a2e63.

📒 Files selected for processing (2)
  • src/PickerInput/RangePicker.tsx
  • tests/range.spec.tsx

@afc163 afc163 requested a review from zombieJ April 20, 2026 13:11
Comment thread src/PickerInput/RangePicker.tsx Outdated
@QDyanbing QDyanbing force-pushed the codex/fix-range-picker-unconfirmed-blur branch from 5853032 to 2e520bb Compare April 22, 2026 09:50
Comment thread src/PickerInput/RangePicker.tsx Outdated
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.

RangePicker: spurious onChange on blur clears end date when using allowEmpty showTime without confirming via OK

2 participants