mango-ui-v3/components/Input.tsx

77 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-02-22 15:30:54 -08:00
import { forwardRef, ReactNode } from 'react'
2022-02-07 02:28:36 -08:00
interface InputProps {
type: string
value: any
onChange: (e) => void
className?: string
disabled?: boolean
2021-09-13 14:14:59 -07:00
prefixClassname?: string
error?: boolean
[x: string]: any
}
2022-04-05 21:18:02 -07:00
const Input = forwardRef<HTMLInputElement, InputProps>((props, ref) => {
const {
2022-02-07 02:28:36 -08:00
type,
value,
onChange,
className,
error,
wrapperClassName = 'w-full',
disabled,
prefix,
prefixClassName,
suffix,
2022-04-05 21:18:02 -07:00
} = props
return (
<div className={`relative flex ${wrapperClassName}`}>
{prefix ? (
<div
className={`absolute left-2 top-1/2 -translate-y-1/2 transform ${prefixClassName}`}
>
{prefix}
</div>
) : null}
<input
className={`${className} h-10 w-full flex-1 rounded-md border bg-th-bkg-1 px-2 pb-px
text-th-fgd-1 ${
2022-02-20 03:38:40 -08:00
error ? 'border-th-red' : 'border-th-bkg-4'
} default-transition hover:border-th-fgd-4
focus:border-th-fgd-4 focus:outline-none
2021-04-24 19:10:28 -07:00
${
disabled
? 'cursor-not-allowed bg-th-bkg-3 text-th-fgd-3 hover:border-th-fgd-4'
2021-04-24 19:10:28 -07:00
: ''
}
${prefix ? 'pl-7' : ''}
${suffix ? 'pr-11' : ''}`}
2022-04-05 21:18:02 -07:00
disabled={disabled}
ref={ref}
{...props}
type={type}
value={value}
onChange={onChange}
/>
{suffix ? (
<span className="absolute right-0 flex h-full items-center bg-transparent pr-2 text-xs text-th-fgd-4">
{suffix}
</span>
) : null}
</div>
)
})
export default Input
2022-02-22 15:30:54 -08:00
interface LabelProps {
children: ReactNode
className?: string
}
export const Label = ({ children, className }: LabelProps) => (
<label className={`mb-1.5 block text-th-fgd-2 ${className}`}>
2022-02-22 15:30:54 -08:00
{children}
</label>
)