2025-03-09 15:14:57 +08:00
|
|
|
import { Field, Label, Input as HeadlessInput } from '@headlessui/react'
|
2025-03-14 23:50:27 +08:00
|
|
|
import { CloudArrowUpIcon } from '@heroicons/react/24/solid'
|
2025-03-10 12:21:08 +08:00
|
|
|
import { useEffect, type ComponentProps, type ReactNode } from 'react'
|
2025-03-12 10:57:48 +08:00
|
|
|
import { get, type FieldError, type RegisterOptions } from 'react-hook-form'
|
2025-03-09 15:14:57 +08:00
|
|
|
import { useRemixFormContext } from 'remix-hook-form'
|
|
|
|
|
import { twMerge } from 'tailwind-merge'
|
|
|
|
|
|
2025-03-10 14:32:32 +08:00
|
|
|
import { useAdminContext, type TUpload } from '~/contexts/admin'
|
2025-03-09 20:52:50 +08:00
|
|
|
|
2025-03-09 15:14:57 +08:00
|
|
|
import { Button } from './button'
|
|
|
|
|
|
2025-03-12 10:57:48 +08:00
|
|
|
type TInputProperties = Omit<ComponentProps<'input'>, 'size'> & {
|
2025-03-09 15:14:57 +08:00
|
|
|
id: string
|
|
|
|
|
label?: ReactNode
|
2025-03-12 10:57:48 +08:00
|
|
|
name: string
|
2025-03-09 15:14:57 +08:00
|
|
|
rules?: RegisterOptions
|
|
|
|
|
containerClassName?: string
|
|
|
|
|
labelClassName?: string
|
2025-03-12 10:57:48 +08:00
|
|
|
category: TUpload
|
2025-03-09 15:14:57 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-12 10:57:48 +08:00
|
|
|
export const InputFile = (properties: TInputProperties) => {
|
2025-03-09 15:14:57 +08:00
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
label,
|
|
|
|
|
name,
|
|
|
|
|
rules,
|
|
|
|
|
placeholder,
|
|
|
|
|
disabled,
|
|
|
|
|
className,
|
|
|
|
|
containerClassName,
|
|
|
|
|
labelClassName,
|
2025-03-12 09:47:21 +08:00
|
|
|
category,
|
2025-03-09 15:14:57 +08:00
|
|
|
...restProperties
|
|
|
|
|
} = properties
|
2025-03-10 13:37:50 +08:00
|
|
|
const { setIsUploadOpen, uploadedFile, setUploadedFile, isUploadOpen } =
|
|
|
|
|
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(() => {
|
2025-03-12 10:57:48 +08:00
|
|
|
if (uploadedFile && isUploadOpen === (category || name)) {
|
|
|
|
|
setValue(name, uploadedFile)
|
2025-03-10 12:31:29 +08:00
|
|
|
setUploadedFile(undefined)
|
2025-03-10 14:32:32 +08:00
|
|
|
setIsUploadOpen(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={() => {
|
2025-03-12 10:57:48 +08:00
|
|
|
setIsUploadOpen(category)
|
2025-03-09 20:52:50 +08:00
|
|
|
}}
|
2025-03-09 15:14:57 +08:00
|
|
|
>
|
|
|
|
|
<CloudArrowUpIcon className="h-4 w-4 text-gray-500/50" />
|
|
|
|
|
</Button>
|
|
|
|
|
</Field>
|
|
|
|
|
)
|
|
|
|
|
}
|