All files / src/components/TimeSelection/Wheel Wheel.tsx

54.16% Statements 39/72
42.85% Branches 18/42
66.66% Functions 8/12
54.41% Lines 37/68

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 1711x                 1x   1x                 40x 40x 40x 38x 810x         40x 879x 40x       40x 35x 35x   35x 35x 35x   35x   35x             40x 38x 33x 33x   33x 33x 33x   33x   33x             1356x   40x               40x                             40x                                                                         40x                         1356x 1356x   1356x                         1x                  
import React from 'react';
 
import {Flex, Text} from '@gravity-ui/uikit';
 
import {block} from '../../../utils/cn';
import type {WheelProps, WheelValue} from '../TimeSelection.types';
 
import './Wheel.scss';
 
const b = block('time-selection-wheel');
 
export const Wheel = ({
    values,
    value,
    setValue,
    isActive,
    onActivate,
    onChange,
    disabled = false,
}: WheelProps) => {
    const containerRef = React.useRef<HTMLDivElement>(null);
    const selectedRef = React.useRef<HTMLDivElement>(null);
    const [currentIndex, setCurrentIndex] = React.useState(() =>
        Math.max(
            values.findIndex((v) => v.value === value),
            0,
        ),
    );
 
    React.useEffect(() => {
        const newIndex = values.findIndex((v) => v.value === value);
        Iif (newIndex !== -1 && newIndex !== currentIndex) {
            setCurrentIndex(newIndex);
        }
 
        if (selectedRef.current && containerRef.current) {
            const container = containerRef.current;
            const selected = selectedRef.current;
 
            const containerHeight = container.clientHeight;
            const selectedHeight = selected.clientHeight;
            const selectedTop = selected.offsetTop;
 
            const scrollTop = selectedTop - containerHeight / 2 + selectedHeight / 2;
 
            container.scrollTo({
                top: scrollTop,
                behavior: 'smooth',
            });
        }
    }, [value, values, currentIndex]);
 
    React.useEffect(() => {
        if (selectedRef.current && containerRef.current) {
            const container = containerRef.current;
            const selected = selectedRef.current;
 
            const containerHeight = container.clientHeight;
            const selectedHeight = selected.clientHeight;
            const selectedTop = selected.offsetTop;
 
            const scrollTop = selectedTop - containerHeight / 2 + selectedHeight / 2;
 
            container.scrollTo({
                top: scrollTop,
                behavior: 'instant' as ScrollBehavior,
            });
        }
    }, []);
 
    const isItemDisabled = (val: WheelValue): boolean => !!val.disabled || disabled;
 
    const handleClick = (val: WheelValue, idx: number) => {
        if (isItemDisabled(val)) return;
        setCurrentIndex(idx);
        setValue(val.value);
        onChange?.(val.value);
        onActivate?.();
    };
 
    const findNextEnabledIndex = (startIndex: number, direction: 1 | -1): number => {
        let newIndex = startIndex;
        let attempts = 0;
 
        while (isItemDisabled(values[newIndex]) && attempts < values.length) {
            newIndex =
                direction === 1
                    ? (newIndex + 1) % values.length
                    : (newIndex - 1 + values.length) % values.length;
            attempts++;
        }
 
        return isItemDisabled(values[newIndex]) ? startIndex : newIndex;
    };
 
    const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
        if (disabled || !isActive) return;
 
        let newIndex = currentIndex;
 
        switch (e.key) {
            case 'ArrowUp':
                e.preventDefault();
                newIndex = findNextEnabledIndex(
                    currentIndex > 0 ? currentIndex - 1 : values.length - 1,
                    -1,
                );
                break;
            case 'ArrowDown':
                e.preventDefault();
                newIndex = findNextEnabledIndex(
                    currentIndex < values.length - 1 ? currentIndex + 1 : 0,
                    1,
                );
                break;
            case 'Home':
                e.preventDefault();
                newIndex = findNextEnabledIndex(0, 1);
                break;
            case 'End':
                e.preventDefault();
                newIndex = findNextEnabledIndex(values.length - 1, -1);
                break;
            default:
                return;
        }
 
        if (newIndex !== currentIndex) {
            handleClick(values[newIndex], newIndex);
        }
    };
 
    return (
        <div
            className={b({active: isActive, disabled})}
            role="listbox"
            tabIndex={disabled ? -1 : 0}
            aria-activedescendant={value}
            aria-label="time-section"
            ref={containerRef}
            onClick={onActivate}
            onKeyDown={handleKeyDown}
        >
            <Flex direction="column">
                {values.map((val, i) => {
                    const selected = val.value === value;
                    const itemDisabled = isItemDisabled(val);
 
                    return (
                        <Flex
                            justifyContent="center"
                            alignItems="center"
                            id={val.value}
                            role="option"
                            aria-selected={selected}
                            aria-disabled={itemDisabled}
                            className={b('cell', {selected, disabled: itemDisabled})}
                            key={val.value}
                            ref={selected ? selectedRef : null}
                            onClick={() => handleClick(val, i)}
                        >
                            <Text variant="body-1">{val.label}</Text>
                        </Flex>
                    );
                })}
            </Flex>
            <div className={b('highlight')} />
        </div>
    );
};