mango-v4-ui/components/forms/Input.tsx

83 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-02-27 23:20:11 -08:00
import { ChangeEvent, forwardRef, InputHTMLAttributes } from 'react'
2022-07-14 16:36:31 -07:00
2023-02-27 23:20:11 -08:00
interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
value: string
2022-07-14 16:36:31 -07:00
onChange: (e: ChangeEvent<HTMLInputElement>) => void
2023-02-27 23:20:11 -08:00
type?: string
2023-01-25 01:19:12 -08:00
maxLength?: number
2022-07-14 16:36:31 -07:00
className?: string
disabled?: boolean
prefixClassname?: string
2022-11-19 11:20:36 -08:00
wrapperClassName?: string
2023-04-14 04:48:09 -07:00
hasError?: boolean
2023-02-27 23:20:11 -08:00
prefix?: string
prefixClassName?: string
suffix?: string
2022-07-14 16:36:31 -07:00
}
const Input = forwardRef<HTMLInputElement, InputProps>((props, ref) => {
const {
type,
value,
onChange,
2023-01-25 01:19:12 -08:00
maxLength,
2022-07-14 16:36:31 -07:00
className,
2023-04-14 04:48:09 -07:00
hasError,
2022-07-14 16:36:31 -07:00
wrapperClassName = 'w-full',
disabled,
prefix,
prefixClassName,
suffix,
} = props
return (
<>
<div className={`relative flex ${wrapperClassName}`}>
{prefix ? (
<div
className={`absolute left-2 top-1/2 -translate-y-1/2 ${prefixClassName}`}
>
{prefix}
</div>
) : null}
<input
2022-12-07 18:34:24 -08:00
className={`${className} default-transition h-12 w-full flex-1 rounded-md border bg-th-input-bkg px-3 text-base
2022-07-14 16:36:31 -07:00
text-th-fgd-1 ${
2023-04-14 04:48:09 -07:00
hasError ? 'border-th-down' : 'border-th-input-border'
2022-12-07 18:34:24 -08:00
} focus:outline-none
md:hover:border-th-input-border-hover
2022-07-14 16:36:31 -07:00
${
disabled
2022-07-23 19:48:26 -07:00
? 'cursor-not-allowed bg-th-bkg-3 text-th-fgd-3 hover:border-th-fgd-4'
2022-07-14 16:36:31 -07:00
: ''
}
2022-07-16 04:22:59 -07:00
${prefix ? 'pl-8' : ''}
2022-07-14 16:36:31 -07:00
${suffix ? 'pr-11' : ''}`}
disabled={disabled}
ref={ref}
type={type}
value={value}
onChange={onChange}
2023-01-25 01:19:12 -08:00
maxLength={maxLength ? maxLength : undefined}
/>
{suffix ? (
<span className="absolute right-0 flex h-full items-center bg-transparent pr-2 text-xs text-th-fgd-4">
{suffix}
</span>
) : null}
2023-01-25 01:19:12 -08:00
{maxLength ? (
<p
className={`absolute -top-7 right-0 mt-1 flex justify-end text-xs ${
2023-01-25 01:19:12 -08:00
value.length === maxLength ? 'text-th-down' : 'text-th-fgd-4'
}`}
>
2023-01-25 01:19:12 -08:00
{`${value.length}/${maxLength}`}
</p>
) : null}
</div>
</>
2022-07-14 16:36:31 -07:00
)
})
2022-07-22 08:40:53 -07:00
Input.displayName = 'Input'
2022-07-14 16:36:31 -07:00
export default Input