พฤติกรรม
ปิดเมื่อเลือก
เมื่อเปิดใช้งาน shouldCloseOnSelect ตัวเลือกจะยืนยันและปิดโดยอัตโนมัติเมื่อมีการคลิกวันที่
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 />;}หลายเดือน
แสดงหลายเดือนข้างกัน
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} />;}การรวมฟอร์ม
ใช้ prop name เพื่อรวมอินพุตที่ซ่อนอยู่สำหรับการส่งฟอร์มแบบเนทีฟ
💡 Tip
สไตล์ของปุ่มส่งในตัวอย่างนี้เป็นแบบกำหนดเอง — ไลบรารีมีให้เฉพาะตัวเลือกวันที่เท่านั้น
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> );}