All files / src/components/RelativeDateField RelativeDateField.tsx

81.48% Statements 22/27
64.28% Branches 9/14
75% Functions 6/8
81.48% Lines 22/27

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 147    7x                                                     7x                                         19x 19x   19x   19x   19x 19x   19x   2x 1x           19x 19x   19x               7x           3x 3x                                                             1x 1x   1x     1x 1x                                          
'use client';
 
import React from 'react';
 
import {Popup, TextInput, useFocusWithin, useMobile} from '@gravity-ui/uikit';
 
import {block} from '../../utils/cn';
import {Calendar} from '../Calendar';
import {DateField} from '../DateField';
import {HiddenInput} from '../HiddenInput/HiddenInput';
import type {
    AccessibilityProps,
    DomProps,
    FocusableProps,
    InputDOMProps,
    KeyboardEvents,
    PopupStyleProps,
    StyleProps,
    TextInputExtendProps,
    TextInputProps,
} from '../types';
import {filterDOMProps} from '../utils/filterDOMProps';
 
import {useRelativeDateFieldProps} from './hooks/useRelativeDateFieldProps';
import {useRelativeDateFieldState} from './hooks/useRelativeDateFieldState';
import type {RelativeDateFieldOptions} from './hooks/useRelativeDateFieldState';
 
import './RelativeDateField.scss';
 
const b = block('relative-date-field');
 
export interface RelativeDateFieldProps
    extends
        RelativeDateFieldOptions,
        TextInputProps,
        TextInputExtendProps,
        DomProps,
        InputDOMProps,
        StyleProps,
        AccessibilityProps,
        FocusableProps,
        KeyboardEvents,
        PopupStyleProps {
    /**
     * Show time field in popup
     * @default false
     */
    hasTime?: boolean;
}
export function RelativeDateField(props: RelativeDateFieldProps) {
    const state = useRelativeDateFieldState(props);
    const {inputProps, calendarProps, timeInputProps} = useRelativeDateFieldProps(state, props);
 
    const isMobile = useMobile();
 
    const [anchor, setAnchor] = React.useState<HTMLElement | null>(null);
 
    const [isOpen, setOpen] = React.useState(false);
    const dialogClosing = React.useRef(false);
 
    const {focusWithinProps} = useFocusWithin({
        onFocusWithinChange: (isFocusWithin) => {
            if (!dialogClosing.current) {
                setOpen(isFocusWithin);
            }
        },
        isDisabled: isMobile,
    });
 
    const DOMProps = filterDOMProps(props);
    delete DOMProps.id;
 
    return (
        <div
            {...DOMProps}
            role="group"
            className={b(null, props.className)}
            style={props.style}
            {...focusWithinProps}
        >
            <TextInput
                {...inputProps}
                className={b('field')}
                ref={setAnchor}
                onBlur={props.onBlur}
                onKeyDown={(e) => {
                    inputProps.onKeyDown?.(e);
                    Iif (
                        !e.defaultPrevented &&
                        e.altKey &&
                        (e.key === 'ArrowDown' || e.key === 'ArrowUp')
                    ) {
                        e.preventDefault();
                        e.stopPropagation();
                        setOpen(true);
                    }
                }}
                controlProps={{
                    ...inputProps.controlProps,
                    onClick: () => {
                        setOpen(true);
                    },
                }}
            />
            <HiddenInput
                name={props.name}
                value={state.value}
                onReset={(value) => {
                    state.setValue(value);
                }}
                disabled={state.disabled}
                form={props.form}
            />
            {!isMobile && (
                <Popup
                    anchorElement={anchor}
                    open={isOpen}
                    onOpenChange={(open) => {
                        Eif (!open) {
                            setOpen(false);
                        }
                        dialogClosing.current = !open;
                    }}
                    onTransitionOutComplete={() => {
                        setTimeout(() => {
                            dialogClosing.current = false;
                        });
                    }}
                    placement={props.popupPlacement}
                    offset={props.popupOffset}
                    className={props.popupClassName}
                    style={props.popupStyle}
                >
                    <div className={b('popup-content')}>
                        <Calendar {...calendarProps} />
                        {props.hasTime ? (
                            <div className={b('time-field-wrapper')}>
                                <DateField className={b('time-field')} {...timeInputProps} />
                            </div>
                        ) : null}
                    </div>
                </Popup>
            )}
        </div>
    );
}