UNPKG

element-plus

Version:

A Component Library for Vue 3

214 lines (211 loc) 8.91 kB
import { defineComponent, ref, computed, watch, nextTick, openBlock, createElementBlock, unref, normalizeClass, createElementVNode, Fragment, renderList, withKeys, withModifiers, toDisplayString } from 'vue'; import dayjs from 'dayjs'; import '../../../../hooks/index.mjs'; import '../../../time-picker/index.mjs'; import '../../../../utils/index.mjs'; import { basicMonthTableProps } from '../props/basic-month-table.mjs'; import _export_sfc from '../../../../_virtual/plugin-vue_export-helper.mjs'; import { rangeArr } from '../../../time-picker/src/utils.mjs'; import { useNamespace } from '../../../../hooks/use-namespace/index.mjs'; import { useLocale } from '../../../../hooks/use-locale/index.mjs'; import { castArray } from '../../../../utils/arrays.mjs'; import { hasClass } from '../../../../utils/dom/style.mjs'; const _hoisted_1 = ["aria-label"]; const _hoisted_2 = ["aria-selected", "aria-label", "tabindex", "onKeydown"]; const _hoisted_3 = { class: "cell" }; const _sfc_main = /* @__PURE__ */ defineComponent({ __name: "basic-month-table", props: basicMonthTableProps, emits: ["changerange", "pick", "select"], setup(__props, { expose, emit }) { const props = __props; const datesInMonth = (year, month, lang2) => { const firstDay = dayjs().locale(lang2).startOf("month").month(month).year(year); const numOfDays = firstDay.daysInMonth(); return rangeArr(numOfDays).map((n) => firstDay.add(n, "day").toDate()); }; const ns = useNamespace("month-table"); const { t, lang } = useLocale(); const tbodyRef = ref(); const currentCellRef = ref(); const months = ref(props.date.locale("en").localeData().monthsShort().map((_) => _.toLowerCase())); const tableRows = ref([ [], [], [] ]); const lastRow = ref(); const lastColumn = ref(); const rows = computed(() => { var _a, _b; const rows2 = tableRows.value; const now = dayjs().locale(lang.value).startOf("month"); for (let i = 0; i < 3; i++) { const row = rows2[i]; for (let j = 0; j < 4; j++) { const cell = row[j] || (row[j] = { row: i, column: j, type: "normal", inRange: false, start: false, end: false, text: -1, disabled: false }); cell.type = "normal"; const index = i * 4 + j; const calTime = props.date.startOf("year").month(index); const calEndDate = props.rangeState.endDate || props.maxDate || props.rangeState.selecting && props.minDate || null; cell.inRange = !!(props.minDate && calTime.isSameOrAfter(props.minDate, "month") && calEndDate && calTime.isSameOrBefore(calEndDate, "month")) || !!(props.minDate && calTime.isSameOrBefore(props.minDate, "month") && calEndDate && calTime.isSameOrAfter(calEndDate, "month")); if ((_a = props.minDate) == null ? void 0 : _a.isSameOrAfter(calEndDate)) { cell.start = !!(calEndDate && calTime.isSame(calEndDate, "month")); cell.end = props.minDate && calTime.isSame(props.minDate, "month"); } else { cell.start = !!(props.minDate && calTime.isSame(props.minDate, "month")); cell.end = !!(calEndDate && calTime.isSame(calEndDate, "month")); } const isToday = now.isSame(calTime); if (isToday) { cell.type = "today"; } cell.text = index; cell.disabled = ((_b = props.disabledDate) == null ? void 0 : _b.call(props, calTime.toDate())) || false; } } return rows2; }); const focus = () => { var _a; (_a = currentCellRef.value) == null ? void 0 : _a.focus(); }; const getCellStyle = (cell) => { const style = {}; const year = props.date.year(); const today = new Date(); const month = cell.text; style.disabled = props.disabledDate ? datesInMonth(year, month, lang.value).every(props.disabledDate) : false; style.current = castArray(props.parsedValue).findIndex((date) => dayjs.isDayjs(date) && date.year() === year && date.month() === month) >= 0; style.today = today.getFullYear() === year && today.getMonth() === month; if (cell.inRange) { style["in-range"] = true; if (cell.start) { style["start-date"] = true; } if (cell.end) { style["end-date"] = true; } } return style; }; const isSelectedCell = (cell) => { const year = props.date.year(); const month = cell.text; return castArray(props.date).findIndex((date) => date.year() === year && date.month() === month) >= 0; }; const handleMouseMove = (event) => { var _a; if (!props.rangeState.selecting) return; let target = event.target; if (target.tagName === "A") { target = (_a = target.parentNode) == null ? void 0 : _a.parentNode; } if (target.tagName === "DIV") { target = target.parentNode; } if (target.tagName !== "TD") return; const row = target.parentNode.rowIndex; const column = target.cellIndex; if (rows.value[row][column].disabled) return; if (row !== lastRow.value || column !== lastColumn.value) { lastRow.value = row; lastColumn.value = column; emit("changerange", { selecting: true, endDate: props.date.startOf("year").month(row * 4 + column) }); } }; const handleMonthTableClick = (event) => { var _a; const target = (_a = event.target) == null ? void 0 : _a.closest("td"); if ((target == null ? void 0 : target.tagName) !== "TD") return; if (hasClass(target, "disabled")) return; const column = target.cellIndex; const row = target.parentNode.rowIndex; const month = row * 4 + column; const newDate = props.date.startOf("year").month(month); if (props.selectionMode === "range") { if (!props.rangeState.selecting) { emit("pick", { minDate: newDate, maxDate: null }); emit("select", true); } else { if (props.minDate && newDate >= props.minDate) { emit("pick", { minDate: props.minDate, maxDate: newDate }); } else { emit("pick", { minDate: newDate, maxDate: props.minDate }); } emit("select", false); } } else { emit("pick", month); } }; watch(() => props.date, async () => { var _a, _b; if ((_a = tbodyRef.value) == null ? void 0 : _a.contains(document.activeElement)) { await nextTick(); (_b = currentCellRef.value) == null ? void 0 : _b.focus(); } }); expose({ focus }); return (_ctx, _cache) => { return openBlock(), createElementBlock("table", { role: "grid", "aria-label": unref(t)("el.datepicker.monthTablePrompt"), class: normalizeClass(unref(ns).b()), onClick: handleMonthTableClick, onMousemove: handleMouseMove }, [ createElementVNode("tbody", { ref_key: "tbodyRef", ref: tbodyRef }, [ (openBlock(true), createElementBlock(Fragment, null, renderList(unref(rows), (row, key) => { return openBlock(), createElementBlock("tr", { key }, [ (openBlock(true), createElementBlock(Fragment, null, renderList(row, (cell, key_) => { return openBlock(), createElementBlock("td", { key: key_, ref_for: true, ref: (el) => isSelectedCell(cell) && (currentCellRef.value = el), class: normalizeClass(getCellStyle(cell)), "aria-selected": `${isSelectedCell(cell)}`, "aria-label": unref(t)(`el.datepicker.month${+cell.text + 1}`), tabindex: isSelectedCell(cell) ? 0 : -1, onKeydown: [ withKeys(withModifiers(handleMonthTableClick, ["prevent", "stop"]), ["space"]), withKeys(withModifiers(handleMonthTableClick, ["prevent", "stop"]), ["enter"]) ] }, [ createElementVNode("div", null, [ createElementVNode("span", _hoisted_3, toDisplayString(unref(t)("el.datepicker.months." + months.value[cell.text])), 1) ]) ], 42, _hoisted_2); }), 128)) ]); }), 128)) ], 512) ], 42, _hoisted_1); }; } }); var MonthTable = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "/home/runner/work/element-plus/element-plus/packages/components/date-picker/src/date-picker-com/basic-month-table.vue"]]); export { MonthTable as default }; //# sourceMappingURL=basic-month-table.mjs.map