@@ -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
2634applyBtn . addEventListener ( "click" , ( ) => {
2735 // set the selected date to date input
@@ -57,7 +65,9 @@ monthInput.addEventListener("change", () => {
5765
5866// handle year input change event
5967yearInput . 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
0 commit comments