Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ 修复 InputNumber 在设置为 allow-null 时被赋值为空时未触发更新的问题并支持异步更新 #812

Merged
merged 1 commit into from
Jan 1, 2025

Conversation

Moonofweisheng
Copy link
Owner

@Moonofweisheng Moonofweisheng commented Jan 1, 2025

🤔 这个 PR 的性质是?(至少选择一个)

  • 日常 bug 修复
  • 新特性提交
  • 站点、文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • TypeScript 定义更新
  • CI/CD 改进
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 代码重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

💡 需求背景和解决方案

修复 InputNumber 在设置为 allow-null 时被赋值为空时未触发更新的问题并支持异步更新

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充

Summary by CodeRabbit

  • 新功能

    • wd-input-number 组件新增异步变更拦截功能
    • 支持通过 before-change 属性在输入值变更前进行验证和拦截
  • 文档更新

    • 更新 wd-input-number 组件文档
    • 新增异步变更使用示例
    • 调整 allow-null 属性描述
  • 改进

    • 优化输入数字组件的值变更逻辑
    • 增强组件交互性和灵活性

Copy link

vercel bot commented Jan 1, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
wot-design-uni ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 1, 2025 3:34pm

Copy link

coderabbitai bot commented Jan 1, 2025

遍历

此次更新为 wd-input-number 组件引入了异步变更功能。新增 before-change 属性允许在输入值变更前进行验证和拦截。文档和组件实现均已更新,支持通过返回布尔值或 Promise 来控制输入变更过程。同时,allow-null 属性的描述也得到了澄清。

变更

文件路径 变更概要
docs/component/input-number.md 新增异步变更部分,更新属性表
src/pages/inputNumber/Index.vue 添加异步变更演示代码,引入 value11beforeChange 函数
src/uni_modules/wot-design-uni/components/common/interceptor.ts 新增拦截器工具函数和类型
src/uni_modules/wot-design-uni/components/wd-input-number/types.ts 新增 InputNumberBeforeChange 类型,扩展 inputNumberProps
src/uni_modules/wot-design-uni/components/wd-input-number/wd-input-number.vue 重构输入值处理逻辑,引入 updateValue 方法

序列图

sequenceDiagram
    participant User
    participant InputNumber
    participant BeforeChangeHandler
    participant Component

    User->>InputNumber: 尝试改变值
    InputNumber->>BeforeChangeHandler: 调用 before-change
    BeforeChangeHandler-->>InputNumber: 返回 true/Promise<true>
    InputNumber->>Component: 更新值
    InputNumber-->>User: 显示新值
Loading

可能相关的 PR

诗歌

🐰 数字跳跃,异步舞蹈
输入拦截,魔法神奇
验证闪烁,Promise 轻唱
输入数字,自由飞翔!
兔子微笑,代码欢畅 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Jan 1, 2025

Deploy Preview for wot-design-uni ready!

Name Link
🔨 Latest commit f9a3831
🔍 Latest deploy log https://app.netlify.com/sites/wot-design-uni/deploys/6775606ce219f40008b6b9a0
😎 Deploy Preview https://deploy-preview-812--wot-design-uni.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

Deploying wot-design-uni with  Cloudflare Pages  Cloudflare Pages

Latest commit: f9a3831
Status: ✅  Deploy successful!
Preview URL: https://96486fe1.wot-design-uni.pages.dev
Branch Preview URL: https://fix-input-number-allow-null.wot-design-uni.pages.dev

View logs

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/uni_modules/wot-design-uni/components/common/interceptor.ts (1)

5-5: 可将 void 替换为 undefined 以减少歧义!
根据静态分析提示,当返回类型中含有 void 可能会造成疑惑,建议改为 undefined

-export type Interceptor = (...args: any[]) => Promise<boolean> | boolean | undefined | void
+export type Interceptor = (...args: any[]) => Promise<boolean> | boolean | undefined
🧰 Tools
🪛 Biome (1.9.4)

[error] 5-5: void is confusing inside a union type.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 583acc2 and f9a3831.

📒 Files selected for processing (5)
  • docs/component/input-number.md (2 hunks)
  • src/pages/inputNumber/Index.vue (3 hunks)
  • src/uni_modules/wot-design-uni/components/common/interceptor.ts (1 hunks)
  • src/uni_modules/wot-design-uni/components/wd-input-number/types.ts (2 hunks)
  • src/uni_modules/wot-design-uni/components/wd-input-number/wd-input-number.vue (3 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
src/uni_modules/wot-design-uni/components/common/interceptor.ts

[error] 5-5: void is confusing inside a union type.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)

🔇 Additional comments (22)
src/uni_modules/wot-design-uni/components/wd-input-number/wd-input-number.vue (13)

12-12: 在输入框使用 String(inputValue) 确保类型正确!
此处将数值强制转换为字符串以传递给 <input>,有助于避免因类型不一致导致的潜在问题。当前实现逻辑合理,建议继续保持。


40-43: 导入依赖方式已满足组件需求!
在此新增对 computednextTickrefwatch 的引用能够很好地支持后续的响应式操作和生命周期回调,无需额外改动。


