@@ -6,6 +6,7 @@ import React, {
66 ChangeEvent ,
77 FocusEvent ,
88 useRef ,
9+ useCallback ,
910} from "react" ;
1011import BigNumber from "bignumber.js" ;
1112
@@ -87,18 +88,12 @@ const DEFAULT_FORMAT = {
8788} ;
8889
8990export function useBigNumberField ( props : BigNumberFieldProps ) {
90- // Configure BigNumber format
91+ // Configure BigNumber exponential
9192 useEffect ( ( ) => {
92- const formatConfig = {
93- ...DEFAULT_FORMAT ,
94- ...props . formatOptions ,
95- } ;
96-
9793 BigNumber . config ( {
9894 EXPONENTIAL_AT : 1e9 ,
99- FORMAT : formatConfig ,
10095 } ) ;
101- } , [ props . formatOptions ] ) ;
96+ } , [ ] ) ;
10297
10398 const {
10499 value,
@@ -111,8 +106,15 @@ export function useBigNumberField(props: BigNumberFieldProps) {
111106 isReadOnly,
112107 isWheelDisabled,
113108 id,
109+ formatOptions,
114110 } = props ;
115111
112+ const formatBigNumber = useCallback (
113+ ( value : BigNumber ) =>
114+ value . toFormat ( { ...DEFAULT_FORMAT , ...formatOptions } ) ,
115+ [ formatOptions ] ,
116+ ) ;
117+
116118 const stepBig = new BigNumber ( step . toString ( ) ) . abs ( ) ;
117119 const minBig =
118120 minValue !== undefined ? new BigNumber ( minValue . toString ( ) ) : undefined ;
@@ -122,10 +124,10 @@ export function useBigNumberField(props: BigNumberFieldProps) {
122124 // State for the input value
123125 const [ inputValue , setInputValue ] = useState < string > ( ( ) => {
124126 if ( value !== undefined ) {
125- return new BigNumber ( value . toString ( ) ) . toFormat ( ) ;
127+ return formatBigNumber ( new BigNumber ( value . toString ( ) ) ) ;
126128 }
127129 if ( defaultValue !== undefined ) {
128- return new BigNumber ( defaultValue . toString ( ) ) . toFormat ( ) ;
130+ return formatBigNumber ( new BigNumber ( defaultValue . toString ( ) ) ) ;
129131 }
130132 return "" ;
131133 } ) ;
@@ -180,7 +182,7 @@ export function useBigNumberField(props: BigNumberFieldProps) {
180182
181183 if ( numberValue !== null && ! isFormatted ) {
182184 formatTimerRef . current = window . setTimeout ( ( ) => {
183- setInputValue ( numberValue . toFormat ( ) ) ;
185+ setInputValue ( formatBigNumber ( numberValue ) ) ;
184186 setIsFormatted ( true ) ;
185187 formatTimerRef . current = null ;
186188 } , 3000 ) ;
@@ -192,7 +194,7 @@ export function useBigNumberField(props: BigNumberFieldProps) {
192194 formatTimerRef . current = null ;
193195 }
194196 } ;
195- } , [ numberValue , isFormatted ] ) ;
197+ } , [ numberValue , isFormatted , formatBigNumber ] ) ;
196198
197199 // Check if increment/decrement buttons should be disabled
198200 const canIncrement = ( ) : boolean => {
@@ -427,7 +429,7 @@ export function useBigNumberField(props: BigNumberFieldProps) {
427429 const handleBlur = ( ) => {
428430 if ( numberValue !== null ) {
429431 // Format the number using BigNumber.toFormat
430- setInputValue ( numberValue . toFormat ( ) ) ;
432+ setInputValue ( formatBigNumber ( numberValue ) ) ;
431433 setIsFormatted ( true ) ;
432434 } else if ( inputValue !== "" && inputValue !== "-" ) {
433435 setInputValue ( "" ) ;
0 commit comments