@veracity/vui
Version:
Veracity UI is a React component library crafted for use within Veracity applications and pages. Based on Styled Components and @xstyled.
72 lines (70 loc) • 3.77 kB
JavaScript
import { generateDateByUTCValue, getDateUTCValue, sameYear, sameYearSameMonth } from "../utils/dates.js";
import { minDateTimestamp, viewYears } from "./consts.js";
//#region src/calendar/utils.ts
const isExact = (mode) => mode === "exact";
const isFrom = (mode) => mode === "from";
const isTo = (mode) => mode === "to";
const isRange = (mode) => mode === "range";
const isDay = (timeUnitMode) => timeUnitMode === "day";
const isMonth = (timeUnitMode) => timeUnitMode === "month";
const isYear = (timeUnitMode) => timeUnitMode === "year";
/**
* Helper to get the next date item when iterating between two dates
* @param date current date in the iteration
* @param timeUnitMode time unit mode for day, month, year
*/
const getNextDate = (date, timeUnitMode) => {
const { utcYear, utcMonth, utcDate } = getDateUTCValue(date);
if (isDay(timeUnitMode)) return generateDateByUTCValue(utcYear, utcMonth, utcDate + 1);
if (isMonth(timeUnitMode)) return generateDateByUTCValue(utcYear, utcMonth + 1, utcDate);
return generateDateByUTCValue(utcYear + 1, utcMonth, utcDate);
};
const processSelectMonth = (clickedDate, mode, boundaries) => {
const date = clickedDate;
const minDateTime = new Date(minDateTimestamp);
const { utcYear, utcMonth } = getDateUTCValue(clickedDate);
if (isTo(mode)) {
const selectDayInMonth = generateDateByUTCValue(utcYear, utcMonth + 1, 0).getUTCDate();
if (boundaries?.endDate && sameYearSameMonth(clickedDate, boundaries?.endDate)) return boundaries?.endDate;
return generateDateByUTCValue(utcYear, utcMonth, selectDayInMonth);
}
if (isFrom(mode) && boundaries?.startDate && sameYearSameMonth(clickedDate, boundaries?.startDate)) return boundaries?.startDate;
if (isExact(mode) && sameYear(clickedDate, minDateTime)) return minDateTime;
return boundaries?.startDate && date < boundaries?.startDate ? boundaries.startDate : date;
};
/**
* Helper that calcs what new date we should set as viewDate when a year is clicked
*
* @param clickedDate date of year clicked
* @param newDate pre-processed potential new view date
* @param mode calendar mode
* @param boundaries boundaries of date range
*/
const processSelectYear = (clickedData, mode, boundaries) => {
const { utcYear, utcMonth } = getDateUTCValue(clickedData);
const minDateTime = new Date(minDateTimestamp);
const date = clickedData;
if (isTo(mode)) {
const selectDayInMonth = generateDateByUTCValue(utcYear, utcMonth + 1, 0).getUTCDate();
const selectMonthInYear = 11;
if (boundaries?.endDate && sameYear(clickedData, boundaries?.endDate)) return boundaries.endDate;
return generateDateByUTCValue(clickedData.getUTCFullYear(), selectMonthInYear, selectDayInMonth);
}
if (isFrom(mode) && boundaries?.startDate && sameYear(clickedData, boundaries?.startDate)) return boundaries.startDate;
if (isExact(mode) && sameYear(clickedData, minDateTime)) return minDateTime;
return boundaries?.startDate && date < boundaries?.startDate ? boundaries.startDate : date;
};
const processViewDate = (viewDate, direction, timeUnitMode, calendarItems) => {
const { utcYear, utcMonth } = getDateUTCValue(viewDate);
if (isDay(timeUnitMode)) return generateDateByUTCValue(utcYear, utcMonth + (direction === "next" ? 1 : -1));
if (isMonth(timeUnitMode)) return generateDateByUTCValue(utcYear + (direction === "next" ? 1 : -1), 0);
if (calendarItems) {
const year = calendarItems[direction === "next" ? calendarItems.length - 1 : 0].date.getUTCFullYear();
return generateDateByUTCValue(year + (direction === "next" ? 1 : -12), 0);
}
return null;
};
//#endregion
export { getNextDate, isDay, isExact, isFrom, isMonth, isRange, isTo, isYear, processSelectMonth, processSelectYear, processViewDate };
globalThis.__vuiVersion__ = "5.3.1"
//# sourceMappingURL=utils.js.map