Hành vi
Đóng khi chọn
Khi shouldCloseOnSelect được bật, bộ chọn sẽ tự động xác nhận và đóng lại khi một ngày được nhấp.
import { useState } from "react";import { DatePicker } from "react-date-range-picker-styled";import "react-date-range-picker-styled/rdrp-styles.css";
function ShouldCloseOnSelect() { const [value, setValue] = useState<Date | null>(null);
return <DatePicker value={value} onChange={setValue} shouldCloseOnSelect />;}Nhiều tháng
Hiển thị nhiều tháng liền kề nhau.
import { useState } from "react";import { DatePicker } from "react-date-range-picker-styled";import "react-date-range-picker-styled/rdrp-styles.css";
function MultiMonth() { const [value, setValue] = useState<Date | null>(null);
return <DatePicker value={value} onChange={setValue} numberOfMonths={2} />;}Tích hợp biểu mẫu
Sử dụng prop name để bao gồm một đầu vào ẩn cho việc gửi biểu mẫu gốc.
💡 Tip
Kiểu dáng của nút gửi trong ví dụ này là tùy chỉnh — thư viện chỉ cung cấp bộ chọn ngày.
import { type SubmitEvent, useState } from "react";import { DatePicker } from "react-date-range-picker-styled";import "react-date-range-picker-styled/rdrp-styles.css";
function FormIntegration() { const [value, setValue] = useState<Date | null>(null);
const handleSubmit = (e: SubmitEvent<HTMLFormElement>) => { e.preventDefault(); const formData = new FormData(e.currentTarget); const birthday = formData.get("birthday"); alert(`Submitted date: ${typeof birthday === "string" ? birthday : ""}`); };
return ( <form onSubmit={handleSubmit} style={{ display: "flex", alignItems: "center", gap: 8 }}> <DatePicker value={value} onChange={setValue} name="birthday" /> <button type="submit" style={{ padding: "6px 14px", border: "1px solid #d1d5db", borderRadius: 6, background: "#3b82f6", color: "#fff", cursor: "pointer", fontSize: 13, }} > Submit </button> </form> );}