Skip to content

Build your personal knowledge base with Trilium Notes

feat(promoted_attributes): allow repeated values in multi-value fields (closes #12075)

Elian Doran593a6c170663
diff --git a/apps/client/src/widgets/attribute_widgets/multi_value_input.spec.tsx b/apps/client/src/widgets/attribute_widgets/multi_value_input.spec.tsx
index fd34ec68f9..c7bb80a950 100644
--- a/apps/client/src/widgets/attribute_widgets/multi_value_input.spec.tsx
+++ b/apps/client/src/widgets/attribute_widgets/multi_value_input.spec.tsx
@@ -45,6 +45,25 @@ describe("MultiValueInput", () => {
}
});
+ it("takes a free-form value a second time", async () => {
+ // Two payments of the same amount are two values, not one.
+ const onCommit = vi.fn();
+ await mount({ labelType: "number", values: [ "50" ], onCommit });
+
+ const input = container.querySelector("input");
+ await act(async () => {
+ if (input) {
+ input.value = "50";
+ input.dispatchEvent(new Event("input", { bubbles: true }));
+ }
+ });
+ await act(async () => {
+ input?.dispatchEvent(
+ new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }));
+ });
+ expect(onCommit).toHaveBeenCalledWith([ "50", "50" ]);
+ });
+
it("shows each value as its type reads it, rather than as the text it is stored as", async () => {
// `#3d5a80` names nothing to the eye, so a colour is its swatch even while being edited.
await mount({ labelType: "color", values: [ "#3d5a80" ] });
diff --git a/apps/client/src/widgets/attribute_widgets/multi_value_input.tsx b/apps/client/src/widgets/attribute_widgets/multi_value_input.tsx
index a4a458cd2a..1c4f686071 100644
--- a/apps/client/src/widgets/attribute_widgets/multi_value_input.tsx
+++ b/apps/client/src/widgets/attribute_widgets/multi_value_input.tsx
@@ -37,7 +37,9 @@ interface MultiValueInputProps {
*
* Which box that is depends on the kind of value. A closed set — the options of a `select`, or the
* two a flag can be — is picked from a list, so nothing typed is ever kept. Everything else is
- * entered directly, through the same box a single value of that type is typed or picked in.
+ * entered directly, through the same box a single value of that type is typed or picked in, and can
+ * be entered more than once: the labels form a list, so two payments of the same amount are two
+ * values.
*
* Shared by the promoted-attribute grid and the table's own cells, so that the same definition is
* edited the same way wherever the note is opened.
@@ -69,6 +71,7 @@ export default function MultiValueInput({ labelType, values, onCommit, source, o
inputId={inputId}
tabIndex={tabIndex}
disabled={disabled}
+ allowDuplicates
onCommit={onCommit}
/>
);
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 1089573c59..c488b3b6c0 100644
--- a/apps/client/src/widgets/attribute_widgets/values_input.spec.tsx
+++ b/apps/client/src/widgets/attribute_widgets/values_input.spec.tsx
@@ -92,6 +92,27 @@ describe("ValuesInput", () => {
expect(input?.value).toBe("");
});
+ it("takes a repeated value when duplicates are allowed, and drops only the chip pressed", async () => {
+ const onCommit = vi.fn();
+ const input = await mount({
+ labelType: "number", values: [ "5", "7" ], onCommit, allowDuplicates: true
+ });
+
+ await typeInto(input, "5");
+ await press(input, "Enter");
+ expect(onCommit).toHaveBeenCalledWith([ "5", "7", "5" ]);
+
+ // Removing one of two equal values leaves the other.
+ onCommit.mockClear();
+ await mount({
+ labelType: "number", values: [ "5", "7", "5" ], onCommit, allowDuplicates: true
+ });
+ const chips = [ ...container.querySelectorAll(".tn-chip") ];
+ expect(chips.map((chip) => chip.textContent?.trim())).toEqual([ "5", "7", "5" ]);
+ await act(async () => chips[2]?.querySelector<HTMLElement>(".tn-chip-remove")?.click());
+ expect(onCommit).toHaveBeenCalledWith([ "5", "7" ]);
+ });
+
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 });
diff --git a/apps/client/src/widgets/attribute_widgets/values_input.tsx b/apps/client/src/widgets/attribute_widgets/values_input.tsx
index 72f609cc46..304fe8711d 100644
--- a/apps/client/src/widgets/attribute_widgets/values_input.tsx
+++ b/apps/client/src/widgets/attribute_widgets/values_input.tsx
@@ -38,6 +38,11 @@ interface ValuesInputProps {
tabIndex?: number;
placeholder?: string;
disabled?: boolean;
+ /**
+ * Takes a value the field already holds, for values that form a list rather than a set. Left
+ * out, a repeated value is refused.
+ */
+ allowDuplicates?: boolean;
}
/**
@@ -46,10 +51,9 @@ interface ValuesInputProps {
*
* Where a select picks from a closed set, here what is typed *is* the value — so the box hands it
* over on Enter, and again when the field is left, so that a value typed and then abandoned is kept
- * rather than quietly dropped. A value already held is not taken a second time: the chips are a set,
- * and two alike could not be told apart.
+ * rather than quietly dropped. A value already held is refused unless `allowDuplicates` is set.
*/
-export default function ValuesInput({ labelType, values, onCommit, source, removeButtonText, addButtonText, inputId, tabIndex, placeholder, disabled }: ValuesInputProps) {
+export default function ValuesInput({ labelType, values, onCommit, source, removeButtonText, addButtonText, inputId, tabIndex, placeholder, disabled, allowDuplicates }: ValuesInputProps) {
const [ draft, setDraft ] = useState("");
const inputRef = useRef<HTMLInputElement>(null);
const isColor = labelType === "color";
@@ -107,13 +111,13 @@ export default function ValuesInput({ labelType, values, onCommit, source, remov
function take(value: string) {
const trimmed = value.trim();
setDraft("");
- if (disabled || !trimmed || values.includes(trimmed)) return;
+ if (disabled || !trimmed || (!allowDuplicates && values.includes(trimmed))) return;
onCommit([ ...values, trimmed ]);
}
- function drop(value: string) {
+ function drop(index: number) {
if (disabled) return;
- onCommit(values.filter((held) => held !== value));
+ onCommit(values.filter((_, held) => held !== index));
}
function handleKeyDown(e: TargetedKeyboardEvent<HTMLInputElement>) {
@@ -131,16 +135,16 @@ export default function ValuesInput({ labelType, values, onCommit, source, remov
// Backspace on an empty box drops the last chip, the box having nothing of its own to
// erase. With something typed it is the box's own, as anywhere else.
e.preventDefault();
- drop(values[values.length - 1]);
+ drop(values.length - 1);
}
}
- const chips = values.map((value) => (
+ const chips = values.map((value, index) => (
<Chip
- key={value}
+ key={`${index}:${value}`}
removeButtonText={removeButtonText ?? t("promoted_attributes.remove_value")}
disabled={disabled}
- onRemove={() => drop(value)}
+ onRemove={() => drop(index)}
>
{/* A colour reads as its swatch even while being edited, `#3d5a80` naming nothing to
the eye. A swatch rather than the chip's own ground, as the read-only chips wear
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Attributes/Promoted Attributes.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Attributes/Promoted Attributes.html
index 117f503026..afd2e5eb2f 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Attributes/Promoted Attributes.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Attributes/Promoted Attributes.html
@@ -80,6 +80,14 @@
system, meaning that changing the type afterwards does not guarantee that
the values in it correspond to the type (e.g. turning a text into a number).</p>
</aside>
+<h2>Multiple values</h2>
+<p>When the definition allows multiple values, each value is shown as a chip
+ and a new one is entered in the box after them. Press <kbd>Enter</kbd> (or
+ leave the field) to add what was typed, and press the <span class="tn-icon bx bx-x"></span> on
+ a chip to remove that value.</p>
+<p>The same value can be added more than once, for example two payments of
+ the same amount. <em>Select</em> and <em>Checkbox</em> attributes are the exception:
+ an option that is already picked is not offered again.</p>
<h2>Creating a new promoted attribute definition</h2>
<p>To create a new promoted attribute:</p>
<ol>
diff --git a/docs/User Guide/User Guide/Advanced Usage/Attributes/Promoted Attributes.md b/docs/User Guide/User Guide/Advanced Usage/Attributes/Promoted Attributes.md
index dc81b270c8..4a6c101c8f 100644
--- a/docs/User Guide/User Guide/Advanced Usage/Attributes/Promoted Attributes.md
+++ b/docs/User Guide/User Guide/Advanced Usage/Attributes/Promoted Attributes.md
@@ -50,6 +50,12 @@ The following types are supported:
> [!NOTE]
> For label-based attributes, these types are not enforced by the attribute system, meaning that changing the type afterwards does not guarantee that the values in it correspond to the type (e.g. turning a text into a number).
+## Multiple values
+
+When the definition allows multiple values, each value is shown as a chip and a new one is entered in the box after them. Press <kbd>Enter</kbd> (or leave the field) to add what was typed, and press the <span class="tn-icon bx bx-x"></span> on a chip to remove that value.
+
+The same value can be added more than once, for example two payments of the same amount. _Select_ and _Checkbox_ attributes are the exception: an option that is already picked is not offered again.
+
## Creating a new promoted attribute definition
To create a new promoted attribute: