Skip to content

Build your personal knowledge base with Trilium Notes

fix(attributes): commit nothing from a chip field that is disabled

Elian Doran42676c01794b
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 b3766f1fd0..1a45b3faaa 100644
--- a/apps/client/src/widgets/attribute_widgets/values_input.spec.tsx
+++ b/apps/client/src/widgets/attribute_widgets/values_input.spec.tsx
@@ -37,6 +37,21 @@ describe("ValuesInput", () => {
});
}
+ it("commits nothing while disabled, whatever reaches it", async () => {
+ // The box and the buttons are out of reach on the page, but a host may disable the field
+ // while something is typed into it, and leaving the box hands over what it holds.
+ const onCommit = vi.fn();
+ const input = await mount({ labelType: "text", values: [ "one" ], onCommit, disabled: true });
+
+ await typeInto(input, "two");
+ await press(input, "Enter");
+ await act(async () => { input?.dispatchEvent(new FocusEvent("blur", { bubbles: true })); });
+ await act(async () => container.querySelector<HTMLElement>(".tn-chip-remove")?.click());
+ await press(input, "Backspace");
+
+ expect(onCommit).not.toHaveBeenCalled();
+ });
+
it("shows the values held as chips, and drops the one pressed", async () => {
const onCommit = vi.fn();
await mount({ labelType: "text", values: [ "one", "two" ], onCommit });
diff --git a/apps/client/src/widgets/attribute_widgets/values_input.tsx b/apps/client/src/widgets/attribute_widgets/values_input.tsx
index 183b70d35e..79d4ffdeac 100644
--- a/apps/client/src/widgets/attribute_widgets/values_input.tsx
+++ b/apps/client/src/widgets/attribute_widgets/values_input.tsx
@@ -82,14 +82,18 @@ export default function ValuesInput({ labelType, values, onCommit, removeButtonT
return () => picker.removeEventListener("change", onPicked);
});
+ // A field that cannot be edited commits nothing, whatever reaches its handlers. The box and the
+ // buttons are already out of reach, but the box also hands over what it holds on being left —
+ // and a field can be disabled by its host while something is still typed into it.
function take(value: string) {
const trimmed = value.trim();
setDraft("");
- if (!trimmed || values.includes(trimmed)) return;
+ if (disabled || !trimmed || values.includes(trimmed)) return;
onCommit([ ...values, trimmed ]);
}
function drop(value: string) {
+ if (disabled) return;
onCommit(values.filter((held) => held !== value));
}