@@ -24,6 +24,12 @@ const escapeRegExp = (string: string): string => {
2424 return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ;
2525} ;
2626
27+ const hasCorrectDecimals = ( value : string , decimals : number ) => {
28+ const decimalGroups = value . split ( '.' ) ;
29+
30+ return decimalGroups . length > 1 ? decimalGroups [ 1 ] . length <= decimals : true ;
31+ } ;
32+
2733type Props = {
2834 valueUSD ?: number ;
2935 balance ?: ReactNode ;
@@ -33,8 +39,10 @@ type Props = {
3339 size ?: TokenInputSize ;
3440 isInvalid ?: boolean ;
3541 minHeight ?: Spacing ;
36- value ?: string | number ;
37- defaultValue ?: string | number ;
42+ value ?: string ;
43+ defaultValue ?: string ;
44+ // TODO: use Currency from bob-ui
45+ currency : { decimals : number } ;
3846 onValueChange ?: ( value : string | number ) => void ;
3947 onChange ?: ( e : React . ChangeEvent < HTMLInputElement > ) => void ;
4048 onFocus ?: ( e : FocusEvent < Element > ) => void ;
@@ -69,6 +77,7 @@ const BaseTokenInput = forwardRef<HTMLInputElement, BaseTokenInputProps>(
6977 inputMode,
7078 value : valueProp ,
7179 endAdornment,
80+ currency,
7281 onChange,
7382 onValueChange,
7483 ...props
@@ -84,9 +93,13 @@ const BaseTokenInput = forwardRef<HTMLInputElement, BaseTokenInputProps>(
8493 ( e ) => {
8594 const value = e . target . value ;
8695
87- const isValid = value === '' || RegExp ( `^\\d*(?:\\\\[.])?\\d*$` ) . test ( escapeRegExp ( value ) ) ;
96+ const isEmpty = value === '' ;
97+ const hasValidDecimalFormat = RegExp ( `^\\d*(?:\\\\[.])?\\d*$` ) . test ( escapeRegExp ( value ) ) ;
98+ const hasValidDecimalsAmount = hasCorrectDecimals ( value , currency . decimals ) ;
99+
100+ const isValid = hasValidDecimalFormat && hasValidDecimalsAmount ;
88101
89- if ( isValid ) {
102+ if ( isEmpty || isValid ) {
90103 onChange ?.( e ) ;
91104 onValueChange ?.( value ) ;
92105 setValue ( value ) ;
0 commit comments