Overview
TheDatePickerElement component provides an interface for users to select due dates for payments. It wraps the react-datepicker library with custom positioning and event handling tailored for the Pagosapp use case.
Features
- Controlled Date Selection: Integrates with parent component state
- Calendar Overlay: Positioned absolutely for overlay display
- Smart Event Handling: Manages calendar open/close states
- Click Outside Detection: Automatically closes when clicking outside
- Custom Styling: Uses dedicated DatePicker CSS
Location
Props
The currently selected date (controlled component)
- Can be
nullif no date is set - Should be a JavaScript Date object
- Displayed as the selected date in the calendar
Callback function invoked when a date is selectedSignature:Typically updates state and persists to localStorage via the
usePago hook.Controls whether the date picker calendar is visible
true: Calendar is displayedfalse: Calendar is hidden
State setter for controlling calendar visibilitySignature:Called by calendar lifecycle events (open, close, click outside).
Usage Example
Basic Implementation
Integration with usePago Hook
As used in thePago component:
Component Structure
The component renders a positioned wrapper containing the DatePicker:Positioning
The wrapper usesposition: absolute to overlay the calendar on top of other content. Parent components should provide appropriate positioning context.
Typical Parent Structure
The parent div should use
position: relative to establish a positioning context for the absolute-positioned DatePicker.Event Handling
Calendar Lifecycle Events
The component manages three calendar events:| Event | Handler | Purpose |
|---|---|---|
onCalendarClose | setOpenDatePicker(false) | Sync state when calendar closes |
onCalendarOpen | setOpenDatePicker(true) | Sync state when calendar opens |
onClickOutside | setOpenDatePicker(false) | Close calendar on outside click |
Date Selection
When a date is selected:onChangeis triggered with the selected Date objecthandleDateChange(date)is called- Parent component updates state (typically also saves to localStorage)
- Calendar automatically closes via
handleDateChangeimplementation
Custom Input
The component uses an invisible custom input:Dependencies
react-datepicker- Core date picker functionality- Custom styles from
../styles/DatePicker.css
Installing react-datepicker
Styling
The component imports custom styles from../styles/DatePicker.css. This file should contain:
- Calendar appearance customization
- Day/week styling
- Header styling
- Selected date highlighting
- Hover effects
Date Persistence
In the Pagosapp context, date changes are persisted via theusePago hook:
Click Event Propagation
When used inside clickable containers (like thePago component’s list item), wrap the DatePickerElement to prevent unwanted event propagation:
Accessibility Considerations
- The parent trigger button should include proper ARIA labels
- Keyboard navigation is handled by the underlying
react-datepickerlibrary - Screen readers can navigate the calendar using arrow keys
Accessible Trigger Button
Related Components
- Pago Component - Primary consumer of DatePickerElement
- SwitchElement - Complementary control in Pago
react-datepicker Props Reference
For additional customization options, see the react-datepicker documentation. Common additional props you might want to add:dateFormat- Custom date display formatminDate/maxDate- Restrict selectable date rangeshowTimeSelect- Enable time selectioninline- Display calendar inline instead of popuplocale- Localization support
Source Code Reference
Component implementation atsrc/components/DatePickerElement.jsx:5-19
Absolute positioning at src/components/DatePickerElement.jsx:7