Skip to content

fix: 🐛 修复 InputNumber 设置了precision后无法输入小数点的问题#886

Merged
Moonofweisheng merged 1 commit into
masterfrom
fix/input-number-precision
Feb 11, 2025
Merged

fix: 🐛 修复 InputNumber 设置了precision后无法输入小数点的问题#886
Moonofweisheng merged 1 commit into
masterfrom
fix/input-number-precision

Conversation

@Moonofweisheng

@Moonofweisheng Moonofweisheng commented Feb 11, 2025

Copy link
Copy Markdown
Owner

Closes: #878

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

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

🔗 相关 Issue

#878

💡 需求背景和解决方案

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

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

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

Summary by CodeRabbit

  • 新功能
    • 优化了数字输入组件,支持更精确的增减操作和数值更新。
    • 改进了数值精度与格式化处理,确保输入值在更新最大值、最小值与精度设置时更加准确。
    • 增强了组件的响应性,通过自动校准初始值提升了用户交互体验。

@vercel

vercel Bot commented Feb 11, 2025

Copy link
Copy Markdown

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 Feb 11, 2025 10:49am

@coderabbitai

coderabbitai Bot commented Feb 11, 2025

Copy link
Copy Markdown

Walkthrough

该 PR 修改了 wd-input-number 组件,更新了 precision 属性的类型定义(从 makeNumberProp 改为 makeNumericProp),并在组件逻辑中增加了多个新函数(如 addsubgetInitValuetoPrecisiongetPrecisiontoStrictlyStepupdateValuechangeValueformatValue)。另外,通过添加 watchers 来响应 modelValuemaxminprecision 属性的变化,从而改进了数值格式化和精度管理机制。

Changes

文件 变更摘要
…/wd-input-number/types.ts 修改 inputNumberProps 中的 precision 属性实现,从 makeNumberProp(0) 变更为 makeNumericProp(0)
…/wd-input-number/wd-input-number.vue 新增 addsubgetInitValuetoPrecisiongetPrecisiontoStrictlyStepupdateValuechangeValueformatValue 等方法;添加多个 watchers 监测属性变化,并调整了按钮和交互逻辑。

Sequence Diagram(s)

sequenceDiagram
    participant U as 用户
    participant C as wd-input-number 组件
    participant F as 格式化函数

    U->>C: 点击增加或减少按钮
    C->>C: 调用 add/sub 方法
    C->>C: 执行 changeValue 方法
    C->>F: 使用 toPrecision 和 formatValue 格式化值
    F-->>C: 返回格式化后的数值
    C->>C: 更新内部值并触发 watchers
    C-->>U: 组件状态更新
Loading

Assessment against linked issues

