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

96.96% Statements 32/33
94.11% Branches 32/34
88.88% Functions 8/9
96.96% Lines 32/33

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 139 140 141 142 143 144 145 146 14710x                                               149x 149x 149x   149x 8x 8x     149x 149x   149x   149x     3x 3x     10x 7x         149x 149x   149x           29x         1x 1x 1x                                             30x 30x         1x 1x 1x                                       3x 3x   3x     2x 2x                                          
import React from 'react';
 
import {useFocusWithin} from '@gravity-ui/uikit';
import type {PopupProps, TextInputProps} from '@gravity-ui/uikit';
 
import type {CalendarProps} from '../../Calendar';
import type {DateFieldProps} from '../../DateField';
import {filterDOMProps} from '../../utils/filterDOMProps.js';
import type {RelativeDateFieldProps} from '../RelativeDateField';
 
import type {RelativeDateFieldState} from './useRelativeDateFieldState';
 
interface RelativeDateProps {
    groupProps: React.HTMLAttributes<unknown>;
    inputProps: TextInputProps & {ref: React.Ref<HTMLElement>};
    popupProps: PopupProps;
    calendarProps: CalendarProps;
    timeInputProps: DateFieldProps;
}
 
export function useRelativeDateFieldProps(
    state: RelativeDateFieldState,
    props: RelativeDateFieldProps,
): RelativeDateProps {
    const lastCorrectDate = state.lastCorrectDate ? state.lastCorrectDate.startOf('day') : null;
    const [prevCorrectDate, setPrevCorrectDate] = React.useState(lastCorrectDate);
    const [focusedDate, setFocusedDate] = React.useState(lastCorrectDate);
 
    if (lastCorrectDate && (!prevCorrectDate || !lastCorrectDate.isSame(prevCorrectDate, 'day'))) {
        setPrevCorrectDate(lastCorrectDate);
        setFocusedDate(lastCorrectDate);
    }
 
    const [isOpen, setOpen] = React.useState(false);
    const dialogClosing = React.useRef(false);
 
    const [anchor, setAnchor] = React.useState<HTMLElement | null>(null);
 
    const {focusWithinProps} = useFocusWithin({
        onFocusWithin: props.onFocus,
        onBlurWithin: (e) => {
            props.onBlur?.(e);
            state.confirmValue();
        },
        onFocusWithinChange: (isFocusWithin) => {
            if (!dialogClosing.current) {
                setOpen(isFocusWithin);
            }
        },
    });
 
    const DOMProps = filterDOMProps(props);
    delete DOMProps.id;
 
    return {
        groupProps: {
            ...DOMProps,
            ...focusWithinProps,
            role: 'group',
            onKeyDown: (e) => {
                if (
                    isOpen &&
                    e.key === 'Escape' &&
                    (e.currentTarget as HTMLElement).contains(e.target as Node)
                ) {
                    e.preventDefault();
                    e.stopPropagation();
                    setOpen(false);
                }
            },
        },
        inputProps: {
            ref: setAnchor,
            size: props.size,
            autoFocus: props.autoFocus,
            value: state.text,
            onUpdate: state.setText,
            disabled: state.disabled,
            hasClear: props.hasClear,
            validationState: state.validationState,
            errorMessage: props.errorMessage,
            errorPlacement: props.errorPlacement,
            label: props.label,
            id: props.id,
            startContent: props.startContent,
            endContent: props.endContent,
            pin: props.pin,
            view: props.view,
            placeholder: props.placeholder,
            onKeyDown: (e) => {
                props.onKeyDown?.(e);
                if (
                    !e.defaultPrevented &&
                    e.altKey &&
                    (e.key === 'ArrowDown' || e.key === 'ArrowUp')
                ) {
                    e.preventDefault();
                    e.stopPropagation();
                    setOpen(true);
                }
            },
            onKeyUp: props.onKeyUp,
            controlProps: {
                'aria-label': props['aria-label'] || undefined,
                'aria-labelledby': props['aria-labelledby'] || undefined,
                'aria-describedby': props['aria-describedby'] || undefined,
                'aria-details': props['aria-details'] || undefined,
                'aria-disabled': state.disabled || undefined,
                readOnly: state.readOnly,
                onClick: () => {
                    setOpen(true);
                },
            },
        },
        popupProps: {
            anchorElement: anchor,
            open: isOpen,
            onOpenChange: (open) => {
                Eif (!open) {
                    setOpen(false);
                }
                dialogClosing.current = !open;
            },
            onTransitionOutComplete: () => {
                setTimeout(() => {
                    dialogClosing.current = false;
                });
            },
            placement: props.popupPlacement,
            offset: props.popupOffset,
        },
        calendarProps: {
            size: props.size === 's' ? 'm' : props.size,
            readOnly: true,
            value: state.lastCorrectDate,
            focusedValue: focusedDate,
            onFocusUpdate: setFocusedDate,
        },
        timeInputProps: {
            size: props.size,
            readOnly: true,
            value: state.lastCorrectDate,
            format: 'LTS',
        },
    };
}