{"version":3,"file":"use-date-table.js","sources":["../../../../../../packages/components/calendar/src/use-date-table.ts"],"sourcesContent":["import { computed } from 'vue'\nimport dayjs from 'dayjs'\nimport localeData from 'dayjs/plugin/localeData.js'\nimport { useLocale } from '@element-plus/hooks'\nimport { rangeArr } from '@element-plus/components/time-picker'\nimport { WEEK_DAYS } from '@element-plus/constants'\nimport { getMonthDays, getPrevMonthLastDays, toNestedArr } from './date-table'\n\nimport type { SetupContext } from 'vue'\nimport type { Dayjs } from 'dayjs'\nimport type {\n CalendarDateCell,\n CalendarDateCellType,\n DateTableEmits,\n DateTableProps,\n} from './date-table'\n\nexport const useDateTable = (\n props: DateTableProps,\n emit: SetupContext['emit']\n) => {\n dayjs.extend(localeData)\n // https://day.js.org/docs/en/i18n/locale-data\n const firstDayOfWeek: number = dayjs.localeData().firstDayOfWeek()\n\n const { t, lang } = useLocale()\n const now = dayjs().locale(lang.value)\n\n const isInRange = computed(() => !!props.range && !!props.range.length)\n\n const rows = computed(() => {\n let days: CalendarDateCell[] = []\n if (isInRange.value) {\n const [start, end] = props.range!\n const currentMonthRange: CalendarDateCell[] = rangeArr(\n end.date() - start.date() + 1\n ).map((index) => ({\n text: start.date() + index,\n type: 'current',\n }))\n\n let remaining = currentMonthRange.length % 7\n remaining = remaining === 0 ? 0 : 7 - remaining\n const nextMonthRange: CalendarDateCell[] = rangeArr(remaining).map(\n (_, index) => ({\n text: index + 1,\n type: 'next',\n })\n )\n days = currentMonthRange.concat(nextMonthRange)\n } else {\n const firstDay = props.date.startOf('month').day()\n const prevMonthDays: CalendarDateCell[] = getPrevMonthLastDays(\n props.date,\n (firstDay - firstDayOfWeek + 7) % 7\n ).map((day) => ({\n text: day,\n type: 'prev',\n }))\n const currentMonthDays: CalendarDateCell[] = getMonthDays(props.date).map(\n (day) => ({\n text: day,\n type: 'current',\n })\n )\n days = [...prevMonthDays, ...currentMonthDays]\n const remaining = 7 - (days.length % 7 || 7)\n const nextMonthDays: CalendarDateCell[] = rangeArr(remaining).map(\n (_, index) => ({\n text: index + 1,\n type: 'next',\n })\n )\n days = days.concat(nextMonthDays)\n }\n return toNestedArr(days)\n })\n\n const weekDays = computed(() => {\n const start = firstDayOfWeek\n if (start === 0) {\n return WEEK_DAYS.map((_) => t(`el.datepicker.weeks.${_}`))\n } else {\n return WEEK_DAYS.slice(start)\n .concat(WEEK_DAYS.slice(0, start))\n .map((_) => t(`el.datepicker.weeks.${_}`))\n }\n })\n\n const getFormattedDate = (day: number, type: CalendarDateCellType): Dayjs => {\n switch (type) {\n case 'prev':\n return props.date.startOf('month').subtract(1, 'month').date(day)\n case 'next':\n return props.date.startOf('month').add(1, 'month').date(day)\n case 'current':\n return props.date.date(day)\n }\n }\n\n const handlePickDay = ({ text, type }: CalendarDateCell) => {\n const date = getFormattedDate(text, type)\n emit('pick', date)\n }\n\n const getSlotData = ({ text, type }: CalendarDateCell) => {\n const day = getFormattedDate(text, type)\n return {\n isSelected: day.isSame(props.selectedDay),\n type: `${type}-month`,\n day: day.format('YYYY-MM-DD'),\n date: day.toDate(),\n }\n }\n\n return {\n now,\n isInRange,\n rows,\n weekDays,\n getFormattedDate,\n handlePickDay,\n getSlotData,\n }\n}\n"],"names":["dayjs","localeData","useLocale","computed","rangeArr","getPrevMonthLastDays","getMonthDays","toNestedArr","WEEK_DAYS"],"mappings":";;;;;;;;;;;;;;;;;;;;AAOY,MAAC,YAAY,GAAG,CAAC,KAAK,EAAE,IAAI,KAAK;AAC7C,EAAEA,yBAAK,CAAC,MAAM,CAACC,8BAAU,CAAC,CAAC;AAC3B,EAAE,MAAM,cAAc,GAAGD,yBAAK,CAAC,UAAU,EAAE,CAAC,cAAc,EAAE,CAAC;AAC7D,EAAE,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,GAAGE,eAAS,EAAE,CAAC;AAClC,EAAE,MAAM,GAAG,GAAGF,yBAAK,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACzC,EAAE,MAAM,SAAS,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