Objective ([#878]) Addressed Explanation
正确输入小数

Possibly related PRs

Poem

我是一只快乐的小兔子,
跳跃在代码的田野里,
精度精准如星辰,
增减之间皆有理,
代码绽放如花朵,
喜悦满载跳不停,
嗨,代码世界真迷人!

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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.

@netlify

netlify Bot commented Feb 11, 2025

Copy link
Copy Markdown

Deploy Preview for wot-design-uni ready!

Name Link
🔨 Latest commit e32fe64
🔍 Latest deploy log https://app.netlify.com/sites/wot-design-uni/deploys/67ab2b1f4cad5800080884e6
😎 Deploy Preview https://deploy-preview-886--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.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying wot-design-uni with  Cloudflare Pages  Cloudflare Pages

Latest commit: e32fe64
Status: ✅  Deploy successful!
Preview URL: https://d3dace38.wot-design-uni.pages.dev
Branch Preview URL: https://fix-input-number-precision.wot-design-uni.pages.dev

View logs

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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/wd-input-number/wd-input-number.vue (1)

174-177: 建议增强输入值的错误处理

当前的输入处理较为简单,建议增加以下改进:

 function handleInput(event: any) {
   let value = event.detail.value || ''
+  // 验证输入值的格式
+  if (value && !/^-?\d*\.?\d*$/.test(value)) {
+    return
+  }
   updateValue(value, true)
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a0da88c and e32fe64.

📒 Files selected for processing (2)
  • 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 (8 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (4)
src/uni_modules/wot-design-uni/components/wd-input-number/types.ts (1)

40-40: 从 makeNumberProp 切换到 makeNumericProp 是个好的改进

这个改动使得精度属性的类型处理更加灵活,有助于解决小数点输入的问题。

src/uni_modules/wot-design-uni/components/wd-input-number/wd-input-number.vue (3)

3-7: 代码注释的添加提高了可读性

为减号按钮和加号按钮添加的注释使代码结构更清晰。

Also applies to: 23-24


66-78: 监听器的添加增强了组件的响应性

新增的 watch 确保了在 modelValue、max、min 和 precision 变化时能够正确更新输入值,这对于保持精度设置的一致性很重要。


95-98: 精度处理逻辑的改进很好地解决了小数点输入问题

  1. toPrecision 函数现在正确处理了精度转换
  2. getPrecision 函数准确计算数值的精度
  3. changeStep 函数通过取最大精度确保了步进值变化的准确性

建议添加单元测试来验证这些改动:

Also applies to: 101-110, 146-154

✅ Verification successful

🌐 Web query:

Vue 3 组件如何编写小数点输入的单元测试?

💡 Result:

在Vue3中编写小数点输入的单元测试需要重点关注数值格式化、输入验证和事件触发机制。以下是基于最佳实践的实现方法:

核心测试场景

1. 数值格式化验证

it('自动格式化为两位小数', async () => {
  const wrapper = mount(InputComponent);
  const input = wrapper.find('input');
  
  await input.setValue('123.456');
  expect(wrapper.vm.inputValue).toBe('123.46'); // 四舍五入到两位小数
});

2. 非法字符过滤

it('过滤非数字字符', async () => {
  const wrapper = mount(InputComponent);
  const input = wrapper.find('input');

  await input.setValue('12a3.4b5c');
  expect(wrapper.vm.inputValue).toBe('123.45');
});

3. 边界条件处理

describe('边界条件验证', () => {
  it('处理空值输入', async () => {
    const wrapper = mount(InputComponent);
    const input = wrapper.find('input');
    
    await input.setValue('');
    expect(wrapper.vm.inputValue).toBe('');
  });

  it('处理单个小数点开头', async () => {
    const wrapper = mount(InputComponent);
    const input = wrapper.find('input');
    
    await input.setValue('.56');
    expect(wrapper.vm.inputValue).toBe('0.56');
  });
});

完整测试套件示例

import { mount } from '@vue/test-utils';
import DecimalInput from './DecimalInput.vue';

describe('DecimalInput Component', () => {
  it('正确绑定v-model', async () => {
    const wrapper = mount(DecimalInput, {
      props: { modelValue: '12.34' }
    });
    
    expect(wrapper.find('input').element.value).toBe('12.34');
    await wrapper.find('input').setValue('56.78');
    expect(wrapper.emitted('update:modelValue')[0]).toEqual(['56.78']);
  });

  it('限制最大小数位数', async () => {
    const wrapper = mount(DecimalInput, {
      props: { precision: 3 }
    });

    await wrapper.find('input').setValue('1.2345');
    expect(wrapper.vm.inputValue).toBe('1.235'); // [4][6]
  });

  it('正确处理负号', async () => {
    const wrapper = mount(DecimalInput, { props: { allowNegative: true } });
    
    await wrapper.find('input').setValue('-123.45');
    expect(wrapper.vm.inputValue).toBe('-123.45');
    
    await wrapper.find('input').setValue('--123');
    expect(wrapper.vm.inputValue).toBe('-123');
  });
});

关键实现技巧

  1. 使用setValue模拟输入:这是触发v-model更新的推荐方式[2][7]
  2. 异步操作处理:所有DOM更新都需要使用await等待响应式系统完成更新
  3. 多场景覆盖:包括合法/非法输入、边界值、特殊符号等不同情况
  4. 事件验证:通过wrapper.emitted()检查是否触发正确事件[2][7]

对于需要复杂数值处理的组件,建议结合Jest的快照测试功能验证渲染输出的一致性。同时可以使用jest.useFakeTimers来测试防抖等延迟逻辑的实现效果[7][9]。

通过以上方法可以确保小数点输入组件在不同场景下都能正确处理用户输入并保持数据一致性。实际项目中应根据具体业务需求扩展更多边界条件的测试用例。[1][3][5]

Citations:


精度处理逻辑改进验证结果

代码中对 toPrecisiongetPrecisionchangeStep 函数的修改确实有效地解决了小数点输入的问题。结合查询到的 Vue 3 单元测试最佳实践示例,建议:

  • 为数值格式化(如四舍五入、限制小数位数)添加单元测试
  • 为非法字符过滤和边界条件(例如空值、单个小数点开头)添加测试用例
  • 检查 v-model 绑定与事件触发是否正常

这些改动不仅适用于文件 src/uni_modules/wot-design-uni/components/wd-input-number/wd-input-number.vue 的第 95-98 行,也应扩展到 101-110 行和 146-154 行的相应逻辑验证。

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.

[Bug 上报] wd-input-number设置小数精度不能输入小数点

1 participant