All files / src/components/RangeDateSelection/components/SelectionControl SelectionControl.tsx

43.33% Statements 26/60
20% Branches 7/35
7.14% Functions 1/14
43.33% Lines 26/60

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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 2251x                       1x   1x 1x 1x 1x             5x 5x   5x   5x         5x   5x         5x                                                         5x                                   5x                                     5x 5x         5x           5x 5x 5x   5x 5x   5x   5x                           1x                                                                                                                                                                    
import React from 'react';
 
import {useMove} from '../../../../hooks/useMove';
import {block} from '../../../../utils/cn';
import type {AccessibilityProps, DomProps, StyleProps} from '../../../types';
import type {RangeDateSelectionState} from '../../hooks/useRangeDateSelectionState';
import {i18n} from '../../i18n';
import {lerp} from '../../utils/span';
import {useViewportDimensions, useViewportInterval} from '../Ruler/Ruler';
 
import './SelectionControl.scss';
 
const b = block('range-date-selection-control');
 
const MIN_SELECTION_WIDTH = 12; // px;
const MIN_DRAG_SCALE = 0.01;
const MAX_DRAG_SCALE = 100;
const DRAG_SCALE_STRENGTH = 20;
 
interface SelectionControlProps extends DomProps, StyleProps, AccessibilityProps {
    state: RangeDateSelectionState;
}
 
