All files / core/blong-browser/src/widgets BigIntWidget.tsx

83.33% Statements 10/12
75% Branches 9/12
100% Functions 3/3
81.81% Lines 9/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56    6x                                                   3x 3x 3x       3x 2x 2x     3x                 3x              
import type {IWidgetProps} from '@feasibleone/blong';
 
const INTEGER_RE = /^-?\d+$/;
 
/**
 * BigIntWidget — numeric input for large integers (e.g. `bigint` columns).
 *
 * NumberWidget/IntegerWidget build on PrimeReact InputNumber, which is
 * JS-number based and silently rounds integers beyond Number.MAX_SAFE_INTEGER
 * (2^53−1).  This widget keeps the raw text in local state so 64-bit values
 * are not corrupted, and on change it emits:
 *  - a JS `number` when the value is within the safe range — so strict
 *    integer validation and existing consumers keep working; or
 *  - the raw `string` otherwise — preserving full bigint precision.
 *
 * The `bigInt*` schema types accept both forms (BigInt, Integer, or an
 * integer string), and the database column coerces the string back to bigint.
 */
export function BigIntWidget({
    id,
    name,
    value,
    onChange,
    onBlur,
    error,
    readOnly,
    disabled,
}: IWidgetProps) {
    const handleChange = (raw: string) => {
        const trimmed = raw.trim();
        Iif (trimmed === '') {
            onChange(null);
            return;
        }
        if (!INTEGER_RE.test(trimmed)) return; // incomplete/invalid — do not emit
        const num = Number(trimmed);
        onChange(Number.isSafeInteger(num) ? num : trimmed);
    };
 
    return (
        <input
            id={id ?? name}
            name={name}
            type="text"
            inputMode="numeric"
            autoComplete="off"
            className={`blong-bigint w-full p-inputtext p-component${error ? ' p-invalid' : ''}`}
            value={value == null ? '' : String(value)}
            onChange={e => handleChange(e.target.value)}
            onBlur={onBlur}
            readOnly={readOnly}
            disabled={disabled}
        />
    );
}