birdpaper-ui
Version:
一个通用的 vue3 UI组件库。A common vue3 UI component library.
81 lines (80 loc) • 2.51 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const dayjs = require("dayjs");
const localeData = require("dayjs/plugin/localeData");
require("dayjs/locale/zh-cn");
const vue = require("vue");
const useDayJs = (lang, modelValue) => {
dayjs.locale(lang);
dayjs.extend(localeData);
const toDay = dayjs().format("YYYY-MM-DD");
const current = vue.ref(!modelValue ? dayjs() : dayjs(modelValue));
const currentMonth = vue.computed(() => current.value.month());
const currentYear = vue.computed(() => current.value.year());
const weeks = dayjs.weekdaysMin();
const months = dayjs.monthsShort();
const dates = vue.ref([[], [], [], [], [], []]);
const setDates = (valueFormat) => {
let sum = 0;
const firstDay = current.value.startOf("month").day() || 1;
const lastDate = current.value.endOf("month").date();
const startDate = current.value.startOf("month").subtract(firstDay || 7, "day");
for (let row = 0; row < dates.value.length; row++) {
for (let col = 0; col < 7; col++) {
const value = startDate.add(sum, "day");
const label = value.date().toString();
let type = "normal";
if (sum < firstDay) {
type = "prev";
}
if (sum - firstDay >= lastDate) {
type = "next";
}
dates.value[row][col] = { type, value: value.format("YYYY-MM-DD"), label };
sum++;
}
}
};
const changeMonth = (m) => current.value = current.value.month(m);
const changeYear = (y) => current.value = current.value.year(y);
const monthCell = vue.ref([]);
const setMonthCell = (valueFormat) => {
for (let i = 0; i < months.length; i++) {
const label = months[i];
const value = current.value.month(i);
monthCell.value[i] = {
value: value.format(valueFormat),
label
};
}
};
const yearCell = vue.ref([]);
const firstYear = vue.ref(current.value.subtract(5, "year").year());
const setYearCell = (valueFormat) => {
for (let i = 1; i < 13; i++) {
const value = current.value.year(firstYear.value + i);
yearCell.value[i - 1] = {
value: value.format(valueFormat),
label: value.year() + ""
};
}
};
return {
toDay,
current,
currentMonth,
currentYear,
dates,
setDates,
weeks,
months,
monthCell,
setMonthCell,
changeMonth,
changeYear,
yearCell,
firstYear,
setYearCell
};
};
exports.useDayJs = useDayJs;