generated from hackforla/.github-hackforla-base-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathDateRangePicker.jsx
132 lines (124 loc) · 3.75 KB
/
DateRangePicker.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
import React, { useState } from "react";
import PropTypes from "prop-types";
import DatePicker from "react-datepicker";
import { createUseStyles } from "react-jss";
// TODO: we want the calendar popup to appear as if it were in the normal CSS flow.
// We should be able to do this with a React portal, but I (John Darragh) was not
// able to get this to work (see https://reactdatepicker.com/#example-date-range-with-portalInstead
// for the example that should work). Instead, we use a total hack by putting a spacer div below
// the datepickers, and conditionally showi it if the calendar is open. Would be
// good to replace this with something more solid, cause it somethimes doesn't work well
// e.g. if the popup doesn't have enough space in the broswser window below the datepicker,
// the popup appears above the date picker and is then not positioned over the
// spacer div.
const useStyles = createUseStyles({
container: {
width: "6rem",
padding: "0.1rem",
border: "1px solid black"
}
});
const DateRangePicker = ({
startDate,
endDate,
setStartDate,
setEndDate,
startDatePlaceholder,
endDatePlaceholder
}) => {
const [calendarDivHeight, setCalendarDivHeight] = useState(false);
const classes = useStyles();
const handleCalendarOpen = () => {
setCalendarDivHeight(true);
};
const handleCalendarClose = () => {
setCalendarDivHeight(false);
};
return (
<>
<div
style={{
display: "flex",
flexDirection: "row",
justifyContent: "space-between",
minWidth: "15.5rem"
}}
onClick={e => {
// We don't want a click event to bubble up to the parent container
e.stopPropagation();
}}
>
<DatePicker
className={classes.container}
selected={startDate}
selectsStart
onChange={date => setStartDate(date)}
startDate={startDate}
endDate={endDate}
dateFormat="yyyy-MM-dd"
placeholderText={startDatePlaceholder || ""}
popperPlacement="bottom-start"
popperModifiers={[
{
name: "flip",
enabled: false
},
{
name: "hide",
enabled: false
}
]}
onCalendarOpen={handleCalendarOpen}
onCalendarClose={handleCalendarClose}
onClick={e => {
// We don't want a click event to bubble up to the parent container
e.stopPropagation();
}}
// withPortal
/>
<div
style={{ margin: "auto 0", minWidth: "2.5rem", textAlign: "center" }}
>
to
</div>
<DatePicker
className={classes.container}
selected={endDate}
selectsEnd
onChange={date => setEndDate(date)}
startDate={startDate}
endDate={endDate}
dateFormat="yyyy-MM-dd"
placeholderText={endDatePlaceholder || ""}
popperPlacement="bottom-end"
popperModifiers={[
{
name: "flip",
enabled: false
},
{
name: "hide",
enabled: false
}
]}
onCalendarOpen={handleCalendarOpen}
onCalendarClose={handleCalendarClose}
// withPortal
/>
</div>
{calendarDivHeight ? (
<div style={{ height: "15rem" }}>{calendarDivHeight}</div>
) : null}
</>
);
};
DateRangePicker.propTypes = {
portalId: PropTypes.string,
startDate: PropTypes.any,
endDate: PropTypes.any,
setStartDate: PropTypes.func,
setEndDate: PropTypes.func,
startDatePlaceholder: PropTypes.string,
endDatePlaceholder: PropTypes.string
};
export default DateRangePicker;