The DatePicker component provides a date selection interface with a calendar popup. It's commonly used for forms, scheduling, and any application that requires date input.
import { DatePicker } from '@dashflowx/core'export default function DatePickerExample() {const [date, setDate] = useState<Date | undefined>(new Date());return (<DatePickermode="single"date={date}setDate={setDate}/>)}
Preview:
| Prop | Type | Default | Description |
|---|---|---|---|
mode | 'default' | 'single' | 'multiple' | 'range' | 'default' | The selection mode for the date picker |
date | Date | undefined | undefined | The selected date value |
setDate | function | - | Callback function to update the selected date |
className | string | '' | Additional CSS classes for the container |
initialFocus | boolean | false | Whether to focus the calendar initially |
const [date, setDate] = useState<Date | undefined>(new Date());<DatePickermode="single"date={date}setDate={setDate}/>
Preview:
const [dateRange, setDateRange] = useState<DateRange | undefined>({from: new Date(),to: addDays(new Date(), 7)});<DatePickermode="range"date={dateRange}setDate={setDateRange}/>
Preview:
const [dates, setDates] = useState<Date[]>([]);<DatePickermode="multiple"date={dates}setDate={setDates}/>
Preview:
<DatePickermode="single"date={date}setDate={setDate}className="border-2 border-blue-500 rounded-xl"/>
Preview:
<DatePickermode="single"date={date}setDate={setDate}initialFocus={true}/>
Preview:
const [formData, setFormData] = useState({name: '',email: '',birthDate: undefined as Date | undefined});const handleDateChange = (date: Date | undefined) => {setFormData(prev => ({...prev,birthDate: date}));};<form className="space-y-4"><div><label className="block text-sm font-medium mb-2">Name</label><inputtype="text"value={formData.name}onChange={(e) => setFormData(prev => ({ ...prev, name: e.target.value }))}className="w-full border rounded px-3 py-2"/></div><div><label className="block text-sm font-medium mb-2">Birth Date</label><DatePickermode="single"date={formData.birthDate}setDate={handleDateChange}/></div></form>
const [eventDate, setEventDate] = useState<Date | undefined>(new Date());const [eventTime, setEventTime] = useState('09:00');<div className="space-y-4"><div><label className="block text-sm font-medium mb-2">Event Date</label><DatePickermode="single"date={eventDate}setDate={setEventDate}/></div><div><label className="block text-sm font-medium mb-2">Event Time</label><inputtype="time"value={eventTime}onChange={(e) => setEventTime(e.target.value)}className="w-full border rounded px-3 py-2"/></div></div>
const [checkIn, setCheckIn] = useState<Date | undefined>(new Date());const [checkOut, setCheckOut] = useState<Date | undefined>(addDays(new Date(), 1));<div className="grid grid-cols-2 gap-4"><div><label className="block text-sm font-medium mb-2">Check-in Date</label><DatePickermode="single"date={checkIn}setDate={setCheckIn}/></div><div><label className="block text-sm font-medium mb-2">Check-out Date</label><DatePickermode="single"date={checkOut}setDate={setCheckOut}/></div></div>
const [task, setTask] = useState({title: '',description: '',dueDate: undefined as Date | undefined,priority: 'medium'});<div className="space-y-4"><div><label className="block text-sm font-medium mb-2">Task Title</label><inputtype="text"value={task.title}onChange={(e) => setTask(prev => ({ ...prev, title: e.target.value }))}className="w-full border rounded px-3 py-2"/></div><div><label className="block text-sm font-medium mb-2">Due Date</label><DatePickermode="single"date={task.dueDate}setDate={(date) => setTask(prev => ({ ...prev, dueDate: date }))}/></div></div>
const [appointment, setAppointment] = useState({patientName: '',doctor: '',date: undefined as Date | undefined,time: '',notes: ''});<div className="space-y-4"><div className="grid grid-cols-2 gap-4"><div><label className="block text-sm font-medium mb-2">Patient Name</label><inputtype="text"value={appointment.patientName}onChange={(e) => setAppointment(prev => ({ ...prev, patientName: e.target.value }))}className="w-full border rounded px-3 py-2"/></div><div><label className="block text-sm font-medium mb-2">Doctor</label><selectvalue={appointment.doctor}onChange={(e) => setAppointment(prev => ({ ...prev, doctor: e.target.value }))}className="w-full border rounded px-3 py-2"><option value="">Select Doctor</option><option value="dr-smith">Dr. Smith</option><option value="dr-jones">Dr. Jones</option></select></div></div><div><label className="block text-sm font-medium mb-2">Appointment Date</label><DatePickermode="single"date={appointment.date}setDate={(date) => setAppointment(prev => ({ ...prev, date }))}/></div></div>
Use clear and descriptive labels for date pickers.
// ✅ Good - Clear labels<div><label className="block text-sm font-medium mb-2">Event Date</label><DatePicker mode="single" date={date} setDate={setDate} /></div>// ❌ Avoid - Unclear labels<div><label>Date</label><DatePicker mode="single" date={date} setDate={setDate} /></div>
Set appropriate date ranges for different contexts.
// ✅ Good - Appropriate date range<DatePickermode="single"date={date}setDate={setDate}disabled={(date) => date < new Date()}/>// ❌ Avoid - No date restrictions<DatePickermode="single"date={date}setDate={setDate}/>
Use consistent date formatting across your application.
// ✅ Good - Consistent formattingconst formatDate = (date: Date) => {return format(date, 'MMM dd, yyyy');};// ❌ Avoid - Inconsistent formattingconst formatDate = (date: Date) => {return date.toLocaleDateString();};
Ensure date pickers are accessible to all users.
// ✅ Good - Accessible<DatePickermode="single"date={date}setDate={setDate}aria-label="Select event date"/>
Handle date validation and errors gracefully.
// ✅ Good - Error handlingconst [error, setError] = useState('');const handleDateChange = (date: Date | undefined) => {if (date && date < new Date()) {setError('Date cannot be in the past');return;}setError('');setDate(date);};
You can customize the date picker appearance using CSS classes:
<DatePickermode="single"date={date}setDate={setDate}className="border-2 border-purple-500 rounded-xl"/>
Use different themes for different contexts:
// Light theme<DatePickermode="single"date={date}setDate={setDate}className="bg-white border-gray-200"/>// Dark theme<DatePickermode="single"date={date}setDate={setDate}className="bg-gray-900 border-gray-700 text-white"/>
Use different sizes for different emphasis levels:
// Small date picker<DatePickermode="single"date={date}setDate={setDate}className="w-48"/>// Large date picker<DatePickermode="single"date={date}setDate={setDate}className="w-80"/>
Implement date validation:
const [date, setDate] = useState<Date | undefined>();const [error, setError] = useState('');const validateDate = (date: Date | undefined) => {if (!date) {setError('Date is required');return false;}if (date < new Date()) {setError('Date cannot be in the past');return false;}if (date > addYears(new Date(), 1)) {setError('Date cannot be more than 1 year in the future');return false;}setError('');return true;};const handleDateChange = (newDate: Date | undefined) => {if (validateDate(newDate)) {setDate(newDate);}};
Implement date range validation:
const [dateRange, setDateRange] = useState<DateRange | undefined>();const [error, setError] = useState('');const validateDateRange = (range: DateRange | undefined) => {if (!range?.from) {setError('Start date is required');return false;}if (!range?.to) {setError('End date is required');return false;}if (range.from >= range.to) {setError('End date must be after start date');return false;}const daysDiff = differenceInDays(range.to, range.from);if (daysDiff > 30) {setError('Date range cannot exceed 30 days');return false;}setError('');return true;};
Implement custom date formatting:
const formatDate = (date: Date | undefined) => {if (!date) return 'Select a date';return format(date, 'EEEE, MMMM do, yyyy');};<DatePickermode="single"date={date}setDate={setDate}formatDate={formatDate}/>
Combine date picker with time selection:
const [dateTime, setDateTime] = useState<Date | undefined>();const handleDateTimeChange = (date: Date | undefined, time: string) => {if (date && time) {const [hours, minutes] = time.split(':');const newDateTime = new Date(date);newDateTime.setHours(parseInt(hours), parseInt(minutes));setDateTime(newDateTime);}};<div className="space-y-4"><div><label className="block text-sm font-medium mb-2">Date</label><DatePickermode="single"date={dateTime}setDate={(date) => handleDateTimeChange(date, time)}/></div><div><label className="block text-sm font-medium mb-2">Time</label><inputtype="time"value={time}onChange={(e) => handleDateTimeChange(dateTime, e.target.value)}className="w-full border rounded px-3 py-2"/></div></div>
- Calendar: For calendar display
- Popover: For popup content
- Button: For trigger button
- Input: For text input
- Form: For form integration
For the complete API reference and advanced usage patterns, see the DatePicker API documentation.
