mirror of
https://github.com/element-plus/element-plus.git
synced 2025-04-06 16:30:35 +08:00
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:
parent
15b98154c1
commit
d4bb395291
@ -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', () => {
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user