diff --git a/src/components.tsx b/src/components.tsx index 578477e..6ae2491 100644 --- a/src/components.tsx +++ b/src/components.tsx @@ -720,13 +720,24 @@ export function TagInput({ onChange, suggestions, placeholder, + text: textProp, + onTextChange, }: { values: string[]; onChange: (values: string[]) => void; suggestions: string[]; placeholder?: string; + /** The typed, not yet added text, for a parent that wants to act on it + * (say, add it when its dialog is confirmed). Uncontrolled if omitted. */ + text?: string; + onTextChange?: (text: string) => void; }) { - const [text, setText] = useState(""); + const [ownText, setOwnText] = useState(""); + const text = textProp ?? ownText; + const setText = (t: string) => { + setOwnText(t); + onTextChange?.(t); + }; const [sel, setSel] = useState(0); const query = fold(text.trim()); diff --git a/src/views/DeckView.tsx b/src/views/DeckView.tsx index ee9da8e..3db07d0 100644 --- a/src/views/DeckView.tsx +++ b/src/views/DeckView.tsx @@ -552,6 +552,7 @@ function TagCardsDialog({ }) { const [busy, setBusy] = useState(false); const [error, setError] = useState(null); + const [pending, setPending] = useState(""); const counts = new Map(); for (const e of entries) { for (const t of e.tags) { @@ -562,18 +563,27 @@ function TagCardsDialog({ } const used = [...counts.values()].sort((a, b) => a.tag.localeCompare(b.tag)); - async function apply(add: string[], remove: string[]) { + async function apply(add: string[], remove: string[]): Promise { setBusy(true); setError(null); try { await onChange(add, remove); + return true; } catch (e) { setError(String(e)); + return false; } finally { setBusy(false); } } + // A tag typed but not yet added with Enter is still meant for the cards. + async function done() { + const tag = pending.trim(); + if (tag && !(await apply([tag], []))) return; + onClose(); + } + return ( {used.length > 0 && ( @@ -602,11 +612,13 @@ function TagCardsDialog({ onChange={(next) => next.length > 0 && apply(next, [])} suggestions={suggestions} placeholder="New tag…" + text={pending} + onTextChange={setPending} /> {error &&

{error}

}
-