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 { DateTimePicker } from "react-date-range-picker-tailwind4";
function ShouldCloseOnSelect() { const [value, setValue] = useState<Date | null>(null);
return ( <DateTimePicker value={value} onChange={setValue} shouldCloseOnSelect time={{ minuteStep: 5 }} /> );}Nhiều tháng
Hiển thị nhiều tháng cạnh nhau.
import { useState } from "react";import { DateTimePicker } from "react-date-range-picker-tailwind4";
function MultiMonth() { const [value, setValue] = useState<Date | null>(null);
return ( <DateTimePicker value={value} onChange={setValue} numberOfMonths={2} time={{ minuteStep: 5 }} /> );}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 { DateTimePicker } from "react-date-range-picker-tailwind4";
function FormIntegration() { const [value, setValue] = useState<Date | null>(null);
const handleSubmit = (e: SubmitEvent<HTMLFormElement>) => { e.preventDefault(); const formData = new FormData(e.currentTarget); const appointment = formData.get("appointment"); alert(`Submitted datetime: ${typeof appointment === "string" ? appointment : ""}`); };
return ( <form onSubmit={handleSubmit}> <DateTimePicker value={value} onChange={setValue} name="appointment" /> <button type="submit" style={{ marginTop: 8 }}> Submit </button> </form> );}