fix(components): [calendar] fix range year bug (#7227)

This commit is contained in:
Dreamcreative 2022-04-23 22:30:32 +08:00 committed by GitHub
parent d26fa73420
commit 8be7123c75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -158,4 +158,27 @@ describe('Calendar.vue', () => {
await nextBtn?.trigger('click')
expect(wrapper.find('.is-selected').text()).toBe('1')
})
it('range two years', async () => {
const wrapper = mount(() => (
<Calendar
range={[new Date(2022, 0, 1), new Date(2022, 0, 31)]}
></Calendar>
))
const titleEl = wrapper.find('.el-calendar__title')
expect(/2021.*December/.test(titleEl.element.innerHTML)).toBeTruthy()
const dateTables = wrapper.element.querySelectorAll(
'.el-calendar-table.is-range'
)
expect(dateTables.length).toBe(3)
const rows = wrapper.element.querySelectorAll('.el-calendar-table__row')
expect(rows.length).toBe(6)
const cell = rows[rows.length - 1].firstElementChild as HTMLElement
cell.click()
await nextTick()
expect(/2022.*January/.test(titleEl.element.innerHTML)).toBeTruthy()
expect(cell?.classList.contains('is-selected')).toBeTruthy()
})
})

View File

@ -152,7 +152,10 @@ const calculateValidatedDateRange = (
]
}
// Three consecutive months (compatible: 2021-01-30 to 2021-02-28)
else if (firstMonth + 2 === lastMonth) {
else if (
firstMonth + 2 === lastMonth ||
(firstMonth + 1) % 11 === lastMonth
) {
const firstMonthLastDay = firstDay.endOf('month')
const secondMonthFirstDay = firstDay.add(1, 'month').startOf('month')