From 8acadca768f1fe497217bfd05e093bd31271695f Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 13 Aug 2024 17:01:29 -0700 Subject: [PATCH] fix: chatbot input fixes (#376) * fix: chatbot input fixes --- .../assistant-chatbot/Chatbot.spec.tsx | 38 +++++++++++++++++++ .../components/assistant-chatbot/Chatbot.tsx | 34 +++++++++++------ .../assistant-chatbot/ChatbotInputBox.tsx | 33 +++++++++++++++- .../components/assistant-chatbot/chatbot.css | 4 ++ .../ChatbotConversationContainer.tsx | 7 +++- 5 files changed, 101 insertions(+), 15 deletions(-) diff --git a/packages/react-components/src/components/assistant-chatbot/Chatbot.spec.tsx b/packages/react-components/src/components/assistant-chatbot/Chatbot.spec.tsx index c06353925..c5e05506f 100644 --- a/packages/react-components/src/components/assistant-chatbot/Chatbot.spec.tsx +++ b/packages/react-components/src/components/assistant-chatbot/Chatbot.spec.tsx @@ -193,4 +193,42 @@ describe(Chatbot, () => { }); expect(mockOnClose).toBeCalled(); }); + + it('should resize message container when customer types breakline', async () => { + const user = userEvent.setup(); + render( + + ); + + const textarea = screen.getByPlaceholderText( + 'Ask me anything about your IoT data' + ); + expect(textarea).toBeInTheDocument(); + + await act(async () => { + return await user.type(textarea!, 'some text\n'); + }); + + await act(async () => { + return await user.type(textarea!, 'some text\n'); + }); + + const inputContainer = screen.getByTestId( + 'assistant-chatbot-conversation-container' + ); + expect(inputContainer).toBeInTheDocument(); + expect(inputContainer.style.height).toBe('450px'); + }); }); diff --git a/packages/react-components/src/components/assistant-chatbot/Chatbot.tsx b/packages/react-components/src/components/assistant-chatbot/Chatbot.tsx index d5db9ee77..8d34f1a1e 100644 --- a/packages/react-components/src/components/assistant-chatbot/Chatbot.tsx +++ b/packages/react-components/src/components/assistant-chatbot/Chatbot.tsx @@ -15,6 +15,8 @@ export interface ChatbotProps { onClose?: () => void; } +const MIN_HEIGHT = 400; + export const Chatbot = ({ messages, visible, @@ -25,18 +27,22 @@ export const Chatbot = ({ const [adjustedHeight, setAdjustedHeight] = useState(height || 400); const lastMessage = messages[messages.length - 1]; + const adjustHeight = () => { + const chatbotInput = document.querySelector( + '.iot-app-kit-assistant-chatbot-input' + ) as HTMLElement; + const chatbotHeader = document.querySelector( + '.iot-app-kit-assistant-chatbot-header' + ) as HTMLElement; + const chatbotInputHeight = chatbotInput?.clientHeight || 0; + const chatbotHeaderHeight = chatbotHeader?.clientHeight || 0; + const newHeight = height - chatbotHeaderHeight - chatbotInputHeight - 50; + setAdjustedHeight(newHeight); + }; + useEffect(() => { - if (height > 400) { - const chatbotInput = document.querySelector( - '.iot-app-kit-assistant-chatbot-input' - ) as HTMLElement; - const chatbotHeader = document.querySelector( - '.iot-app-kit-assistant-chatbot-header' - ) as HTMLElement; - const chatbotInputHeight = chatbotInput?.clientHeight || 0; - const chatbotHeaderHeight = chatbotHeader?.clientHeight || 0; - const newHeight = height - chatbotHeaderHeight - chatbotInputHeight - 50; - setAdjustedHeight(newHeight); + if (height > MIN_HEIGHT) { + adjustHeight(); } }, [visible, height]); @@ -44,7 +50,11 @@ export const Chatbot = ({
+ } header={ diff --git a/packages/react-components/src/components/assistant-chatbot/ChatbotInputBox.tsx b/packages/react-components/src/components/assistant-chatbot/ChatbotInputBox.tsx index 7fcf213ca..c695ca533 100644 --- a/packages/react-components/src/components/assistant-chatbot/ChatbotInputBox.tsx +++ b/packages/react-components/src/components/assistant-chatbot/ChatbotInputBox.tsx @@ -6,13 +6,22 @@ import Textarea from '@cloudscape-design/components/textarea'; import Button from '@cloudscape-design/components/button'; import Box from '@cloudscape-design/components/box'; import { IMessage, SenderType } from '../../hooks/useAssistant/types'; +import { + InputProps, + NonCancelableCustomEvent, +} from '@cloudscape-design/components'; export interface ChatbotInputBox { onSubmit: (utterance: string) => void; + onResize: () => void; lastMessage?: IMessage; } -export const ChatbotInputBox = ({ onSubmit, lastMessage }: ChatbotInputBox) => { +export const ChatbotInputBox = ({ + lastMessage, + onSubmit, + onResize, +}: ChatbotInputBox) => { const [value, setValue] = useState(''); const [disabled, setDisabled] = useState(false); @@ -39,13 +48,33 @@ export const ChatbotInputBox = ({ onSubmit, lastMessage }: ChatbotInputBox) => { } }; + const handleChange = ( + event: NonCancelableCustomEvent + ) => { + const { value: newValue } = event.detail; + setValue(newValue); + + if (newValue.match(/\n/g)) { + onResize(); + } + }; + + const handleKeyDown = ( + event: NonCancelableCustomEvent + ) => { + if (event.detail.key === 'Enter' && !event.detail.shiftKey) { + handleClick(); + } + }; + return (