@@ -9,13 +9,14 @@ import { Add, Subtract } from '@carbon/icons-react';
99import cx from 'classnames' ;
1010import PropTypes from 'prop-types' ;
1111import React , {
12+ FC ,
13+ MouseEvent ,
14+ ReactElement ,
1215 ReactNode ,
1316 useContext ,
17+ useEffect ,
1418 useRef ,
1519 useState ,
16- useEffect ,
17- FC ,
18- ReactElement ,
1920} from 'react' ;
2021import { useMergedRefs } from '../../internal/useMergedRefs' ;
2122import { useNormalizedInputProps as normalize } from '../../internal/useNormalizedInputProps' ;
@@ -352,27 +353,37 @@ const NumberInput = React.forwardRef<HTMLInputElement, NumberInputProps>(
352353
353354 const Icon = normalizedProps . icon as any ;
354355
355- function handleStepperClick ( event , direction ) {
356+ const getDecimalPlaces = ( num : number ) => {
357+ const parts = num . toString ( ) . split ( '.' ) ;
358+
359+ return parts [ 1 ] ? parts [ 1 ] . length : 0 ;
360+ } ;
361+
362+ const handleStepperClick = (
363+ event : MouseEvent < HTMLButtonElement > ,
364+ direction : 'up' | 'down'
365+ ) => {
356366 if ( inputRef . current ) {
357367 const currentValue = Number ( inputRef . current . value ) ;
358- let newValue =
368+ const rawValue =
359369 direction === 'up' ? currentValue + step : currentValue - step ;
360- if ( min !== undefined ) {
361- newValue = Math . max ( newValue , min ) ;
362- }
363- if ( max !== undefined ) {
364- newValue = Math . min ( newValue , max ) ;
365- }
366- const currentInputValue = inputRef . current
367- ? inputRef . current . value
368- : '' ;
370+ const precision = Math . max (
371+ getDecimalPlaces ( currentValue ) ,
372+ getDecimalPlaces ( step )
373+ ) ;
374+ const floatValue = parseFloat ( rawValue . toFixed ( precision ) ) ;
375+ const newValue =
376+ typeof min !== 'undefined' && typeof max !== 'undefined'
377+ ? Math . min ( Math . max ( floatValue , min ) , max )
378+ : floatValue ;
369379 const state = {
370380 value :
371- allowEmpty && currentInputValue === '' && step === 0
381+ allowEmpty && inputRef . current . value === '' && step === 0
372382 ? ''
373383 : newValue ,
374- direction : direction ,
384+ direction,
375385 } ;
386+
376387 setValue ( state . value ) ;
377388
378389 if ( onChange ) {
@@ -383,7 +394,7 @@ const NumberInput = React.forwardRef<HTMLInputElement, NumberInputProps>(
383394 onClick ( event , state ) ;
384395 }
385396 }
386- }
397+ } ;
387398
388399 // AILabel always size `mini`
389400 let normalizedDecorator = React . isValidElement ( slug ?? decorator )
0 commit comments