diff --git a/docs/zh-CN/components/form/input-number.md b/docs/zh-CN/components/form/input-number.md index b7c1c6082..2b7521df1 100755 --- a/docs/zh-CN/components/form/input-number.md +++ b/docs/zh-CN/components/form/input-number.md @@ -24,6 +24,37 @@ order: 32 } ``` +## 设置精度 + +`precision` 设置数字的显示精度,一般需要配合`step`属性使用,以实现细粒度调整。 + +```schema: scope="body" +{ + "type": "form", + "api": "/api/mock2/form/saveForm", + "data": { + "number2": 3.1234 + }, + "body": [ + { + "type": "input-number", + "name": "number1", + "label": "数字", + "precision": 2, + "step": 0.01, + "value": 2.98786 + }, + { + "type": "input-number", + "name": "number2", + "label": "数字2", + "precision": 3, + "step": 0.001 + } + ] +} +``` + ## 前后缀、千分分隔 ```schema: scope="body" diff --git a/packages/amis-ui/src/components/NumberInput.tsx b/packages/amis-ui/src/components/NumberInput.tsx index 18db2e173..da4d8d951 100644 --- a/packages/amis-ui/src/components/NumberInput.tsx +++ b/packages/amis-ui/src/components/NumberInput.tsx @@ -104,7 +104,7 @@ export class NumberInput extends React.Component { } const target = getMiniDecimal(val).add(stepDecimal.toString()); const getPrecision = (numStr: string) => { - if (precision! >= 0) { + if (precision != null && precision >= 0) { return precision; } return Math.max( diff --git a/packages/amis/__tests__/renderers/Form/__snapshots__/number.test.tsx.snap b/packages/amis/__tests__/renderers/Form/__snapshots__/number.test.tsx.snap index 504132fdd..44b19448e 100644 --- a/packages/amis/__tests__/renderers/Form/__snapshots__/number.test.tsx.snap +++ b/packages/amis/__tests__/renderers/Form/__snapshots__/number.test.tsx.snap @@ -459,6 +459,165 @@ exports[`Renderer:number with borderMode 2`] = ` `; +exports[`Renderer:number with precision and default value 1`] = ` +
+
+
+

+ + + 表单 + + +

+
+
+
+ +
+ +
+
+
+ + + + + + +
+
+ +
+
+
+
+
+
+
+ +
+