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:
parent
2b7b756212
commit
140e36b6b7
5 changed files with 58 additions and 44 deletions
17
src/hooks/dom/useForwardRef.ts
Normal file
17
src/hooks/dom/useForwardRef.ts
Normal 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;
|
||||
};
|
|
@ -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]);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue