Các ràng buộc
Bắt buộc
Khi required được đặt, nút xóa sẽ bị vô hiệu hóa và giá trị không thể để trống.
import { useState } from "react";import { DatePicker } from "react-date-range-picker-tailwind3";
function Required() { const [value, setValue] = useState<Date | null>(new Date());
return <DatePicker value={value} onChange={setValue} required />;}Ngày tối thiểu & tối đa
Giới hạn các ngày có thể chọn trong một phạm vi cụ thể bằng cách sử dụng minDate và maxDate.
import { useState } from "react";import { DatePicker } from "react-date-range-picker-tailwind3";
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} />;}Các ngày bị vô hiệu hóa
Sử dụng isDateUnavailable để tự động vô hiệu hóa các ngày cụ thể.
import { useState } from "react";import { DatePicker } from "react-date-range-picker-tailwind3";
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} />;}Vô hiệu hóa quá khứ / tương lai
Sử dụng disablePast hoặc disableFuture để nhanh chóng vô hiệu hóa các ngày trước/sau hôm nay.
import { useState } from "react";import { DatePicker } from "react-date-range-picker-tailwind3";
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
Đánh dấu các ngày
Đánh dấu trực quan các ngày cụ thể (ví dụ: ngày lễ, sự kiện) bằng cách sử dụng prop highlightDates.
import { useState } from "react";import { DatePicker } from "react-date-range-picker-tailwind3";
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} />;}Tháng ban đầu
Đặt tháng hiển thị ban đầu khi bộ chọn được mở lần đầu tiên.
import { useState } from "react";import { DatePicker } from "react-date-range-picker-tailwind3";
function InitialMonth() { const [value, setValue] = useState<Date | null>(null);
return <DatePicker value={value} onChange={setValue} initialMonth={new Date(2026, 5, 1)} />;}