2025-03-09 15:14:57 +08:00
|
|
|
import { Field, Label, Input as HeadlessInput } from '@headlessui/react'
|
|
|
|
|
import { CloudArrowUpIcon } from '@heroicons/react/20/solid'
|
2025-03-10 12:21:08 +08:00
|
|
|
import { useEffect, type ComponentProps, type ReactNode } from 'react'
|
2025-03-09 15:14:57 +08:00
|
|
|
import {
|
|
|
|
|
get,
|
|
|
|
|
type FieldError,
|
|
|
|
|
type FieldValues,
|
|
|
|
|
type Path,
|
|
|
|
|
type RegisterOptions,
|
|
|
|
|
} from 'react-hook-form'
|
|
|
|
|
import { useRemixFormContext } from 'remix-hook-form'
|
|
|
|
|
import { twMerge } from 'tailwind-merge'
|
|
|
|
|
|
2025-03-09 20:52:50 +08:00
|
|
|
import { useAdminContext } from '~/contexts/admin'
|
|
|
|
|
|
2025-03-09 15:14:57 +08:00
|
|
|
import { Button } from './button'
|
|
|
|
|
|
|
|
|
|
type TInputProperties<T extends FieldValues> = Omit<
|
|
|
|
|
ComponentProps<'input'>,
|
|
|
|
|
'size'
|
|
|
|
|
> & {
|
|
|
|
|
id: string
|
|
|
|
|
label?: ReactNode
|
|
|
|
|
name: Path<T>
|
|
|
|
|
rules?: RegisterOptions
|
|
|
|
|
containerClassName?: string
|
|
|
|
|
labelClassName?: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const InputFile = <TFormValues extends Record<string, unknown>>(
|
|
|
|
|
properties: TInputProperties<TFormValues>,
|
|
|
|
|
) => {
|
|
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
label,
|
|
|
|
|
name,
|
|
|
|
|
rules,
|
|
|
|
|
placeholder,
|
|
|
|
|
disabled,
|
|
|
|
|
className,
|
|
|
|
|
containerClassName,
|
|
|
|
|
labelClassName,
|
|
|
|
|
...restProperties
|
|
|
|
|
} = properties
|
2025-03-10 12:31:29 +08:00
|
|
|
const { setIsUploadOpen, uploadedFile, setUploadedFile } = useAdminContext()
|
2025-03-09 15:14:57 +08:00
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
register,
|
|
|
|
|
formState: { errors },
|
2025-03-10 12:21:08 +08:00
|
|
|
setValue,
|
2025-03-09 15:14:57 +08:00
|
|
|
} = useRemixFormContext()
|
|
|
|
|
|
|
|
|
|
const error: FieldError = get(errors, name)
|
|
|
|
|
|
2025-03-10 12:21:08 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (uploadedFile) {
|
2025-03-10 12:26:56 +08:00
|
|
|
setValue(name as string, uploadedFile)
|
2025-03-10 12:31:29 +08:00
|
|
|
setUploadedFile(undefined)
|
2025-03-10 12:21:08 +08:00
|
|
|
}
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [uploadedFile])
|
|
|
|
|
|
2025-03-09 15:14:57 +08:00
|
|
|
return (
|
|
|
|
|
<Field
|
|
|
|
|
className={twMerge('relative', containerClassName)}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
id={id}
|
|
|
|
|
>
|
|
|
|
|
<Label className={twMerge('mb-1 block text-gray-700', labelClassName)}>
|
|
|
|
|
{label} {error && <span className="text-red-500">{error.message}</span>}
|
|
|
|
|
</Label>
|
|
|
|
|
<HeadlessInput
|
|
|
|
|
className={twMerge(
|
2025-03-09 21:20:29 +08:00
|
|
|
'h-[42px] w-full rounded-md border border-[#DFDFDF] p-2 pr-8',
|
2025-03-09 15:14:57 +08:00
|
|
|
className,
|
|
|
|
|
)}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
{...register(name, rules)}
|
|
|
|
|
{...restProperties}
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="icon"
|
|
|
|
|
size="fit"
|
|
|
|
|
className="absolute right-3 h-[42px]"
|
2025-03-09 20:52:50 +08:00
|
|
|
onClick={() => {
|
|
|
|
|
setIsUploadOpen('featured_image')
|
|
|
|
|
}}
|
2025-03-09 15:14:57 +08:00
|
|
|
>
|
|
|
|
|
<CloudArrowUpIcon className="h-4 w-4 text-gray-500/50" />
|
|
|
|
|
</Button>
|
|
|
|
|
</Field>
|
|
|
|
|
)
|
|
|
|
|
}
|