mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Some checks failed
Auto release excalidraw next / Auto-release-excalidraw-next (push) Failing after 2m36s
Build Docker image / build-docker (push) Failing after 6s
Cancel previous runs / cancel (push) Failing after 1s
Publish Docker / publish-docker (push) Failing after 31s
New Sentry production release / sentry (push) Failing after 2m3s
54 lines
1.2 KiB
TypeScript
54 lines
1.2 KiB
TypeScript
import { useEffect, useRef } from "react";
|
|
|
|
import { EVENT, KEYS } from "@excalidraw/common";
|
|
|
|
import type { ChangeEventHandler } from "react";
|
|
|
|
interface TTDDialogInputProps {
|
|
input: string;
|
|
placeholder: string;
|
|
onChange: ChangeEventHandler<HTMLTextAreaElement>;
|
|
onKeyboardSubmit?: () => void;
|
|
}
|
|
|
|
export const TTDDialogInput = ({
|
|
input,
|
|
placeholder,
|
|
onChange,
|
|
onKeyboardSubmit,
|
|
}: TTDDialogInputProps) => {
|
|
const ref = useRef<HTMLTextAreaElement>(null);
|
|
|
|
const callbackRef = useRef(onKeyboardSubmit);
|
|
callbackRef.current = onKeyboardSubmit;
|
|
|
|
useEffect(() => {
|
|
if (!callbackRef.current) {
|
|
return;
|
|
}
|
|
const textarea = ref.current;
|
|
if (textarea) {
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
|
if (event[KEYS.CTRL_OR_CMD] && event.key === KEYS.ENTER) {
|
|
event.preventDefault();
|
|
callbackRef.current?.();
|
|
}
|
|
};
|
|
textarea.addEventListener(EVENT.KEYDOWN, handleKeyDown);
|
|
return () => {
|
|
textarea.removeEventListener(EVENT.KEYDOWN, handleKeyDown);
|
|
};
|
|
}
|
|
}, []);
|
|
|
|
return (
|
|
<textarea
|
|
className="ttd-dialog-input"
|
|
onChange={onChange}
|
|
value={input}
|
|
placeholder={placeholder}
|
|
autoFocus
|
|
ref={ref}
|
|
/>
|
|
);
|
|
};
|