47-47: 初始化 inputValue 并调用 getInitValue 的做法可读性高!
通过在 ref 中直接调用 getInitValue() 初始化,便于在组件加载时对传入的 modelValue 进行格式化或修复,减少后续不必要的重复判断。


49-54: minDisabled 计算属性逻辑清晰!
基于当前值判断是否小于等于最小值并且考虑到 disabled 状态,能正确地控制减号按钮的可用性。实现上无明显问题。


56-61: maxDisabled 计算属性和 minDisabled 一致性良好!
minDisabled 类似,这里的判断同样准确地维护了加号按钮的启用状态,且对各种属性都做了兼容性判断,暂无改进建议。


64-73: modelValuemaxminprecision 的监控有助于动态更新!
watch 钩子中使用 updateValue(value) 能及时同步组件显示与各种限制条件。逻辑实现正确,满足动态场景需求。


79-84: getInitValue 函数:确保初始值符合规范!
此函数在初始化时验证并格式化 props.modelValue,若修正了初始值还会通过 emit('update:modelValue', formatted) 同步外部值,逻辑应用清晰,值得保留。


108-130: updateValue 函数核心:统一处理用户/程序更新 & 拦截流程!

  1. 使用 isValueEqual 避免重复赋值,优化性能。
  2. 对用户触发的更新调用 callInterceptor,支持异步等待 Promise
  3. 更新值后再发出事件保证外部监听方拿到的值永远正确。
    本段逻辑思路清晰,维护性良好。

142-146: changeValue 函数确保在正确时机变更数值!
该函数先判断按钮与数值上下限状态,若可操作则通过 updateValue 走一套完整的流程更新。该设计能有效保证边界安全性。


148-153: subadd 分别调用 changeValue,操作简洁!
加减函数只需传入正负 step 即可实现一致的更新逻辑,避免重复代码。


158-158: handleInput:在输入事件中立刻更新!
使用 event.detail.value 并直接调用 updateValue(value, true),可第一时间做拦截判断与格式化操作,保证输入有效性。


165-167: handleBlur:在失焦时二次更新及发出 blur 事件!
此处逻辑能确保在输入框失去焦点后,组件值与外部保持同步,并提供相关的 blur 回调数据。


178-194: formatValue 函数全面处理各种边界与可为空场景!

  1. allowNulltrue,允许返回空值。
  2. 对非法数值使用最小值替代,防止出现 NaN
  3. 可选择性地对浮点数应用步长严格模式或指定精度。
    该设计灵活且边界完备,符合预期。
src/uni_modules/wot-design-uni/components/common/interceptor.ts (1)

7-43: callInterceptor 实现流程清晰且扩展性好!

  1. 通过 isPromise 判断返回值是否为 Promise
  2. Promise 体内成功或失败逻辑均能恰当调用 done/canceled/error 回调;
  3. 支持简单布尔值拦截。
    此设计能在多种场景下灵活使用,当前实现合理。
src/uni_modules/wot-design-uni/components/wd-input-number/types.ts (2)

13-14: 新增 InputNumberBeforeChange 类型清晰且与 beforeChange 匹配!
支持同步或异步布尔返回,为输入拦截功能提供了强大扩展性。当前声明符合 PR 目标需求。


76-80: beforeChange 属性类型声明充分利用了 PropType<InputNumberBeforeChange>
此处通过 Function as PropType<InputNumberBeforeChange>,在调用端可获得更完整的类型推断与编辑器提示,推荐继续保持。

src/pages/inputNumber/Index.vue (4)

36-38: 新示例区块为异步变更功能提供了直观演示!
展示了 before-change 在处理有延迟的场景下如何拦截与放行数值更新,对用户理解这项新特性帮助极大。


42-45: 导入 useToast 与类型声明增强开发体验!
使用 useToast 提示用户加载进度,搭配 InputNumberBeforeChange 确保类型安全,是很好的实践。


57-57: value11 用于演示异步刷新场景!
作为示例中的数据字段,清晰区分于其他示例值,让用户可直观查看拦截与异步操作效果。


87-95: beforeChange 函数实现异步操作示例,用户体验优先!

  1. 先调用 loading 提示用户加载中;
  2. setTimeout 模拟耗时场景并在完成后调用 resolve(true)
  3. 及时关闭提示,提高交互友好度。
    此示例易于理解,可完整展示拦截机制与异步流程。
docs/component/input-number.md (2)

100-104: 文档中对 before-change 的异步用法示例易于上手!
通过简要代码段说明了如何在函数里返回 Promise 去拦截变更,为实际项目场景提供重要指引。


140-146: 补充 allow-null 与新增 before-change 属性说明使文档更完善!

  1. 让用户了解允许空值的功能;
  2. 详细说明了 before-change 的异步场景和返回值含义。
    推荐在示例或属性表中进一步突出其实际用法场景,便于读者快速上手。

@Moonofweisheng Moonofweisheng merged commit 0fc90dd into master Jan 1, 2025
7 checks passed
@Moonofweisheng Moonofweisheng deleted the fix/input-number-allow-null branch January 1, 2025 16:36
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