Skip to content

Commit c45bf39

Browse files
committed
Fix for incorrect calendar display on certain dates, ex '2017-01-01'. Resolves #40.
1 parent 03131b4 commit c45bf39

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

src/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const Calendar = React.createClass({
7070
const year = this.props.displayDate.getFullYear();
7171
const month = this.props.displayDate.getMonth();
7272
const firstDay = new Date(year, month, 1);
73-
const startingDay = this.props.weekStartsOnMonday ? (firstDay.getDay() - 1) : firstDay.getDay();
73+
const startingDay = this.props.weekStartsOnMonday ? (firstDay.getDay() === 0 ? 6 : firstDay.getDay() - 1) : firstDay.getDay();
7474
let monthLength = daysInMonth[month];
7575
if (month == 1) {
7676
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){

test/core.test.jsx

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ describe("Date Picker", function() {
6767
});
6868
const hiddenInputElement = document.getElementById(id);
6969
assertIsoStringsHaveSameDate(hiddenInputElement.value, value);
70-
console.log(hiddenInputElement.value, hiddenInputElement.getAttribute('data-formattedvalue'));
7170
assert.equal(hiddenInputElement.getAttribute('data-formattedvalue'), `${value.slice(5,7)}/${value.slice(8,10)}/${value.slice(0,4)}`);
7271
ReactDOM.unmountComponentAtNode(container);
7372
}));
@@ -605,4 +604,99 @@ describe("Date Picker", function() {
605604
assertIsoStringsHaveSameDate(value, originalValue);
606605
ReactDOM.unmountComponentAtNode(container);
607606
}));
607+
it("should display the correct day of the week in the calendar.", co.wrap(function *(){
608+
const id = UUID.v4();
609+
let value = null;
610+
let formattedValue = null;
611+
const App = React.createClass({
612+
handleChange: function(newValue, newFormattedValue){
613+
value = newValue;
614+
formattedValue = newFormattedValue;
615+
},
616+
render: function(){
617+
return <div>
618+
<DatePicker id={id} onChange={this.handleChange} dateFormat="MM/DD/YYYY" />
619+
</div>;
620+
}
621+
});
622+
yield new Promise(function(resolve, reject){
623+
ReactDOM.render(<App />, container, resolve);
624+
});
625+
const inputElement = document.querySelector("input.form-control");
626+
const hiddenInputElement = document.getElementById(id);
627+
const checkMonthAndYear = function(startValue) {
628+
inputElement.value = `${startValue.slice(5,7)}/${startValue.slice(8,10)}/${startValue.slice(0,4)}`;
629+
TestUtils.Simulate.change(inputElement);
630+
TestUtils.Simulate.focus(inputElement);
631+
const weekElements = document.querySelectorAll("table tbody tr");
632+
weekElements.forEach(weekElement => {
633+
const dayElements = weekElement.querySelectorAll("td");
634+
dayElements.forEach((dayElement, index) => {
635+
if(dayElement.innerHTML === '') {
636+
return;
637+
}
638+
TestUtils.Simulate.click(dayElement);
639+
let date = new Date(hiddenInputElement.value);
640+
assert.equal(date.getDay(), index);
641+
});
642+
});
643+
}
644+
const today = new Date();
645+
for(let year = today.getFullYear() - 2; year < today.getFullYear() + 2; year++) {
646+
for(let month = 0; month < 12; month++) {
647+
const date = new Date();
648+
date.setMonth(month);
649+
date.setYear(year);
650+
checkMonthAndYear(date.toISOString());
651+
}
652+
}
653+
}));
654+
it("should display the correct day of the week in the calendar when starting on Monday.", co.wrap(function *(){
655+
const id = UUID.v4();
656+
let value = null;
657+
let formattedValue = null;
658+
const App = React.createClass({
659+
handleChange: function(newValue, newFormattedValue){
660+
value = newValue;
661+
formattedValue = newFormattedValue;
662+
},
663+
render: function(){
664+
return <div>
665+
<DatePicker id={id} onChange={this.handleChange} dateFormat="MM/DD/YYYY" weekStartsOnMonday />
666+
</div>;
667+
}
668+
});
669+
yield new Promise(function(resolve, reject){
670+
ReactDOM.render(<App />, container, resolve);
671+
});
672+
const inputElement = document.querySelector("input.form-control");
673+
const hiddenInputElement = document.getElementById(id);
674+
const checkMonthAndYear = function(startValue) {
675+
inputElement.value = `${startValue.slice(5,7)}/${startValue.slice(8,10)}/${startValue.slice(0,4)}`;
676+
TestUtils.Simulate.change(inputElement);
677+
TestUtils.Simulate.focus(inputElement);
678+
const weekElements = document.querySelectorAll("table tbody tr");
679+
weekElements.forEach(weekElement => {
680+
const dayElements = weekElement.querySelectorAll("td");
681+
dayElements.forEach((dayElement, index) => {
682+
if(dayElement.innerHTML === '') {
683+
return;
684+
}
685+
TestUtils.Simulate.click(dayElement);
686+
let date = new Date(hiddenInputElement.value);
687+
assert.equal(date.getDay(), index === 6 ? 0 : index + 1);
688+
});
689+
});
690+
}
691+
const today = new Date();
692+
for(let year = today.getFullYear() - 2; year < today.getFullYear() + 2; year++) {
693+
for(let month = 0; month < 12; month++) {
694+
const date = new Date();
695+
date.setMonth(month);
696+
date.setYear(year);
697+
checkMonthAndYear(date.toISOString());
698+
}
699+
}
700+
}));
701+
608702
});

0 commit comments

Comments
 (0)