-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatePicker.js
153 lines (135 loc) · 4.28 KB
/
DatePicker.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import React, { useState, useEffect, useRef } from "react";
import "./DatePicker.css";
export const DatePicker = ({handleChange, ref}) => {
const [currentDay, setCurrentDay] = useState(1);
const [dayOfWeek, setDayOfWeek] = useState(0);
const [days, setDays] = useState([]);
const [year, setYear] = useState(2021);
const [month, setMonth] = useState("February");
const [isOpen, setIsOpen] = useState(false);
const datePickerRef = useRef(null);
let weekdays = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"];
const monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const generateCalendar = (year, month) => {
console.log("Right now i am using " + month + " / " + year);
let firstDay = new Date(year, month, 1);
console.log("First day of " + month + " - " + firstDay);
let weekday = firstDay.getDay();
setDayOfWeek(weekday);
console.log("days of week " + weekday); //0 = sunday //1 == mon
const daysInMonth = new Date(year, month + 1, 0).getDate();
console.log(daysInMonth + " in month" + month + " year" + year);
const daysArray = Array.from(Array(daysInMonth).keys()).map(
(i, index) => i + 1
);
console.log(daysArray);
setDays(daysArray);
};
const isFirstDay = (day) => {
if (day === 1) return true;
};
const changeOpenState = (e) => {
setIsOpen(!isOpen);
};
const increaseMonth = () => {
const currIndex = monthNames.indexOf(month);
if (currIndex >= monthNames.length - 1) {
setMonth(monthNames[0]);
return;
}
setMonth(monthNames[currIndex + 1]);
};
const decreateMonth = () => {
const currIndex = monthNames.indexOf(month);
if (currIndex === 0) {
setMonth(monthNames[monthNames.length - 1]);
return;
}
setMonth(monthNames[currIndex - 1]);
};
const increaseYear = () => {
if (year > 2100) return;
setYear(year + 1);
};
const decreaseYear = () => {
if (year === 1) return;
setYear(year - 1);
};
useEffect(() => {
// ref?.current.focus();
// ref?.current.blur();
datePickerRef?.current?.focus();
datePickerRef?.current?.blur();
},[month, year, currentDay])
useEffect(() => {
generateCalendar(year, monthNames.indexOf(month));
}, [month, year]);
return (
<div className="container">
{isOpen ? (
<div className="calendar">
<div className="calendar-inner">
<div className="calendar-top">
<div className="calendar-month">
<i onClick={decreateMonth} className="fa fa-chevron-left"></i>
<span>{month}</span>
<i onClick={increaseMonth} className="fa fa-chevron-right"></i>
</div>
<div className="calendar-year">
{" "}
<i onClick={decreaseYear} className="fa fa-chevron-left"></i>
<span> {year} </span>
<i onClick={increaseYear} className="fa fa-chevron-right"></i>
</div>{" "}
</div>
<div className="calendar-content">
<div className="calendar-label">
{weekdays.map((day, index) => (
<li>{day}</li>
))}
</div>
<div className="calendar-days">
{dayOfWeek > 0
? Array.from(
Array(dayOfWeek - 1).keys()
).map((day, index) => <li className="day-empty"></li>)
: null}
{days.map((day, index) => (
<li onClick={(e) => setCurrentDay(day)} className="day">
{day}
</li>
))}
</div>
<div className="choosen-date">{`${currentDay}/${month}/${year}`}</div>
</div>
</div>
</div>
) : null}
<input
ref={datePickerRef}
// ref={register}
onBlur={(e) => handleChange(e)}
onClick={(e) => changeOpenState(e)}
className="datepicker"
type="text"
id="datepicker"
placeholder="DD/MM/YYYY"
name="date"
value={`${currentDay} ${month} ${year}`}
/>
</div>
);
};