From 73553c425fd5fad426a415f9a934f97186eb1514 Mon Sep 17 00:00:00 2001 From: NicoSerranoP Date: Tue, 17 Dec 2024 12:09:15 -0500 Subject: [PATCH] fix(image): pr comments --- .../interface/src/components/ImageUpload.tsx | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/packages/interface/src/components/ImageUpload.tsx b/packages/interface/src/components/ImageUpload.tsx index 28e56e33..4d10eb39 100644 --- a/packages/interface/src/components/ImageUpload.tsx +++ b/packages/interface/src/components/ImageUpload.tsx @@ -1,14 +1,14 @@ +/* eslint-disable react/require-default-props */ + import { useMutation } from "@tanstack/react-query"; import clsx from "clsx"; import { ImageIcon } from "lucide-react"; -import { type ComponentProps, ElementType, forwardRef, useRef, useCallback } from "react"; +import { type ComponentProps, forwardRef, useRef, useCallback, ChangeEvent } from "react"; import { Controller, useFormContext } from "react-hook-form"; import { toast } from "sonner"; import { IconButton } from "~/components/ui/Button"; -import { PolymorphicRef } from "./ui"; - export interface IImageUploadProps extends ComponentProps<"img"> { name?: string; maxSize?: number; @@ -22,7 +22,7 @@ export const ImageUpload = forwardRef( className, }: IImageUploadProps, // eslint-disable-next-line @typescript-eslint/no-unused-vars - ref: PolymorphicRef, + _, ): JSX.Element => { const internalRef = useRef(null); const { control } = useFormContext(); @@ -44,18 +44,31 @@ export const ImageUpload = forwardRef( e.preventDefault(); }, []); + const onClickContainer = useCallback(() => { + internalRef.current?.click(); + }, [internalRef]); + + const onChangeInputImage = useCallback( + (event: ChangeEvent, onChange: (event: string) => void) => { + const [file] = event.target.files ?? []; + if (file) { + select.mutate(file, { + onSuccess: (objectUrl) => { + onChange(objectUrl); + }, + }); + } + }, + [select], + ); + return ( ( // eslint-disable-next-line jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events -
{ - internalRef.current?.click(); - }} - > +
{ - const [file] = event.target.files ?? []; - if (file) { - select.mutate(file, { - onSuccess: (objectUrl) => { - onChange(objectUrl); - }, - }); - } + onChangeInputImage(event, onChange); }} />