All files / src/components/CalendarView/hooks useCalendarProps.ts

70.45% Statements 31/44
61.53% Branches 32/52
36.36% Functions 4/11
70.45% Lines 31/44

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 16314x                       14x     41x   41x                 41x         41x               41x 41x 41x 41x   41x                                       41x 41x   41x 41x           41x   41x                                                 41x 41x   41x 41x           41x                                                 41x                 41x   41x 41x 41x 2x     39x           39x 39x    
import React from 'react';
 
import {useFocusWithin, useLang} from '@gravity-ui/uikit';
import type {ButtonButtonProps} from '@gravity-ui/uikit';
 
import type {CalendarProps} from '../../Calendar/Calendar';
import {formatDateTime} from '../../utils/dates';
import {filterDOMProps} from '../../utils/filterDOMProps';
import {i18n} from '../i18n';
 
import type {CalendarLayout, CalendarState, RangeCalendarState} from './types';
 
const buttonDisabledClassName = 'yc-button_disabled g-button_disabled';
 
export function useCalendarProps(props: CalendarProps, state: CalendarState | RangeCalendarState) {
    const {lang} = useLang();
    const title =
        state.mode === 'years' || state.mode === 'quarters'
            ? `${state.startDate.year()} — ${state.endDate.year()}`
            : formatDateTime(
                  state.focusedDate,
                  state.mode === 'days' ? 'MMMM YYYY' : 'YYYY',
                  state.timeZone,
                  lang,
              );
 
    const {focusWithinProps} = useFocusWithin({
        onFocusWithin: props.onFocus,
        onBlurWithin: props.onBlur,
    });
 
    const calendarProps: React.HTMLAttributes<HTMLElement> = {
        ...filterDOMProps(props, {labelable: true}),
        role: 'group',
        'aria-label': [props['aria-label'], title].filter(Boolean).join(', '),
        'aria-disabled': state.disabled || undefined,
        ...focusWithinProps,
    };
 
    const modeIndex = state.availableModes.indexOf(state.mode);
    const isModeLast = modeIndex + 1 === state.availableModes.length;
    const isNextModeLast = modeIndex + 2 === state.availableModes.length;
    const modeDisabled = state.disabled || isModeLast;
 
    const modeButtonProps: ButtonButtonProps = {
        // Always set a tabIndex so that Safari allows focusing native buttons
        tabIndex: 0,
        disabled: state.disabled,
        // FIXME: do not use button class name
        className: modeDisabled ? buttonDisabledClassName : undefined,
        onClick: modeDisabled
            ? undefined
            : () => {
                  state.zoomOut();
                  if (isNextModeLast) {
                      state.setFocused(true);
                  }
              },
        'aria-disabled': modeDisabled ? 'true' : undefined,
        'aria-description': useAriaDescriptionForModeButton(state.mode, state.availableModes),
        'aria-live': 'polite',
        children: title,
    };
 
    const previousFocused = React.useRef(false);
    const previousDisabled = state.disabled || state.isPreviousPageInvalid();
 
    React.useLayoutEffect(() => {
        Iif (previousDisabled && previousFocused.current) {
            previousFocused.current = false;
            state.setFocused(true);
        }
    });
 
    const {t} = i18n.useTranslation();
 
    const previousButtonProps: ButtonButtonProps = {
        // Always set a tabIndex so that Safari allows focusing native buttons
        tabIndex: 0,
        disabled: state.disabled,
        // FIXME: do not use button class name
        className: previousDisabled ? buttonDisabledClassName : undefined,
        onClick: previousDisabled
            ? undefined
            : () => {
                  state.focusPreviousPage();
              },
        onFocus: previousDisabled
            ? undefined
            : () => {
                  previousFocused.current = true;
              },
        onBlur: previousDisabled
            ? undefined
            : () => {
                  previousFocused.current = false;
              },
        'aria-label': t('Previous'),
        'aria-disabled': previousDisabled ? 'true' : undefined,
    };
 
    const nextFocused = React.useRef(false);
    const nextDisabled = state.disabled || state.isNextPageInvalid();
 
    React.useLayoutEffect(() => {
        Iif (nextDisabled && nextFocused.current) {
            nextFocused.current = false;
            state.setFocused(true);
        }
    });
 
    const nextButtonProps: ButtonButtonProps = {
        // Always set a tabIndex so that Safari allows focusing native buttons
        tabIndex: 0,
        disabled: state.disabled,
        // FIXME: do not use button class name
        className: nextDisabled ? buttonDisabledClassName : undefined,
        onClick: nextDisabled
            ? undefined
            : () => {
                  state.focusNextPage();
              },
        onFocus: nextDisabled
            ? undefined
            : () => {
                  nextFocused.current = true;
              },
        onBlur: nextDisabled
            ? undefined
            : () => {
                  nextFocused.current = false;
              },
        'aria-label': t('Next'),
        'aria-disabled': previousDisabled ? 'true' : undefined,
    };
 
    return {
        calendarProps,
        modeButtonProps,
        nextButtonProps,
        previousButtonProps,
    };
}
 
function useAriaDescriptionForModeButton(mode: CalendarLayout, availableModes: CalendarLayout[]) {
    const {t} = i18n.useTranslation();
 
    const nextModeIndex = availableModes.indexOf(mode) + 1;
    const isModeLast = nextModeIndex === availableModes.length;
    if (isModeLast) {
        return undefined;
    }
 
    const ariaLabelMap: Record<CalendarLayout, string> = {
        days: '',
        months: t('Switch to months view'),
        quarters: t('Switch to quarters view'),
        years: t('Switch to years view'),
    };
    const nextMode = availableModes[nextModeIndex];
    return ariaLabelMap[nextMode];
}