Skip to content

Build your personal knowledge base with Trilium Notes

fix(promoted-attributes): make multi-value url chips clickable (closes #12312)

Kevin Leutzinger39d34fde1e7a
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 c488b3b6c0..bd33f19937 100644
--- a/apps/client/src/widgets/attribute_widgets/values_input.spec.tsx
+++ b/apps/client/src/widgets/attribute_widgets/values_input.spec.tsx
@@ -74,6 +74,18 @@ describe("ValuesInput", () => {
expect(onCommit).toHaveBeenCalledWith([ "two" ]);
});
+ it("links the chips of a url, email or phone field, and only those", async () => {
+ await mount({ labelType: "url", values: [ "https://a.test/", "javascript:alert(1)" ], onCommit: vi.fn() });
+ expect([ ...container.querySelectorAll(".tn-chip a") ].map((a) => a.getAttribute("href")))
+ .toEqual([ "https://a.test/", "about:blank" ]);
+
+ await mount({ labelType: "email", values: [ "[email protected]" ], onCommit: vi.fn() });
+ expect(container.querySelector(".tn-chip a")?.getAttribute("href")).toBe("mailto:[email protected]");
+
+ await mount({ labelType: "text", values: [ "https://a.test/" ], onCommit: vi.fn() });
+ expect(container.querySelector(".tn-chip a")).toBeNull();
+ });
+
it("takes what was typed on Enter and empties the box, refusing a second of the same", async () => {
const onCommit = vi.fn();
const input = await mount({ labelType: "text", values: [ "one" ], onCommit });
diff --git a/apps/client/src/widgets/attribute_widgets/values_input.tsx b/apps/client/src/widgets/attribute_widgets/values_input.tsx
index 304fe8711d..eee232f2dc 100644
--- a/apps/client/src/widgets/attribute_widgets/values_input.tsx
+++ b/apps/client/src/widgets/attribute_widgets/values_input.tsx
@@ -10,7 +10,7 @@ import ActionButton from "../react/ActionButton";
import Chip from "../react/Chip";
import FormAutocomplete from "../react/FormAutocomplete";
import FormTextBox from "../react/FormTextBox";
-import { ColorSwatch } from "./label_value_display";
+import { ColorSwatch, renderLabelValue } from "./label_value_display";
import { DEFAULT_COLOR, LABEL_MAPPINGS } from "./label_value_input";
interface ValuesInputProps {
@@ -150,7 +150,7 @@ export default function ValuesInput({ labelType, values, onCommit, source, remov
the eye. A swatch rather than the chip's own ground, as the read-only chips wear
it: this chip also holds the button removing it, which no ground a user may pick
can be trusted to keep visible. */}
- <span>{isColor ? <ColorSwatch color={value} /> : value}</span>
+ <span>{isColor ? <ColorSwatch color={value} /> : LINK_TYPES.has(labelType) ? renderLabelValue(value, labelType) : value}</span>
</Chip>
));
@@ -252,6 +252,9 @@ export default function ValuesInput({ labelType, values, onCommit, source, remov
*/
const PICKED_TYPES = new Set<LabelType>([ "color", "date", "datetime", "time" ]);
+/** The types whose chips are links, so that a value can be followed without leaving the field. */
+const LINK_TYPES = new Set<LabelType>([ "url", "email", "phone" ]);
+
/**
* The box and the button beside it, held together as the one thing they are, so that the pair moves
* and wraps as a pair. Where there is no button there is nothing to hold: the box is the whole of it.