Skip to content

Conversation

@wanpan11
Copy link

@wanpan11 wanpan11 commented Oct 28, 2025

ref: ant-design/ant-design#55465 (comment)

duration = 0 | false 时 不自动关闭

Summary by CodeRabbit

  • 新功能

    • 通知组件的 duration 属性现在支持传入 false 来显式禁用自动关闭,相关行为(如进度显示与超时判断)被统一为对“假值”的一致处理。
  • 文档更新

    • 通知 API 文档将 duration 类型说明为 number | false,默认时长示例更新为 4.5 秒,表格中“type”列宽扩大以提升可读性,并补充 0/false 可禁用自动关闭的说明。
  • 重构

    • 内部时长处理被规范化以确保进度和自动关闭逻辑对假值一致响应。

@vercel
Copy link

vercel bot commented Oct 28, 2025

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

coderabbitai bot commented Oct 28, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

将通知的 duration 类型扩展为 number | false | null,在 Notice 组件中引入 mergedDuration 以统一处理 falsy 值并将基于 duration 的判断改为基于 mergedDuration 的 truthiness;同时更新文档表格与若干测试中 nullfalse 的用法。

Changes

Cohort / File(s) Change summary
类型定义
src/interface.ts, src/hooks/useNotification.tsx
NoticeConfig.durationOpenConfig.duration 的类型由 number | null 扩展为 number | false | null(接口签名更新)。
组件逻辑
src/Notice.tsx
引入 mergedDuration(将非数值归一为 0),将所有基于 duration 的判断、超时调度、进度计算与 effect 依赖改为使用 mergedDuration(从而按 truthiness 处理 false/null/0)。
文档
README.md
将 props 表格中 “type” 列宽从 50px 增加到 200px;更新 notification.noticeduration 的类型、默认值描述及说明(0/false 禁用自动关闭的解释)。
测试
tests/index.test.tsx, tests/stack.test.tsx
将多处测试用例中 duration: null 替换为 duration: false,以匹配新的显式禁用语义。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant API as Notification API
    participant Notice as Notice Component
    participant Timer

    User->>API: open(config)
    API->>Notice: render with config.duration
    Note right of Notice #DDEBF7: Notice 内部计算 mergedDuration\n(非数值归一为 0)
    alt mergedDuration > 0
        Note right of Notice #E6F4EA: 启动自动关闭与进度显示
        Notice->>Timer: setTimeout(mergedDuration * 1000)
        Timer-->>Notice: timeout -> trigger close
        Notice->>API: emit close
    else mergedDuration === 0
        Note right of Notice #FFF4E6: 不启动超时,进度隐藏
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • 变更涉及类型声明、通知组件核心定时逻辑、文档与测试,模式一致但有细微行为边界需核验。
  • 重点检查:
    • src/Notice.tsx 中对 0falsenull 的区分和预期行为(是否应把 0 视为禁用或有效短时);
    • effect 依赖数组与定时器清理是否正确避免竞态;
    • 公共接口变更对外部调用方的兼容性和测试覆盖。

Suggested reviewers

  • zombieJ
  • afc163

Poem

🐰 我是小兔跑通知,
duration 有了新身分,
false 把超时悄悄藏,
mergedDuration 细心算,
测试文档都跳舞,胡萝卜请我尝。 🥕

Pre-merge checks and finishing touches

❌ 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%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed 该拉取请求的标题"feat: Change duration to number|false type"直接反映了核心变更:将duration属性的类型从原来的number | null扩展为number | false | null,从而允许使用false值来禁用通知的自动关闭功能。标题简洁明确,特别指出了duration类型的改变,使得查看代码历史的团队成员能够迅速理解这次变更的主要目的。虽然标题在技术上没有完整列出所有类型选项(缺少null),但标题已清晰表达了这次提交的关键创新点,即引入false作为新的有效值。
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

Summary of Changes

Hello @wanpan11, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant functional improvement by extending the duration property of notifications to accept a false value. This change allows developers to create persistent notifications that do not automatically close, providing more flexible control over user alerts. The implementation involves updating type definitions, adjusting internal logic to correctly interpret false as an indefinite duration, and updating documentation and test cases to align with this new behavior.

Highlights

  • Enhanced Notification Control: The duration property for notifications now accepts false as a valid value, allowing notifications to remain open indefinitely until manually dismissed.
  • Type Definition Updates: Type definitions across src/hooks/useNotification.tsx and src/interface.ts have been updated to reflect duration as number | false, ensuring type safety for the new behavior.
  • Logic Adjustment for Auto-Closing: The internal logic in src/Notice.tsx for mergedShowProgress and the auto-closing useEffect now correctly handles duration: false, preventing the timer from starting when indefinite duration is intended.
  • Documentation and Test Alignment: The README.md has been updated to reflect the new duration type and an adjusted default value. Existing test cases in tests/index.test.tsx and tests/stack.test.tsx have been modified to use duration: false instead of duration: null.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@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

这个 PR 正确地将 duration prop 的类型更改为 number | false,以控制通知的自动关闭行为。该实现在类型定义、组件逻辑和测试中都是一致的,现在可以正确处理 false0 来阻止自动关闭。我唯一的建议是改进 README.md 中的文档,以更清晰地反映这一新行为。

Copy link

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 307d8a7 and cc4a0db.

📒 Files selected for processing (6)
  • README.md (2 hunks)
  • src/Notice.tsx (2 hunks)
  • src/hooks/useNotification.tsx (1 hunks)
  • src/interface.ts (2 hunks)
  • tests/index.test.tsx (4 hunks)
  • tests/stack.test.tsx (2 hunks)
