use-date-table.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
  2. const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
  3. const require_date = require("../../../constants/date.js");
  4. const require_index = require("../../../hooks/use-locale/index.js");
  5. const require_constants = require("../../time-picker/src/constants.js");
  6. const require_utils = require("../../time-picker/src/utils.js");
  7. const require_date_table = require("./date-table.js");
  8. let vue = require("vue");
  9. let dayjs = require("dayjs");
  10. dayjs = require_runtime.__toESM(dayjs);
  11. let dayjs_plugin_localeData_js = require("dayjs/plugin/localeData.js");
  12. dayjs_plugin_localeData_js = require_runtime.__toESM(dayjs_plugin_localeData_js);
  13. //#region ../../packages/components/calendar/src/use-date-table.ts
  14. const useDateTable = (props, emit) => {
  15. dayjs.default.extend(dayjs_plugin_localeData_js.default);
  16. const firstDayOfWeek = dayjs.default.localeData().firstDayOfWeek();
  17. const { t, lang } = require_index.useLocale();
  18. const now = (0, dayjs.default)().locale(lang.value);
  19. const isInRange = (0, vue.computed)(() => !!props.range && !!props.range.length);
  20. const rows = (0, vue.computed)(() => {
  21. let days = [];
  22. if (isInRange.value) {
  23. const [start, end] = props.range;
  24. const currentMonthRange = require_utils.rangeArr(end.date() - start.date() + 1).map((index) => ({
  25. text: start.date() + index,
  26. type: "current"
  27. }));
  28. let remaining = currentMonthRange.length % 7;
  29. remaining = remaining === 0 ? 0 : 7 - remaining;
  30. const nextMonthRange = require_utils.rangeArr(remaining).map((_, index) => ({
  31. text: index + 1,
  32. type: "next"
  33. }));
  34. days = currentMonthRange.concat(nextMonthRange);
  35. } else {
  36. const firstDay = props.date.startOf("month").day();
  37. const prevMonthDays = require_date_table.getPrevMonthLastDays(props.date, (firstDay - firstDayOfWeek + 7) % 7).map((day) => ({
  38. text: day,
  39. type: "prev"
  40. }));
  41. const currentMonthDays = require_date_table.getMonthDays(props.date).map((day) => ({
  42. text: day,
  43. type: "current"
  44. }));
  45. days = [...prevMonthDays, ...currentMonthDays];
  46. const nextMonthDays = require_utils.rangeArr(7 - (days.length % 7 || 7)).map((_, index) => ({
  47. text: index + 1,
  48. type: "next"
  49. }));
  50. days = days.concat(nextMonthDays);
  51. }
  52. return require_date_table.toNestedArr(days);
  53. });
  54. const weekDays = (0, vue.computed)(() => {
  55. const start = firstDayOfWeek;
  56. if (start === 0) return require_date.WEEK_DAYS.map((_) => t(`el.datepicker.weeks.${_}`));
  57. else return require_date.WEEK_DAYS.slice(start).concat(require_date.WEEK_DAYS.slice(0, start)).map((_) => t(`el.datepicker.weeks.${_}`));
  58. });
  59. const getFormattedDate = (day, type) => {
  60. switch (type) {
  61. case "prev": return props.date.startOf("month").subtract(1, "month").date(day);
  62. case "next": return props.date.startOf("month").add(1, "month").date(day);
  63. case "current": return props.date.date(day);
  64. }
  65. };
  66. const handlePickDay = ({ text, type }) => {
  67. emit("pick", getFormattedDate(text, type));
  68. };
  69. const getSlotData = ({ text, type }) => {
  70. const day = getFormattedDate(text, type);
  71. return {
  72. isSelected: day.isSame(props.selectedDay),
  73. type: `${type}-month`,
  74. day: day.format(require_constants.DEFAULT_FORMATS_DATE),
  75. date: day.toDate()
  76. };
  77. };
  78. return {
  79. now,
  80. isInRange,
  81. rows,
  82. weekDays,
  83. getFormattedDate,
  84. handlePickDay,
  85. getSlotData
  86. };
  87. };
  88. //#endregion
  89. exports.useDateTable = useDateTable;
  90. //# sourceMappingURL=use-date-table.js.map