UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 2.54 kB
{"version":3,"file":"select-controller2.mjs","names":[],"sources":["../../../../../../packages/components/calendar/src/select-controller.vue"],"sourcesContent":["<template>\n <el-select\n :model-value=\"yearValue\"\n size=\"small\"\n :class=\"nsSelect.e('year')\"\n :validate-event=\"false\"\n :options=\"yearOptions\"\n @change=\"handleYearChange\"\n />\n <el-select\n :model-value=\"monthValue\"\n size=\"small\"\n :class=\"nsSelect.e('month')\"\n :validate-event=\"false\"\n :options=\"monthOptions\"\n @change=\"handleMonthChange\"\n />\n <el-button size=\"small\" @click=\"selectToday\">\n {{ t('el.datepicker.today') }}\n </el-button>\n</template>\n\n<script lang=\"ts\" setup>\nimport { computed } from 'vue'\nimport dayjs from 'dayjs'\nimport { useLocale, useNamespace } from '@element-plus/hooks'\nimport ElSelect from '@element-plus/components/select'\nimport { ElButton } from '@element-plus/components/button'\nimport { isFunction } from '@element-plus/utils'\nimport { selectControllerEmits } from './select-controller'\n\nimport type { SelectControllerProps } from './select-controller'\n\ndefineOptions({\n name: 'SelectController',\n})\n\nconst props = defineProps<SelectControllerProps>()\nconst emit = defineEmits(selectControllerEmits)\n\nconst nsSelect = useNamespace('calendar-select')\nconst { t, lang } = useLocale()\n\nconst monthOptions = Array.from({ length: 12 }, (_, index) => {\n const actualMonth = index + 1\n const label = isFunction(props.formatter)\n ? props.formatter(actualMonth, 'month')\n : actualMonth\n return {\n value: actualMonth,\n label,\n }\n})\n\nconst yearValue = computed(() => props.date.year())\nconst monthValue = computed(() => props.date.month() + 1)\n// Get an array of 20 years\nconst yearOptions = computed(() => {\n const years = []\n for (let i = -10; i < 10; i++) {\n const year = yearValue.value + i\n if (year > 0) {\n const label = isFunction(props.formatter)\n ? props.formatter(year, 'year')\n : year\n years.push({ value: year, label })\n }\n }\n return years\n})\n\nconst handleYearChange = (year: number) => {\n emit(\n 'date-change',\n dayjs(new Date(year, monthValue.value - 1, 1)).locale(lang.value)\n )\n}\n\nconst handleMonthChange = (month: number) => {\n emit(\n 'date-change',\n dayjs(new Date(yearValue.value, month - 1, 1)).locale(lang.value)\n )\n}\n\nconst selectToday = () => {\n emit('date-change', 'today')\n}\n</script>\n"],"mappings":""}