All files / src/components/DatePicker MobileCalendar.tsx

74.35% Statements 29/39
57.14% Branches 20/35
100% Functions 4/4
74.35% Lines 29/39

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 11915x                           15x                 16x 16x 10x 6x 6x     16x   16x 16x         16x                       14x     14x 14x 14x       14x           14x 14x                       14x                   16x 16x 16x       16x   6x     10x                       48x 32x     16x 16x    
'use client';
 
import {dateTime} from '@gravity-ui/date-utils';
import type {DateTime} from '@gravity-ui/date-utils';
 
import {block} from '../../utils/cn';
import type {FormatInfo} from '../DateField/types';
import {createPlaceholderValue, mergeDateTime} from '../utils/dates';
 
import type {DatePickerProps} from './DatePicker';
import type {DatePickerState} from './hooks/useDatePickerState';
 
import './MobileCalendar.scss';
 
const b = block('mobile-calendar');
 
type InputDateType = 'date' | 'time' | 'datetime-local';
 
interface MobileCalendarProps {
    props: DatePickerProps;
    state: DatePickerState;
}
export function MobileCalendar({props, state}: MobileCalendarProps) {
    let type: InputDateType = 'date';
    if (state.formatInfo.hasTime && state.formatInfo.hasDate) {
        type = 'datetime-local';
    } else Eif (state.formatInfo.hasTime) {
        type = 'time';
    }
 
    const precision = state.formatInfo.minTimeUnit;
    let step;
    if (state.formatInfo.hasTime && precision === 'millisecond') {
        step = 0.001;
    } else Eif (state.formatInfo.hasTime && precision === 'second') {
        step = 1;
    }
 
    return (
        <input
            className={b()}
            disabled={props.disabled}
            type={type}
            step={step}
            value={formatNative(state.dateFieldState.value, type, precision)}
            id={props.id}
            min={formatNative(props.minValue?.timeZone(state.timeZone), type, precision)}
            max={formatNative(props.maxValue?.timeZone(state.timeZone), type, precision)}
            tabIndex={-1}
            onChange={(e) => {
                Iif (props.readOnly) {
                    return;
                }
                const newValue = e.target.value;
                if (newValue) {
                    const localDate = dateTime({
                        input: type === 'time' ? `1970-01-01T${newValue}` : newValue,
                        timeZone: 'system',
                    }).timeZone(state.timeZone, true);
                    let newDate = state.formatInfo.hasDate
                        ? localDate
                        : createPlaceholderValue({
                              placeholderValue: props.placeholderValue?.timeZone(state.timeZone),
                              timeZone: state.timeZone,
                          });
                    if (state.formatInfo.hasTime) {
                        newDate = mergeDateTime(newDate, localDate);
                    } else Eif (state.value) {
                        newDate = mergeDateTime(newDate, state.value.timeZone(state.timeZone));
                    } else {
                        newDate = mergeDateTime(
                            newDate,
                            createPlaceholderValue({
                                placeholderValue: props.placeholderValue?.timeZone(state.timeZone),
                                timeZone: state.timeZone,
                            }),
                        );
                    }
                    state.setValue(newDate);
                } else E{
                    state.setValue(null);
                }
            }}
        />
    );
}
 
function getDateFormat(type: InputDateType, precision: FormatInfo['minTimeUnit']) {
    let timeFormat = 'HH:mm';
    if (precision === 'millisecond') {
        timeFormat = 'HH:mm:ss.SSS';
    } else Eif (precision === 'second') {
        timeFormat = 'HH:mm:ss';
    }
    switch (type) {
        case 'time': {
            return timeFormat;
        }
        case 'datetime-local': {
            return `YYYY-MM-DD[T]${timeFormat}`;
        }
        default: {
            return 'YYYY-MM-DD';
        }
    }
}
function formatNative(
    date: DateTime | null | undefined,
    type: InputDateType,
    precision: FormatInfo['minTimeUnit'],
) {
    if (!date) {
        return '';
    }
 
    const format = getDateFormat(type, precision);
    return date.format(format);
}