/
DateInputV2.tsx
351 lines (324 loc) · 11.2 KB
/
DateInputV2.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import { useState, useEffect } from "react";
import {
format,
subMonths,
addMonths,
subYears,
addYears,
isEqual,
getDaysInMonth,
getDay,
} from "date-fns";
import { DropdownTransition } from "./components/HelperComponents";
import { Popover } from "@headlessui/react";
import { classNames } from "../../Utils/utils";
import CareIcon from "../../CAREUI/icons/CareIcon";
type DatePickerType = "date" | "month" | "year";
export type DatePickerPosition = "LEFT" | "RIGHT" | "CENTER";
interface Props {
className?: string;
value: Date | undefined;
min?: Date;
max?: Date;
onChange: (date: Date) => void;
position?: DatePickerPosition;
disabled?: boolean;
placeholder?: string;
}
const DAYS = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
const DateInputV2: React.FC<Props> = ({
className,
value,
min,
max,
onChange,
position,
disabled,
placeholder,
}) => {
const [dayCount, setDayCount] = useState<Array<number>>([]);
const [blankDays, setBlankDays] = useState<Array<number>>([]);
const [datePickerHeaderDate, setDatePickerHeaderDate] = useState(new Date());
const [type, setType] = useState<DatePickerType>("date");
const [year, setYear] = useState(new Date());
const decrement = () => {
switch (type) {
case "date":
setDatePickerHeaderDate((prev) => subMonths(prev, 1));
break;
case "month":
setDatePickerHeaderDate((prev) => subYears(prev, 1));
break;
case "year":
setDatePickerHeaderDate((prev) => subYears(prev, 1));
setYear((prev) => subYears(prev, 10));
break;
}
};
const increment = () => {
switch (type) {
case "date":
setDatePickerHeaderDate((prev) => addMonths(prev, 1));
break;
case "month":
setDatePickerHeaderDate((prev) => addYears(prev, 1));
break;
case "year":
setDatePickerHeaderDate((prev) => addYears(prev, 1));
setYear((prev) => addYears(prev, 10));
break;
}
};
const isSelectedDate = (date: number) => {
if (value)
return isEqual(
new Date(value.getFullYear(), value.getMonth(), date),
value
);
};
const setDateValue = (date: number) => () => {
isDateWithinConstraints(date) &&
onChange(
new Date(
datePickerHeaderDate.getFullYear(),
datePickerHeaderDate.getMonth(),
date
)
);
};
const getDayCount = (date: Date) => {
const daysInMonth = getDaysInMonth(date);
const dayOfWeek = getDay(new Date(date.getFullYear(), date.getMonth(), 1));
const blankDaysArray = [];
for (let i = 1; i <= dayOfWeek; i++) {
blankDaysArray.push(i);
}
const daysArray = [];
for (let i = 1; i <= daysInMonth; i++) {
daysArray.push(i);
}
setBlankDays(blankDaysArray);
setDayCount(daysArray);
};
const getLastDay = (
year = datePickerHeaderDate.getFullYear(),
month = datePickerHeaderDate.getMonth()
) => {
return new Date(year, month + 1, 0).getDate();
};
const isDateWithinConstraints = (
day = datePickerHeaderDate.getDate(),
month = datePickerHeaderDate.getMonth(),
year = datePickerHeaderDate.getFullYear()
) => {
const date = new Date(year, month, day);
if (min) if (date < min) return false;
if (max) if (date > max) return false;
return true;
};
const isSelectedMonth = (month: number) =>
month === datePickerHeaderDate.getMonth();
const isSelectedYear = (year: number) =>
year === datePickerHeaderDate.getFullYear();
const setMonthValue = (month: number) => () => {
setDatePickerHeaderDate(
new Date(
datePickerHeaderDate.getFullYear(),
month,
datePickerHeaderDate.getDate()
)
);
setType("date");
};
const setYearValue = (year: number) => () => {
setDatePickerHeaderDate(
new Date(
year,
datePickerHeaderDate.getMonth(),
datePickerHeaderDate.getDate()
)
);
setType("date");
};
const showMonthPicker = () => setType("month");
const showYearPicker = () => setType("year");
useEffect(() => {
getDayCount(datePickerHeaderDate);
}, [datePickerHeaderDate]);
const getPosition = () => {
switch (position) {
case "LEFT":
return "left-0";
case "RIGHT":
return "right-0 transform translate-x-1/2";
case "CENTER":
return "transform -translate-x-1/2";
default:
return "left-0";
}
};
return (
<div>
<div className="container mx-auto text-black">
<Popover className="relative">
<Popover.Button disabled={disabled} className="w-full">
<input type="hidden" name="date" />
<input
type="text"
readOnly
disabled={disabled}
className={`cui-input-base cursor-pointer disabled:cursor-not-allowed ${className}`}
placeholder={placeholder || "Select date"}
value={value && format(value, "yyyy-MM-dd")}
/>
<div className="absolute top-1/2 right-0 p-2 -translate-y-1/2">
<CareIcon className="care-l-calendar-alt text-lg text-gray-600" />
</div>
</Popover.Button>
<DropdownTransition>
<Popover.Panel
className={classNames(
"cui-dropdown-base divide-y-0 w-72 p-4 absolute mt-0.5",
getPosition()
)}
>
<div className="flex justify-between items-center w-full mb-4">
<button
type="button"
disabled={!isDateWithinConstraints()}
className="transition ease-in-out duration-100 p-2 rounded inline-flex items-center justify-center aspect-square cursor-pointer hover:bg-gray-300"
onClick={decrement}
>
<CareIcon className="care-l-angle-left-b text-lg" />
</button>
<div className="flex items-center justify-center text-sm">
{type === "date" && (
<div
onClick={showMonthPicker}
className="py-1 px-3 font-medium text-black text-center cursor-pointer hover:bg-gray-300 rounded"
>
{format(datePickerHeaderDate, "MMMM")}
</div>
)}
<div
onClick={showYearPicker}
className="py-1 px-3 font-medium text-black cursor-pointer hover:bg-gray-300 rounded"
>
<p className="text-center">
{type == "year"
? year.getFullYear()
: format(datePickerHeaderDate, "yyyy")}
</p>
</div>
</div>
<button
type="button"
disabled={
(type === "year" &&
new Date().getFullYear() === year.getFullYear()) ||
!isDateWithinConstraints(getLastDay())
}
className="transition ease-in-out duration-100 p-2 rounded inline-flex items-center justify-center aspect-square cursor-pointer hover:bg-gray-300"
onClick={increment}
>
<CareIcon className="care-l-angle-right-b text-lg" />
</button>
</div>
{type === "date" && (
<>
<div className="flex flex-wrap mb-3">
{DAYS.map((day) => (
<div key={day} className="aspect-square w-[14.26%]">
<div className="text-gray-800 font-medium text-center text-sm">
{day}
</div>
</div>
))}
</div>
<div className="flex flex-wrap">
{blankDays.map((_, i) => (
<div
key={i}
className="aspect-square w-[14.26%] text-center border p-1 border-transparent text-sm"
/>
))}
{dayCount.map((d, i) => (
<div key={i} className="aspect-square w-[14.26%]">
<div
onClick={setDateValue(d)}
className={classNames(
"cursor-pointer flex items-center justify-center text-center h-full text-sm rounded leading-loose transition ease-in-out duration-100 text-black",
value && isSelectedDate(d)
? "bg-primary-500 text-white font-bold"
: "hover:bg-gray-300",
!isDateWithinConstraints(d) && "!text-gray-300"
)}
>
{d}
</div>
</div>
))}
</div>
</>
)}
{type === "month" && (
<div className="flex flex-wrap">
{Array(12)
.fill(null)
.map((_, i) => (
<div
key={i}
className={classNames(
"cursor-pointer w-1/4 font-semibold py-4 px-2 text-center text-sm rounded-lg",
value && isSelectedMonth(i)
? "bg-primary-500 text-white"
: "text-gray-700 hover:bg-gray-300"
)}
onClick={setMonthValue(i)}
>
{format(
new Date(
datePickerHeaderDate.getFullYear(),
i,
datePickerHeaderDate.getDate()
),
"MMM"
)}
</div>
))}
</div>
)}
{type === "year" && (
<div className="flex flex-wrap">
{Array(12)
.fill(null)
.map((_, i) => {
const y = year.getFullYear() - 11 + i;
return (
<div
key={i}
className={classNames(
"cursor-pointer w-1/4 font-semibold py-4 px-2 text-center text-sm rounded-lg",
value && isSelectedYear(y)
? "bg-primary-500 text-white"
: "text-gray-700 hover:bg-gray-300"
)}
onClick={setYearValue(y)}
>
{y}
</div>
);
})}
</div>
)}
</Popover.Panel>
</DropdownTransition>
</Popover>
</div>
</div>
);
};
DateInputV2.defaultProps = {
position: "CENTER",
};
export default DateInputV2;