Skip to content

Commit ada8c63

Browse files
committed
update UI and fixed date selection
1 parent 542184d commit ada8c63

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>Custom Datepicker</title>
77
<link rel="stylesheet" href="style.css" />
8-
<script type="module" src="script.js"></script>
8+
<script defer src="script.js"></script>
99
<!-- social links for refinedguides.com -->
1010
<script
1111
src="https://refinedguides.com/assets/social.js?linkColor=%23fff&textColor=%32000"
@@ -36,7 +36,7 @@
3636
<option>November</option>
3737
<option>December</option>
3838
</select>
39-
<input type="number" class="year-input" />
39+
<input type="number" class="year-input" min="1900" max="2100" />
4040
</div>
4141

4242
<button class="next">Next</button>

script.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ cancelBtn.addEventListener("click", () => {
2222
datepicker.hidden = true;
2323
});
2424

25+
// close datepicker on outside click
26+
document.addEventListener("click", (e) => {
27+
const datepickerContainer = datepicker.parentNode;
28+
if (!datepickerContainer.contains(e.target)) {
29+
datepicker.hidden = true;
30+
}
31+
});
32+
2533
// handle apply button click event
2634
applyBtn.addEventListener("click", () => {
2735
// set the selected date to date input
@@ -57,7 +65,9 @@ monthInput.addEventListener("change", () => {
5765

5866
// handle year input change event
5967
yearInput.addEventListener("change", () => {
60-
year = yearInput.value;
68+
const newYear = parseInt(yearInput.value, 10) || new Date().getFullYear();
69+
year = Math.min(2100, Math.max(1900, newYear));
70+
yearInput.value = year;
6171
displayDates();
6272
});
6373

@@ -94,17 +104,20 @@ const displayDates = () => {
94104
const lastOfPrevMonth = new Date(year, month, 0);
95105

96106
for (let i = 0; i <= lastOfPrevMonth.getDay(); i++) {
107+
// if the last day is Saturday don't show the leading dates
108+
if (lastOfPrevMonth.getDay() === 6) break;
109+
97110
const text = lastOfPrevMonth.getDate() - lastOfPrevMonth.getDay() + i;
98-
const button = createButton(text, true, -1);
111+
const button = createButton(text, true);
99112
dates.appendChild(button);
100113
}
101114

102115
//* display the current month
103116

104117
// get the last date of the month
105-
const lastOfMOnth = new Date(year, month + 1, 0);
118+
const lastOfMonth = new Date(year, month + 1, 0);
106119

107-
for (let i = 1; i <= lastOfMOnth.getDate(); i++) {
120+
for (let i = 1; i <= lastOfMonth.getDate(); i++) {
108121
const button = createButton(i, false);
109122
button.addEventListener("click", handleDateClick);
110123
dates.appendChild(button);
@@ -115,33 +128,27 @@ const displayDates = () => {
115128
const firstOfNextMonth = new Date(year, month + 1, 1);
116129

117130
for (let i = firstOfNextMonth.getDay(); i < 7; i++) {
118-
const text = firstOfNextMonth.getDate() - firstOfNextMonth.getDay() + i;
131+
// if the first day starts on Sunday don't show the trailing dates
132+
if (firstOfNextMonth.getDay() === 0) break;
119133

120-
const button = createButton(text, true, 1);
134+
const text = firstOfNextMonth.getDate() - firstOfNextMonth.getDay() + i;
135+
const button = createButton(text, true);
121136
dates.appendChild(button);
122137
}
123138
};
124139

125-
const createButton = (text, isDisabled = false, type = 0) => {
126-
const currentDate = new Date();
127-
128-
// determine the date to compare based on the button type
129-
let comparisonDate = new Date(year, month + type, text);
130-
131-
// check if the current button is the date today
132-
const isToday =
133-
currentDate.getDate() === text &&
134-
currentDate.getFullYear() === year &&
135-
currentDate.getMonth() === month;
136-
137-
// check if the current button is selected
138-
const selected = selectedDate.getTime() === comparisonDate.getTime();
139-
140+
const createButton = (text, isDisabled = false) => {
140141
const button = document.createElement("button");
141142
button.textContent = text;
142143
button.disabled = isDisabled;
143-
button.classList.toggle("today", isToday && !isDisabled);
144-
button.classList.toggle("selected", selected);
144+
if (!isDisabled) {
145+
const buttonDate = new Date(year, month, text).toDateString();
146+
const today = buttonDate === new Date().toDateString();
147+
const selected = buttonDate === selectedDate.toDateString();
148+
149+
button.classList.toggle("today", today);
150+
button.classList.toggle("selected", selected);
151+
}
145152
return button;
146153
};
147154

style.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ body {
2323
font-size: 1rem;
2424
border-radius: 5px;
2525
border: 1px solid #eee;
26+
outline-color: mediumpurple;
2627
}
2728

2829
/* datepicker styles */
@@ -49,7 +50,6 @@ body {
4950
font-size: 14px;
5051
font-weight: 500;
5152
text-transform: uppercase;
52-
transition: 0.3s;
5353
touch-action: manipulation;
5454
}
5555

@@ -105,6 +105,7 @@ body {
105105
.datepicker-header input {
106106
font-size: 14px;
107107
border: 1px solid #eee;
108+
outline-color: mediumpurple;
108109
}
109110

110111
.datepicker-header input {

0 commit comments

Comments
 (0)