🔇 Additional comments (7)
README.md (1)

100-100: 列宽调整合理

将 type 列宽从 50px 增加到 200px 是合理的,因为新的类型 number | false 需要更多空间来显示。

tests/stack.test.tsx (1)

20-20: 测试更新正确对齐新的 API

测试正确地将 duration: null 更改为 duration: false,这与新的类型定义 number | false 一致。使用 false 来禁用自动关闭功能是清晰且符合语义的。

Also applies to: 59-59

tests/index.test.tsx (1)

165-165: 测试用例统一更新为新的 API

所有相关测试用例都正确地从 duration: null 迁移到 duration: false。这些更改:

  • 与新的类型定义一致
  • 保持了禁用自动关闭的原有功能
  • 使用 falsenull 语义更清晰

Also applies to: 195-195, 207-207, 493-493

src/hooks/useNotification.tsx (1)

22-22: 类型定义更新正确

NotificationConfig 接口中的 duration 类型从 number 扩展到 number | false,这与 src/interface.ts 中的类型定义保持一致,允许用户通过传入 false 来禁用自动关闭功能。

src/Notice.tsx (2)

41-41: 进度条显示逻辑更新正确

将条件从 duration > 0 && showProgress 改为 duration && showProgress 是正确的。新逻辑:

  • duration = falseduration = 0 → 不显示进度条(正确)
  • duration > 0 → 显示进度条(如果 showProgress 为 true)

这与新的 API 语义一致,其中 false0 都表示禁用自动关闭。


56-56: 自动关闭逻辑更新正确

将条件从 duration > 0 改为 duration 的真值检查是正确的。新行为:

  • duration = false → 不会自动关闭(新功能)
  • duration = 0 → 不会自动关闭(保持原有行为)
  • duration > 0 → 按指定时间自动关闭

注意:此更改确保了 duration = 0duration = false 行为一致,都表示禁用自动关闭。这比之前的 duration > 0 检查更符合 JavaScript 的真值语义。

src/interface.ts (1)

9-9: 核心类型定义更新合理

duration 类型从 number | null 更改为 number | false 是一个很好的改进:

优点

  1. falsenull 更明确地表达"禁用"的语义
  2. 在 TypeScript 中,false 作为布尔值比 null 更容易进行类型检查
  3. 与其他配置选项的常见模式一致(如 closable: boolean | object

一致性

  • src/hooks/useNotification.tsx 中的 NotificationConfig 类型一致
  • src/Notice.tsx 中的真值检查逻辑一致

Also applies to: 35-35

@codecov
Copy link

codecov bot commented Oct 28, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.74%. Comparing base (307d8a7) to head (e8483f3).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #379   +/-   ##
=======================================
  Coverage   99.74%   99.74%           
=======================================
  Files           7        7           
  Lines         775      776    +1     
  Branches      126      127    +1     
=======================================
+ Hits          773      774    +1     
  Misses          2        2           

☔ 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

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/Notice.tsx (1)

41-41: mergedDuration 归一化方法合理

将非数字类型的 duration 值统一转换为 0 是清晰的实现方式,后续的 > 0 检查可以统一处理所有禁用自动关闭的情况(falsenull0)。

可选建议: 考虑增加对 Infinity 和负数的显式防护,确保只有有效的正数才会触发自动关闭:

-const mergedDuration: number = typeof duration === 'number' ? duration : 0;
+const mergedDuration: number = typeof duration === 'number' && isFinite(duration) && duration > 0 ? duration : 0;

这样可以避免边缘情况(虽然极少见):

  • Infinity 会导致 setTimeout 永不触发且进度条卡在 0%
  • 负数的语义不明确(虽然当前实现安全地将其视为"不自动关闭")
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b9ce131 and e8483f3.

📒 Files selected for processing (3)
  • src/Notice.tsx (5 hunks)
  • src/hooks/useNotification.tsx (1 hunks)
  • src/interface.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/hooks/useNotification.tsx
🔇 Additional comments (3)
src/interface.ts (1)

9-9: 类型扩展正确,保持了向后兼容性

duration 类型从 number | null 扩展到 number | false | null 是合理的改动:

  • 添加 false 选项允许显式禁用自动关闭功能
  • 保留 null 确保现有代码升级时不会出现兼容性问题

两个接口(NoticeConfigOpenConfig)的类型定义保持一致。

Also applies to: 35-35

src/Notice.tsx (2)

42-42: 自动关闭逻辑正确更新

相关改动正确地使用了 mergedDuration

  • Line 42: mergedShowProgress 基于 mergedDuration > 0 判断,只有有效正数才显示进度条
  • Line 57: 自动关闭的触发条件改为 mergedDuration > 0,符合预期语义
  • Line 63: 超时计算使用 mergedDuration * 1000
  • Line 74: 依赖数组正确更新为 [mergedDuration, ...],因为 effect 内部使用的是归一化后的值

这些改动确保了只有有效的正数 duration 才会触发自动关闭,0falsenull 等值都会被正确处理为"不自动关闭"。

Also applies to: 57-74


85-85: 进度条计算逻辑正确更新

进度条相关的改动与超时逻辑保持一致:

  • Line 85: 进度计算使用 mergedDuration * 1000 作为总时长
  • Line 102: 依赖数组正确包含 mergedDuration

改动确保了进度条的行为与自动关闭逻辑同步,只有在 mergedDuration > 0 时才会显示和更新进度。

Also applies to: 102-102

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.

3 participants