@@ -2,15 +2,29 @@ import * as React from 'react';
2
2
import classNames from 'classnames' ;
3
3
import KeyCode from 'rc-util/lib/KeyCode' ;
4
4
import { composeRef } from 'rc-util/lib/ref' ;
5
- import getMiniDecimal , { DecimalClass , toFixed , ValueType } from './utils/MiniDecimal' ;
5
+ import getMiniDecimal , {
6
+ DecimalClass ,
7
+ roundDownUnsignedDecimal ,
8
+ roundUpUnsignedDecimal ,
9
+ toFixed ,
10
+ ValueType
11
+ } from './utils/MiniDecimal' ;
6
12
import StepHandler from './StepHandler' ;
7
- import { getNumberPrecision , num2str , validateNumber } from './utils/numberUtil' ;
13
+ import { getNumberPrecision , num2str , trimNumber , validateNumber } from './utils/numberUtil' ;
8
14
import useCursor from './hooks/useCursor' ;
9
15
import useUpdateEffect from './hooks/useUpdateEffect' ;
10
16
import useFrame from './hooks/useFrame' ;
11
17
12
18
/**
13
19
* We support `stringMode` which need handle correct type when user call in onChange
20
+ * format max or min value
21
+ * 1. if isInvalid return null
22
+ * 2. if precision is undefined, return decimal
23
+ * 3. format with precision
24
+ * I. if max > 0, round down with precision. Example: max= 3.5, precision=0 afterFormat: 3
25
+ * II. if max < 0, round up with precision. Example: max= -3.5, precision=0 afterFormat: -4
26
+ * III. if min > 0, round up with precision. Example: min= 3.5, precision=0 afterFormat: 4
27
+ * IV. if min < 0, round down with precision. Example: max= -3.5, precision=0 afterFormat: -3
14
28
*/
15
29
const getDecimalValue = ( stringMode : boolean , decimalValue : DecimalClass ) => {
16
30
if ( stringMode || decimalValue . isEmpty ( ) ) {
@@ -20,9 +34,24 @@ const getDecimalValue = (stringMode: boolean, decimalValue: DecimalClass) => {
20
34
return decimalValue . toNumber ( ) ;
21
35
} ;
22
36
23
- const getDecimalIfValidate = ( value : ValueType ) => {
37
+ const getDecimalIfValidate = ( value : ValueType , precision : number | undefined , isMax ?: boolean ) => {
24
38
const decimal = getMiniDecimal ( value ) ;
25
- return decimal . isInvalidate ( ) ? null : decimal ;
39
+ if ( decimal . isInvalidate ( ) ) {
40
+ return null ;
41
+ }
42
+
43
+ if ( precision === undefined ) {
44
+ return decimal ;
45
+ }
46
+
47
+ const { negative, integerStr, decimalStr, negativeStr} = trimNumber ( decimal . toString ( ) ) ;
48
+ const unSignedNumberStr = integerStr + '.' + decimalStr ;
49
+
50
+ if ( ( isMax && ! negative ) || ( ! isMax && negative ) ) {
51
+ return getMiniDecimal ( negativeStr + roundDownUnsignedDecimal ( unSignedNumberStr , precision ) ) ;
52
+ } else {
53
+ return getMiniDecimal ( negativeStr + roundUpUnsignedDecimal ( unSignedNumberStr , precision ) ) ;
54
+ }
26
55
} ;
27
56
28
57
export interface InputNumberProps < T extends ValueType = ValueType >
@@ -232,8 +261,8 @@ const InputNumber = React.forwardRef(
232
261
}
233
262
234
263
// >>> Max & Min limit
235
- const maxDecimal = React . useMemo ( ( ) => getDecimalIfValidate ( max ) , [ max ] ) ;
236
- const minDecimal = React . useMemo ( ( ) => getDecimalIfValidate ( min ) , [ min ] ) ;
264
+ const maxDecimal = React . useMemo ( ( ) => getDecimalIfValidate ( max , precision , true ) , [ max , precision ] ) ;
265
+ const minDecimal = React . useMemo ( ( ) => getDecimalIfValidate ( min , precision , false ) , [ min , precision ] ) ;
237
266
238
267
const upDisabled = React . useMemo ( ( ) => {
239
268
if ( ! maxDecimal || ! decimalValue || decimalValue . isInvalidate ( ) ) {
0 commit comments