date-picker: fix first-day-of-week computation (#14523)

Fixes #14523
pull/14605/head
Jeff Wen 2019-03-06 16:10:40 +08:00 committed by Jiewei Qian
parent 5a66e1021f
commit b7ee84f39a
2 changed files with 50 additions and 11 deletions

View File

@ -137,7 +137,6 @@
const offset = this.offsetDay; const offset = this.offsetDay;
const rows = this.tableRows; const rows = this.tableRows;
let count = 1; let count = 1;
let firstDayPosition;
const startDate = this.startDate; const startDate = this.startDate;
const disabledDate = this.disabledDate; const disabledDate = this.disabledDate;
@ -173,21 +172,17 @@
} }
if (i >= 0 && i <= 1) { if (i >= 0 && i <= 1) {
if (j + i * 7 >= (day + offset)) { const numberOfDaysFromPreviousMonth = day + offset < 0 ? 7 + day + offset : day + offset;
if (j + i * 7 >= numberOfDaysFromPreviousMonth) {
cell.text = count++; cell.text = count++;
if (count === 2) {
firstDayPosition = i * 7 + j;
}
} else { } else {
cell.text = dateCountOfLastMonth - (day + offset - j % 7) + 1 + i * 7; cell.text = dateCountOfLastMonth - (numberOfDaysFromPreviousMonth - j % 7) + 1 + i * 7;
cell.type = 'prev-month'; cell.type = 'prev-month';
} }
} else { } else {
if (count <= dateCountOfMonth) { if (count <= dateCountOfMonth) {
cell.text = count++; cell.text = count++;
if (count === 2) {
firstDayPosition = i * 7 + j;
}
} else { } else {
cell.text = count++ - dateCountOfMonth; cell.text = count++ - dateCountOfMonth;
cell.type = 'next-month'; cell.type = 'next-month';
@ -213,8 +208,6 @@
} }
} }
rows.firstDayPosition = firstDayPosition;
return rows; return rows;
} }
}, },

View File

@ -2670,4 +2670,50 @@ describe('DatePicker', () => {
}, DELAY); }, DELAY);
}); });
}); });
describe('picker-options:firstDayOfWeek especial', () => {
let vm;
beforeEach(done => {
vm = createTest(DatePicker, {
value: new Date(),
pickerOptions: {
firstDayOfWeek: 3
}
}, true);
const input = vm.$el.querySelector('input');
input.blur();
input.focus();
setTimeout(done, DELAY);
});
afterEach(() => destroyVM(vm));
it('set value equal to 2019-01-01', done => {
const date = new Date('2019-01-01');
vm.picker.value = date;
setTimeout(_ => {
const currentElement = vm.picker.$el.querySelector('tr:nth-child(2) td:nth-child(7) span');
const lastPrevMonthElement = vm.picker.$el.querySelector('tr:nth-child(2) td:nth-child(6) span');
expect(currentElement.innerText.trim()).to.equal('1');
expect(lastPrevMonthElement.innerText.trim()).to.equal('31');
done();
}, DELAY);
});
it('set value equal to 2018-01-01', done => {
const date = new Date('2018-01-01');
vm.picker.value = date;
setTimeout(_ => {
const currentElement = vm.picker.$el.querySelector('tr:nth-child(2) td:nth-child(6) span');
const lastPrevMonthElement = vm.picker.$el.querySelector('tr:nth-child(2) td:nth-child(5) span');
expect(currentElement.innerText.trim()).to.equal('1');
expect(lastPrevMonthElement.innerText.trim()).to.equal('31');
done();
}, DELAY);
});
});
}); });