/** * @description vue version datePicker * @author 阿怪 * @date 2023/05/18 23:44 * @version v1.0.0 * * 江湖的业务千篇一律,复杂的代码好几百行。 * * todo fix month type */ import { defineComponent, watch } from 'vue'; import { BASE_MONTH_NAME, BASE_WEEK_NAME, CalendarItem, DisplayCalendarType, toDayjs, useDatePicker, } from './useDatePicker'; import MDivider from '../../other/divider/MDivider'; import { props } from './api.ts'; import usePopover from '../../../compositions/usePopover.tsx'; import useBorder from '../../../compositions/useBorder.tsx'; import { DatePickerProps } from './index'; import './datePicker.css'; const MCalendarColumn = defineComponent((props: { columns: DisplayCalendarType[] }, { emit }) => { const clickItem = (item: DisplayCalendarType) => { emit('click', item); }; return () => { return
{props.columns.map((item, index) => { return
clickItem(item)}> {item.day}
; })}
; }; }, { props: { columns: { type: Array, default: () => [] } }, emits: ['click'], }); const MPrevMonthArrow = defineComponent(() => () =>
); const MNextMonthArrow = defineComponent(() => () =>
); const MPrevYearArrow = defineComponent(() => () =>
); const MNextYearArrow = defineComponent(() => () =>
); export default defineComponent((_props: DatePickerProps, { emit }) => { const props = _props as Required; const { updateDateRef, popoverOptions, getCalendar, toPrevMonth, toNextMonth, toNextYear, toPrevYear, getValue, clickCurrentYear, clickYearItem, clickCurrentMonth, clickMonthItem, dateRef, spanClass, displayValue, calendarTypeRef, currentRef, yearsRef, } = useDatePicker({ props }); const { withPopover } = usePopover(popoverOptions, 'm-date-picker'); const { withBorder } = useBorder(); const clickDay = (item: CalendarItem) => { const value = getValue(item); updateDateRef(value); currentRef.value = toDayjs(value); emit('update:modelValue', value); }; const clickMonth = (month: number) => { clickCurrentMonth(month); }; const clickYear = (year: number) => { clickCurrentYear(year); }; const clickYearDiv = (year: number) => { clickYearItem(year); }; const clickMonthDiv = (month: number) => { clickMonthItem(month); // when type="month", set value when click month item if (props.type === 'month') { const calendarItem: CalendarItem = { year: dateRef.value.year, month: month, day: 1, isCurrentMonth: false, }; const value = getValue(calendarItem); updateDateRef(value); currentRef.value = toDayjs(value); emit('update:modelValue', value); } }; watch(() => props.modelValue, value => { currentRef.value = toDayjs(value); updateDateRef(value); }); return () => { const getDateCalendar = () => { return getCalendar(dateRef)?.map((column, index) => { return ; }); }; const getMonthCalendar = () => { const isCurrent = (month: number) => { return month === dateRef.value?.month; }; return (
{BASE_MONTH_NAME.map((month, index) => (
clickMonthDiv(index + 1)}> {month}
))}
); }; const getYearCalendar = () => { const isCurrent = (year: number) => { return year === dateRef.value?.year; }; return
{yearsRef.value.map((year, index) => { return
clickYearDiv(year)}>{year}
; })}
; }; const getCalendarRender = () => { switch (calendarTypeRef.value) { case 'date': return getDateCalendar(); case 'month': return getMonthCalendar(); case 'year': return getYearCalendar(); } }; const calenderHead = () =>
clickYear(dateRef.value!.year)}>{dateRef.value?.year} , clickMonth(dateRef.value!.month)}>{dateRef.value?.month}
; return withPopover({ default: () => withBorder({displayValue.value}, 'm-date-picker-active'), content: () => withBorder(
{calenderHead()} {calendarTypeRef.value === 'date' ? : undefined}
{getCalendarRender()}
, 'm-date-picker-calendar'), }); }; }, { name: 'MDatePicker', props, emits: ['update:modelValue'], });