element-plus/packages/components/date-picker/src/date-picker.ts
三咲智子 55348b30b6
style: use prettier (#3228)
* style: use prettier

* style: just prettier format, no code changes

* style: eslint fix
object-shorthand, prefer-const

* style: fix no-void

* style: no-console
2021-09-04 19:29:28 +08:00

86 lines
2.5 KiB
TypeScript

import { defineComponent, h, provide, ref } from 'vue'
import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat'
import advancedFormat from 'dayjs/plugin/advancedFormat'
import localeData from 'dayjs/plugin/localeData'
import weekOfYear from 'dayjs/plugin/weekOfYear'
import weekYear from 'dayjs/plugin/weekYear'
import dayOfYear from 'dayjs/plugin/dayOfYear'
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter'
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'
import {
CommonPicker,
DEFAULT_FORMATS_DATE,
DEFAULT_FORMATS_DATEPICKER,
timePickerDefaultProps,
} from '@element-plus/components/time-picker'
import DatePickPanel from './date-picker-com/panel-date-pick.vue'
import DateRangePickPanel from './date-picker-com/panel-date-range.vue'
import MonthRangePickPanel from './date-picker-com/panel-month-range.vue'
import type { PropType } from 'vue'
import type { IDatePickerType } from './date-picker.type'
dayjs.extend(localeData)
dayjs.extend(advancedFormat)
dayjs.extend(customParseFormat)
dayjs.extend(weekOfYear)
dayjs.extend(weekYear)
dayjs.extend(dayOfYear)
dayjs.extend(isSameOrAfter)
dayjs.extend(isSameOrBefore)
const getPanel = function (type: IDatePickerType) {
if (type === 'daterange' || type === 'datetimerange') {
return DateRangePickPanel
} else if (type === 'monthrange') {
return MonthRangePickPanel
}
return DatePickPanel
}
export default defineComponent({
name: 'ElDatePicker',
install: null,
props: {
...timePickerDefaultProps,
type: {
type: String as PropType<IDatePickerType>,
default: 'date',
},
},
emits: ['update:modelValue'],
setup(props, ctx) {
provide('ElPopperOptions', props.popperOptions)
const commonPicker = ref(null)
const refProps = {
...props,
focus: () => {
commonPicker.value?.handleFocus()
},
}
ctx.expose(refProps)
return () => {
// since props always have all defined keys on it, {format, ...props} will always overwrite format
// pick props.format or provide default value here before spreading
const format =
props.format ??
(DEFAULT_FORMATS_DATEPICKER[props.type] || DEFAULT_FORMATS_DATE)
return h(
CommonPicker,
{
...props,
format,
type: props.type,
ref: commonPicker,
'onUpdate:modelValue': (value) =>
ctx.emit('update:modelValue', value),
},
{
default: (scopedProps) => h(getPanel(props.type), scopedProps),
}
)
}
},
})