Decks: save a typed tag when the bulk Tag dialog's Done is clicked
This commit is contained in:
@@ -720,13 +720,24 @@ export function TagInput({
|
|||||||
onChange,
|
onChange,
|
||||||
suggestions,
|
suggestions,
|
||||||
placeholder,
|
placeholder,
|
||||||
|
text: textProp,
|
||||||
|
onTextChange,
|
||||||
}: {
|
}: {
|
||||||
values: string[];
|
values: string[];
|
||||||
onChange: (values: string[]) => void;
|
onChange: (values: string[]) => void;
|
||||||
suggestions: string[];
|
suggestions: string[];
|
||||||
placeholder?: 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 [sel, setSel] = useState(0);
|
||||||
|
|
||||||
const query = fold(text.trim());
|
const query = fold(text.trim());
|
||||||
|
|||||||
@@ -552,6 +552,7 @@ function TagCardsDialog({
|
|||||||
}) {
|
}) {
|
||||||
const [busy, setBusy] = useState(false);
|
const [busy, setBusy] = useState(false);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const [pending, setPending] = useState("");
|
||||||
const counts = new Map<string, { tag: string; n: number }>();
|
const counts = new Map<string, { tag: string; n: number }>();
|
||||||
for (const e of entries) {
|
for (const e of entries) {
|
||||||
for (const t of e.tags) {
|
for (const t of e.tags) {
|
||||||
@@ -562,18 +563,27 @@ function TagCardsDialog({
|
|||||||
}
|
}
|
||||||
const used = [...counts.values()].sort((a, b) => a.tag.localeCompare(b.tag));
|
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<boolean> {
|
||||||
setBusy(true);
|
setBusy(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
await onChange(add, remove);
|
await onChange(add, remove);
|
||||||
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setError(String(e));
|
setError(String(e));
|
||||||
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
setBusy(false);
|
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 (
|
return (
|
||||||
<DialogFrame title={`Tag ${entries.length} ${entries.length === 1 ? "card" : "cards"}`} onCancel={onClose}>
|
<DialogFrame title={`Tag ${entries.length} ${entries.length === 1 ? "card" : "cards"}`} onCancel={onClose}>
|
||||||
{used.length > 0 && (
|
{used.length > 0 && (
|
||||||
@@ -602,11 +612,13 @@ function TagCardsDialog({
|
|||||||
onChange={(next) => next.length > 0 && apply(next, [])}
|
onChange={(next) => next.length > 0 && apply(next, [])}
|
||||||
suggestions={suggestions}
|
suggestions={suggestions}
|
||||||
placeholder="New tag…"
|
placeholder="New tag…"
|
||||||
|
text={pending}
|
||||||
|
onTextChange={setPending}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
{error && <p className="dialog-error">{error}</p>}
|
{error && <p className="dialog-error">{error}</p>}
|
||||||
<div className="dialog-actions">
|
<div className="dialog-actions">
|
||||||
<button className="primary" onClick={onClose}>
|
<button className="primary" onClick={done} disabled={busy}>
|
||||||
Done
|
Done
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user