@kelvininc/ui-components
Version:
Kelvin UI Components
38 lines (37 loc) • 1.49 kB
JavaScript
import { getArrayOfIndexes } from "../../utils/arrays.helper";
import { fromDateFields, getFirstWeekdayIndexOfMonth, getNumberOfDaysInMonth } from "../../utils/date.helper";
import { CALENDAR_FILLED_ROWS_NUMBER_OF_DAYS, DATE_FORMAT } from "./calendar.config";
export const getCalendarStartDisabledDays = (month, year) => {
const currentMonthFirstWeekday = getFirstWeekdayIndexOfMonth(month, year);
const lastMonthNumberOfDays = getNumberOfDaysInMonth(month - 1, year);
const lastMonthDays = [];
let index = currentMonthFirstWeekday;
while (index > 0) {
lastMonthDays.push(lastMonthNumberOfDays - index + 1);
index = index - 1;
}
return lastMonthDays;
};
export const getCalendarEndDisabledDays = (filledDays) => {
return getArrayOfIndexes(CALENDAR_FILLED_ROWS_NUMBER_OF_DAYS - filledDays).map(item => item + 1);
};
export const getSelectedRange = (selectedDates = []) => {
if (selectedDates.length > 0) {
const [startDate] = selectedDates;
if (selectedDates.length === 1) {
return [startDate];
}
const [endDate] = selectedDates.slice(-1);
return [startDate, endDate];
}
return [];
};
export const getHooveredDate = (hoveredDay, month, year, hoveredDate) => {
if (hoveredDay !== undefined) {
return fromDateFields(hoveredDay, month, year).format(DATE_FORMAT);
}
if (hoveredDate !== undefined) {
return hoveredDate;
}
return undefined;
};