forked from flippingbitss/react-daterange-picker
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Header.tsx
130 lines (118 loc) · 2.9 KB
/
Header.tsx
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
/* eslint-disable radix */
import {
Grid,
makeStyles,
IconButton,
Select,
MenuItem,
} from '@material-ui/core';
import React from 'react';
import ChevronLeft from '@material-ui/icons/ChevronLeft';
import ChevronRight from '@material-ui/icons/ChevronRight';
import {
setMonth,
getMonth,
setYear,
getYear,
} from 'date-fns';
const useStyles = makeStyles(() => ({
iconContainer: {
padding: 5,
},
icon: {
padding: 10,
'&:hover': {
background: 'none',
},
},
}));
interface HeaderProps {
date: Date;
setDate: (date: Date) => void;
nextDisabled: boolean;
prevDisabled: boolean;
onClickNext: () => void;
onClickPrevious: () => void;
}
const MONTHS = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'June',
'July',
'Aug',
'Sept',
'Oct',
'Nov',
'Dec',
];
const generateYears = (relativeTo: Date, count: number) => {
const half = Math.floor(count / 2);
return Array(count)
.fill(0)
.map((_y, i) => relativeTo.getFullYear() - half + i); // TODO: make part of the state
};
const Header: React.FunctionComponent<HeaderProps> = ({
date,
setDate,
nextDisabled,
prevDisabled,
onClickNext,
onClickPrevious,
}: HeaderProps) => {
const classes = useStyles();
const handleMonthChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
setDate(setMonth(date, parseInt(event.target.value)));
};
const handleYearChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
setDate(setYear(date, parseInt(event.target.value)));
};
return (
<Grid container justifyContent="space-between" alignItems="center">
<Grid item className={classes.iconContainer}>
<IconButton
className={classes.icon}
disabled={prevDisabled}
onClick={onClickPrevious}
>
<ChevronLeft color={prevDisabled ? 'disabled' : 'action'} />
</IconButton>
</Grid>
<Grid item>
<Select
value={getMonth(date)}
onChange={handleMonthChange}
MenuProps={{ disablePortal: true }}
>
{MONTHS.map((month, idx) => (
<MenuItem key={month} value={idx}>
{month}
</MenuItem>
))}
</Select>
</Grid>
<Grid item>
<Select
value={getYear(date)}
onChange={handleYearChange}
MenuProps={{ disablePortal: true }}
>
{generateYears(date, 30).map((year) => (
<MenuItem key={year} value={year}>
{year}
</MenuItem>
))}
</Select>
{/* <Typography>{format(date, "MMMM YYYY")}</Typography> */}
</Grid>
<Grid item className={classes.iconContainer}>
<IconButton className={classes.icon} disabled={nextDisabled} onClick={onClickNext}>
<ChevronRight color={nextDisabled ? 'disabled' : 'action'} />
</IconButton>
</Grid>
</Grid>
);
};
export default Header;