export function SelectionControl({state, style, className, ...props}: SelectionControlProps) {
    const {start: startDate, end: endDate} = useViewportInterval();
    const viewport = useViewportDimensions();
 
    const [isDragging, setIsDragging] = React.useState<'dragger' | 'start' | 'end' | null>(null);
 
    const moveDataRef = React.useRef<{isZooming: boolean}>({
        isZooming: false,
    });
 
    const visualMinDuration =
        (MIN_SELECTION_WIDTH * (endDate.valueOf() - startDate.valueOf())) / viewport.width;
 
    const handleMoveEnd = () => {
        state.endDragging();
        setIsDragging(null);
    };
 
    const {moveProps} = useMove({
        onMoveStart: (e) => {
            setIsDragging('dragger');
            state.startDragging();
            moveDataRef.current = {
                isZooming: state.canResize && (e.ctrlKey || e.metaKey),
            };
        },
        onMoveEnd: handleMoveEnd,
        onMove: (e) => {
            if (e.deltaX === 0) {
                return;
            }
            if (moveDataRef.current.isZooming) {
                let scale = Math.exp((e.deltaX * DRAG_SCALE_STRENGTH) / viewport.width);
                scale = Math.min(Math.max(scale, MIN_DRAG_SCALE), MAX_DRAG_SCALE);
                state.scale(scale, {visualMinDuration});
            } else {
                let delta = (e.deltaX * (endDate.valueOf() - startDate.valueOf())) / viewport.width;
                if (e.pointerType === 'keyboard') {
                    delta =
                        delta > 0
                            ? Math.max(delta, state.align)
                            : Math.min(delta, -1 * state.align);
                }
                state.move(delta, {visualMinDuration});
            }
        },
    });
    const {moveProps: moveStartProps} = useMove({
        onMoveStart: () => {
            setIsDragging('start');
            state.startDragging();
        },
        onMoveEnd: handleMoveEnd,
        onMove: (e) => {
            if (e.deltaX === 0) {
                return;
            }
            let delta = (e.deltaX * (endDate.valueOf() - startDate.valueOf())) / viewport.width;
            if (e.pointerType === 'keyboard') {
                delta =
                    delta > 0 ? Math.max(delta, state.align) : Math.min(delta, -1 * state.align);
            }
            state.moveStart(delta, {visualMinDuration});
        },
    });
    const {moveProps: moveEndProps} = useMove({
        onMoveStart: () => {
            setIsDragging('end');
            state.startDragging();
        },
        onMoveEnd: handleMoveEnd,
        onMove: (e) => {
            if (e.deltaX === 0) {
                return;
            }
            let delta = (e.deltaX * (endDate.valueOf() - startDate.valueOf())) / viewport.width;
            if (e.pointerType === 'keyboard') {
                delta =
                    delta > 0 ? Math.max(delta, state.align) : Math.min(delta, -1 * state.align);
            }
            state.moveEnd(delta, {visualMinDuration});
        },
    });
 
    const {start: startValue, end: endValue} = state.value;
    const startX = lerp(
        {start: startDate.valueOf(), end: endDate.valueOf()},
        {start: 0, end: viewport.width},
        startValue.valueOf(),
    );
    const endX = lerp(
        {start: startDate.valueOf(), end: endDate.valueOf()},
        {start: 0, end: viewport.width},
        endValue.valueOf(),
    );
 
    const draggerInputRef = React.useRef<HTMLInputElement>(null);
    const startInputRef = React.useRef<HTMLInputElement>(null);
    const endInputRef = React.useRef<HTMLInputElement>(null);
 
    let id = React.useId();
    id = props.id ?? id;
 
    const {t} = i18n.useTranslation();
 
    return (
        <div className={b(null, className)} style={{...style, left: startX, width: endX - startX}}>
            {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
            <div
                className={b('dragger', {
                    dragging: isDragging === 'dragger',
                    disabled: Boolean(isDragging && isDragging !== 'dragger'),
                })}
                {...moveProps}
                onClick={() => {
                    draggerInputRef.current?.focus();
                }}
            >
                <div className={b('slider-input-wrapper')}>
                    <input
                        id={`${id}-range`}
                        ref={draggerInputRef}
                        className={b('slider-input')}
                        type="range"
                        step={1}
                        aria-label={t('Range')}
                        aria-labelledby={[`${id}-range`, props['aria-labelledby']]
                            .filter(Boolean)
                            .join(' ')}
                        aria-orientation="horizontal"
                        aria-valuetext={`${startValue.format()} - ${endValue.format()}`}
                        value={0}
                        onChange={() => {}}
                    />
                </div>
            </div>
            {state.canResize ? (
                /* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */
                <div
                    className={b('start', {
                        dragging: isDragging === 'start',
                        disabled: Boolean(isDragging && isDragging !== 'start'),
                    })}
                    {...moveStartProps}
                    onClick={() => {
                        startInputRef.current?.focus();
                    }}
                >
                    <div className={b('slider-input-wrapper')}>
                        <input
                            id={`${id}-start`}
                            ref={startInputRef}
                            className={b('slider-input')}
                            type="range"
                            step={1}
                            aria-label={t('Start of range')}
                            aria-labelledby={[`${id}-start`, props['aria-labelledby']]
                                .filter(Boolean)
                                .join(' ')}
                            aria-orientation="horizontal"
                            aria-valuetext={`${startValue.format()}`}
                            value={0}
                            onChange={() => {}}
                        />
                    </div>
                </div>
            ) : null}
            {state.canResize ? (
                /* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */
                <div
                    className={b('end', {
                        dragging: isDragging === 'end',
                        disabled: Boolean(isDragging && isDragging !== 'end'),
                    })}
                    {...moveEndProps}
                    onClick={() => {
                        endInputRef.current?.focus();
                    }}
                >
                    <div className={b('slider-input-wrapper')}>
                        <input
                            id={`${id}-end`}
                            ref={endInputRef}
                            className={b('slider-input')}
                            type="range"
                            step={1}
                            aria-label={t('End of range')}
                            aria-labelledby={[`${id}-end`, props['aria-labelledby']]
                                .filter(Boolean)
                                .join(' ')}
                            aria-orientation="horizontal"
                            aria-valuetext={`${endValue.format()}`}
                            value={0}
                            onChange={() => {}}
                        />
                    </div>
                </div>
            ) : null}
        </div>
    );
}