import { useEffect, useRef, useState } from "react"; import { Button } from "./primitives"; import { Icon } from "./Icon"; export interface ChatInputProps { onSend: (text: string, image?: string) => void; sending: boolean; onVoice: () => void; placeholder?: string; } export function ChatInput({ onSend, sending, onVoice, placeholder = "给昔涟发消息…", }: ChatInputProps): React.ReactNode { const [value, setValue] = useState(""); const [pendingImage, setPendingImage] = useState(undefined); const taRef = useRef(null); const fileRef = useRef(null); // auto-grow the textarea up to a max height useEffect(() => { const el = taRef.current; if (!el) return; el.style.height = "auto"; el.style.height = Math.min(el.scrollHeight, 140) + "px"; }, [value]); const onPickFile = (e: React.ChangeEvent) => { const f = e.target.files?.[0]; if (!f) return; const reader = new FileReader(); reader.onload = () => { if (typeof reader.result === "string") setPendingImage(reader.result); }; reader.readAsDataURL(f); e.target.value = ""; }; const canSend = value.trim().length > 0 || Boolean(pendingImage); const submit = () => { if (sending || !canSend) return; onSend(value.trim(), pendingImage); setValue(""); setPendingImage(undefined); }; return (
{pendingImage ? (
待发送图片
) : null}