element-plus
Version:
A Component Library for Vue 3
1 lines • 6.47 kB
Source Map (JSON)
{"version":3,"file":"panel-time-pick.mjs","names":[],"sources":["../../../../../../../packages/components/time-picker/src/time-picker-com/panel-time-pick.vue"],"sourcesContent":["<template>\n <transition :name=\"transitionName\">\n <div v-if=\"actualVisible || visible\" :class=\"ns.b('panel')\">\n <div :class=\"[ns.be('panel', 'content'), { 'has-seconds': showSeconds }]\">\n <time-spinner\n ref=\"spinner\"\n :role=\"datetimeRole || 'start'\"\n :arrow-control=\"arrowControl\"\n :show-seconds=\"showSeconds\"\n :am-pm-mode=\"amPmMode\"\n :spinner-date=\"\n // https://github.com/vuejs/language-tools/issues/2104#issuecomment-3092541527\n parsedValue as any\n \"\n :disabled-hours=\"disabledHours\"\n :disabled-minutes=\"disabledMinutes\"\n :disabled-seconds=\"disabledSeconds\"\n @change=\"handleChange\"\n @set-option=\"onSetOption\"\n @select-range=\"setSelectionRange\"\n />\n </div>\n <div :class=\"ns.be('panel', 'footer')\">\n <button\n type=\"button\"\n :class=\"[ns.be('panel', 'btn'), 'cancel']\"\n @click=\"handleCancel\"\n >\n {{ t('el.datepicker.cancel') }}\n </button>\n <button\n type=\"button\"\n :class=\"[ns.be('panel', 'btn'), 'confirm']\"\n @click=\"handleConfirm()\"\n >\n {{ t('el.datepicker.confirm') }}\n </button>\n </div>\n </div>\n </transition>\n</template>\n\n<script lang=\"ts\" setup>\nimport { computed, inject, nextTick, ref } from 'vue'\nimport dayjs from 'dayjs'\nimport { EVENT_CODE } from '@element-plus/constants'\nimport { useLocale, useNamespace } from '@element-plus/hooks'\nimport { getEventCode, isUndefined } from '@element-plus/utils'\nimport { PICKER_BASE_INJECTION_KEY } from '../constants'\nimport { panelTimePickerProps } from '../props/panel-time-picker'\nimport { useTimePanel } from '../composables/use-time-panel'\nimport {\n buildAvailableTimeSlotGetter,\n useOldValue,\n} from '../composables/use-time-picker'\nimport TimeSpinner from './basic-time-spinner.vue'\n\nimport type { Dayjs } from 'dayjs'\n\nconst props = defineProps(panelTimePickerProps)\nconst emit = defineEmits(['pick', 'select-range', 'set-picker-option'])\n\n// Injections\nconst pickerBase = inject(PICKER_BASE_INJECTION_KEY) as any\nconst {\n arrowControl,\n disabledHours,\n disabledMinutes,\n disabledSeconds,\n defaultValue,\n} = pickerBase.props\nconst { getAvailableHours, getAvailableMinutes, getAvailableSeconds } =\n buildAvailableTimeSlotGetter(disabledHours, disabledMinutes, disabledSeconds)\n\nconst ns = useNamespace('time')\nconst { t, lang } = useLocale()\n// data\nconst selectionRange = ref([0, 2])\n\nconst oldValue = useOldValue(props, {\n modelValue: computed(() => pickerBase.props.modelValue),\n valueOnClear: computed(() =>\n pickerBase?.emptyValues ? pickerBase.emptyValues.valueOnClear.value : null\n ),\n})\n\n// computed\nconst transitionName = computed(() => {\n return isUndefined(props.actualVisible)\n ? `${ns.namespace.value}-zoom-in-top`\n : ''\n})\nconst showSeconds = computed(() => {\n return props.format.includes('ss')\n})\nconst amPmMode = computed(() => {\n if (props.format.includes('A')) return 'A'\n if (props.format.includes('a')) return 'a'\n return ''\n})\n// method\nconst isValidValue = (_date: Dayjs) => {\n const parsedDate = dayjs(_date).locale(lang.value)\n const result = getRangeAvailableTime(parsedDate)\n return parsedDate.isSame(result)\n}\nconst handleCancel = () => {\n const old = oldValue.value\n emit('pick', old, false)\n nextTick(() => {\n oldValue.value = old\n })\n}\nconst handleConfirm = (visible = false, first = false) => {\n if (first) return\n emit('pick', props.parsedValue, visible)\n}\nconst handleChange = (_date: Dayjs) => {\n // visible avoids edge cases, when use scrolls during panel closing animation\n if (!props.visible) {\n return\n }\n const result = getRangeAvailableTime(_date).millisecond(0)\n emit('pick', result, true)\n}\n\nconst setSelectionRange = (start: number, end: number) => {\n emit('select-range', start, end)\n selectionRange.value = [start, end]\n}\n\nconst changeSelectionRange = (step: number) => {\n const actualFormat = props.format\n const hourIndex = actualFormat.indexOf('HH')\n const minuteIndex = actualFormat.indexOf('mm')\n const secondIndex = actualFormat.indexOf('ss')\n const list: number[] = []\n const mapping: string[] = []\n if (hourIndex !== -1) {\n list.push(hourIndex)\n mapping.push('hours')\n }\n if (minuteIndex !== -1) {\n list.push(minuteIndex)\n mapping.push('minutes')\n }\n if (secondIndex !== -1 && showSeconds.value) {\n list.push(secondIndex)\n mapping.push('seconds')\n }\n\n const index = list.indexOf(selectionRange.value[0])\n const next = (index + step + list.length) % list.length\n timePickerOptions['start_emitSelectRange'](mapping[next])\n}\n\nconst handleKeydown = (event: KeyboardEvent) => {\n const code = getEventCode(event)\n\n const { left, right, up, down } = EVENT_CODE\n\n if ([left, right].includes(code)) {\n const step = code === left ? -1 : 1\n changeSelectionRange(step)\n event.preventDefault()\n return\n }\n\n if ([up, down].includes(code)) {\n const step = code === up ? -1 : 1\n timePickerOptions['start_scrollDown'](step)\n event.preventDefault()\n return\n }\n}\n\nconst { timePickerOptions, onSetOption, getAvailableTime } = useTimePanel({\n getAvailableHours,\n getAvailableMinutes,\n getAvailableSeconds,\n})\n\nconst getRangeAvailableTime = (date: Dayjs) => {\n return getAvailableTime(date, props.datetimeRole || '', true)\n}\n\nconst parseUserInput = (value: Dayjs) => {\n if (!value) return null\n return dayjs(value, props.format).locale(lang.value)\n}\n\nconst getDefaultValue = () => {\n return dayjs(defaultValue).locale(lang.value)\n}\n\nemit('set-picker-option', ['isValidValue', isValidValue])\nemit('set-picker-option', ['parseUserInput', parseUserInput])\nemit('set-picker-option', ['handleKeydownInput', handleKeydown])\nemit('set-picker-option', ['getRangeAvailableTime', getRangeAvailableTime])\nemit('set-picker-option', ['getDefaultValue', getDefaultValue])\nemit('set-picker-option', ['handleCancel', handleCancel])\n</script>\n"],"mappings":""}