이미지를 업로드하는 컴포넌트 구현하기
shadcn의 input 컴포넌트로 이미지 등록 버튼을 클릭했을 때, 파일 창이 열리도록 구현
Last updated
shadcn의 input 컴포넌트로 이미지 등록 버튼을 클릭했을 때, 파일 창이 열리도록 구현
Last updated
<div className='flex justify-center'>
<Avatar
className='w-24 h-24 rounded-xl border border-input cursor-pointer'
onClick={handleAvatarClick}
>
{preview ? (
<AvatarImage
src={preview}
className='object-cover object-center'
alt='Profile Preview'
/>
) : (
<div className='flex flex-col justify-center items-center h-full w-full'>
<span>이미지</span>
<span>등록</span>
</div>
)}
</Avatar>
</div>const handleAvatarClick = () => {
if (fileInputRef.current != null) {
fileInputRef.current.click();
}
};<FormField
control={form.control}
name='logo'
render={({ field: { onChange, value, ref, ...rest } }) => ( // ✅ ref
<FormItem>
<FormLabel>Logo</FormLabel>
<FormControl>
<Input
type='file'
ref={fileInputRef}
{...rest}
onChange={(event) => {
const files = getImageData(event);
onChange(files);
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>