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(client): commit a number box stepped by its spinner, which never focuses it
Elian Doran1cf994995d05
diff --git a/apps/client/src/widgets/react/FormTextBox.spec.tsx b/apps/client/src/widgets/react/FormTextBox.spec.tsxnew file mode 100644index 0000000000..e1e2303a98--- /dev/null+++ b/apps/client/src/widgets/react/FormTextBox.spec.tsx@@ -0,0 +1,79 @@+import { render } from "preact";+import { act } from "preact/test-utils";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++import FormTextBox, { FormTextBoxProps } from "./FormTextBox";++describe("FormTextBox", () => {+ let container: HTMLElement;++ beforeEach(() => {+ container = document.createElement("div");+ document.body.appendChild(container);+ });++ afterEach(() => {+ render(null, container);+ container.remove();+ });++ function renderBox(props: FormTextBoxProps) {+ act(() => render(<FormTextBox {...props} />, container));+ const input = container.querySelector("input");+ expect(input).not.toBeNull();+ return input as HTMLInputElement;+ }++ /** As leaving a box fires it, under either name the handler may be bound to (preact/compat+ * remaps `onBlur` to focusout when it is loaded, and the suite must not care whether it is). */+ function leave(input: HTMLInputElement) {+ input.dispatchEvent(new Event("change", { bubbles: true }));+ input.dispatchEvent(new Event("focusout", { bubbles: true }));+ input.dispatchEvent(new Event("blur"));+ }++ it("commits a number box on the spinner's own change event, no focus ever having arrived", () => {+ const onBlur = vi.fn();+ const input = renderBox({ type: "number", min: 1, currentValue: "1", onBlur });++ // A spinner step: the value moves and change fires, but focus — and so blur — never does.+ input.value = "2";+ act(() => { input.dispatchEvent(new Event("change", { bubbles: true })); });++ expect(onBlur).toHaveBeenCalledTimes(1);+ expect(onBlur).toHaveBeenCalledWith("2");+ });++ it("commits a typed-in number box once, though leaving it fires change and blur together", () => {+ const onBlur = vi.fn();+ const input = renderBox({ type: "number", min: 1, currentValue: "1", onBlur });++ input.value = "7";+ act(() => leave(input));++ expect(onBlur).toHaveBeenCalledTimes(1);+ expect(onBlur).toHaveBeenCalledWith("7");+ });++ it("clamps a number box's commit to its floor, the spinner not being the only way in", () => {+ const onBlur = vi.fn();+ const input = renderBox({ type: "number", min: 1, currentValue: "5", onBlur });++ input.value = "0";+ act(() => { input.dispatchEvent(new Event("change", { bubbles: true })); });++ expect(onBlur).toHaveBeenCalledWith("1");+ expect(input.value).toBe("1");+ });++ it("still commits a text box as the focus leaves it", () => {+ const onBlur = vi.fn();+ const input = renderBox({ currentValue: "before", onBlur });++ input.value = "after";+ act(() => leave(input));++ expect(onBlur).toHaveBeenCalledTimes(1);+ expect(onBlur).toHaveBeenCalledWith("after");+ });+});diff --git a/apps/client/src/widgets/react/FormTextBox.tsx b/apps/client/src/widgets/react/FormTextBox.tsxindex a5d5f773c6..471c02213a 100644--- a/apps/client/src/widgets/react/FormTextBox.tsx+++ b/apps/client/src/widgets/react/FormTextBox.tsx@@ -1,4 +1,4 @@-import { useEffect, type InputHTMLAttributes, type RefObject } from "preact/compat";+import { useEffect, useRef, type InputHTMLAttributes, type RefObject } from "preact/compat";export interface FormTextBoxProps extends Omit<InputHTMLAttributes<HTMLInputElement>, "onChange" | "onBlur" | "value"> {id?: string;@@ -9,9 +9,11 @@ export interface FormTextBoxProps extends Omit<InputHTMLAttributes<HTMLInputElem}export default function FormTextBox({ inputRef, className, type, currentValue, onChange, onBlur, autoFocus, ...rest}: FormTextBoxProps) {+ const innerRef = useRef<HTMLInputElement | null>(null);+useEffect(() => {if (autoFocus) {- inputRef?.current?.focus();+ innerRef.current?.focus();}}, []);@@ -32,9 +34,38 @@ export default function FormTextBox({ inputRef, className, type, currentValue, oreturn value;}+ // A number box's spinner steps the value without the focus ever arriving, so a commit waiting+ // on the blur may never come; the native change event is what a step fires — once per step, and+ // once alongside the blur for a value typed in — so the commit rides on it instead. Bound by+ // hand because there is no JSX way to it: preact/compat, loaded module-graph-wide by any+ // compat-using import, remaps `onChange` — in any casing — to the input event. Held in a ref so+ // the once-bound listener commits through the props of the render it fires in, not the one it+ // was bound in.+ const commitNumber = useRef<() => void>();+ commitNumber.current = () => {+ const input = innerRef.current;+ if (!input) return;++ const value = applyLimits(input.value);+ input.value = value;+ onBlur?.(value);+ };++ useEffect(() => {+ const input = innerRef.current;+ if (type !== "number" || !input) return;++ const onNativeChange = () => commitNumber.current?.();+ input.addEventListener("change", onNativeChange);+ return () => input.removeEventListener("change", onNativeChange);+ }, [ type ]);+return (<input- ref={inputRef}+ ref={(element) => {+ innerRef.current = element;+ if (inputRef) inputRef.current = element;+ }}className={`form-control ${className ?? ""}`}type={type ?? "text"}value={currentValue}@@ -46,7 +77,9 @@ export default function FormTextBox({ inputRef, className, type, currentValue, oonBlur={(e => {const currentValue = applyLimits(e.currentTarget.value);e.currentTarget.value = currentValue;- onBlur?.(currentValue);+ // A number box commits through the change listener above, which the blur already+ // fired where anything changed; committing here too would only say it twice.+ if (type !== "number") onBlur?.(currentValue);})}{...rest}/>