-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathDatePicker.tsx
More file actions
166 lines (159 loc) · 4.69 KB
/
DatePicker.tsx
File metadata and controls
166 lines (159 loc) · 4.69 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
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
164
165
166
import classNames from "classnames";
import dayjs from "dayjs";
import React, { useEffect, useRef } from "react";
import ReactDatePicker, { ReactDatePickerProps } from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import { isDark } from "@core/util/color.utils";
import { theme } from "@web/common/styles/theme";
import { Flex } from "@web/components/Flex";
import { AlignItems, JustifyContent } from "@web/components/Flex/styled";
import { Text } from "@web/components/Text";
import { StyledInput } from "../Input/styled";
import {
ChangeDayButtonsStyledFlex,
MonthContainerStyled,
StyledDatePicker,
StyledHeaderFlex,
TodayStyledText,
} from "./styled";
export interface Props extends ReactDatePickerProps {
animationOnToggle?: boolean;
bgColor?: string;
displayDate?: Date;
inputColor?: string;
isOpen?: boolean;
onInputBlur?: (e: React.FocusEvent<HTMLInputElement>) => void;
view: "sidebar" | "grid";
withTodayButton?: boolean;
}
export interface CalendarRef extends HTMLDivElement {
input: HTMLInputElement;
}
export const DatePicker: React.FC<Props> = ({
animationOnToggle = true,
autoFocus: _autoFocus = false,
bgColor = theme.color.bg.primary,
calendarClassName,
inputColor,
isOpen = true,
onSelect = () => null,
onInputBlur,
onCalendarClose = () => null,
onCalendarOpen = () => null,
view,
withTodayButton = true,
...props
}) => {
const datepickerRef = useRef<CalendarRef>(null);
const headerColor =
view === "sidebar"
? theme.color.text.light
: isDark(bgColor || "")
? theme.color.text.lighter
: theme.color.text.dark;
useEffect(() => {
if (_autoFocus) {
setTimeout(() => {
datepickerRef.current?.input.click();
datepickerRef.current?.input.focus();
});
}
}, [_autoFocus]);
return (
<ReactDatePicker
calendarClassName={classNames("calendar", calendarClassName, {
"calendar--open": isOpen,
"calendar--animation": animationOnToggle,
})}
calendarContainer={(containerProps) => (
<StyledDatePicker
{...containerProps}
bgColor={bgColor}
selectedColor={theme.color.text.accent}
role="combobox"
aria-label="datepicker"
view={view}
/>
)}
customInput={<StyledInput bgColor={inputColor} />}
dateFormat={"M-d-yyyy"}
formatWeekDay={(day) => day[0]}
open={isOpen}
onCalendarOpen={() => {
onCalendarOpen();
}}
onCalendarClose={() => {
onCalendarClose();
}}
onClickOutside={() => {
onCalendarClose();
}}
onSelect={(date, event: React.SyntheticEvent<Event> | undefined) => {
onSelect(date, event);
}}
portalId="root"
ref={datepickerRef as any}
showPopperArrow={false}
renderCustomHeader={({
monthDate,
increaseMonth,
decreaseMonth,
changeMonth,
changeYear,
customHeaderCount,
}) => {
const selectedMonth = dayjs(monthDate).format("MMM YYYY");
const currentMonth = dayjs().format("MMM YYYY");
return (
<StyledHeaderFlex
alignItems={AlignItems.CENTER}
justifyContent={JustifyContent.LEFT}
>
<MonthContainerStyled>
<Text color={headerColor} size="xl">
{selectedMonth}
</Text>
</MonthContainerStyled>
{!customHeaderCount && (
<Flex alignItems={AlignItems.CENTER}>
<ChangeDayButtonsStyledFlex>
<Text
cursor="pointer"
onClick={decreaseMonth}
color={headerColor}
size="l"
>
{"<"}
</Text>
<Text
cursor="pointer"
onClick={increaseMonth}
color={headerColor}
size="l"
>
{">"}
</Text>
</ChangeDayButtonsStyledFlex>
{withTodayButton && (
<TodayStyledText
isCurrentDate={currentMonth === selectedMonth}
cursor="pointer"
onClick={() => {
changeMonth(dayjs().month());
changeYear(dayjs().year());
}}
color={theme.color.text.light}
size="l"
>
Today
</TodayStyledText>
)}
</Flex>
)}
</StyledHeaderFlex>
);
}}
{...props}
/>
);
};