Skip to content

Commit

Permalink
Update the TokenBalanceInput component
Browse files Browse the repository at this point in the history
We should not truncate the trailing zeros in the deposit amount input so aggressively. Best way to handle this is to use `inputValue` instead an `amount`. The `inputValue` value is of type string so we will not truncate zeros as it was done using `fixedPointNumberToString`. However, a `bigint` value will still be passed to the form values.
  • Loading branch information
kkosiorowska committed Aug 19, 2024
1 parent ff3cc86 commit dbe9ad9
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions dapp/src/components/shared/TokenBalanceInput/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef } from "react"
import React, { useRef, useState } from "react"
import {
Box,
Button,
Expand Down Expand Up @@ -121,12 +121,24 @@ export default function TokenBalanceInput({
...inputProps
}: TokenBalanceInputProps) {
const valueRef = useRef<bigint | undefined>(amount)
const [inputValue, setInputValue] = useState<string | undefined>()
const styles = useMultiStyleConfig("TokenBalanceInput", { size })

const { decimals } = getCurrencyByType(currency)

const handleValueChange = (value: string) => {
const onValueChange = (values: NumberFormatInputValues) => {
const { value } = values

valueRef.current = value ? userAmountToBigInt(value, decimals) : undefined
setInputValue(value)
}

const onChange = () => {
setAmount(valueRef.current)
}

const onClickMaxButton = () => {
setAmount(tokenBalance)
setInputValue(fixedPointNumberToString(tokenBalance, decimals))
}

return (
Expand All @@ -152,23 +164,17 @@ export default function TokenBalanceInput({
variant={VARIANT}
isInvalid={hasError}
placeholder={placeholder}
{...inputProps}
value={
amount ? fixedPointNumberToString(amount, decimals) : undefined
}
onValueChange={(values: NumberFormatInputValues) =>
handleValueChange(values.value)
}
onChange={() => {
setAmount(valueRef?.current)
}}
decimalScale={decimals}
allowNegative={false}
{...inputProps}
value={inputValue}
onValueChange={onValueChange}
onChange={onChange}
/>

{withMaxButton && (
<InputRightElement>
<Button h="70%" onClick={() => setAmount(tokenBalance)}>
<Button h="70%" onClick={onClickMaxButton}>
Max
</Button>
</InputRightElement>
Expand Down

0 comments on commit dbe9ad9

Please sign in to comment.