All files / src/components/RelativeRangeDatePicker RelativeRangeDatePicker.tsx

75% Statements 30/40
67.74% Branches 21/31
81.25% Functions 13/16
75% Lines 30/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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178    3x                               3x     25x   25x   25x 25x   25x   25x         4x           2x         25x 25x   25x               3x                                                 1x                 17x             17x             17x             17x             1x     17x                   2x 2x     2x 2x                                                               34x 14x   20x 10x   10x    
'use client';
 
import React from 'react';
 
import {useControlledState, useFocusWithin, useMobile} from '@gravity-ui/uikit';
 
import {block} from '../../utils/cn';
import {HiddenInput} from '../HiddenInput/HiddenInput';
import type {Value} from '../RelativeDatePicker';
import {filterDOMProps} from '../utils/filterDOMProps';
 
import {Control} from './components/Control/Control';
import {PickerDialog} from './components/PickerDialog/PickerDialog';
import {useRelativeRangeDatePickerState} from './hooks/useRelativeRangeDatePickerState';
import type {RelativeRangeDatePickerProps} from './types';
 
import './RelativeRangeDatePicker.scss';
 
const b = block('relative-range-date-picker');
 
export function RelativeRangeDatePicker(props: RelativeRangeDatePickerProps) {
    const state = useRelativeRangeDatePickerState(props);
 
    const isMobile = useMobile();
 
    const [anchor, setAnchor] = React.useState<HTMLDivElement | null>(null);
    const dialogClosing = React.useRef(false);
 
    const [open, setOpen] = useControlledState<boolean>(undefined, false, props.onOpenChange);
 
    const {focusWithinProps} = useFocusWithin({
        isDisabled: props.disabled,
        onFocusWithin: props.onFocus,
        onBlurWithin: props.onBlur,
        onFocusWithinChange: (isFocusWithin) => {
            if (
                isFocusWithin &&
                !dialogClosing.current &&
                !props.renderControl &&
                document.activeElement?.tagName !== 'BUTTON'
            ) {
                setOpen(true);
            }
        },
    });
 
    const DOMProps = filterDOMProps(props);
    delete DOMProps.id;
 
    return (
        <div
            {...DOMProps}
            ref={setAnchor}
            {...focusWithinProps}
            className={b(null, props.className)}
            style={props.style}
        >
            <Control
                props={props}
                state={state}
                open={open}
                setOpen={setOpen}
                isMobile={isMobile}
                onClick={() => {
                    if (props.disabled) {
                        return;
                    }
                    setOpen(true);
                }}
                onKeyDown={(e) => {
                    if (props.disabled) {
                        return;
                    }
                    if (e.altKey && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {
                        e.preventDefault();
                        setOpen(true);
                    }
                }}
                onClickCalendar={() => {
                    setOpen(!open);
                }}
                onUpdate={(v: string) => {
                    Iif (!props.readOnly && !v) {
                        state.setValue(null, 'default');
                    }
                }}
            />
            <HiddenInput
                name={props.name}
                form={props.form}
                value={state.value}
                toStringValue={(v) => v?.start?.type ?? ''}
                disabled={props.disabled}
            />
            <HiddenInput
                name={props.name}
                form={props.form}
                value={state.value}
                toStringValue={(v) => getNativeValue(v?.start ?? null)}
                disabled={props.disabled}
            />
            <HiddenInput
                name={props.name}
                form={props.form}
                value={state.value}
                toStringValue={(v) => v?.end?.type ?? ''}
                disabled={props.disabled}
            />
            <HiddenInput
                name={props.name}
                form={props.form}
                value={state.value}
                toStringValue={(v) => getNativeValue(v?.end ?? null)}
                disabled={props.disabled}
            />
            <HiddenInput
                name={props.name}
                form={props.form}
                onReset={(v) => {
                    state.setValue(v.value, v.timeZone);
                }}
                value={{value: state.value, timeZone: state.timeZone}}
                toStringValue={(v) => v.timeZone}
                disabled={props.disabled}
            />
 
            <PickerDialog
                value={state.value}
                onUpdate={state.setValue}
                timeZone={state.timeZone}
                open={open}
                onClose={() => {
                    setOpen(false);
                    dialogClosing.current = true;
                }}
                onRemove={() => {
                    setTimeout(() => {
                        dialogClosing.current = false;
                    });
                }}
                anchor={anchor}
                modal
                returnFocus
                popupClassName={props.popupClassName}
                popupStyle={props.popupStyle}
                popupPlacement={props.popupPlacement}
                popupOffset={props.popupOffset}
                isMobile={isMobile}
                size={props.size}
                format={props.format}
                readOnly={props.readOnly}
                minValue={props.minValue}
                maxValue={props.maxValue}
                allowNullableValues={props.allowNullableValues}
                isDateUnavailable={props.isDateUnavailable}
                placeholderValue={props.placeholderValue}
                withPresets={props.withPresets}
                applyPresetsImmediately={props.applyPresetsImmediately}
                presetTabs={props.presetTabs}
                docs={props.docs}
                withApplyButton={props.withApplyButton}
                withZonesList={props.withZonesList}
                withHeader={props.withHeader}
            />
        </div>
    );
}
 
function getNativeValue(value: Value | null) {
    if (!value) {
        return '';
    }
    if (value.type === 'relative') {
        return value.value;
    }
    return value.value.toISOString();
}