@@ -2,15 +2,29 @@ import * as React from 'react';
22import classNames from 'classnames' ;
33import KeyCode from 'rc-util/lib/KeyCode' ;
44import { 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' ;
612import StepHandler from './StepHandler' ;
7- import { getNumberPrecision , num2str , validateNumber } from './utils/numberUtil' ;
13+ import { getNumberPrecision , num2str , trimNumber , validateNumber } from './utils/numberUtil' ;
814import useCursor from './hooks/useCursor' ;
915import useUpdateEffect from './hooks/useUpdateEffect' ;
1016import useFrame from './hooks/useFrame' ;
1117
1218/**
1319 * 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
1428 */
1529const getDecimalValue = ( stringMode : boolean , decimalValue : DecimalClass ) => {
1630 if ( stringMode || decimalValue . isEmpty ( ) ) {
@@ -20,9 +34,24 @@ const getDecimalValue = (stringMode: boolean, decimalValue: DecimalClass) => {
2034 return decimalValue . toNumber ( ) ;
2135} ;
2236
23- const getDecimalIfValidate = ( value : ValueType ) => {
37+ const getDecimalIfValidate = ( value : ValueType , precision : number | undefined , isMax ?: boolean ) => {
2438 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+ }
2655} ;
2756
2857export interface InputNumberProps < T extends ValueType = ValueType >
@@ -232,8 +261,8 @@ const InputNumber = React.forwardRef(
232261 }
233262
234263 // >>> 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 ] ) ;
237266
238267 const upDisabled = React . useMemo ( ( ) => {
239268 if ( ! maxDecimal || ! decimalValue || decimalValue . isInvalidate ( ) ) {
0 commit comments