Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(collections): commit a number property once, and let it be emptied
Elian Doranba77800571bf
diff --git a/apps/client/src/widgets/react/FormTextBox.tsx b/apps/client/src/widgets/react/FormTextBox.tsxindex 2cc279dec8..5d03cf92a4 100644--- a/apps/client/src/widgets/react/FormTextBox.tsx+++ b/apps/client/src/widgets/react/FormTextBox.tsx@@ -7,9 +7,11 @@ export interface FormTextBoxProps extends Omit<InputHTMLAttributes<HTMLInputElemonChange?(newValue: string, validity: ValidityState): void;onBlur?(newValue: string): void;inputRef?: RefObject<HTMLInputElement | null>;+ /** Keeps an emptied number box empty, rather than putting `min` in it. */+ allowEmpty?: boolean;}-export default function FormTextBox({ inputRef, className, type, currentValue, onChange, onBlur, autoFocus, ...rest}: FormTextBoxProps) {+export default function FormTextBox({ inputRef, className, type, currentValue, onChange, onBlur, autoFocus, allowEmpty, ...rest}: FormTextBoxProps) {const innerRef = useRef<HTMLInputElement | null>(null);useEffect(() => {@@ -20,6 +22,7 @@ export default function FormTextBox({ inputRef, className, type, currentValue, ofunction applyLimits(value: string) {if (type === "number") {+ if (allowEmpty && !value.trim()) return "";const { min, max } = rest;const currentValueNum = parseInt(value, 10);if (!Number.isFinite(currentValueNum)) {diff --git a/apps/client/src/widgets/react/NotePropertyMenu.spec.tsx b/apps/client/src/widgets/react/NotePropertyMenu.spec.tsxindex 45cf5c9590..cd23b00ded 100644--- a/apps/client/src/widgets/react/NotePropertyMenu.spec.tsx+++ b/apps/client/src/widgets/react/NotePropertyMenu.spec.tsx@@ -145,4 +145,34 @@ describe("ViewPropertySection", () => {act(() => { select.dispatchEvent(new Event("change", { bubbles: true })); });expect(setLabel).toHaveBeenCalledWith(note.noteId, "calendar:slotDuration", "00:30:00");});++ it("writes a number once the box commits, and removes the label when the box is emptied", () => {+ const setLabel = vi.spyOn(attributes, "setLabel").mockResolvedValue(undefined);+ const removeLabel = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);+ const note = buildNote({ title: "Map", "#maxNestingDepth": "2" }) as unknown as FNote;+ const element = draw(note, [+ { type: "number", label: "Maximum nesting depth", bindToLabel: "maxNestingDepth" }+ ]);+ const input = element.querySelector<HTMLInputElement>("input");+ if (!input) throw new Error("The number property has no box.");+ expect(input.value).toBe("2");++ const type = (value: string) => act(() => {+ input.value = value;+ input.dispatchEvent(new Event("input", { bubbles: true }));+ });+ const commit = () => act(() => { input.dispatchEvent(new Event("change")); });++ type("1");+ type("12");+ expect(setLabel).not.toHaveBeenCalled();+ commit();+ expect(setLabel).toHaveBeenCalledExactlyOnceWith(note.noteId, "maxNestingDepth", "12");++ type("");+ commit();+ expect(input.value).toBe("");+ expect(removeLabel).toHaveBeenCalledExactlyOnceWith(note, "maxNestingDepth");+ expect(setLabel).toHaveBeenCalledTimes(1);+ });});diff --git a/apps/client/src/widgets/react/NotePropertyMenu.tsx b/apps/client/src/widgets/react/NotePropertyMenu.tsxindex d53db5fb3b..cc0467f2a3 100644--- a/apps/client/src/widgets/react/NotePropertyMenu.tsx+++ b/apps/client/src/widgets/react/NotePropertyMenu.tsx@@ -1,6 +1,6 @@import { FilterLabelsByType } from "@triliumnext/commons";import { Fragment, VNode } from "preact";-import { useContext } from "preact/hooks";+import { useContext, useEffect, useState } from "preact/hooks";import Component from "../../components/component";import FNote from "../../entities/fnote";@@ -265,8 +265,7 @@ function SplitButtonPropertyView({ note, property }: { note: FNote, property: Sp}function NumberPropertyView({ note, property }: { note: FNote, property: NumberProperty }) {- //@ts-expect-error Interop with text box which takes in string values even for numbers.- const [ value, setValue ] = useNoteLabel(note, property.bindToLabel);+ const [ draft, setDraft, commit ] = useNumberLabelDraft(note, property.bindToLabel);const disabled = property.disabled?.(note);return (@@ -278,7 +277,8 @@ function NumberPropertyView({ note, property }: { note: FNote, property: NumberP<PropertyLabel property={property} /><FormTextBoxtype="number"- currentValue={value ?? ""} onChange={setValue}+ allowEmpty+ currentValue={draft} onChange={setDraft} onBlur={commit}style={{ width: `${property.width ?? 100}px` }}min={property.min ?? 0}disabled={disabled}@@ -380,8 +380,7 @@ function OptionGroupPropertySection({ note, property }: {}function NumberPropertySection({ note, property }: { note: FNote, property: NumberProperty & PropertyHelp }) {- //@ts-expect-error Interop with text box which takes in string values even for numbers.- const [ value, setValue ] = useNoteLabel(note, property.bindToLabel);+ const [ draft, setDraft, commit ] = useNumberLabelDraft(note, property.bindToLabel);return (<OptionCardSectionname={property.bindToLabel}@@ -390,8 +389,10 @@ function NumberPropertySection({ note, property }: { note: FNote, property: Numb><FormTextBoxtype="number"- currentValue={value ?? ""}- onChange={setValue}+ allowEmpty+ currentValue={draft}+ onChange={setDraft}+ onBlur={commit}min={property.min ?? 0}disabled={property.disabled?.(note)}/>@@ -422,3 +423,24 @@ function ComboBoxPropertySection({ note, property }: {</OptionCardSection>);}++/**+ * A number label edited in a box: the typed value is held as a draft, written once the box commits+ * (on leaving it, or on a spinner step), and an emptied box removes the label.+ */+function useNumberLabelDraft(note: FNote, labelName: NumberProperty["bindToLabel"]) {+ //@ts-expect-error Interop with text box which takes in string values even for numbers.+ const [ value, setValue ] = useNoteLabel(note, labelName);+ const [ draft, setDraft ] = useState(value ?? "");++ useEffect(() => setDraft(value ?? ""), [ value ]);++ function commit(newValue: string) {+ const next = newValue.trim() || null;+ if (next !== (value ?? null)) {+ setValue(next);+ }+ }++ return [ draft, setDraft, commit ] as const;+}