From b8e474089b67a89cc2e62d7b81c71b6a89c83878 Mon Sep 17 00:00:00 2001 From: kamesan012 <79386745+kamesan012@users.noreply.github.com> Date: Tue, 15 Aug 2023 23:02:45 +0800 Subject: [PATCH] fix(components): fix the attribute conflict (#13504) * fix(components): fix the attribute conflict When value is input by shortcut, it's value will not be changed by defaultTime anymore closed #13483 * fix(components): change param to pass the test * fix(components): delete unnecessary changes * fix(components): use let to replace ref variable * fix(components): add test cases --- .../date-picker/__tests__/date-picker.test.ts | 30 +++++++++++++++++++ .../__tests__/date-time-picker.test.tsx | 30 +++++++++++++++++++ .../src/date-picker-com/panel-date-pick.vue | 11 ++++++- 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/packages/components/date-picker/__tests__/date-picker.test.ts b/packages/components/date-picker/__tests__/date-picker.test.ts index db31db9d32..f0dd779c5c 100644 --- a/packages/components/date-picker/__tests__/date-picker.test.ts +++ b/packages/components/date-picker/__tests__/date-picker.test.ts @@ -1560,4 +1560,34 @@ describe('MonthRange', () => { await nextTick() expect(pickHandler).toHaveBeenCalledTimes(1) }) + + it('prop defaultTime should not confilt with prop shortcuts', async () => { + const wrapper = _mount( + ``, + () => ({ value: '' }) + ) + const input = wrapper.find('input') + input.trigger('blur') + input.trigger('focus') + await nextTick() + document + .querySelector('.el-picker-panel__sidebar .el-picker-panel__shortcut') + .click() + await nextTick() + const vm = wrapper.vm as any + expect(vm.value).toBeDefined() + expect(vm.value.getFullYear()).toBe(2023) + expect(vm.value.getMonth()).toBe(0) + expect(vm.value.getDate()).toBe(1) + expect(vm.value.getHours()).toBe(12) + }) }) diff --git a/packages/components/date-picker/__tests__/date-time-picker.test.tsx b/packages/components/date-picker/__tests__/date-time-picker.test.tsx index 63ea5d147a..cfc80e29ca 100644 --- a/packages/components/date-picker/__tests__/date-time-picker.test.tsx +++ b/packages/components/date-picker/__tests__/date-time-picker.test.tsx @@ -773,4 +773,34 @@ describe('Datetimerange', () => { expect(startInput.element.value).toBe('') expect(endInput.element.value).toBe('') }) + + it('prop defaultTime should not confilt with prop shortcuts', async () => { + const value = ref('') + const wrapper = _mount(() => ( + + )) + const input = wrapper.find('input') + input.trigger('blur') + input.trigger('focus') + await nextTick() + ;( + document.querySelector( + '.el-picker-panel__sidebar .el-picker-panel__shortcut' + ) as HTMLElement + ).click() + await nextTick() + expect(value.value).toBeDefined() + expect(dayjs(value.value).format('YYYY-MM-DD HH:mm:ss')).toStrictEqual( + '2023-01-01 12:00:00' + ) + }) }) diff --git a/packages/components/date-picker/src/date-picker-com/panel-date-pick.vue b/packages/components/date-picker/src/date-picker-com/panel-date-pick.vue index 4fa7cbd5bd..ffbd93f7ef 100644 --- a/packages/components/date-picker/src/date-picker-com/panel-date-pick.vue +++ b/packages/components/date-picker/src/date-picker-com/panel-date-pick.vue @@ -253,6 +253,8 @@ const innerDate = ref(dayjs().locale(lang.value)) const isChangeToNow = ref(false) +let isShortcut = false + const defaultTimeD = computed(() => { return dayjs(defaultTime).locale(lang.value) }) @@ -275,7 +277,12 @@ const checkDateWithinRange = (date: ConfigType) => { : true } const formatEmit = (emitDayjs: Dayjs) => { - if (defaultTime && !visibleTime.value && !isChangeToNow.value) { + if ( + defaultTime && + !visibleTime.value && + !isChangeToNow.value && + !isShortcut + ) { return defaultTimeD.value .year(emitDayjs.year()) .month(emitDayjs.month()) @@ -296,6 +303,7 @@ const emit = (value: Dayjs | Dayjs[], ...args: any[]) => { userInputDate.value = null userInputTime.value = null isChangeToNow.value = false + isShortcut = false } const handleDatePick = (value: DateTableEmits, keepOpen?: boolean) => { if (selectionMode.value === 'date') { @@ -366,6 +374,7 @@ const handleShortcutClick = (shortcut: Shortcut) => { ? shortcut.value() : shortcut.value if (shortcutValue) { + isShortcut = true emit(dayjs(shortcutValue).locale(lang.value)) return }