From 20c4ead7cce97c045cb40dc5af4aca24112c4279 Mon Sep 17 00:00:00 2001 From: tjtanjin Date: Sun, 28 Jan 2024 18:43:48 +0800 Subject: [PATCH] fix: Minor bug fixes for setting character count --- src/components/ChatBotInput/ChatBotInput.tsx | 22 +++++++++----------- src/services/VoiceService.tsx | 13 ++++++------ 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/components/ChatBotInput/ChatBotInput.tsx b/src/components/ChatBotInput/ChatBotInput.tsx index a9120ccf..a9af7aaa 100644 --- a/src/components/ChatBotInput/ChatBotInput.tsx +++ b/src/components/ChatBotInput/ChatBotInput.tsx @@ -122,18 +122,15 @@ const ChatBotInput = ({ return; } - const characterLimit = botOptions.chatInput?.characterLimit - if (characterLimit != null && characterLimit > 0) { - if (inputRef.current && inputRef.current.value.length > characterLimit) { - inputRef.current.value = inputRef.current.value.slice(0, characterLimit); - setInputLength(inputRef.current.value.length); - return; - } - } - if (inputRef.current) { + const characterLimit = botOptions.chatInput?.characterLimit + const newInput = event.target.value.replace(/\n/g, " "); + if (characterLimit != null && characterLimit > 0 && newInput.length > characterLimit) { + inputRef.current.value = newInput.slice(0, characterLimit); + } else { + inputRef.current.value = newInput + } setInputLength(inputRef.current.value.length); - inputRef.current.value = event.target.value.replace(/\n/g, " "); } }; @@ -181,8 +178,9 @@ const ChatBotInput = ({ />
{!botOptions.voice?.disabled && isDesktop && - } diff --git a/src/services/VoiceService.tsx b/src/services/VoiceService.tsx index 9d0e4a7d..99291beb 100644 --- a/src/services/VoiceService.tsx +++ b/src/services/VoiceService.tsx @@ -19,7 +19,8 @@ let toggleOn = false; * @param inputRef reference to textarea for input */ export const startVoiceRecording = (botOptions: Options, handleToggleVoice: () => void, - triggerSendVoiceInput: () => void, setInputLength: Dispatch>, inputRef: RefObject) => { + triggerSendVoiceInput: () => void, setInputLength: Dispatch>, + inputRef: RefObject) => { if (recognition == null) { return; @@ -44,12 +45,10 @@ export const startVoiceRecording = (botOptions: Options, handleToggleVoice: () = if (inputRef.current) { const characterLimit = botOptions.chatInput?.characterLimit const newInput = inputRef.current.value + voiceInput; - if (characterLimit != null && characterLimit > 0) { - if (newInput.length > characterLimit) { - inputRef.current.value = newInput.slice(0, characterLimit); - } else { - inputRef.current.value = newInput - } + if (characterLimit != null && characterLimit > 0 && newInput.length > characterLimit) { + inputRef.current.value = newInput.slice(0, characterLimit); + } else { + inputRef.current.value = newInput } setInputLength(inputRef.current.value.length); }