fix(datepicker): 解决未传默认值时限制时间边界不准确问题#3265
Conversation
Walkthrough本次更改调整了 Changes
Poem
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## feat_v3.x #3265 +/- ##
==========================================
Coverage 87.75% 87.75%
==========================================
Files 290 290
Lines 19111 19111
Branches 2938 2939 +1
==========================================
Hits 16771 16771
Misses 2335 2335
Partials 5 5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/packages/datepicker/utils.ts(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: test
- GitHub Check: build
| const selected = selectedDate ? new Date(selectedDate) : new Date(startDate) | ||
| if (!selected) return [] | ||
|
|
There was a problem hiding this comment.
🛠️ Refactor suggestion
selected 的有效性依旧得不到保证
Date 对象在 JS 中始终为 truthy 值,if (!selected) 无法判断其是否为 Invalid Date。
当 selectedDate 为非法时间戳(如 NaN)或 undefined 时,selected 依旧通过检查,随后参与边界计算会产生 NaN 级联,导致组件异常。
建议同时校验 selected 的合法性,或直接复用已引入的 isDate 工具:
- const selected = selectedDate ? new Date(selectedDate) : new Date(startDate)
- if (!selected) return []
+ const selected = selectedDate
+ ? new Date(selectedDate)
+ : new Date(startDate.getTime()) // 避免引用同一实例
+
+ // 无效日期直接返回空数组,防止后续 NaN 传播
+ if (!isDate(selected)) return []这样既能处理 selectedDate 缺省的场景,也能防止非法值进入后续逻辑。
另外,startDate 已是 Date 实例,如无必要可避免 new Date(startDate) 的重复包装。
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const selected = selectedDate ? new Date(selectedDate) : new Date(startDate) | |
| if (!selected) return [] | |
| const selected = selectedDate | |
| ? new Date(selectedDate) | |
| : new Date(startDate.getTime()) // 避免引用同一实例 | |
| // 无效日期直接返回空数组,防止后续 NaN 传播 | |
| if (!isDate(selected)) return [] |
🤖 Prompt for AI Agents
In src/packages/datepicker/utils.ts around lines 76 to 78, the check for
selected date validity is incorrect because a Date object is always truthy even
if invalid. Fix this by using the existing isDate utility to verify that
selected is a valid date instance. Also, avoid unnecessary new Date wrapping of
startDate since it is already a Date object. Update the code to validate
selected with isDate and handle invalid dates by returning an empty array early.
🤔 这个变动的性质是?
🔗 相关 Issue
💡 需求背景和解决方案
☑️ 请求合并前的自查清单
Summary by CodeRabbit