Skip to content

Commit 0caa17d

Browse files
committed
feat(button): add decimalLimit prop for InputNumber component
1 parent 542f754 commit 0caa17d

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

packages/devui-vue/devui/input-number/src/input-number-types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export const inputNumberProps = {
3939
type: Boolean,
4040
default: true,
4141
},
42+
decimalLimit: {
43+
type: Number || null,
44+
default: 2,
45+
},
4246
} as const;
4347

4448
export type InputNumberProps = ExtractPropTypes<typeof inputNumberProps>;

packages/devui-vue/devui/input-number/src/use-input-number.ts

+18
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ export function useEvent(props: InputNumberProps, ctx: SetupContext, inputRef: R
8484
}
8585
});
8686

87+
const decimalLimit = computed(() => {
88+
if (isUndefined(props.decimalLimit)) {
89+
return null;
90+
}
91+
return props.decimalLimit;
92+
});
93+
8794
const inputVal = computed(() => {
8895
if (!isUndefined(state.userInputValue)) {
8996
return state.userInputValue;
@@ -132,6 +139,17 @@ export function useEvent(props: InputNumberProps, ctx: SetupContext, inputRef: R
132139
if (newVal > max.value || newVal < min.value) {
133140
newVal = newVal > max.value ? max.value : min.value;
134141
}
142+
143+
// 精度限制存在才做限制
144+
if (decimalLimit.value) {
145+
const splitVal = newVal.toString().split('.');
146+
if (splitVal.length > 1) {
147+
const decimalVal = splitVal[1];
148+
if (decimalVal.length > decimalLimit.value) {
149+
newVal = Number.parseFloat(newVal.toFixed(decimalLimit.value));
150+
}
151+
}
152+
}
135153
return newVal;
136154
};
137155

packages/devui-vue/docs/components/input-number/index.md

+26
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,32 @@ export default defineComponent({
223223

224224
:::
225225

226+
### 限制小数
227+
228+
:::demo 设置 `decimalLimit` 属性可以限制小数位数。设置此参数后会将所有小数点后面的数字截断,而不是四舍五入。
229+
230+
```vue
231+
<template>
232+
<div>
233+
<d-input-number v-model="num" :decimalLimit="1" :step="0.1"></d-input-number>
234+
</div>
235+
</template>
236+
<script>
237+
import { defineComponent, ref } from 'vue';
238+
239+
export default defineComponent({
240+
setup() {
241+
const num = ref(3.9);
242+
return {
243+
num
244+
};
245+
},
246+
});
247+
</script>
248+
```
249+
250+
:::
251+
226252
### InputNumber 参数
227253

228254
| 参数名 | 类型 | 默认值 | 说明 | 跳转 Demo |

0 commit comments

Comments
 (0)