UNPKG

birdpaper-ui

Version:

一个通用的 vue3 UI组件库。A common vue3 UI component library.

81 lines (80 loc) 2.39 kB
import dayjs from "dayjs"; import localeData from "dayjs/plugin/localeData"; import "dayjs/locale/zh-cn"; import { ref, computed } from "vue"; const useDayJs = (lang, modelValue) => { dayjs.locale(lang); dayjs.extend(localeData); const toDay = dayjs().format("YYYY-MM-DD"); const current = ref(!modelValue ? dayjs() : dayjs(modelValue)); const currentMonth = computed(() => current.value.month()); const currentYear = computed(() => current.value.year()); const weeks = dayjs.weekdaysMin(); const months = dayjs.monthsShort(); const dates = 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 = 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 = ref([]); const firstYear = 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 }; }; export { useDayJs };