Skip to content

Build your personal knowledge base with Trilium Notes

fix(table): gather several values through a widget in the one visit

Elian Doranee522dd7a85a
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index a22ab53570..12b2d1138e 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1338,7 +1338,8 @@
"remove_color": "Remove the color label",
"remove_value": "Remove this value",
"select_values_placeholder": "Select an option or create one",
- "values_placeholder": "Type a value and press Enter"
+ "values_placeholder": "Type a value and press Enter",
+ "add_value": "Add this value"
},
"script_executor": {
"query": "Query",
diff --git a/apps/client/src/widgets/attribute_widgets/values_input.css b/apps/client/src/widgets/attribute_widgets/values_input.css
index 304abaef9c..23fa09490e 100644
--- a/apps/client/src/widgets/attribute_widgets/values_input.css
+++ b/apps/client/src/widgets/attribute_widgets/values_input.css
@@ -13,3 +13,45 @@
padding: 2px;
border: 1px solid var(--main-border-color);
}
+
+/*
+ * A field whose values are entered through a widget the browser brings: a calendar, a clock, a colour
+ * dialog. Each asks for about the width of the field itself, so a chip sharing the line with one
+ * would be squeezed to nothing — the values stack above it instead, a line each, and the widget waits
+ * at the foot of them.
+ */
+.values-input.stacked {
+ flex-direction: column;
+ align-items: stretch;
+
+ /* Held to their content's height, a column stretching them to fill the field otherwise. */
+ > .tn-chip,
+ > .form-control {
+ flex: 0 0 auto;
+ }
+
+ /*
+ * The widget leads and the values follow, rather than the other way about. Not for the reading of
+ * it: a box at the foot of a growing stack moves down with every value taken, and a browser keeps
+ * the box being typed in in view by scrolling whatever it sits in — which in a table is the pane
+ * the rows scroll in, and scrolling that hands the row being edited back to be rendered afresh,
+ * editor and all. Kept at the head, it does not move, and nothing is scrolled.
+ */
+ > .form-control {
+ order: -1;
+ align-self: flex-start;
+ }
+
+ /* Beside the widget it belongs to, which is now the first line rather than the last. */
+ > .values-input-add {
+ position: absolute;
+ inset-block-start: 4px;
+ inset-inline-end: 4px;
+ }
+}
+
+/* Made room for, so that a wide widget does not run under the button that takes its value. */
+.values-input.stacked:has(> .values-input-add) {
+ position: relative;
+ padding-inline-end: 28px;
+}
diff --git a/apps/client/src/widgets/attribute_widgets/values_input.spec.tsx b/apps/client/src/widgets/attribute_widgets/values_input.spec.tsx
index a3d4cebe5e..7e41284478 100644
--- a/apps/client/src/widgets/attribute_widgets/values_input.spec.tsx
+++ b/apps/client/src/widgets/attribute_widgets/values_input.spec.tsx
@@ -66,6 +66,37 @@ describe("ValuesInput", () => {
expect(input?.value).toBe("");
});
+ it("asks for a date rather than taking it as the field reports one", async () => {
+ const onCommit = vi.fn();
+ const input = await mount({ labelType: "date", values: [], onCommit });
+
+ // A date and a time report a change per part of them, so a wheel spun through the minutes
+ // says "settled" at every minute passed — a chip for each of them.
+ await typeInto(input, "2026-07-29");
+ await act(async () => {
+ input?.dispatchEvent(new Event("change", { bubbles: true }));
+ });
+ expect(onCommit).not.toHaveBeenCalled();
+
+ // The button beside the field is what asks for it, Enter doing as much from the keyboard.
+ await act(async () => container.querySelector<HTMLElement>(".values-input-add")?.click());
+ expect(onCommit).toHaveBeenCalledWith([ "2026-07-29" ]);
+ // Emptied rather than left showing what is now a chip, so the next one can be entered.
+ expect(input?.value).toBe("");
+ });
+
+ it("offers that button only where a value is entered through a widget of the browser's own", async () => {
+ // Text is confirmed by the Enter it is already typed with; a button beside it would be a
+ // second way of saying the same thing.
+ await mount({ labelType: "text", values: [], onCommit: vi.fn() });
+ expect(container.querySelector(".values-input-add")).toBeNull();
+
+ // A colour settles in one gesture — a dialog opened, a colour chosen, the dialog gone — so
+ // it is taken as it is reported and needs no asking either.
+ await mount({ labelType: "color", values: [], onCommit: vi.fn() });
+ expect(container.querySelector(".values-input-add")).toBeNull();
+ });
+
it("takes what was typed when the field is left, rather than throwing it away", async () => {
// What is typed here is the value itself, so leaving with something in the box would lose it
// — and a date is picked rather than typed, with no Enter to end it.
diff --git a/apps/client/src/widgets/attribute_widgets/values_input.tsx b/apps/client/src/widgets/attribute_widgets/values_input.tsx
index 5f6b0433b0..ee890eee09 100644
--- a/apps/client/src/widgets/attribute_widgets/values_input.tsx
+++ b/apps/client/src/widgets/attribute_widgets/values_input.tsx
@@ -1,10 +1,12 @@
import "./values_input.css";
import { type LabelType } from "@triliumnext/commons";
+import clsx from "clsx";
import type { ComponentChildren, TargetedKeyboardEvent } from "preact";
import { useEffect, useRef, useState } from "preact/hooks";
import { t } from "../../services/i18n";
+import ActionButton from "../react/ActionButton";
import Chip from "../react/Chip";
import FormTextBox from "../react/FormTextBox";
import { DEFAULT_COLOR, LABEL_MAPPINGS } from "./label_value_input";
@@ -36,26 +38,34 @@ interface ValuesInputProps {
*/
export default function ValuesInput({ labelType, values, onCommit, renderValue, placeholder, disabled }: ValuesInputProps) {
const [ draft, setDraft ] = useState("");
- const pickerRef = useRef<HTMLInputElement>(null);
- // A colour is picked rather than typed, so there is no Enter to end it and nothing worth keeping
- // half-entered: the pick itself is the whole gesture, and `change` is when it is settled.
- const isPicked = labelType === "color";
+ const inputRef = useRef<HTMLInputElement>(null);
+ const isColor = labelType === "color";
+ // Every widget but the colour dialog has to be asked for its value, having no plain way of
+ // saying it is done with.
+ const needsAsking = PICKED_TYPES.has(labelType) && !isColor;
- // The picker holds its own value and is never told one: it is set at birth and only read from
- // after that. Anything written into it can reach a dialog that is still open — a colour input
- // reports its pick before the dialog is dismissed — and the browser takes its value changing
- // underneath as reason to close, dropping the pick. So it keeps showing the colour last taken,
- // which is also the truer thing for it to show.
+ // A colour picker holds its own value and is never told one: it is set at birth and only read
+ // from after that. Anything written into it can reach a dialog that is still open — it reports
+ // its pick before the dialog is dismissed — and the browser takes its value changing underneath
+ // as reason to close, dropping the pick. So it keeps showing the colour last taken, which is
+ // also the truer thing for it to show.
useEffect(() => {
- if (pickerRef.current) {
- pickerRef.current.value = DEFAULT_COLOR;
+ if (isColor && inputRef.current) {
+ inputRef.current.value = DEFAULT_COLOR;
}
- }, []);
+ }, [ isColor ]);
useEffect(() => {
- const picker = pickerRef.current;
- if (!picker) return;
+ const picker = inputRef.current;
+ if (!isColor || !picker) return;
+ // A colour is settled in one gesture — a dialog opened, a colour chosen, the dialog gone —
+ // so its `change` marks the end of the whole thing and can be taken as the value.
+ //
+ // No other widget reports so plainly. A date and a time report a change per part of them,
+ // so a wheel spun through the minutes says "settled" at every minute passed; those are taken
+ // by asking instead, through Enter or the button beside the field.
+ //
// Bound by hand rather than through the JSX: preact/compat — loaded module-graph-wide by any
// compat-using import — remaps `onChange` onto the input event, which a colour picker fires
// at every step of a drag through it, taking a chip for each shade passed over.
@@ -91,7 +101,10 @@ export default function ValuesInput({ labelType, values, onCommit, renderValue,
}
return (
- <div className="tn-field values-input">
+ // A widget of the browser's own asks for the width of a calendar or a clock, which leaves a
+ // chip beside it too little to be read — so where the field is one of those, the chips stack
+ // above it instead of sharing its line.
+ <div className={clsx("tn-field values-input", PICKED_TYPES.has(labelType) && "stacked")}>
{values.map((value) => (
<Chip
key={value}
@@ -102,15 +115,21 @@ export default function ValuesInput({ labelType, values, onCommit, renderValue,
<span>{renderValue ? renderValue(value) : value}</span>
</Chip>
))}
- {isPicked ? (
+ {/* Keyed, as the chips before them are: a field of keyed and unkeyed siblings together
+ is matched up by position as it grows, so taking a second value would rebuild the box
+ rather than keep it — losing the focus in it, and with it the widget it had open. */}
+ {isColor ? (
<input
- ref={pickerRef}
+ key="field"
+ ref={inputRef}
className="form-control"
type="color"
disabled={disabled}
/>
) : (
<FormTextBox
+ key="field"
+ inputRef={inputRef}
type={LABEL_MAPPINGS[labelType] ?? "text"}
currentValue={draft}
// Only while the field is empty: beside chips it would read as one of them.
@@ -121,6 +140,28 @@ export default function ValuesInput({ labelType, values, onCommit, renderValue,
onKeyDown={handleKeyDown}
/>
)}
+
+ {/* A widget says nothing useful about when it is done — a date reports a change per part
+ of it — so a field entered through one is asked rather than watched. Enter does as
+ much for the keyboard; this is the same thing where it can be seen. */}
+ {needsAsking && (
+ <ActionButton
+ key="add"
+ className="values-input-add"
+ icon="bx bx-plus"
+ text={t("promoted_attributes.add_value")}
+ disabled={disabled}
+ // The value is read from the field rather than from the draft: leaving the field
+ // to press this takes it already, and the draft is empty by the time this runs.
+ onClick={() => take(inputRef.current?.value ?? "")}
+ />
+ )}
</div>
);
}
+
+/**
+ * The types entered through a widget the browser brings — a calendar, a clock, a colour dialog —
+ * rather than by typing into a box.
+ */
+const PICKED_TYPES = new Set<LabelType>([ "color", "date", "datetime", "time" ]);
diff --git a/apps/client/src/widgets/react/FormAutocomplete.tsx b/apps/client/src/widgets/react/FormAutocomplete.tsx
index 160761822b..4bd2308ab0 100644
--- a/apps/client/src/widgets/react/FormAutocomplete.tsx
+++ b/apps/client/src/widgets/react/FormAutocomplete.tsx
@@ -208,6 +208,10 @@ export default function FormAutocomplete({ currentValue, onChange, source, openO
const field = (
<FormTextBox
+ // Keyed for the sake of a field carrying chips, which are keyed themselves: keyed and
+ // unkeyed siblings together are matched up by position as the chips grow, which rebuilds
+ // the box rather than keeping it — and a rebuilt box is one the focus has left.
+ key="field"
{...restProps}
inputRef={inputEl}
currentValue={currentValue}