@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
37 lines • 1.18 kB
JavaScript
import { isAfter, isBefore, startOfMonth, startOfYear } from "date-fns";
/**
* Makes sure the month is within the min and max daterange to avoid showing disabled months
* @note We do not warn the user if start > end now
*/
const clampDisplayMonth = ({ month, start, end, }) => {
if (!month) {
return undefined;
}
let monthToShow = month;
if (start && isBefore(monthToShow, start)) {
monthToShow = start;
}
if (end && isAfter(monthToShow, end)) {
monthToShow = end;
}
return startOfMonth(monthToShow);
};
/**
* Makes sure the month is within the min and max daterange to avoid showing disabled months
* @note We do not warn the user if start > end now
*/
const clampDisplayYear = ({ month, start, end, }) => {
if (!month) {
return undefined;
}
let monthToShow = month;
if (start && monthToShow.getFullYear() < start.getFullYear()) {
monthToShow = start;
}
if (end && monthToShow.getFullYear() > end.getFullYear()) {
monthToShow = end;
}
return startOfYear(monthToShow);
};
export { clampDisplayYear, clampDisplayMonth };
//# sourceMappingURL=clamp-dates.js.map