@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
43 lines • 1.54 kB
JavaScript
import { addYears, eachMonthOfInterval, endOfYear, format, getMonth, getYear, isBefore, isSameYear, startOfMonth, startOfYear, } from "date-fns";
/** Return the months to show in the dropdown. */
export function getMonthOptions({ displayMonth, navStart, navEnd, locale, }) {
const months = eachMonthOfInterval({
start: startOfYear(displayMonth),
end: endOfYear(displayMonth),
});
const options = months.map((month) => {
const label = format(month, "LLLL", {
locale,
}).replace(".", "");
const value = getMonth(month);
const disabled = (navStart && month < startOfMonth(navStart)) ||
(navEnd && month > startOfMonth(navEnd)) ||
false;
return { value, label, disabled };
});
return options;
}
/** Return the years to show in the dropdown. */
export function getYearOptions({ navStart, navEnd, locale, }) {
if (!navStart)
return undefined;
if (!navEnd)
return undefined;
const firstNavYear = startOfYear(navStart);
const lastNavYear = endOfYear(navEnd);
const years = [];
let year = firstNavYear;
while (isBefore(year, lastNavYear) || isSameYear(year, lastNavYear)) {
years.push(year);
year = addYears(year, 1);
}
return years.map((_year) => {
const label = format(_year, "yyyy", { locale });
return {
value: getYear(_year),
label,
disabled: false,
};
});
}
//# sourceMappingURL=dropdown-options.js.map