-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathdate-picker.component.js
132 lines (123 loc) · 3.73 KB
/
date-picker.component.js
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, { useMemo } from "react";
import PropTypes from "prop-types";
import DayPicker from "react-day-picker";
import { getDisabledDays } from "../utils";
import Popover from "../../../../__internal__/popover";
import useLocale from "../../../../hooks/__internal__/useLocale";
import Navbar from "../navbar";
import Weekday from "../weekday";
import StyledDayPicker from "./day-picker.style";
const DatePicker = React.forwardRef(
(
{
inputElement,
minDate,
maxDate,
selectedDays,
disablePortal,
onDayClick,
pickerMouseDown,
pickerProps,
},
ref
) => {
const l = useLocale();
const { localize, options } = l.date.dateFnsLocale();
const { weekStartsOn } = options;
const monthsLong = Array.from({ length: 12 }).map((_, i) =>
localize.month(i)
);
const monthsShort = Array.from({ length: 12 }).map((_, i) =>
localize.month(i, { width: "abbreviated" }).substring(0, 3)
);
const weekdaysLong = Array.from({ length: 7 }).map((_, i) =>
localize.day(i)
);
const weekdaysShort = Array.from({ length: 7 }).map((_, i) =>
localize
.day(i, l.locale() === "de" ? {} : { width: "abbreviated" })
.substring(0, 3)
);
const popoverModifiers = useMemo(
() => [
{
name: "offset",
options: {
offset: [0, 3],
},
},
{
name: "preventOverflow",
options: {
mainAxis: false,
},
},
],
[]
);
const handleDayClick = (date, { disabled }, ev) => {
if (!disabled) {
onDayClick(date, ev);
}
};
const formatDay = (date) =>
`${weekdaysShort[date.getDay()]} ${date.getDate()} ${
monthsShort[date.getMonth()]
} ${date.getFullYear()}`;
return (
<Popover
placement="bottom-start"
reference={inputElement}
modifiers={popoverModifiers}
disablePortal={disablePortal}
>
<StyledDayPicker ref={ref} onMouseDown={pickerMouseDown}>
<DayPicker
month={selectedDays}
months={monthsLong}
firstDayOfWeek={weekStartsOn}
onDayClick={handleDayClick}
selectedDays={selectedDays}
date={selectedDays}
weekdayElement={(weekdayElementProps) => {
const { className, weekday } = weekdayElementProps;
return (
<Weekday className={className} title={weekdaysLong[weekday]}>
{weekdaysShort[weekday]}
</Weekday>
);
}}
navbarElement={<Navbar />}
enableOutsideDays
fixedWeeks
initialMonth={selectedDays || undefined}
disabledDays={getDisabledDays(minDate, maxDate)}
inline
locale={l.locale()}
localeUtils={{ formatDay }}
{...pickerProps}
/>
</StyledDayPicker>
</Popover>
);
}
);
DatePicker.propTypes = {
/** Minimum possible date */
minDate: PropTypes.string,
/** Maximum possible date */
maxDate: PropTypes.string,
/** Boolean to toggle where DatePicker is rendered in relation to the Date Input */
disablePortal: PropTypes.bool,
/** Element that the DatePicker will be displayed under */
inputElement: PropTypes.object.isRequired,
/** Currently selected date */
selectedDays: PropTypes.instanceOf(Date),
/** Callback to set selected date */
onDayClick: PropTypes.func,
/** Pass any props that match the DayPickerProps interface to override default behaviors */
pickerProps: PropTypes.object,
/** Callback to handle mousedown event on picker */
pickerMouseDown: PropTypes.func,
};
export default DatePicker;