fix: 修复发送图片按回车时会同时触发发送消息的bug

pull/81/head
moonrailgun 4 years ago
parent ee77a29be5
commit 12d43322ad

@ -20,13 +20,20 @@ export const ImageUploadPreviewer: React.FC<ImageUploadPreviewerProps> =
} }
}, [props.onConfirm]); }, [props.onConfirm]);
useGlobalKeyDown((e) => { useGlobalKeyDown(
(e) => {
if (isEnterHotkey(e)) { if (isEnterHotkey(e)) {
e.stopPropagation();
handleConfirm(); handleConfirm();
} else if (isEscHotkey(e)) { } else if (isEscHotkey(e)) {
e.stopPropagation();
props.onCancel(); props.onCancel();
} }
}); },
{
capture: true,
}
);
return ( return (
<ModalWrapper style={{ maxHeight: '60vh', maxWidth: '60vw' }}> <ModalWrapper style={{ maxHeight: '60vh', maxWidth: '60vw' }}>

@ -5,7 +5,10 @@ import { useUpdateRef } from 'tailchat-shared';
* keydown hooks * keydown hooks
* *
*/ */
export function useGlobalKeyDown(fn: (e: KeyboardEvent) => void) { export function useGlobalKeyDown(
fn: (e: KeyboardEvent) => void,
options?: AddEventListenerOptions
) {
const fnRef = useUpdateRef(fn); const fnRef = useUpdateRef(fn);
useLayoutEffect(() => { useLayoutEffect(() => {
@ -13,10 +16,10 @@ export function useGlobalKeyDown(fn: (e: KeyboardEvent) => void) {
typeof fnRef.current === 'function' && fnRef.current(e); typeof fnRef.current === 'function' && fnRef.current(e);
}; };
window.addEventListener('keydown', handleKeyDown); window.addEventListener('keydown', handleKeyDown, options);
return () => { return () => {
window.removeEventListener('keydown', handleKeyDown); window.removeEventListener('keydown', handleKeyDown, options);
}; };
}, []); }, []);
} }

Loading…
Cancel
Save