From 741589606553ffc091c0195c7a7a5de2892937af Mon Sep 17 00:00:00 2001 From: michael Date: Fri, 9 Aug 2024 23:49:12 +0800 Subject: [PATCH] prevent enter from sending empty message --- packages/jupyter-ai/src/components/chat-input.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/jupyter-ai/src/components/chat-input.tsx b/packages/jupyter-ai/src/components/chat-input.tsx index 4ab9bcd36..ddc2d4209 100644 --- a/packages/jupyter-ai/src/components/chat-input.tsx +++ b/packages/jupyter-ai/src/components/chat-input.tsx @@ -207,6 +207,7 @@ export function ChatInput(props: ChatInputProps): JSX.Element { props.chatHandler.sendMessage({ prompt, selection }); } + const inputExists = !!input.trim(); function handleKeyDown(event: React.KeyboardEvent) { if (event.key !== 'Enter') { return; @@ -218,6 +219,12 @@ export function ChatInput(props: ChatInputProps): JSX.Element { return; } + if (!inputExists) { + event.stopPropagation(); + event.preventDefault(); + return; + } + if ( event.key === 'Enter' && ((props.sendWithShiftEnter && event.shiftKey) || @@ -240,7 +247,6 @@ export function ChatInput(props: ChatInputProps): JSX.Element { ); - const inputExists = !!input.trim(); const sendButtonProps: SendButtonProps = { onSend, sendWithShiftEnter: props.sendWithShiftEnter,