2025-03-01 16:14:03 +08:00
|
|
|
import { Field, Label, Input as HeadlessInput } from '@headlessui/react'
|
2025-02-23 22:08:47 +08:00
|
|
|
import { useState, type ComponentProps, type ReactNode } from 'react'
|
|
|
|
|
import {
|
|
|
|
|
get,
|
|
|
|
|
type FieldError,
|
|
|
|
|
type FieldValues,
|
|
|
|
|
type Path,
|
|
|
|
|
type RegisterOptions,
|
|
|
|
|
} from 'react-hook-form'
|
2025-02-28 09:12:38 +08:00
|
|
|
import { useRemixFormContext } from 'remix-hook-form'
|
2025-02-28 09:23:36 +08:00
|
|
|
import { twMerge } from 'tailwind-merge'
|
2025-02-23 22:08:47 +08:00
|
|
|
|
|
|
|
|
import { EyeIcon } from '~/components/icons/eye'
|
|
|
|
|
|
|
|
|
|
import { Button } from './button'
|
|
|
|
|
|
|
|
|
|
type TInputProperties<T extends FieldValues> = Omit<
|
|
|
|
|
ComponentProps<'input'>,
|
|
|
|
|
'size'
|
|
|
|
|
> & {
|
|
|
|
|
id: string
|
|
|
|
|
label?: ReactNode
|
|
|
|
|
name: Path<T>
|
|
|
|
|
rules?: RegisterOptions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const Input = <TFormValues extends Record<string, unknown>>(
|
|
|
|
|
properties: TInputProperties<TFormValues>,
|
|
|
|
|
) => {
|
2025-02-28 09:23:36 +08:00
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
label,
|
|
|
|
|
name,
|
|
|
|
|
rules,
|
|
|
|
|
type = 'text',
|
|
|
|
|
placeholder,
|
2025-03-01 06:32:20 +08:00
|
|
|
disabled,
|
2025-02-28 09:23:36 +08:00
|
|
|
...rest
|
|
|
|
|
} = properties
|
2025-02-23 22:08:47 +08:00
|
|
|
const [inputType, setInputType] = useState(type)
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
register,
|
|
|
|
|
formState: { errors },
|
2025-02-28 09:12:38 +08:00
|
|
|
} = useRemixFormContext()
|
2025-02-23 22:08:47 +08:00
|
|
|
|
|
|
|
|
const error: FieldError = get(errors, name)
|
|
|
|
|
|
|
|
|
|
return (
|
2025-03-01 06:32:20 +08:00
|
|
|
<Field
|
|
|
|
|
className="relative"
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
id={id}
|
|
|
|
|
>
|
|
|
|
|
<Label className="mb-1 block text-gray-700">
|
2025-02-23 22:08:47 +08:00
|
|
|
{label} {error && <span className="text-red-500">{error.message}</span>}
|
2025-03-01 06:32:20 +08:00
|
|
|
</Label>
|
2025-03-01 16:14:03 +08:00
|
|
|
<HeadlessInput
|
2025-02-23 22:08:47 +08:00
|
|
|
type={inputType}
|
2025-02-28 09:23:36 +08:00
|
|
|
className="h-[42px] w-full rounded-md border border-[#DFDFDF] p-2"
|
|
|
|
|
placeholder={inputType === 'password' ? '******' : placeholder}
|
2025-02-23 22:08:47 +08:00
|
|
|
{...register(name, rules)}
|
|
|
|
|
{...rest}
|
|
|
|
|
/>
|
|
|
|
|
{type === 'password' && (
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="icon"
|
|
|
|
|
size="fit"
|
2025-02-28 09:23:36 +08:00
|
|
|
className="absolute right-3 h-[42px] text-gray-500"
|
2025-02-23 22:08:47 +08:00
|
|
|
onClick={() =>
|
|
|
|
|
setInputType(inputType === 'password' ? 'text' : 'password')
|
|
|
|
|
}
|
|
|
|
|
>
|
2025-02-28 09:23:36 +08:00
|
|
|
<EyeIcon
|
|
|
|
|
className={twMerge(
|
|
|
|
|
'h-4 w-4',
|
|
|
|
|
inputType === 'password' ? 'text-gray-500/50' : 'text-gray-500',
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2025-02-23 22:08:47 +08:00
|
|
|
</Button>
|
|
|
|
|
)}
|
2025-03-01 06:32:20 +08:00
|
|
|
</Field>
|
2025-02-23 22:08:47 +08:00
|
|
|
)
|
|
|
|
|
}
|