From b4c969ca29afd3842ef073fc579718e7228bf5fa Mon Sep 17 00:00:00 2001 From: momei <78132554+momei-LJM@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:50:43 +0800 Subject: [PATCH] fix(components): [time-picker] avoid update initial value when using disabledHours & isRange (#17813) * fix(components): [time-picker] fix wrong trigger * fix(components): [time-picker] add test for time-picker * fix(components): [time-picker] add a test for time-picker * fix(components): [time-picker] add test for time-picker --- .../__tests__/time-picker.test.tsx | 65 +++++++++++++++++++ .../time-picker/src/common/picker.vue | 14 ++-- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/packages/components/time-picker/__tests__/time-picker.test.tsx b/packages/components/time-picker/__tests__/time-picker.test.tsx index f74a406ffb..25727aa54b 100644 --- a/packages/components/time-picker/__tests__/time-picker.test.tsx +++ b/packages/components/time-picker/__tests__/time-picker.test.tsx @@ -128,6 +128,7 @@ describe('TimePicker', () => { input.trigger('focus') await nextTick() ;(document.querySelector('.el-time-panel__btn.confirm') as any).click() + expect(value.value).toBeInstanceOf(Date) }) @@ -869,4 +870,68 @@ describe('TimePicker(range)', () => { expect(startInput.element.value).toBe('') expect(endInput.element.value).toBe('') }) + + it('avoid update initial value when using disabledHours', async () => { + const value = ref([]) + + const disabledHours = () => { + const curH = dayjs().hour() + if (curH === 0) { + return [curH, 1] + } else if (curH === 23) { + return [curH - 1, curH] + } + return [curH - 1, curH + 1] + } + const wrapper = mount(() => ( + + )) + await nextTick() + + const [startInput, endInput] = wrapper.findAll('input') + + expect(startInput.element.value).toBe('') + expect(endInput.element.value).toBe('') + expect(value.value).toEqual([]) + }) + + it('can clear when using disabledHours', async () => { + const value = ref([ + new Date(2016, 9, 10, 9, 40), + new Date(2016, 9, 10, 15, 40), + ]) + + const disabledHours = () => { + const curH = dayjs().hour() + if (curH === 0) { + return [curH, 1] + } else if (curH === 23) { + return [curH - 1, curH] + } + return [curH - 1, curH + 1] + } + const wrapper = mount(() => ( + + )) + + await nextTick() + const findInputWrapper = () => wrapper.find('.el-date-editor') + const findClear = () => wrapper.find('.el-range__close-icon') + + await nextTick() + const inputWrapper = findInputWrapper() + await inputWrapper.trigger('mouseenter') + const clearIcon = findClear() + await clearIcon.trigger('click') + await nextTick() + expect(value.value).toEqual(null) + }) }) diff --git a/packages/components/time-picker/src/common/picker.vue b/packages/components/time-picker/src/common/picker.vue index 5d784bbe73..43d63aa4bc 100644 --- a/packages/components/time-picker/src/common/picker.vue +++ b/packages/components/time-picker/src/common/picker.vue @@ -453,11 +453,15 @@ const parsedValue = computed(() => { ) if (!isEqual(availableResult, dayOrDays!)) { dayOrDays = availableResult - emitInput( - (isArray(dayOrDays) - ? dayOrDays.map((_) => _.toDate()) - : dayOrDays.toDate()) as SingleOrRange - ) + + // The result is corrected only when model-value exists + if (!valueIsEmpty.value) { + emitInput( + (isArray(dayOrDays) + ? dayOrDays.map((_) => _.toDate()) + : dayOrDays.toDate()) as SingleOrRange + ) + } } } if (isArray(dayOrDays!) && dayOrDays.some((day) => !day)) {