fix(components): make Datepicker value-format="x" emit number timestamp value (#5187)

This commit is contained in:
Carter Li 2022-01-09 12:42:08 +08:00 committed by GitHub
parent 3c03e5610e
commit 5062ff106b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 7 deletions

View File

@ -376,8 +376,8 @@ describe('DatePicker', () => {
)
const vm = wrapper.vm as any
const input = wrapper.find('input')
input.trigger('blur')
input.trigger('focus')
await input.trigger('blur')
await input.trigger('focus')
await nextTick()
{
;(document.querySelector('td.available') as HTMLElement).click()
@ -389,10 +389,51 @@ describe('DatePicker', () => {
valueFormat
).format(valueFormat)
)
wrapper.find('button').trigger('click')
await wrapper.find('button').trigger('click')
await nextTick()
expect(wrapper.findComponent(Input).vm.modelValue).toBe('2021-05-31')
})
it('with "x"', async () => {
const format = 'YYYY/MM/DD'
const dateStr = '2021/05/31'
const valueFormat = 'x'
const value = Date.now()
const wrapper = _mount(
`
<el-date-picker
ref="compo"
v-model="value"
type="date"
format="${format}"
value-format="${valueFormat}" />
<button @click="changeValue">click</button>
`,
() => {
return {
value,
}
},
{
methods: {
changeValue() {
this.value = +new Date(dateStr)
},
},
}
)
const vm = wrapper.vm as any
const input = wrapper.find('input')
await input.trigger('blur')
await input.trigger('focus')
await nextTick()
;(document.querySelector('td.available') as HTMLElement).click()
await nextTick()
expect(vm.value).toBe(+dayjs().startOf('M'))
await wrapper.find('button').trigger('click')
await nextTick()
expect(wrapper.findComponent(Input).vm.modelValue).toBe(dateStr)
})
})
})

View File

@ -212,14 +212,17 @@ const parser = function (
format: string,
lang: string
): Dayjs {
const day = isEmpty(format)
? dayjs(date).locale(lang)
: dayjs(date, format).locale(lang)
const day =
isEmpty(format) || format === 'x'
? dayjs(date).locale(lang)
: dayjs(date, format).locale(lang)
return day.isValid() ? day : undefined
}
const formatter = function (date: number | Date, format: string, lang: string) {
return isEmpty(format) ? date : dayjs(date).locale(lang).format(format)
if (isEmpty(format)) return date
if (format === 'x') return +date
return dayjs(date).locale(lang).format(format)
}
export default defineComponent({