element-plus
Version:
A Component Library for Vue 3
1 lines • 9.41 kB
Source Map (JSON)
{"version":3,"file":"basic-month-table.mjs","sources":["../../../../../../../packages/components/date-picker/src/date-picker-com/basic-month-table.vue"],"sourcesContent":["<template>\n <table\n class=\"el-month-table\"\n @click=\"handleMonthTableClick\"\n @mousemove=\"handleMouseMove\"\n >\n <tbody>\n <tr v-for=\"(row, key) in rows\" :key=\"key\">\n <td v-for=\"(cell, key_) in row\" :key=\"key_\" :class=\"getCellStyle(cell)\">\n <div>\n <a class=\"cell\">{{\n t('el.datepicker.months.' + months[cell.text])\n }}</a>\n </div>\n </td>\n </tr>\n </tbody>\n </table>\n</template>\n\n<script lang=\"ts\">\nimport { defineComponent, computed, ref } from 'vue'\nimport dayjs from 'dayjs'\nimport { useLocale } from '@element-plus/hooks'\nimport { rangeArr } from '@element-plus/components/time-picker'\nimport { hasClass, castArray } from '@element-plus/utils'\n\nimport type { PropType } from 'vue'\nimport type { Dayjs } from 'dayjs'\n\nconst datesInMonth = (year: number, month: number, lang: string) => {\n const firstDay = dayjs().locale(lang).startOf('month').month(month).year(year)\n const numOfDays = firstDay.daysInMonth()\n return rangeArr(numOfDays).map((n) => firstDay.add(n, 'day').toDate())\n}\n\nexport default defineComponent({\n props: {\n disabledDate: {\n type: Function as PropType<(_: Date) => void>,\n },\n selectionMode: {\n type: String,\n default: 'month',\n },\n minDate: {\n type: Object as PropType<Dayjs>,\n },\n maxDate: {\n type: Object as PropType<Dayjs>,\n },\n date: {\n type: Object as PropType<Dayjs>,\n },\n parsedValue: {\n type: Object as PropType<Dayjs>,\n },\n rangeState: {\n type: Object,\n default: () => ({\n endDate: null,\n selecting: false,\n }),\n },\n },\n\n emits: ['changerange', 'pick', 'select'],\n\n setup(props, ctx) {\n const { t, lang } = useLocale()\n const months = ref(\n props.date\n .locale('en')\n .localeData()\n .monthsShort()\n .map((_) => _.toLowerCase())\n )\n const tableRows = ref([[], [], []])\n const lastRow = ref(null)\n const lastColumn = ref(null)\n const rows = computed(() => {\n // TODO: refactory rows / getCellClasses\n const rows = tableRows.value\n const now = dayjs().locale(lang.value).startOf('month')\n\n for (let i = 0; i < 3; i++) {\n const row = rows[i]\n for (let j = 0; j < 4; j++) {\n let cell = row[j]\n if (!cell) {\n cell = {\n row: i,\n column: j,\n type: 'normal',\n inRange: false,\n start: false,\n end: false,\n }\n }\n\n cell.type = 'normal'\n\n const index = i * 4 + j\n const calTime = props.date.startOf('year').month(index)\n\n const calEndDate =\n props.rangeState.endDate ||\n props.maxDate ||\n (props.rangeState.selecting && props.minDate)\n\n cell.inRange =\n (props.minDate &&\n calTime.isSameOrAfter(props.minDate, 'month') &&\n calEndDate &&\n calTime.isSameOrBefore(calEndDate, 'month')) ||\n (props.minDate &&\n calTime.isSameOrBefore(props.minDate, 'month') &&\n calEndDate &&\n calTime.isSameOrAfter(calEndDate, 'month'))\n\n if (props.minDate?.isSameOrAfter(calEndDate)) {\n cell.start = calEndDate && calTime.isSame(calEndDate, 'month')\n cell.end = props.minDate && calTime.isSame(props.minDate, 'month')\n } else {\n cell.start = props.minDate && calTime.isSame(props.minDate, 'month')\n cell.end = calEndDate && calTime.isSame(calEndDate, 'month')\n }\n\n const isToday = now.isSame(calTime)\n\n if (isToday) {\n cell.type = 'today'\n }\n cell.text = index\n const cellDate = calTime.toDate()\n cell.disabled = props.disabledDate && props.disabledDate(cellDate)\n row[j] = cell\n }\n }\n return rows\n })\n const getCellStyle = (cell) => {\n const style = {} as any\n const year = props.date.year()\n const today = new Date()\n const month = cell.text\n\n style.disabled = props.disabledDate\n ? datesInMonth(year, month, lang.value).every(props.disabledDate)\n : false\n style.current =\n castArray(props.parsedValue).findIndex(\n (date) => date.year() === year && date.month() === month\n ) >= 0\n style.today = today.getFullYear() === year && today.getMonth() === month\n\n if (cell.inRange) {\n style['in-range'] = true\n\n if (cell.start) {\n style['start-date'] = true\n }\n\n if (cell.end) {\n style['end-date'] = true\n }\n }\n return style\n }\n\n const handleMouseMove = (event) => {\n if (!props.rangeState.selecting) return\n\n let target = event.target\n if (target.tagName === 'A') {\n target = target.parentNode.parentNode\n }\n if (target.tagName === 'DIV') {\n target = target.parentNode\n }\n if (target.tagName !== 'TD') return\n\n const row = target.parentNode.rowIndex\n const column = target.cellIndex\n // can not select disabled date\n if (rows.value[row][column].disabled) return\n\n // only update rangeState when mouse moves to a new cell\n // this avoids frequent Date object creation and improves performance\n if (row !== lastRow.value || column !== lastColumn.value) {\n lastRow.value = row\n lastColumn.value = column\n ctx.emit('changerange', {\n selecting: true,\n endDate: props.date.startOf('year').month(row * 4 + column),\n })\n }\n }\n const handleMonthTableClick = (event) => {\n let target = event.target\n if (target.tagName === 'A') {\n target = target.parentNode.parentNode\n }\n if (target.tagName === 'DIV') {\n target = target.parentNode\n }\n if (target.tagName !== 'TD') return\n if (hasClass(target, 'disabled')) return\n const column = target.cellIndex\n const row = target.parentNode.rowIndex\n const month = row * 4 + column\n const newDate = props.date.startOf('year').month(month)\n if (props.selectionMode === 'range') {\n if (!props.rangeState.selecting) {\n ctx.emit('pick', { minDate: newDate, maxDate: null })\n ctx.emit('select', true)\n } else {\n if (newDate >= props.minDate) {\n ctx.emit('pick', { minDate: props.minDate, maxDate: newDate })\n } else {\n ctx.emit('pick', { minDate: newDate, maxDate: props.minDate })\n }\n ctx.emit('select', false)\n }\n } else {\n ctx.emit('pick', month)\n }\n }\n\n return {\n handleMouseMove,\n handleMonthTableClick,\n rows,\n getCellStyle,\n t,\n months,\n }\n },\n})\n</script>\n"],"names":["_openBlock"],"mappings":";;;;;;;;;;;AA8BA,MAAM,eAAe,CAAC,MAAc,OAAe,SAAiB;AAClE,QAAM,WAAW,QAAQ,OAAO,MAAM,QAAQ,SAAS,MAAM,OAAO,KAAK;AACzE,QAAM,YAAY,SAAS;AAC3B,SAAO,SAAS,WAAW,IAAI,CAAC,MAAM,SAAS,IAAI,GAAG,OAAO;AAAA;AAG/D,MAAK,YAAa,gBAAa;AAAA,EAC7B,OAAO;AAAA,IACL,cAAc;AA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