fix(components): [date-picker] fix user input error in monthrange (#12943)

* fix(components): [date-picker] monthrange类型选择器在输入结束后报错

* fix: the format change

---------

Co-authored-by: qiang <qw13131wang@gmail.com>
This commit is contained in:
Masanori Doizaki 2024-08-03 22:39:42 +09:00 committed by GitHub
parent 94a24dcbc8
commit 76b0ab0a5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 3 deletions

View File

@ -1569,6 +1569,26 @@ describe('MonthRange', () => {
).toEqual(ElPopperOptions)
})
it('user input', async () => {
const wrapper = _mount(
`<el-date-picker
type='monthrange'
v-model="value"
valueFormat="YYYY-MM"
/>`,
() => ({ value: ['2022-01', '2022-02'] })
)
const [startInput, endInput] = wrapper.findAll('input')
await startInput.setValue('2015-01')
await endInput.setValue('2017-01')
await nextTick()
const vm = wrapper.vm
expect(vm.value[0]).toBe('2015-01')
expect(vm.value[1]).toBe('2017-01')
})
describe('form item accessibility integration', () => {
it('automatic id attachment', async () => {
const wrapper = _mount(

View File

@ -99,11 +99,13 @@
</template>
<script lang="ts" setup>
import { computed, inject, ref, toRef } from 'vue'
import { computed, inject, ref, toRef, unref } from 'vue'
import dayjs from 'dayjs'
import ElIcon from '@element-plus/components/icon'
import { isArray } from '@element-plus/utils'
import { useLocale } from '@element-plus/hooks'
import { DArrowLeft, DArrowRight } from '@element-plus/icons-vue'
import { getDefaultValue, isValidRange } from '../utils'
import {
panelMonthRangeEmits,
panelMonthRangeProps,
@ -193,8 +195,26 @@ const handleRangePick = (val: RangePickValue, close = true) => {
handleRangeConfirm()
}
const formatToString = (days: Dayjs[]) => {
return days.map((day) => day.format(format.value))
const handleClear = () => {
leftDate.value = getDefaultValue(unref(defaultValue), {
lang: unref(lang),
unit: 'year',
unlinkPanels: props.unlinkPanels,
})[0]
rightDate.value = leftDate.value.add(1, 'year')
emit('pick', null)
}
const formatToString = (value: Dayjs | Dayjs[]) => {
return isArray(value)
? value.map((_) => _.format(format.value))
: value.format(format.value)
}
const parseUserInput = (value: Dayjs | Dayjs[]) => {
return isArray(value)
? value.map((_) => dayjs(_, format.value).locale(lang.value))
: dayjs(value, format.value).locale(lang.value)
}
function onParsedValueChanged(
@ -211,5 +231,8 @@ function onParsedValueChanged(
}
}
emit('set-picker-option', ['isValidValue', isValidRange])
emit('set-picker-option', ['formatToString', formatToString])
emit('set-picker-option', ['parseUserInput', parseUserInput])
emit('set-picker-option', ['handleClear', handleClear])
</script>