manifold/web/components/amount-input.tsx

151 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-01-11 03:41:42 +00:00
import clsx from 'clsx'
import React from 'react'
import { useUser } from 'web/hooks/use-user'
import { formatMoney } from 'common/util/format'
2022-01-11 03:41:42 +00:00
import { Col } from './layout/col'
import { SiteLink } from './site-link'
2022-05-17 12:17:22 +00:00
import { ENV_CONFIG } from 'common/envs/constants'
import { useWindowSize } from 'web/hooks/use-window-size'
2022-01-11 03:41:42 +00:00
export function AmountInput(props: {
amount: number | undefined
onChange: (newAmount: number | undefined) => void
error: string | undefined
label: string
2022-01-11 03:41:42 +00:00
disabled?: boolean
className?: string
inputClassName?: string
// Needed to focus the amount input
inputRef?: React.MutableRefObject<any>
2022-01-11 03:41:42 +00:00
}) {
const {
amount,
onChange,
error,
label,
2022-01-11 03:41:42 +00:00
disabled,
className,
inputClassName,
inputRef,
2022-01-11 03:41:42 +00:00
} = props
const onAmountChange = (str: string) => {
const amount = parseInt(str.replace(/\D/g, ''))
const isInvalid = !str || isNaN(amount)
onChange(isInvalid ? undefined : amount)
2022-01-11 03:41:42 +00:00
}
const { width } = useWindowSize()
const isMobile = (width ?? 0) < 768
2022-01-11 03:41:42 +00:00
return (
<Col className={className}>
<label className="input-group mb-4">
<span className="bg-gray-200 text-sm">{label}</span>
2022-01-11 03:41:42 +00:00
<input
className={clsx(
'input input-bordered max-w-[200px] text-lg placeholder:text-gray-400',
2022-01-11 03:41:42 +00:00
error && 'input-error',
inputClassName
)}
ref={inputRef}
type="text"
pattern="[0-9]*"
inputMode="numeric"
2022-01-11 03:41:42 +00:00
placeholder="0"
maxLength={6}
autoFocus={!isMobile}
2022-01-11 03:41:42 +00:00
value={amount ?? ''}
disabled={disabled}
onChange={(e) => onAmountChange(e.target.value)}
/>
</label>
{error && (
<div className="mb-2 mr-auto self-center whitespace-nowrap text-xs font-medium tracking-wide text-red-500">
{error === 'Insufficient balance' ? (
<>
Not enough funds.
<span className="ml-1 text-indigo-500">
<SiteLink href="/add-funds">Buy more?</SiteLink>
</span>
</>
) : (
error
)}
</div>
)}
</Col>
)
}
export function BuyAmountInput(props: {
amount: number | undefined
onChange: (newAmount: number | undefined) => void
error: string | undefined
setError: (error: string | undefined) => void
minimumAmount?: number
disabled?: boolean
2022-09-05 22:11:32 +00:00
showSliderOnMobile?: boolean
className?: string
inputClassName?: string
// Needed to focus the amount input
inputRef?: React.MutableRefObject<any>
}) {
const {
amount,
onChange,
error,
setError,
2022-09-05 22:11:32 +00:00
showSliderOnMobile: showSlider,
disabled,
className,
inputClassName,
minimumAmount,
inputRef,
} = props
const user = useUser()
const onAmountChange = (amount: number | undefined) => {
onChange(amount)
// Check for errors.
if (amount !== undefined) {
2022-04-13 17:52:12 +00:00
if (user && user.balance < amount) {
setError('Insufficient balance')
} else if (minimumAmount && amount < minimumAmount) {
setError('Minimum amount: ' + formatMoney(minimumAmount))
} else {
setError(undefined)
}
} else {
setError(undefined)
}
}
return (
2022-09-05 22:11:32 +00:00
<>
<AmountInput
amount={amount}
onChange={onAmountChange}
label={ENV_CONFIG.moneyMoniker}
error={error}
disabled={disabled}
className={className}
inputClassName={inputClassName}
inputRef={inputRef}
/>
{showSlider && (
<input
type="range"
min="0"
max="250"
value={amount ?? 0}
onChange={(e) => onAmountChange(parseInt(e.target.value))}
className="range range-lg z-40 mb-2 xl:hidden"
2022-09-05 22:11:32 +00:00
step="25"
/>
)}
</>
)
}