Skip to content

Build your personal knowledge base with Trilium Notes

4.5 KB
Raw Blame
import "./FormTextBox.css";

import type { InputHTMLAttributes, LooseInputHTMLAttributes, RefObject } from "preact";
import { useEffect, useRef } from "preact/hooks";

import PasteButton from "./PasteButton";

export interface FormTextBoxProps extends Omit<LooseInputHTMLAttributes<HTMLInputElement>, "onChange" | "onBlur" | "value"> {
    id?: string;
    currentValue?: string;
    onChange?(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, allowEmpty, ...rest}: FormTextBoxProps) {
    const innerRef = useRef<HTMLInputElement | null>(null);

    useEffect(() => {
        if (autoFocus) {
            innerRef.current?.focus();
        }
    }, []);

    function applyLimits(value: string) {
        if (type === "number") {
            if (allowEmpty && !value.trim()) return "";
            const { min, max } = rest;
            const currentValueNum = parseInt(value, 10);
            if (!Number.isFinite(currentValueNum)) {
                return String(min ?? "");
            }
            if (min && currentValueNum < parseInt(String(min), 10)) {
                return String(min);
            } else if (max && currentValueNum > parseInt(String(max), 10)) {
                return String(max);
            }
        }

        return 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) | undefined>(undefined);
    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={(element) => {
                innerRef.current = element;
                if (inputRef) inputRef.current = element;
            }}
            className={`form-control ${className ?? ""}`}
            value={currentValue}
            onInput={onChange && (e => {
                const target = e.currentTarget;
                const currentValue = applyLimits(e.currentTarget.value);
                onChange?.(currentValue, target.validity);
            })}
            onBlur={(e => {
                const currentValue = applyLimits(e.currentTarget.value);
                e.currentTarget.value = 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,
                type: type ?? "text"
            } as InputHTMLAttributes<HTMLInputElement>)}
        />
    );
}

export function FormTextBoxWithUnit(props: FormTextBoxProps & { unit: string }) {
    return (
        <label class="input-group tn-number-unit-pair">
            <FormTextBox {...props} />
            <span class="input-group-text">{props.unit}</span>
        </label>
    )
}

/** A text box with a {@link PasteButton} at its end. */
export function FormTextBoxWithPaste(props: FormTextBoxProps) {
    const ownRef = useRef<HTMLInputElement | null>(null);
    const inputRef = props.inputRef ?? ownRef;

    return (
        <div className="input-group">
            <FormTextBox {...props} inputRef={inputRef} />
            <PasteButton inputRef={inputRef} />
        </div>
    );
}