From 482cc48e4e4f31ab531a27c346d622c512638017 Mon Sep 17 00:00:00 2001 From: Juan Di Nella Date: Tue, 10 Oct 2023 09:59:44 -0300 Subject: [PATCH] refactor MessageInput --- .../molecules/MessageInput/MessageInput.js | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/components/molecules/MessageInput/MessageInput.js b/src/components/molecules/MessageInput/MessageInput.js index 81c3990..1ee8d27 100644 --- a/src/components/molecules/MessageInput/MessageInput.js +++ b/src/components/molecules/MessageInput/MessageInput.js @@ -18,34 +18,28 @@ const MessageInput = ({ onChange = () => null, placeholder, }) => { - const [message, setMessage] = useState('') const [disableButton, setDisableButton] = useState(true) - const sendMessage = () => { - if (message.trim().length > 0) { - onSendMessage(message.trim()) - setMessage('') + const handleSubmit = (e) => { + e.preventDefault() + const text = e.target.value || e.target.elements[0].value + if (text.trim().length > 0) { + onSendMessage(e) setDisableButton(true) } } const handleChange = (e) => { - setMessage(e.target.value) - setDisableButton(e.target.value.trim().length === 0) + const text = e.target.value + setDisableButton(text.trim().length === 0) if (onChange) { - onChange(e.target.value) + onChange(text) } } - const handleSubmit = (e) => { - e.preventDefault() - sendMessage() - } - const handleKeyDown = (e) => { if (e.key === 'Enter' && !e.shiftKey) { - e.preventDefault() - sendMessage() + handleSubmit(e) } } @@ -53,7 +47,6 @@ const MessageInput = ({