All files / src/components/TimeSelection TimeSelection.tsx

84.61% Statements 11/13
83.33% Branches 10/12
60% Functions 3/5
83.33% Lines 10/12

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 831x                       1x   1x   1x                                 17x                         17x                 40x   17x                 40x 1x                              
import React from 'react';
 
import {Flex} from '@gravity-ui/uikit';
 
import {block} from '../../utils/cn';
 
import type {TimeSelectionProps} from './TimeSelection.types';
import {Wheel} from './Wheel';
import {useTimeRanges, useTimeSelection} from './hooks';
 
import './TimeSelection.scss';
 
const b = block('time-selection');
 
const DEFAULT_VIEWS: TimeSelectionProps['views'] = ['hours', 'minutes'];
 
export const TimeSelection = ({
    ampm = false,
    views = DEFAULT_VIEWS,
    defaultValue,
    value,
    readOnly = false,
    disabled = false,
    onUpdate,
    timeSteps,
    isTimeDisabled,
    minValue,
    maxValue,
    focusedView,
    onFocusViewUpdate,
    timeZone,
}: TimeSelectionProps) => {
    const {currentValue, activeWheel, order, handleChange, handleActivate, getCurrentValue} =
        useTimeSelection({
            defaultValue,
            value,
            readOnly,
            disabled,
            timeZone,
            ampm,
            views: views || DEFAULT_VIEWS,
            focusedView,
            onUpdate,
            onFocusViewUpdate,
        });
 
    const ranges = useTimeRanges({
        ampm,
        timeSteps,
        isTimeDisabled,
        minValue,
        maxValue,
        value: currentValue,
    });
 
    const isNotLastWheel = (index: number): boolean => index !== order.length - 1;
 
    return (
        <Flex
            className={b({disabled, 'read-only': readOnly})}
            role="group"
            aria-label="Time selection"
            alignItems="flex-start"
            gap={2}
        >
            {order.map((key, index) => (
                <React.Fragment key={key}>
                    <Wheel
                        values={ranges[key] || []}
                        value={getCurrentValue(key)}
                        setValue={(val) => handleChange(key, val)}
                        isActive={activeWheel === key}
                        onActivate={() => handleActivate(key)}
                        isInfinite={false}
                        disabled={disabled || readOnly}
                    />
                    {isNotLastWheel(index) && <div className={b('divider')} />}
                </React.Fragment>
            ))}
        </Flex>
    );
};