Files
sorsfonalak/services/frontend/src/components/forum/Composer.tsx
T

78 lines
1.9 KiB
TypeScript
Raw Normal View History

"use client";
import { useState } from "react";
import { convertEmoticons, withEmoji } from "@/lib/emoticons";
/**
* The text box and the send button. Character smileys (`:)`, `<3`) turn into
* emoji as they are typed; phones bring their own emoji keyboard.
* `onTyping`: called on every keystroke (the "… gépel" notice).
*/
export function Composer({
id,
label,
placeholder,
initial = "",
submitLabel = "Küldés",
busy,
onSubmit,
onCancel,
onTyping,
rows = 3,
autoFocus = false,
}: {
id: string;
label: string;
placeholder?: string;
initial?: string;
submitLabel?: string;
busy: boolean;
onSubmit: (text: string) => Promise<boolean>;
onCancel?: () => void;
onTyping?: () => void;
rows?: number;
autoFocus?: boolean;
}) {
const [text, setText] = useState(initial);
async function submit(e: React.FormEvent) {
e.preventDefault();
if (!text.trim() || busy) return;
const final = convertEmoticons(text, text.length, true).text;
if (await onSubmit(final)) setText("");
}
return (
<form className="fc" onSubmit={submit}>
<label htmlFor={id} className="sr-only">
{label}
</label>
<textarea
id={id}
className="fc-text"
rows={rows}
placeholder={placeholder}
value={text}
onChange={(e) => {
withEmoji(e, setText);
onTyping?.();
}}
maxLength={10000}
autoFocus={autoFocus}
/>
<div className="fc-actions">
<span className="fc-spacer" />
{onCancel && (
<button type="button" className="btn btn-ghost btn-sm" onClick={onCancel}>
Mégse
</button>
)}
<button type="submit" className="btn btn-primary btn-sm" disabled={busy || !text.trim()}>
{busy ? "Küldés…" : submitLabel}
</button>
</div>
</form>
);
}