Skip to content

Commit 2506813

Browse files
committed
fix dateinput as per pull/476
is a better solution as previous pull request as it works for navigation back also and fixes api method addMonth
1 parent 1359517 commit 2506813

File tree

1 file changed

+12
-35
lines changed

1 file changed

+12
-35
lines changed

src/dateinput/dateinput.js

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
}
158158

159159
// invalid offset
160-
if (!/^-?\d+$/.test(val)) { return; }
160+
if ( !(/^-?\d+$/).test(val) ) { return; }
161161

162162
// convert to integer
163163
val = integer(val);
@@ -339,10 +339,10 @@
339339

340340

341341
if (index > 41) {
342-
showNextMonth();
342+
self.addMonth();
343343
el = $("#" + css.weeks + " a:eq(" + (index-42) + ")");
344344
} else if (index < 0) {
345-
showPreviousMonth();
345+
self.addMonth(-1);
346346
el = $("#" + css.weeks + " a:eq(" + (index+42) + ")");
347347
} else {
348348
el = days.eq(index);
@@ -354,8 +354,8 @@
354354
}
355355

356356
// pageUp / pageDown
357-
if (key == 34) { return showNextMonth(); }
358-
if (key == 33) { return showPreviousMonth(); }
357+
if (key == 34) { return self.addMonth(); }
358+
if (key == 33) { return self.addMonth(-1); }
359359

360360
// home
361361
if (key == 36) { return self.today(); }
@@ -384,33 +384,6 @@
384384
//}}}
385385

386386

387-
/**
388-
* @private
389-
*
390-
* Calculates the days in the next month to properly switch months
391-
*
392-
*/
393-
function showNextMonth() {
394-
395-
var daysNextMonth = dayAm(currYear, currMonth + 1);
396-
397-
/*
398-
* If next month has less days than the current date
399-
* add number of days in the next month, otherwise add
400-
* number of days in the current month
401-
*/
402-
return self.addDay(currDay > daysNextMonth ? daysNextMonth : dayAm(currYear, currMonth));
403-
}
404-
405-
/**
406-
* @private
407-
*
408-
* Return to previous month
409-
*/
410-
function showPreviousMonth() {
411-
return self.addDay(-dayAm(currYear, currMonth));
412-
}
413-
414387
$.extend(self, {
415388

416389

@@ -447,14 +420,14 @@
447420
// prev / next month
448421
pm = root.find("#" + css.prev).unbind("click").click(function(e) {
449422
if (!pm.hasClass(css.disabled)) {
450-
showPreviousMonth();
423+
self.addMonth(-1);
451424
}
452425
return false;
453426
});
454427

455428
nm = root.find("#" + css.next).unbind("click").click(function(e) {
456429
if (!nm.hasClass(css.disabled)) {
457-
showNextMonth();
430+
self.addMonth();
458431
}
459432
return false;
460433
});
@@ -657,7 +630,11 @@
657630
},
658631

659632
addMonth: function(amount) {
660-
return this.setValue(currYear, currMonth + (amount || 1), currDay);
633+
var targetMonth = currMonth + (amount || 1),
634+
daysInTargetMonth = dayAm(currYear, targetMonth),
635+
targetDay = currDay <= daysInTargetMonth ? currDay : daysInTargetMonth;
636+
637+
return this.setValue(currYear, targetMonth, targetDay);
661638
},
662639

663640
addYear: function(amount) {

0 commit comments

Comments
 (0)