2023-03-22 14:12:19 +00:00
|
|
|
'use client'
|
|
|
|
|
2023-04-04 14:11:36 +00:00
|
|
|
import BigNumber from 'bignumber.js'
|
2023-03-22 14:12:19 +00:00
|
|
|
import classNames from 'classnames'
|
|
|
|
import React, { useEffect, useState } from 'react'
|
|
|
|
|
|
|
|
interface Props {
|
2023-04-04 14:11:36 +00:00
|
|
|
asset: Asset
|
|
|
|
amount: number
|
2023-03-22 14:12:19 +00:00
|
|
|
className: string
|
|
|
|
maxDecimals: number
|
|
|
|
minValue?: number
|
|
|
|
max?: number
|
|
|
|
maxLength?: number
|
|
|
|
allowNegative?: boolean
|
|
|
|
style?: {}
|
2023-04-04 14:11:36 +00:00
|
|
|
onChange: (amount: number) => void
|
2023-03-22 14:12:19 +00:00
|
|
|
onBlur?: () => void
|
|
|
|
onFocus?: () => void
|
|
|
|
onRef?: (ref: React.RefObject<HTMLInputElement>) => void
|
|
|
|
}
|
|
|
|
|
2023-04-04 14:11:36 +00:00
|
|
|
function magnify(value: number, asset: Asset) {
|
|
|
|
return value === 0 ? 0 : new BigNumber(value).shiftedBy(asset.decimals).toNumber()
|
|
|
|
}
|
|
|
|
|
|
|
|
function demagnify(amount: number, asset: Asset) {
|
|
|
|
return amount === 0 ? 0 : new BigNumber(amount).dividedBy(10 ** asset.decimals).toNumber()
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:12:19 +00:00
|
|
|
export default function NumberInput(props: Props) {
|
|
|
|
const inputRef = React.useRef<HTMLInputElement>(null)
|
|
|
|
const cursorRef = React.useRef(0)
|
2023-04-04 14:11:36 +00:00
|
|
|
const max = props.max ? demagnify(props.max, props.asset) : undefined
|
|
|
|
|
2023-03-22 14:12:19 +00:00
|
|
|
const [inputValue, setInputValue] = useState({
|
2023-04-04 14:11:36 +00:00
|
|
|
formatted: demagnify(props.amount, props.asset).toString(),
|
|
|
|
value: demagnify(props.amount, props.asset),
|
2023-03-22 14:12:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setInputValue({
|
2023-04-04 14:11:36 +00:00
|
|
|
formatted: demagnify(props.amount, props.asset).toString(),
|
|
|
|
value: demagnify(props.amount, props.asset),
|
2023-03-22 14:12:19 +00:00
|
|
|
})
|
2023-04-04 14:11:36 +00:00
|
|
|
}, [props.amount])
|
2023-03-22 14:12:19 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!props.onRef) return
|
|
|
|
props.onRef(inputRef)
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [inputRef, props.onRef])
|
|
|
|
|
|
|
|
const onInputFocus = () => {
|
|
|
|
inputRef.current?.select()
|
|
|
|
props.onFocus && props.onFocus()
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateValues = (formatted: string, value: number) => {
|
|
|
|
const lastChar = formatted.charAt(formatted.length - 1)
|
|
|
|
if (lastChar === '.') {
|
|
|
|
cursorRef.current = (inputRef.current?.selectionEnd || 0) + 1
|
|
|
|
} else {
|
|
|
|
cursorRef.current = inputRef.current?.selectionEnd || 0
|
|
|
|
}
|
|
|
|
setInputValue({ formatted, value })
|
|
|
|
if (value !== inputValue.value) {
|
2023-04-04 14:11:36 +00:00
|
|
|
props.onChange(magnify(value, props.asset))
|
2023-03-22 14:12:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!inputRef.current) return
|
|
|
|
|
|
|
|
const cursor = cursorRef.current
|
|
|
|
const length = inputValue.formatted.length
|
|
|
|
|
|
|
|
if (cursor > length) {
|
|
|
|
inputRef.current.setSelectionRange(length, length)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
inputRef.current.setSelectionRange(cursor, cursor)
|
|
|
|
}, [inputValue, inputRef])
|
|
|
|
|
|
|
|
const onInputChange = (value: string) => {
|
|
|
|
const numberCount = value.match(/[0-9]/g)?.length || 0
|
|
|
|
const decimals = value.split('.')[1]?.length || 0
|
|
|
|
const lastChar = value.charAt(value.length - 1)
|
|
|
|
const isNumber = !isNaN(Number(value))
|
|
|
|
const hasMultipleDots = (value.match(/[.,]/g)?.length || 0) > 1
|
|
|
|
const isSeparator = lastChar === '.' || lastChar === ','
|
|
|
|
const isNegative = value.indexOf('-') > -1
|
|
|
|
const isLowerThanMinimum = props.minValue !== undefined && Number(value) < props.minValue
|
2023-04-04 14:11:36 +00:00
|
|
|
const isHigherThanMaximum = max !== undefined && Number(value) > max
|
2023-03-22 14:12:19 +00:00
|
|
|
const isTooLong = props.maxLength !== undefined && numberCount > props.maxLength
|
|
|
|
const exceedsMaxDecimals = props.maxDecimals !== undefined && decimals > props.maxDecimals
|
|
|
|
|
|
|
|
if (isNegative && !props.allowNegative) return
|
|
|
|
|
|
|
|
if (isSeparator && value.length === 1) {
|
|
|
|
updateValues('0.', 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSeparator && !hasMultipleDots) {
|
|
|
|
updateValues(value.replace(',', '.'), inputValue.value)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isNumber || hasMultipleDots) return
|
|
|
|
|
|
|
|
if (exceedsMaxDecimals) {
|
|
|
|
value = value.substring(0, value.length - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isTooLong) return
|
|
|
|
|
|
|
|
if (isLowerThanMinimum) {
|
|
|
|
updateValues(String(props.minValue), props.minValue!)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isHigherThanMaximum) {
|
2023-04-04 14:11:36 +00:00
|
|
|
updateValues(String(max), max!)
|
2023-03-22 14:12:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastChar === '0' && Number(value) === Number(inputValue.value)) {
|
|
|
|
cursorRef.current = (inputRef.current?.selectionEnd || 0) + 1
|
|
|
|
setInputValue({ ...inputValue, formatted: value })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
updateValues(value, 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updateValues(String(Number(value)), Number(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<input
|
|
|
|
ref={inputRef}
|
|
|
|
type='text'
|
2023-04-03 11:31:00 +00:00
|
|
|
value={inputValue.formatted === '0' ? '' : inputValue.formatted}
|
2023-03-22 14:12:19 +00:00
|
|
|
onFocus={onInputFocus}
|
|
|
|
onChange={(e) => onInputChange(e.target.value)}
|
|
|
|
onBlur={props.onBlur}
|
|
|
|
className={classNames(
|
|
|
|
'w-full cursor-pointer appearance-none border-none bg-transparent text-right outline-none',
|
|
|
|
props.className,
|
|
|
|
)}
|
|
|
|
style={props.style}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|