asi-calculator/features/strategies/components/FormAmount.tsx

78 lines
1.8 KiB
TypeScript
Raw Normal View History

import { FormInline, Input, Select } from '@/components'
import type { TokenSymbol } from '@/types'
import type { Dispatch, SetStateAction } from 'react'
2024-04-01 14:42:08 +02:00
export function FormAmount({
amount,
setAmount,
token,
setToken,
isFiat
}: {
amount: number
setAmount: Dispatch<SetStateAction<number>>
token: TokenSymbol | string
setToken?: Dispatch<SetStateAction<TokenSymbol>>
isFiat?: boolean
}) {
2024-04-01 15:23:01 +02:00
function handleAmountChange(e: React.ChangeEvent<HTMLInputElement>) {
const { value } = e.target
if (value === '') {
setAmount(0)
} else if (Number.isNaN(Number(value))) {
return
2024-04-01 15:23:01 +02:00
} else {
setAmount(Number(value))
}
}
2024-04-01 14:42:08 +02:00
function handleTokenChange(e: React.ChangeEvent<HTMLSelectElement>) {
if (!setToken) return
setToken(e.target.value as TokenSymbol)
}
function handleFocus(e: React.FocusEvent<HTMLInputElement>) {
e.target.select()
}
2024-04-01 14:42:08 +02:00
const options = isFiat
? [{ value: 'USD', label: 'USD' }]
: [
{ value: 'OCEAN', label: 'OCEAN' },
{ value: 'FET', label: 'FET' },
{ value: 'AGIX', label: 'AGIX' },
{ value: 'CUDOS', label: 'CUDOS' }
2024-04-01 14:42:08 +02:00
]
return (
2024-04-01 15:49:03 +02:00
<FormInline>
2024-04-01 15:23:01 +02:00
<Input
type="text"
inputMode="numeric"
pattern="[0-9]*"
value={amount}
onChange={handleAmountChange}
onFocus={handleFocus}
style={{ width: `${amount.toString().length}ch` }}
2024-04-01 15:23:01 +02:00
/>
2024-04-01 14:42:08 +02:00
<Select
options={options}
value={token}
onChange={handleTokenChange}
disabled={!setToken}
2024-04-06 14:55:43 +02:00
style={
setToken
? {
paddingRight: '1.2rem',
width: `calc(${`${token.length}em`} - 1.75rem)`,
2024-04-06 14:55:43 +02:00
minWidth: '1.85rem'
}
: undefined
}
2024-04-01 14:42:08 +02:00
/>
2024-04-01 15:49:03 +02:00
</FormInline>
2024-04-01 14:42:08 +02:00
)
}