From 140e36b6b7f3d29ff37121564f967f3d07d57d2a Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Wed, 5 Jan 2022 16:32:07 +0700 Subject: [PATCH] fixed ref forwarding on textareas --- src/components/comment/CommentForm/index.tsx | 6 +-- .../LocalCommentFormTextarea/index.tsx | 17 ++++--- src/components/input/Textarea/index.tsx | 45 ++++++++++--------- src/hooks/dom/useForwardRef.ts | 17 +++++++ src/hooks/dom/useInputPasteUpload.ts | 17 ++----- 5 files changed, 58 insertions(+), 44 deletions(-) create mode 100644 src/hooks/dom/useForwardRef.ts diff --git a/src/components/comment/CommentForm/index.tsx b/src/components/comment/CommentForm/index.tsx index a90375d9..d149d8f1 100644 --- a/src/components/comment/CommentForm/index.tsx +++ b/src/components/comment/CommentForm/index.tsx @@ -25,7 +25,7 @@ interface IProps { } const CommentForm: FC = ({ comment, nodeId, saveComment, onCancelEdit }) => { - const [textarea, setTextarea] = useState(); + const [textarea, setTextArea] = useState(null); const uploader = useFileUploader( UPLOAD_SUBJECTS.COMMENT, UPLOAD_TARGETS.COMMENTS, @@ -55,7 +55,7 @@ const CommentForm: FC = ({ comment, nodeId, saveComment, onCancelEdit }) }, [formik]); const error = formik.status || formik.errors.text; - useInputPasteUpload(textarea, uploader.uploadFiles); + const onPaste = useInputPasteUpload(uploader.uploadFiles); return ( @@ -63,7 +63,7 @@ const CommentForm: FC = ({ comment, nodeId, saveComment, onCancelEdit })
- + {!!error && (
diff --git a/src/components/comment/LocalCommentFormTextarea/index.tsx b/src/components/comment/LocalCommentFormTextarea/index.tsx index 1dcc9640..f5e68d59 100644 --- a/src/components/comment/LocalCommentFormTextarea/index.tsx +++ b/src/components/comment/LocalCommentFormTextarea/index.tsx @@ -1,14 +1,18 @@ -import React, { FC, KeyboardEventHandler, useCallback } from 'react'; +import React, { + forwardRef, + KeyboardEventHandler, + TextareaHTMLAttributes, + useCallback, +} from 'react'; import { Textarea } from '~/components/input/Textarea'; import { useCommentFormContext } from '~/hooks/comments/useCommentFormFormik'; import { useRandomPhrase } from '~/constants/phrases'; -interface IProps { +interface IProps extends TextareaHTMLAttributes { isLoading?: boolean; - setRef?: (r: HTMLTextAreaElement) => void; } -const LocalCommentFormTextarea: FC = ({ setRef }) => { +const LocalCommentFormTextarea = forwardRef(({ ...rest }, ref) => { const { values, handleChange, handleSubmit, isSubmitting } = useCommentFormContext(); const onKeyDown = useCallback>( @@ -22,14 +26,15 @@ const LocalCommentFormTextarea: FC = ({ setRef }) => { return (