ด้วยค่าที่ตั้งไว้ล่วงหน้า

กำหนดช่วงวันที่ที่ตั้งไว้ล่วงหน้าเพื่อการเลือกที่รวดเร็ว แต่ละค่าที่ตั้งไว้มี label และ value ซึ่งอาจเป็นออบเจ็กต์คงที่หรือฟังก์ชันที่ส่งคืนค่าก็ได้

import { useState } from "react";
import { DateRangePicker } from "react-date-range-picker-tailwind4";
import type { DateRangePreset } from "react-date-range-picker-tailwind4";
import { today, subtract, startOf, endOf } from "react-date-range-picker-headless";
const presets: DateRangePreset[] = [
{
label: "Last 7 days",
value: () => ({
start: subtract(today(), 6, "day"),
end: today(),
}),
},
{
label: "Last 30 days",
value: () => ({
start: subtract(today(), 29, "day"),
end: today(),
}),
},
{
label: "This month",
value: () => ({
start: startOf(today(), "month"),
end: endOf(today(), "month"),
}),
},
{
label: "Last month",
value: () => {
const lastMonth = subtract(startOf(today(), "month"), 1, "day");
return {
start: startOf(lastMonth, "month"),
end: endOf(lastMonth, "month"),
};
},
},
];
function WithPresets() {
const [value, setValue] = useState<{ start: Date | null; end: Date | null }>({
start: null,
end: null,
});
return <DateRangePicker value={value} onChange={setValue} presets={presets} />;
}