refactor(components): [time-picker] setup migration (#7908)

* refactor(components): [time-picker] setup migration

- Migrate panel-month-range to setup

* chore: remove return expression

* chore: change Array.isArray to isArray

* chore: remove required parsedValue
This commit is contained in:
JeremyWuuuuu 2022-05-27 13:55:03 +08:00 committed by GitHub
parent b6049dab1c
commit 3c0496f1a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 208 additions and 208 deletions

View File

@ -95,46 +95,67 @@
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { computed, defineComponent, inject, ref, toRef, watch } from 'vue' import { computed, inject, ref, toRef, useAttrs, useSlots, watch } from 'vue'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import ElIcon from '@element-plus/components/icon' import ElIcon from '@element-plus/components/icon'
import { useLocale, useNamespace } from '@element-plus/hooks' import { useLocale, useNamespace } from '@element-plus/hooks'
import { isArray, isFunction } from '@element-plus/utils'
import { DArrowLeft, DArrowRight } from '@element-plus/icons-vue' import { DArrowLeft, DArrowRight } from '@element-plus/icons-vue'
import { panelMonthRangeProps } from '../props/panel-month-range' import {
panelMonthRangeEmits,
panelMonthRangeProps,
} from '../props/panel-month-range'
import MonthTable from './basic-month-table.vue' import MonthTable from './basic-month-table.vue'
import type { SetupContext } from 'vue'
import type { Dayjs } from 'dayjs' import type { Dayjs } from 'dayjs'
import type { RangeState } from '../props/shared'
export default defineComponent({ defineOptions({
components: { MonthTable, ElIcon, DArrowLeft, DArrowRight }, name: 'DatePickerMonthRange',
})
props: panelMonthRangeProps, const props = defineProps(panelMonthRangeProps)
const emit = defineEmits(panelMonthRangeEmits)
const attrs = useAttrs()
const slots = useSlots()
emits: ['pick', 'set-picker-option'],
setup(props, ctx) {
const ppNs = useNamespace('picker-panel') const ppNs = useNamespace('picker-panel')
const drpNs = useNamespace('date-range-picker') const drpNs = useNamespace('date-range-picker')
const { t, lang } = useLocale() const { t, lang } = useLocale()
const leftDate = ref(dayjs().locale(lang.value)) const leftDate = ref(dayjs().locale(lang.value))
const rightDate = ref(dayjs().locale(lang.value).add(1, 'year')) const rightDate = ref(dayjs().locale(lang.value).add(1, 'year'))
const hasShortcuts = computed(() => !!shortcuts.length) const hasShortcuts = computed(() => !!shortcuts.length)
const handleShortcutClick = (shortcut) => { // FIXME: extract this to `date-picker.ts`
const shortcutValues = type Shortcut = {
typeof shortcut.value === 'function' ? shortcut.value() : shortcut.value text: string
value: [Date, Date] | (() => [Date, Date])
onClick?: (
ctx: Omit<SetupContext<typeof panelMonthRangeEmits>, 'expose'>
) => void
}
const handleShortcutClick = (shortcut: Shortcut) => {
const shortcutValues = isFunction(shortcut.value)
? shortcut.value()
: shortcut.value
if (shortcutValues) { if (shortcutValues) {
ctx.emit('pick', [ emit('pick', [
dayjs(shortcutValues[0]).locale(lang.value), dayjs(shortcutValues[0]).locale(lang.value),
dayjs(shortcutValues[1]).locale(lang.value), dayjs(shortcutValues[1]).locale(lang.value),
]) ])
return return
} }
if (shortcut.onClick) { if (shortcut.onClick) {
shortcut.onClick(ctx) shortcut.onClick({
attrs,
slots,
emit,
})
} }
} }
@ -181,19 +202,24 @@ export default defineComponent({
return props.unlinkPanels && rightYear.value > leftYear.value + 1 return props.unlinkPanels && rightYear.value > leftYear.value + 1
}) })
const minDate = ref(null) const minDate = ref<Dayjs>()
const maxDate = ref(null) const maxDate = ref<Dayjs>()
const rangeState = ref({ const rangeState = ref<RangeState>({
endDate: null, endDate: null,
selecting: false, selecting: false,
}) })
const handleChangeRange = (val) => { const handleChangeRange = (val: RangeState) => {
rangeState.value = val rangeState.value = val
} }
const handleRangePick = (val, close = true) => { type RangePickValue = {
minDate: Dayjs
maxDate: Dayjs
}
const handleRangePick = (val: RangePickValue, close = true) => {
// const defaultTime = props.defaultTime || [] // const defaultTime = props.defaultTime || []
// const minDate_ = modifyWithTimeString(val.minDate, defaultTime[0]) // const minDate_ = modifyWithTimeString(val.minDate, defaultTime[0])
// const maxDate_ = modifyWithTimeString(val.maxDate, defaultTime[1]) // const maxDate_ = modifyWithTimeString(val.maxDate, defaultTime[1])
@ -210,9 +236,9 @@ export default defineComponent({
handleConfirm() handleConfirm()
} }
const isValidValue = (value) => { const isValidValue = (value: [Dayjs | undefined, Dayjs | undefined]) => {
return ( return (
Array.isArray(value) && isArray(value) &&
value && value &&
value[0] && value[0] &&
value[1] && value[1] &&
@ -222,24 +248,24 @@ export default defineComponent({
const handleConfirm = (visible = false) => { const handleConfirm = (visible = false) => {
if (isValidValue([minDate.value, maxDate.value])) { if (isValidValue([minDate.value, maxDate.value])) {
ctx.emit('pick', [minDate.value, maxDate.value], visible) emit('pick', [minDate.value, maxDate.value], visible)
} }
} }
const onSelect = (selecting) => { const onSelect = (selecting: boolean) => {
rangeState.value.selecting = selecting rangeState.value.selecting = selecting
if (!selecting) { if (!selecting) {
rangeState.value.endDate = null rangeState.value.endDate = null
} }
} }
const formatToString = (value) => { const formatToString = (days: Dayjs[]) => {
return value.map((_) => _.format(format)) return days.map((day) => day.format(format))
} }
const getDefaultValue = () => { const getDefaultValue = () => {
let start: Dayjs let start: Dayjs
if (Array.isArray(defaultValue.value)) { if (isArray(defaultValue.value)) {
const left = dayjs(defaultValue.value[0]) const left = dayjs(defaultValue.value[0])
let right = dayjs(defaultValue.value[1]) let right = dayjs(defaultValue.value[1])
if (!props.unlinkPanels) { if (!props.unlinkPanels) {
@ -256,7 +282,7 @@ export default defineComponent({
} }
// pickerBase.hub.emit('SetPickerOption', ['isValidValue', isValidValue]) // pickerBase.hub.emit('SetPickerOption', ['isValidValue', isValidValue])
ctx.emit('set-picker-option', ['formatToString', formatToString]) emit('set-picker-option', ['formatToString', formatToString])
const pickerBase = inject('EP_PICKER_BASE') as any const pickerBase = inject('EP_PICKER_BASE') as any
const { shortcuts, disabledDate, format } = pickerBase.props const { shortcuts, disabledDate, format } = pickerBase.props
const defaultValue = toRef(pickerBase.props, 'defaultValue') const defaultValue = toRef(pickerBase.props, 'defaultValue')
@ -292,39 +318,12 @@ export default defineComponent({
} }
} else { } else {
const defaultArr = getDefaultValue() const defaultArr = getDefaultValue()
minDate.value = null minDate.value = undefined
maxDate.value = null maxDate.value = undefined
leftDate.value = defaultArr[0] leftDate.value = defaultArr[0]
rightDate.value = defaultArr[1] rightDate.value = defaultArr[1]
} }
}, },
{ immediate: true } { immediate: true }
) )
return {
ppNs,
drpNs,
shortcuts,
disabledDate,
onSelect,
handleRangePick,
rangeState,
handleChangeRange,
minDate,
maxDate,
enableYearArrow,
leftLabel,
rightLabel,
leftNextYear,
leftPrevYear,
rightNextYear,
rightPrevYear,
t,
leftDate,
rightDate,
hasShortcuts,
handleShortcutClick,
}
},
})
</script> </script>

View File

@ -7,4 +7,6 @@ export const panelMonthRangeProps = buildProps({
...panelRangeSharedProps, ...panelRangeSharedProps,
} as const) } as const)
export const panelMonthRangeEmits = ['pick', 'set-picker-option']
export type PanelMonthRangeProps = ExtractPropTypes<typeof panelMonthRangeProps> export type PanelMonthRangeProps = ExtractPropTypes<typeof panelMonthRangeProps>

View File

@ -3,9 +3,9 @@ import { datePickTypes } from '@element-plus/constants'
import type { Dayjs } from 'dayjs' import type { Dayjs } from 'dayjs'
const selectionModes = ['date', 'dates', 'year', 'month', 'week'] const selectionModes = ['date', 'dates', 'year', 'month', 'week', 'range']
type RangeState = { export type RangeState = {
endDate: null | Dayjs endDate: null | Dayjs
selecting: boolean selecting: boolean
} }
@ -26,7 +26,6 @@ export const datePickerSharedProps = buildProps({
}, },
parsedValue: { parsedValue: {
type: definePropType<Dayjs | Dayjs[]>([Object, Array]), type: definePropType<Dayjs | Dayjs[]>([Object, Array]),
required: true,
}, },
rangeState: { rangeState: {
type: definePropType<RangeState>(Object), type: definePropType<RangeState>(Object),