mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-04 04:08:34 +08:00
fix(components): make Datepicker value-format="x"
emit number timestamp value (#5187)
This commit is contained in:
parent
3c03e5610e
commit
5062ff106b
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -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({
|
||||
|
Loading…
Reference in New Issue
Block a user