11 import { default as InputNumber } from "antd/es/input-number" ;
2- import { NumberControl , StringControl } from "comps/controls/codeControl" ;
2+ import { NumberControl , RangeControl , StringControl } from "comps/controls/codeControl" ;
33import { BoolControl } from "comps/controls/boolControl" ;
44import { trans } from "i18n" ;
55import { ColumnTypeCompBuilder , ColumnTypeViewFn } from "../columnTypeCompBuilder" ;
@@ -25,13 +25,16 @@ const InputNumberWrapper = styled.div`
2525const childrenMap = {
2626 text : NumberControl ,
2727 step : withDefault ( NumberControl , 1 ) ,
28+ precision : RangeControl . closed ( 0 , 20 , 0 ) ,
2829 float : BoolControl ,
2930 prefix : StringControl ,
3031 suffix : StringControl ,
3132} ;
3233
3334let float = false ;
3435let step = 1 ;
36+ let precision = 0 ;
37+
3538const getBaseValue : ColumnTypeViewFn < typeof childrenMap , number , number > = (
3639 props
3740) => {
@@ -44,8 +47,13 @@ export const ColumnNumberComp = (function () {
4447 ( props , dispatch ) => {
4548 float = props . float ;
4649 step = props . step ;
47- const value = ! float ? Math . floor ( props . changeValue ?? getBaseValue ( props , dispatch ) ) : props . changeValue ?? getBaseValue ( props , dispatch ) ;
48- return props . prefix + value + props . suffix ;
50+ precision = props . precision ;
51+ const value = props . changeValue ?? getBaseValue ( props , dispatch ) ;
52+ let formattedValue : string | number = ! float ? Math . floor ( value ) : value ;
53+ if ( float ) {
54+ formattedValue = formattedValue . toPrecision ( precision + 1 ) ;
55+ }
56+ return props . prefix + formattedValue + props . suffix ;
4957 } ,
5058 ( nodeValue ) => nodeValue . text . value ,
5159 getBaseValue ,
@@ -62,6 +70,7 @@ export const ColumnNumberComp = (function () {
6270 value = value ?? 0 ;
6371 props . onChange ( ! float ? Math . floor ( value ) : value ) ;
6472 } }
73+ precision = { float ? precision : 0 }
6574 onBlur = { props . onChangeEnd }
6675 onPressEnter = { props . onChangeEnd }
6776 />
@@ -86,6 +95,11 @@ export const ColumnNumberComp = (function () {
8695 }
8796 }
8897 } ) }
98+ { float && (
99+ children . precision . propertyView ( {
100+ label : trans ( "table.precision" ) ,
101+ } )
102+ ) }
89103 { children . prefix . propertyView ( {
90104 label : trans ( "table.prefix" ) ,
91105 } ) }
0 commit comments