element-plus
Version:
A Component Library for Vue 3
250 lines (243 loc) • 9.68 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var vue = require('vue');
var dayjs = require('dayjs');
require('../../../../hooks/index2.js');
require('../../../time-picker/index2.js');
require('../../../../utils/index2.js');
var pluginVue_exportHelper = require('../../../../_virtual/plugin-vue_export-helper.js');
var dateUtils = require('../../../time-picker/src/common/date-utils2.js');
var index = require('../../../../hooks/use-locale/index2.js');
var arrays = require('../../../../utils/arrays2.js');
var style = require('../../../../utils/dom/style2.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);
const datesInMonth = (year, month, lang) => {
const firstDay = dayjs__default["default"]().locale(lang).startOf("month").month(month).year(year);
const numOfDays = firstDay.daysInMonth();
return dateUtils.rangeArr(numOfDays).map((n) => firstDay.add(n, "day").toDate());
};
const _sfc_main = vue.defineComponent({
props: {
disabledDate: {
type: Function
},
selectionMode: {
type: String,
default: "month"
},
minDate: {
type: Object
},
maxDate: {
type: Object
},
date: {
type: Object
},
parsedValue: {
type: Object
},
rangeState: {
type: Object,
default: () => ({
endDate: null,
selecting: false
})
}
},
emits: ["changerange", "pick", "select"],
expose: ["focus"],
setup(props, ctx) {
const { t, lang } = index.useLocale();
const tbodyRef = vue.ref();
const currentCellRef = vue.ref();
const months = vue.ref(props.date.locale("en").localeData().monthsShort().map((_) => _.toLowerCase()));
const tableRows = vue.ref([[], [], []]);
const lastRow = vue.ref(null);
const lastColumn = vue.ref(null);
const rows = vue.computed(() => {
var _a;
const rows2 = tableRows.value;
const now = dayjs__default["default"]().locale(lang.value).startOf("month");
for (let i = 0; i < 3; i++) {
const row = rows2[i];
for (let j = 0; j < 4; j++) {
let cell = row[j];
if (!cell) {
cell = {
row: i,
column: j,
type: "normal",
inRange: false,
start: false,
end: 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;
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;
const cellDate = calTime.toDate();
cell.disabled = props.disabledDate && props.disabledDate(cellDate);
row[j] = cell;
}
}
return rows2;
});
vue.watch(() => props.date, async () => {
var _a, _b;
if ((_a = tbodyRef.value) == null ? void 0 : _a.contains(document.activeElement)) {
await vue.nextTick();
(_b = currentCellRef.value) == null ? void 0 : _b.focus();
}
});
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 = arrays.castArray(props.parsedValue).findIndex((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 arrays.castArray(props.date).findIndex((date) => date.year() === year && date.month() === month) >= 0;
};
const handleMouseMove = (event) => {
if (!props.rangeState.selecting)
return;
let target = event.target;
if (target.tagName === "A") {
target = target.parentNode.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;
ctx.emit("changerange", {
selecting: true,
endDate: props.date.startOf("year").month(row * 4 + column)
});
}
};
const handleMonthTableClick = (event) => {
let target = event.target;
target = target == null ? void 0 : target.closest("td");
if ((target == null ? void 0 : target.tagName) !== "TD")
return;
if (style.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) {
ctx.emit("pick", { minDate: newDate, maxDate: null });
ctx.emit("select", true);
} else {
if (newDate >= props.minDate) {
ctx.emit("pick", { minDate: props.minDate, maxDate: newDate });
} else {
ctx.emit("pick", { minDate: newDate, maxDate: props.minDate });
}
ctx.emit("select", false);
}
} else {
ctx.emit("pick", month);
}
};
return {
tbodyRef,
currentCellRef,
handleMouseMove,
handleMonthTableClick,
focus,
isSelectedCell,
rows,
getCellStyle,
t,
months
};
}
});
const _hoisted_1 = ["aria-label"];
const _hoisted_2 = { ref: "tbodyRef" };
const _hoisted_3 = ["aria-selected", "aria-label", "tabindex"];
const _hoisted_4 = { class: "cell" };
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return vue.openBlock(), vue.createElementBlock("table", {
role: "grid",
"aria-label": _ctx.t("el.datepicker.monthTablePrompt"),
class: "el-month-table",
onClick: _cache[2] || (_cache[2] = (...args) => _ctx.handleMonthTableClick && _ctx.handleMonthTableClick(...args)),
onMousemove: _cache[3] || (_cache[3] = (...args) => _ctx.handleMouseMove && _ctx.handleMouseMove(...args))
}, [
vue.createElementVNode("tbody", _hoisted_2, [
(vue.openBlock(true), vue.createElementBlock(vue.Fragment, null, vue.renderList(_ctx.rows, (row, key) => {
return vue.openBlock(), vue.createElementBlock("tr", { key }, [
(vue.openBlock(true), vue.createElementBlock(vue.Fragment, null, vue.renderList(row, (cell, key_) => {
return vue.openBlock(), vue.createElementBlock("td", {
key: key_,
ref_for: true,
ref: (el) => _ctx.isSelectedCell(cell) && (_ctx.currentCellRef = el),
class: vue.normalizeClass(_ctx.getCellStyle(cell)),
"aria-selected": `${_ctx.isSelectedCell(cell)}`,
"aria-label": _ctx.t(`el.datepicker.month${+cell.text + 1}`),
tabindex: _ctx.isSelectedCell(cell) ? 0 : -1,
onKeydown: [
_cache[0] || (_cache[0] = vue.withKeys(vue.withModifiers((...args) => _ctx.handleMonthTableClick && _ctx.handleMonthTableClick(...args), ["prevent", "stop"]), ["space"])),
_cache[1] || (_cache[1] = vue.withKeys(vue.withModifiers((...args) => _ctx.handleMonthTableClick && _ctx.handleMonthTableClick(...args), ["prevent", "stop"]), ["enter"]))
]
}, [
vue.createElementVNode("div", null, [
vue.createElementVNode("span", _hoisted_4, vue.toDisplayString(_ctx.t("el.datepicker.months." + _ctx.months[cell.text])), 1)
])
], 42, _hoisted_3);
}), 128))
]);
}), 128))
], 512)
], 40, _hoisted_1);
}
var MonthTable = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["render", _sfc_render], ["__file", "/home/runner/work/element-plus/element-plus/packages/components/date-picker/src/date-picker-com/basic-month-table.vue"]]);
exports["default"] = MonthTable;
//# sourceMappingURL=basic-month-table2.js.map