All files / blong-browser/src/widgets DateRangeWidget.tsx

75.67% Statements 28/37
100% Branches 1/1
0% Functions 0/1
75.67% Lines 28/37

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 381x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                    
import type {IWidgetProps} from '@feasibleone/blong';
import {DateRange} from '../components/DateRange/DateRange.js';
 
/**
 * DateRangeWidget — wraps the DateRange component.
 *
 * Form storage: `[Date, Date]` array (or null/undefined).
 * Component interface: JSON string "[from, to]" → onChange fires `{value: [Date, Date]}`.
 */
export function DateRangeWidget({
    name: _name,
    schema,
    value,
    onChange,
    readOnly,
    disabled,
}: IWidgetProps) {
    const {exclusive, timeOnly} = schema.widget ?? {};
 
    // Normalise stored value to the JSON string the DateRange component expects
    let strValue: string | null = null;
    if (Array.isArray(value) && value.length === 2) {
        strValue = JSON.stringify(value);
    } else if (typeof value === 'string') {
        strValue = value;
    }
 
    return (
        <DateRange
            value={strValue}
            onChange={e => onChange(e.value)}
            exclusive={Boolean(exclusive)}
            timeOnly={Boolean(timeOnly)}
            disabled={disabled || readOnly}
        />
    );
}