feat(textarea): handle "/" keys
This commit is contained in:
parent
92abbc5b98
commit
c4f52adb45
|
@ -18,14 +18,21 @@ const EmptyChatMessageInput = ({
|
||||||
|
|
||||||
const inputRef = useRef<HTMLTextAreaElement | null>(null);
|
const inputRef = useRef<HTMLTextAreaElement | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
const handleKeyDown = (e: KeyboardEvent) => {
|
const handleKeyDown = (e: KeyboardEvent) => {
|
||||||
if (e.key === '/') {
|
const activeElement = document.activeElement;
|
||||||
|
|
||||||
|
const isInputFocused =
|
||||||
|
activeElement?.tagName === 'INPUT' ||
|
||||||
|
activeElement?.tagName === 'TEXTAREA' ||
|
||||||
|
activeElement?.hasAttribute('contenteditable');
|
||||||
|
|
||||||
|
if (e.key === '/' && !isInputFocused) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
inputRef.current?.focus();
|
inputRef.current?.focus();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
document.addEventListener('keydown', handleKeyDown);
|
document.addEventListener('keydown', handleKeyDown);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
|
|
@ -27,14 +27,21 @@ const MessageInput = ({
|
||||||
|
|
||||||
const inputRef = useRef<HTMLTextAreaElement | null>(null);
|
const inputRef = useRef<HTMLTextAreaElement | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
const handleKeyDown = (e: KeyboardEvent) => {
|
const handleKeyDown = (e: KeyboardEvent) => {
|
||||||
if (e.key === '/') {
|
const activeElement = document.activeElement;
|
||||||
|
|
||||||
|
const isInputFocused =
|
||||||
|
activeElement?.tagName === 'INPUT' ||
|
||||||
|
activeElement?.tagName === 'TEXTAREA' ||
|
||||||
|
activeElement?.hasAttribute('contenteditable');
|
||||||
|
|
||||||
|
if (e.key === '/' && !isInputFocused) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
inputRef.current?.focus();
|
inputRef.current?.focus();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
document.addEventListener('keydown', handleKeyDown);
|
document.addEventListener('keydown', handleKeyDown);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
|
Loading…
Reference in New Issue