From bee2aafb20176710faf0a5dfec984745646f0839 Mon Sep 17 00:00:00 2001 From: Johnny Sung Date: Wed, 14 Jun 2023 14:00:19 +0800 Subject: [PATCH] change send behaviour to double press enter to send --- pages/index.tsx | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pages/index.tsx b/pages/index.tsx index cadf96f6b..49c8d4382 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -111,12 +111,23 @@ export default function Home() { } } - //prevent empty submissions + let enterCount: number = 0; + let timeoutId: NodeJS.Timeout; + const handleEnter = (e: any) => { - if (e.key === 'Enter' && query) { - handleSubmit(e); - } else if (e.key == 'Enter') { - e.preventDefault(); + if (e.key === 'Enter') { + enterCount++; + timeoutId = setTimeout(() => { + enterCount = 0; + }, 1000); + if (enterCount === 2 && query.trim() !== '') { + enterCount = 0; // reset conunter + clearTimeout(timeoutId); + handleSubmit(e); + } else { + // prevent empty submissions + e.preventDefault(); + } } };