限制

必填

required 被設定時,清除按鈕將被禁用,且值不能被清空。

import { useState } from "react";
import { DatePicker } from "react-date-range-picker-styled";
import "react-date-range-picker-styled/rdrp-styles.css";
function Required() {
const [value, setValue] = useState<Date | null>(new Date());
return <DatePicker value={value} onChange={setValue} required />;
}

最小與最大日期

使用 minDatemaxDate 將可選日期限制在特定範圍內。

import { useState } from "react";
import { DatePicker } from "react-date-range-picker-styled";
import "react-date-range-picker-styled/rdrp-styles.css";
function MinMaxDate() {
const [value, setValue] = useState<Date | null>(null);
const today = new Date();
const minDate = new Date(today.getFullYear(), today.getMonth(), 1);
const maxDate = new Date(today.getFullYear(), today.getMonth() + 1, 0);
return <DatePicker value={value} onChange={setValue} minDate={minDate} maxDate={maxDate} />;
}

禁用日期

使用 isDateUnavailable 來動態禁用特定日期。

import { useState } from "react";
import { DatePicker } from "react-date-range-picker-styled";
import "react-date-range-picker-styled/rdrp-styles.css";
function DisabledDates() {
const [value, setValue] = useState<Date | null>(null);
const isDateUnavailable = (date: Date) => {
const day = date.getDay();
return day === 0 || day === 6; // Disable weekends
};
return <DatePicker value={value} onChange={setValue} isDateUnavailable={isDateUnavailable} />;
}

禁用過去/未來

使用 disablePastdisableFuture 來快速禁用今天之前/之後的日期。

import { useState } from "react";
import { DatePicker } from "react-date-range-picker-styled";
import "react-date-range-picker-styled/rdrp-styles.css";
function DisablePastFuture() {
const [pastValue, setPastValue] = useState<Date | null>(null);
const [futureValue, setFutureValue] = useState<Date | null>(null);
return (
<div style={{ display: "flex", flexDirection: "column", gap: 16 }}>
<div>
<div style={{ fontSize: 13, fontWeight: 600, opacity: 0.7, marginBottom: 8 }}>
Past dates disabled
</div>
<DatePicker value={pastValue} onChange={setPastValue} disablePast />
</div>
<div>
<div style={{ fontSize: 13, fontWeight: 600, opacity: 0.7, marginBottom: 8 }}>
Future dates disabled
</div>
<DatePicker value={futureValue} onChange={setFutureValue} disableFuture />
</div>
</div>
);
}
Past dates disabled
Future dates disabled

突顯日期

使用 highlightDates 屬性來視覺上突顯特定日期(例如假日、活動)。

import { useState } from "react";
import { DatePicker } from "react-date-range-picker-styled";
import "react-date-range-picker-styled/rdrp-styles.css";
function HighlightDates() {
const [value, setValue] = useState<Date | null>(null);
const today = new Date();
const highlightDates = [
new Date(today.getFullYear(), today.getMonth(), 5),
new Date(today.getFullYear(), today.getMonth(), 15),
new Date(today.getFullYear(), today.getMonth(), 25),
];
return <DatePicker value={value} onChange={setValue} highlightDates={highlightDates} />;
}

初始月份

設定選擇器首次開啟時顯示的初始月份。

import { useState } from "react";
import { DatePicker } from "react-date-range-picker-styled";
import "react-date-range-picker-styled/rdrp-styles.css";
function InitialMonth() {
const [value, setValue] = useState<Date | null>(null);
return <DatePicker value={value} onChange={setValue} initialMonth={new Date(2026, 5, 1)} />;
}