All files / src/components/RelativeDateField/hooks useRelativeDateFieldState.ts

90.47% Statements 38/42
88.37% Branches 38/43
100% Functions 6/6
90% Lines 36/40

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 13911x                                                                                 186x           186x 186x 186x 12x 12x     186x   90x 33x     57x         57x         186x   186x 186x 12x     186x   2x       2x         186x 3x 1x     2x 1x 1x     1x 3x 3x     186x   23x       23x   23x         23x 7x           186x   186x                          
import React from 'react';
 
import {dateTimeParse, isLikeRelative} from '@gravity-ui/date-utils';
import type {DateTime} from '@gravity-ui/date-utils';
import {useControlledState} from '@gravity-ui/uikit';
 
import type {InputBase, Validation, ValueBase} from '../../types';
 
export interface RelativeDateFieldState {
    /** The current field value. */
    value: string | null;
    /** Sets the field value */
    setValue: (v: string | null) => void;
    /** Commits current text value on blur. */
    confirmValue: () => void;
    /** Current user input */
    text: string;
    /** Sets text */
    setText: (t: string) => void;
    /** */
    parsedDate: DateTime | null;
    /** */
    lastCorrectDate: DateTime | null;
    /** */
    validationState?: 'invalid';
    /**
     * Whether the field is disabled.
     */
    disabled?: boolean;
    /**
     * Whether the value is immutable.
     */
    readOnly?: boolean;
}
 
export interface RelativeDateFieldOptions extends ValueBase<string | null>, InputBase, Validation {
    timeZone?: string;
    /** Round up parsed date to the nearest granularity. */
    roundUp?: boolean;
}
export function useRelativeDateFieldState(props: RelativeDateFieldOptions): RelativeDateFieldState {
    const [value, setValue] = useControlledState(
        props.value,
        props.defaultValue ?? '',
        props.onUpdate,
    );
 
    const [text, setText] = React.useState(value ?? '');
    const [lastValue, setLastValue] = React.useState(value);
    if (value !== lastValue) {
        setLastValue(value);
        setText(value ?? '');
    }
 
    const parseRelativeDate = React.useCallback(
        (relativeValue: string | null) => {
            if (!isLikeRelative(relativeValue)) {
                return null;
            }
 
            const date = dateTimeParse(relativeValue, {
                timeZone: props.timeZone,
                roundUp: props.roundUp,
            });
 
            return date?.isValid() ? date : null;
        },
        [props.roundUp, props.timeZone],
    );
 
    const parsedDate = React.useMemo(() => parseRelativeDate(text), [parseRelativeDate, text]);
 
    const [lastCorrectDate, setLastCorrectDate] = React.useState(parsedDate);
    if (parsedDate && (!lastCorrectDate || !parsedDate.isSame(lastCorrectDate))) {
        setLastCorrectDate(parsedDate);
    }
 
    const commitValue = React.useCallback(
        (nextValue: string | null) => {
            Iif (props.disabled || props.readOnly) {
                return;
            }
 
            setValue(nextValue);
        },
        [props.disabled, props.readOnly, setValue],
    );
 
    const confirmValue = React.useCallback(() => {
        if (!text) {
            return;
        }
 
        if (parsedDate) {
            commitValue(text);
            return;
        }
 
        const newValue = parseRelativeDate(value) ? value : null;
        setText(newValue ?? '');
        commitValue(newValue);
    }, [commitValue, parseRelativeDate, parsedDate, text, value]);
 
    const handleTextChange = React.useCallback(
        (t: string) => {
            Iif (props.disabled || props.readOnly) {
                return;
            }
 
            setText(t);
 
            Iif (!t) {
                setValue(null);
                return;
            }
 
            if (parseRelativeDate(t)) {
                setValue(t);
            }
        },
        [parseRelativeDate, props.disabled, props.readOnly, setValue],
    );
 
    const validationState = props.validationState ?? (text && !parsedDate ? 'invalid' : undefined);
 
    return {
        value,
        setValue: commitValue,
        confirmValue,
        text,
        setText: handleTextChange,
        parsedDate,
        lastCorrectDate,
        validationState,
        disabled: props.disabled,
        readOnly: props.readOnly,
    };
}