manifold/web/components/number-input.tsx
Sinclair Chen 8d06e4b4d2
refactor text input into one component (#1016)
* Add responsive text input component

* Add styled expanding textarea component
2022-10-09 19:37:24 -07:00

66 lines
1.5 KiB
TypeScript

import clsx from 'clsx'
import { ReactNode } from 'react'
import React from 'react'
import { Col } from './layout/col'
import { Spacer } from './layout/spacer'
import { Input } from './input'
export function NumberInput(props: {
numberString: string
onChange: (newNumberString: string) => void
error: string | undefined
disabled?: boolean
placeholder?: string
className?: string
inputClassName?: string
// Needed to focus the amount input
inputRef?: React.MutableRefObject<any>
children?: ReactNode
}) {
const {
numberString,
onChange,
error,
disabled,
placeholder,
className,
inputClassName,
inputRef,
children,
} = props
return (
<Col className={className}>
<label className="input-group">
<Input
className={clsx(
'max-w-[200px] !text-lg',
error && 'input-error',
inputClassName
)}
ref={inputRef}
type="number"
pattern="[0-9]*"
inputMode="numeric"
placeholder={placeholder ?? '0'}
maxLength={9}
value={numberString}
disabled={disabled}
onChange={(e) => onChange(e.target.value.substring(0, 9))}
/>
</label>
<Spacer h={4} />
{error && (
<div className="mb-2 mr-auto self-center whitespace-nowrap text-xs font-medium tracking-wide text-red-500">
{error}
</div>
)}
{children}
</Col>
)
}