timepicker-ui
4.6.0GitHub
timepicker-ui
Selection / Range Presets

Range Presets

Offer ready-made ranges such as Last 7 days next to the calendar with @pglejzer/datepicker-ui.

Docked

#

calendar.range.presets is a list of { label, value }. value is a { start, end } pair or a function that returns one. The picker calls the function each time it refreshes, so "Today" stays current. In a docked picker the presets are a column beside the calendar when there is room, and a row of chips otherwise.

Docked presets
import DatepickerUI from "@pglejzer/datepicker-ui";
import "@pglejzer/datepicker-ui/index.css";
const daysAgo = (days) => {
const date = new Date();
date.setDate(date.getDate() - days);
return date;
};
const presets = [
{ label: "Today", value: () => ({ start: new Date(), end: new Date() }) },
{ label: "Last 7 days", value: () => ({ start: daysAgo(6), end: new Date() }) },
{ label: "Last 30 days", value: () => ({ start: daysAgo(29), end: new Date() }) },
{
label: "This month",
value: () => {
const now = new Date();
return {
start: new Date(now.getFullYear(), now.getMonth(), 1),
end: new Date(now.getFullYear(), now.getMonth() + 1, 0),
};
},
},
{
label: "Last month",
value: () => {
const now = new Date();
return {
start: new Date(now.getFullYear(), now.getMonth() - 1, 1),
end: new Date(now.getFullYear(), now.getMonth(), 0),
};
},
},
];
new DatepickerUI("#dates", {
calendar: { mode: "range", range: { presets } },
ui: { displayMode: "docked" },
}).create();
#

In a modal the presets are a row of chips. Clicking one picks the range and follows behavior.closeOnSelect and behavior.commitOnChange. The accessible name of the group is labels.presets.

Modal presets
import DatepickerUI from "@pglejzer/datepicker-ui";
import "@pglejzer/datepicker-ui/index.css";
const daysAgo = (days) => {
const date = new Date();
date.setDate(date.getDate() - days);
return date;
};
const presets = [
{ label: "Today", value: () => ({ start: new Date(), end: new Date() }) },
{ label: "Last 7 days", value: () => ({ start: daysAgo(6), end: new Date() }) },
{ label: "Last 30 days", value: () => ({ start: daysAgo(29), end: new Date() }) },
{
label: "This month",
value: () => {
const now = new Date();
return {
start: new Date(now.getFullYear(), now.getMonth(), 1),
end: new Date(now.getFullYear(), now.getMonth() + 1, 0),
};
},
},
{
label: "Last month",
value: () => {
const now = new Date();
return {
start: new Date(now.getFullYear(), now.getMonth() - 1, 1),
end: new Date(now.getFullYear(), now.getMonth(), 0),
};
},
},
];
new DatepickerUI("#dates", {
calendar: { mode: "range", range: { presets } },
ui: { displayMode: "modal" },
}).create();

Docs: Selection Modes · Options

Esc