Skip to content

Commit

Permalink
Fix: BodyText
Browse files Browse the repository at this point in the history
  • Loading branch information
14KGun committed Sep 15, 2023
1 parent bedfc02 commit 976be7e
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/components/Chat/MessageForm/InputText/BodyText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,21 @@ const BodyText = ({ sendMessage }: BodyTextProps) => {
};

/* textarea event handler */
const onChange = (e: Event) => {
if (!textareaRef.current) return;
if (isSendingMessage) refreshTextArea();
setIsMessageValidState(getIsMessageValid(textareaRef.current.value));
const onChange = useCallback(
(e: Event) => {
if (!textareaRef.current) return;
if (isSendingMessage) refreshTextArea();
setIsMessageValidState(getIsMessageValid(textareaRef.current.value));

if (isEnterPressed.current && !isShiftPressed.current) {
onSend();
return;
}
resizeEvent();
};
const onKeyEvent = useCallback((e: KeyboardEvent, v: boolean) => {
if (isEnterPressed.current && !isShiftPressed.current) {
onSend();
return;
}
resizeEvent();
},
[isSendingMessage, getIsMessageValid, onSend]
);
const onKeyEvent = (e: KeyboardEvent, v: boolean) => {
if (e.code === "ShiftLeft" || e.code === "ShiftRight")
isShiftPressed.current = v;
if (e.code === "Enter") {
Expand All @@ -97,12 +100,12 @@ const BodyText = ({ sendMessage }: BodyTextProps) => {
}
isEnterPressed.current = v;
}
}, []);
const onKeyDown = useCallback((e: KeyboardEvent) => onKeyEvent(e, true), []);
const onKeyUp = useCallback((e: KeyboardEvent) => onKeyEvent(e, false), []);
};
const onKeyDown = (e: KeyboardEvent) => onKeyEvent(e, true);
const onKeyUp = (e: KeyboardEvent) => onKeyEvent(e, false);

/* textarea refresh handler */
const refreshTextArea = useCallback(() => {
const refreshTextArea = () => {
if (!wrapRef.current) return;
if (textareaRef.current) wrapRef.current.removeChild(textareaRef.current);
const textarea = document.createElement("textarea");
Expand All @@ -114,8 +117,8 @@ const BodyText = ({ sendMessage }: BodyTextProps) => {
textareaRef.current = textarea;
wrapRef.current.prepend(textarea);
resizeEvent();
}, []);
useEffect(refreshTextArea, []);
};
useEffect(refreshTextArea, [sendMessage]);

return (
<div
Expand Down

0 comments on commit 976be7e

Please sign in to comment.