-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathForecastModeToggle.jsx
More file actions
51 lines (44 loc) · 1.42 KB
/
ForecastModeToggle.jsx
File metadata and controls
51 lines (44 loc) · 1.42 KB
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
import { useRef } from 'react';
import { FORECAST_MODE_LABELS } from './constants';
import styles from './UtilizationChart.module.css';
const MODES = Object.entries(FORECAST_MODE_LABELS);
function ForecastModeToggle({ value, onChange }) {
const buttonRefs = useRef([]);
const handleKeyDown = (e, index) => {
const count = MODES.length;
let nextIndex = -1;
if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
e.preventDefault();
nextIndex = (index + 1) % count;
} else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
e.preventDefault();
nextIndex = (index - 1 + count) % count;
}
if (nextIndex !== -1) {
onChange(MODES[nextIndex][0]);
buttonRefs.current[nextIndex]?.focus();
}
};
return (
<div className={styles.forecastToggle} role="radiogroup" aria-label="Forecast mode">
{MODES.map(([mode, label], index) => (
<button
key={mode}
ref={el => {
buttonRefs.current[index] = el;
}}
type="button"
role="radio"
aria-checked={value === mode}
tabIndex={value === mode ? 0 : -1}
className={`${styles.toggleButton} ${value === mode ? styles.toggleButtonActive : ''}`}
onClick={() => onChange(mode)}
onKeyDown={e => handleKeyDown(e, index)}
>
{label}
</button>
))}
</div>
);
}
export default ForecastModeToggle;