1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 12:56:41 +07:00

fixed ref forwarding on textareas

This commit is contained in:
Fedor Katurov 2022-01-05 16:32:07 +07:00
parent 2b7b756212
commit 140e36b6b7
5 changed files with 58 additions and 44 deletions

View file

@ -0,0 +1,17 @@
import { ForwardedRef, useEffect, useRef } from 'react';
export const useForwardRef = <T>(ref: ForwardedRef<T | null>) => {
const targetRef = useRef<T | null>(null);
useEffect(() => {
if (!ref) return;
if (typeof ref === 'function') {
(ref as any)(targetRef.current);
} else {
(ref as any).current = targetRef.current;
}
}, [ref]);
return targetRef;
};

View file

@ -1,12 +1,9 @@
import { useCallback, useEffect } from 'react';
import { useCallback } from 'react';
import { getImageFromPaste } from '~/utils/uploader';
// useInputPasteUpload attaches event listener to input, that calls onUpload if user pasted any image
export const useInputPasteUpload = (
input: HTMLTextAreaElement | HTMLInputElement | undefined,
onUpload: (files: File[]) => void
) => {
const onPaste = useCallback(
export const useInputPasteUpload = (onUpload: (files: File[]) => void) => {
return useCallback(
async event => {
const image = await getImageFromPaste(event);
@ -16,12 +13,4 @@ export const useInputPasteUpload = (
},
[onUpload]
);
useEffect(() => {
if (!input) return;
input.addEventListener('paste', onPaste);
return () => input.removeEventListener('paste', onPaste);
}, [input, onPaste]);
};