-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathDatePicker.jsx
163 lines (147 loc) · 3.96 KB
/
DatePicker.jsx
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
import React, {
useRef, useState, useEffect, useCallback,
} from 'react';
import ReactDayPicker from '@components/common/ReactDayPicker';
import PropTypes from 'prop-types';
import CalendarIcon from '@material-ui/icons/CalendarToday';
import IconButton from '@material-ui/core/IconButton';
import useOutsideClick from '@components/common/customHooks/useOutsideClick';
import { makeStyles } from '@material-ui/core';
// TODO: Apply gaps (margin, padding) from theme
const useStyles = makeStyles(theme => ({
selector: {
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
width: '100%',
maxWidth: 268,
backgroundColor: theme.palette.primary.dark,
padding: 10,
borderRadius: 5,
fontSize: 14,
color: theme.palette.text.secondaryLight,
},
placeholder: {
color: theme.palette.text.secondaryDark,
},
selectorPopUp: {
position: 'fixed',
zIndex: 1,
},
button: {
padding: 0,
color: theme.palette.text.dark,
'&:hover': {
backgroundColor: theme.palette.primary.dark,
},
'& svg': {
fontSize: 20,
fill: theme.palette.text.secondaryLight,
},
},
}));
const renderSelectedDays = (dates, classes, range) => {
const [from, to] = dates;
const isFromSelected = Boolean(from);
const isBothSelected = Boolean(from && to);
const selectedDaysElements = [];
if (isBothSelected) {
selectedDaysElements.push(
<span key="from">{from.toLocaleDateString('en-US')}</span>,
<span key="delimiter"> - </span>,
<span key="to">{to.toLocaleDateString('en-US')}</span>,
);
return selectedDaysElements;
}
if (isFromSelected) {
selectedDaysElements.push(
<span key="from">
{' '}
{from.toLocaleDateString('en-US')}
{' '}
</span>,
);
return selectedDaysElements;
}
selectedDaysElements.push(
<span className={classes.placeholder} key="N/A">
Select a date
{' '}
{range ? ' range' : ''}
</span>,
);
return selectedDaysElements;
};
const DatePicker = ({
dates, onSelect, open, onToggle, range,
}) => {
const [selectedDays, setSelectedDays] = useState(() => dates);
const [showCalendar, setShowCalendar] = useState(() => open);
const classes = useStyles();
const ref = useRef(null);
const closeCalendar = useCallback(() => setShowCalendar(false), []);
useOutsideClick(ref, closeCalendar);
useEffect(() => {
setShowCalendar(false);
}, [open]);
useEffect(() => {
setSelectedDays(() => dates);
}, [dates]);
const getCoordinates = () => {
if (ref.current) {
const { left, top, height } = ref.current.getClientRects()[0];
const offsetFromSelectorDisplay = 2;
return {
left,
top: top + height + offsetFromSelectorDisplay,
};
}
return {};
};
const toggleCalendar = () => {
setShowCalendar(prevState => !prevState);
if (onToggle) onToggle();
};
const handleDateChage = incomingDates => {
setSelectedDays(() => incomingDates);
if (onSelect) onSelect(incomingDates);
};
return (
<div ref={ref} className={classes.selector}>
<div>{renderSelectedDays(selectedDays, classes, range)}</div>
<IconButton
className={classes.button}
aria-label="toggle calendar datepicker"
onClick={toggleCalendar}
disableFocusRipple
disableRipple
>
<CalendarIcon />
</IconButton>
<div style={getCoordinates()} className={classes.selectorPopUp}>
{showCalendar ? (
<ReactDayPicker
range={range}
initialDates={selectedDays}
onChange={handleDateChage}
/>
) : null}
</div>
</div>
);
};
DatePicker.propTypes = {
onSelect: PropTypes.func,
range: PropTypes.bool,
open: PropTypes.bool,
onToggle: PropTypes.func,
dates: PropTypes.arrayOf(Date),
};
DatePicker.defaultProps = {
open: false,
range: false,
onToggle: null,
onSelect: null,
dates: [],
};
export default DatePicker;