-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonthPicker.tsx
More file actions
71 lines (68 loc) · 2.16 KB
/
MonthPicker.tsx
File metadata and controls
71 lines (68 loc) · 2.16 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React, { FC, useState } from 'react';
import IconButton from '../IconButton/IconButton';
import { parseToMonthInt, parseMonthIntToObj } from '../../utils/helpers';
import { DEFAULT_MONTH_INT, DEFAULT_YEAR_RANGE, MONTHS } from '../../constants/common';
import ArrowLeftIcon from '../Icons/ArrowLeftIcon';
import ArrowRightIcon from '../Icons/ArrowRightIcon';
import styles from './sass/MonthPicker.module.scss';
import { IMonthPickerProps } from './interfaces/IMonthPicker';
const MonthPicker: FC<IMonthPickerProps> = ({
onChange,
monthInt = DEFAULT_MONTH_INT,
yearRange = DEFAULT_YEAR_RANGE,
className = '',
}) => {
const { minYear, maxYear } = yearRange;
const { month, year } = parseMonthIntToObj(monthInt);
const [currentYear, setYear] = useState(year);
return (
<div className={`${styles.container} ${className}`}>
<div className={styles.year}>
<IconButton
tabIndex={-1}
onClick={() => setYear(currentYear - 1)}
aria-label="prevYear"
aria-disabled={currentYear <= minYear}
icon={<ArrowLeftIcon />}
/>
<div
className={styles.selectedYear}
>
<span>
{currentYear}
</span>
</div>
<IconButton
tabIndex={-1}
onClick={() => setYear(currentYear + 1)}
aria-label="nextYear"
aria-disabled={currentYear >= maxYear}
icon={<ArrowRightIcon />}
/>
</div>
<div className={styles.months}>
{MONTHS.map((item, index) => (
<div
aria-label={item}
key={item}
role="gridcell"
tabIndex={-1}
className={`${styles.month} ${month === index && currentYear === year ? styles.selected : ''}`}
onClick={() => {
onChange({
month: parseToMonthInt(currentYear, index + 1),
monthIndex: index,
year: currentYear,
monthName: item,
});
}}
onKeyDown={() => {}}
>
<span>{item}</span>
</div>
))}
</div>
</div>
);
};
export default MonthPicker;