Zachowanie
Zamykanie po wyborze
Gdy opcja shouldCloseOnSelect jest włączona, wybór daty powoduje automatyczne zatwierdzenie i zamknięcie kalendarza.
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 />;}Wiele miesięcy
Wyświetlaj wiele miesięcy obok siebie.
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} />;}Integracja z formularzem
Użyj propa name, aby dodać ukryte pole (input) do natywnej obsługi formularzy.
💡 Tip
Style przycisku “Wyślij” w tym przykładzie są niestandardowe — biblioteka dostarcza tylko komponent wyboru daty.
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> );}