115 lines
2.9 KiB
TypeScript
115 lines
2.9 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useRef, useState } from "react";
|
||
|
|
|
||
|
|
import { EMOJI } from "@/lib/forum";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The text box with an emoji tray (phones have emoji on the keyboard; the
|
||
|
|
* tray is mostly for computers) and the send button.
|
||
|
|
*/
|
||
|
|
export function Composer({
|
||
|
|
id,
|
||
|
|
label,
|
||
|
|
placeholder,
|
||
|
|
initial = "",
|
||
|
|
submitLabel = "Küldés",
|
||
|
|
busy,
|
||
|
|
onSubmit,
|
||
|
|
onCancel,
|
||
|
|
rows = 3,
|
||
|
|
autoFocus = false,
|
||
|
|
}: {
|
||
|
|
id: string;
|
||
|
|
label: string;
|
||
|
|
placeholder?: string;
|
||
|
|
initial?: string;
|
||
|
|
submitLabel?: string;
|
||
|
|
busy: boolean;
|
||
|
|
onSubmit: (text: string) => Promise<boolean>;
|
||
|
|
onCancel?: () => void;
|
||
|
|
rows?: number;
|
||
|
|
autoFocus?: boolean;
|
||
|
|
}) {
|
||
|
|
const [text, setText] = useState(initial);
|
||
|
|
const [tray, setTray] = useState(false);
|
||
|
|
const area = useRef<HTMLTextAreaElement>(null);
|
||
|
|
|
||
|
|
function insert(emoji: string) {
|
||
|
|
const el = area.current;
|
||
|
|
if (!el) {
|
||
|
|
setText((t) => t + emoji);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const start = el.selectionStart ?? text.length;
|
||
|
|
const end = el.selectionEnd ?? text.length;
|
||
|
|
const next = text.slice(0, start) + emoji + text.slice(end);
|
||
|
|
setText(next);
|
||
|
|
requestAnimationFrame(() => {
|
||
|
|
el.focus();
|
||
|
|
el.setSelectionRange(start + emoji.length, start + emoji.length);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
async function submit(e: React.FormEvent) {
|
||
|
|
e.preventDefault();
|
||
|
|
if (!text.trim() || busy) return;
|
||
|
|
if (await onSubmit(text)) {
|
||
|
|
setText("");
|
||
|
|
setTray(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return (
|
||
|
|
<form className="fc" onSubmit={submit}>
|
||
|
|
<label htmlFor={id} className="sr-only">
|
||
|
|
{label}
|
||
|
|
</label>
|
||
|
|
<textarea
|
||
|
|
id={id}
|
||
|
|
ref={area}
|
||
|
|
className="fc-text"
|
||
|
|
rows={rows}
|
||
|
|
placeholder={placeholder}
|
||
|
|
value={text}
|
||
|
|
onChange={(e) => setText(e.target.value)}
|
||
|
|
maxLength={10000}
|
||
|
|
autoFocus={autoFocus}
|
||
|
|
/>
|
||
|
|
{tray && (
|
||
|
|
<div className="fc-tray" role="group" aria-label="Emoji">
|
||
|
|
{EMOJI.map((e) => (
|
||
|
|
<button key={e} type="button" className="fc-emoji" onClick={() => insert(e)} aria-label={`${e} beszúrása`}>
|
||
|
|
{e}
|
||
|
|
</button>
|
||
|
|
))}
|
||
|
|
</div>
|
||
|
|
)}
|
||
|
|
<div className="fc-actions">
|
||
|
|
<button
|
||
|
|
type="button"
|
||
|
|
className="fc-icon-btn"
|
||
|
|
aria-label="Emoji"
|
||
|
|
aria-expanded={tray}
|
||
|
|
onClick={() => setTray((t) => !t)}
|
||
|
|
>
|
||
|
|
<svg viewBox="0 0 24 24" aria-hidden="true">
|
||
|
|
<circle cx="12" cy="12" r="9" />
|
||
|
|
<path d="M8.5 14.5a4 4 0 0 0 7 0" />
|
||
|
|
<path d="M9 9.5h.01M15 9.5h.01" strokeWidth="2.6" />
|
||
|
|
</svg>
|
||
|
|
</button>
|
||
|
|
<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>
|
||
|
|
);
|
||
|
|
}
|