fix(components): [date-picker] support remove same months from different years (#20020)

* fix: support selecting same months from different years

* feat: add test case

* chore: adjust description
This commit is contained in:
DDDDD12138 2025-03-07 09:51:08 +08:00 committed by GitHub
parent 15b98154c1
commit d4bb395291
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 1 deletions

View File

@ -1402,6 +1402,35 @@ describe('DatePicker months', () => {
document.querySelectorAll('.el-month-table tr .current').length
).toBe(1)
})
it('remove same months from different years', async () => {
const wrapper = _mount(
`<el-date-picker
type="months"
v-model="value"
/>`,
() => ({ value: [new Date('2025-03-05'), new Date('2024-03-05')] })
)
const input = wrapper.find('input')
input.trigger('blur')
input.trigger('focus')
await nextTick()
const prevYearButton: HTMLElement = document.querySelector('.d-arrow-left')
prevYearButton.click()
await nextTick()
const currentMonth: HTMLElement = document.querySelector(
'.el-month-table tr .current'
)
currentMonth.click()
await nextTick()
const vm = wrapper.vm
expect(vm.value.length).toBe(1)
expect(vm.value[0].getFullYear()).toBe(2025)
expect(vm.value[0].getMonth()).toBe(2) // March is month 2 (0-indexed)
})
})
describe('DatePicker keyboard events', () => {

View File

@ -232,7 +232,10 @@ const handleMonthTableClick = (event: MouseEvent | KeyboardEvent) => {
)
const newValue = hasClass(target, 'current')
? castArray(props.parsedValue).filter(
(d) => d?.month() !== newMonth.month()
(d) =>
// Filter out the selected month only when both year and month match
// This allows remove same months from different years #20019
d?.year() !== newMonth.year() || d?.month() !== newMonth.month()
)
: castArray(props.parsedValue).concat([dayjs(newMonth)])
emit('pick', newValue)