99 RefObject ,
1010 KeyboardEventHandler ,
1111 MouseEventHandler ,
12+ useMemo ,
1213} from 'react'
1314import styled , { css , useTheme } from 'styled-components'
1415
@@ -21,7 +22,7 @@ import { VisibilityIcon, NotVisibilityIcon, ErrorIcon } from './icons'
2122
2223type BaseElement = HTMLInputElement
2324type BaseProps = InputHTMLAttributes < BaseElement >
24- export type NumberInputType = number | ''
25+ export type NumberInputType = number | string | ''
2526export type InputChangeHandler = ChangeEventHandler < BaseElement >
2627export type InputValueChangeHandler < T > = ( value : T ) => void
2728export type TextInputWidth = 'small' | 'medium' | 'large' | 'full'
@@ -468,9 +469,36 @@ export const TextInput: FC<TextInputProps> = props => (
468469)
469470
470471export interface NumberInputProps extends BaseInputProps < NumberInputType > { }
471- export const NumberInput : FC < NumberInputProps > = props => (
472- < Input { ...props } type = "number" />
473- )
472+ export const NumberInput : FC < NumberInputProps > = ( {
473+ value,
474+ step,
475+ ...props
476+ } ) => {
477+ const decimalValue = useMemo ( ( ) => {
478+ if ( step === undefined ) {
479+ return 0
480+ }
481+ const [ _ , decimals ] = step . toString ( ) . split ( '.' ) as [
482+ string ,
483+ string | undefined
484+ ]
485+
486+ return decimals ?. length ?? 0
487+ } , [ step ] )
488+
489+ return (
490+ < Input
491+ value = {
492+ step !== undefined && value !== ''
493+ ? parseFloat ( value . toString ( ) ) . toFixed ( decimalValue )
494+ : value
495+ }
496+ step = { step }
497+ { ...props }
498+ type = "number"
499+ />
500+ )
501+ }
474502
475503export interface TextInputCredentialsProps extends BaseInputProps < string > {
476504 readonly type : TextInputCredentialsType
0 commit comments