Skip to content

fix(input-number): enforce decimal places limit in user input handling #5522

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions packages/components/input-number/hooks/useInputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,31 @@ export default function useInputNumber(props: TdInputNumberProps) {
const val = formatThousandths(inputValue);
if (!canInputNumber(val, props.largeNumber)) return;

userInput.value = val;
// 先处理小数位数限制,然后再更新显示值和实际值
let processedVal = val;
if (props.decimalPlaces !== undefined && val !== '' && val.includes('.')) {
const parts = val.split('.');
// 获取实际小数位数限制
const decimalLimit = typeof props.decimalPlaces === 'number' ? props.decimalPlaces : props.decimalPlaces.places;

if (parts[1] && parts[1].length > decimalLimit) {
// 直接截断为指定小数位数
processedVal = `${parts[0]}.${parts[1].substring(0, decimalLimit)}`;
}
}

// 更新显示值
userInput.value = processedVal;

if (props.largeNumber) {
setTValue(val, { type: 'input', e });
setTValue(processedVal, { type: 'input', e });
return;
}

if (canSetValue(String(val), Number(tValue.value))) {
const newVal = val === '' ? undefined : Number(val);
setTValue(newVal, { type: 'input', e });
// 当处理后的值与当前值不同时,才更新
const newNumber = processedVal === '' ? undefined : Number(processedVal);
if (canSetValue(processedVal, Number(tValue.value))) {
setTValue(newNumber, { type: 'input', e });
}
};

Expand Down
6 changes: 6 additions & 0 deletions packages/tdesign-vue-next/.changelog/pr-5522.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
pr_number: 5522
contributor: QuentinHsu
---

- fix(InputNumber): 强制限制用户输入的小数位数 @QuentinHsu ([#5522](https://github.com/Tencent/tdesign-vue-next/pull/5522))