DateRangeInput
Input component for selecting a date range with calendar popup
DateRangeInput
An input component for selecting date ranges. Features segmented date input fields, a floating calendar with dual-month display, and smart date validation.
Installation
import { DateRangeInput } from '@geckoui/geckoui';Basic Usage
DD/MM/YYYY - DD/MM/YYYY
// - //
'use client';
import { useState } from 'react';
import { DateRangeInput } from '@geckoui/geckoui';
import type { DateRange } from '@geckoui/geckoui';
function Example() {
const [value, setValue] = useState<DateRange | undefined>();
return (
<DateRangeInput value={value} onChange={(v) => setValue(v ?? undefined)} />
);
}Props API
| Prop | Type | Default | Description |
|---|---|---|---|
value | DateRange | - | Selected date range with from and to properties (ISO format: YYYY-MM-DD) |
onChange | (value: DateRange | null) => void | - | Callback when date range changes |
format | 'DD/MM/YYYY' | 'MM/DD/YYYY' | 'YYYY/MM/DD' | 'DD/MM/YYYY' | Display format for date segments |
separator | string | '/' | Separator between date segments |
rangeSeparator | string | ' - ' | Separator between from and to dates |
numberOfMonths | 1 | 2 | 2 | Number of months to display in calendar |
calendarPlacement | Placement | 'bottom-start' | Calendar popup position (from @floating-ui/react) |
floatingStrategy | 'absolute' | 'fixed' | 'absolute' | Floating UI positioning strategy |
disabled | boolean | false | Disable the input |
readOnly | boolean | false | Make the input read-only |
hasError | boolean | false | Show error styling |
hideCalendar | boolean | false | Hide the calendar popup |
hideCalendarIcon | boolean | false | Hide the calendar icon |
hideClearIcon | boolean | false | Hide the clear button |
placeholder | string | - | Custom placeholder text |
placeholderClassName | string | - | CSS class for placeholder |
prefix | FC | ReactNode | - | Element before the input |
suffix | FC | ReactNode | - | Element after the input |
className | string | - | CSS class for input container |
wrapperClassName | string | - | CSS class for wrapper div |
calendarClassName | string | - | CSS class for calendar container |
onSubmit | () => void | - | Callback on Enter/Space key |
onStateUpdate | (state) => void | - | Callback when input segments change |
DateRange Type
interface DateRange {
from: string | null; // ISO format: 'YYYY-MM-DD'
to?: string | null; // ISO format: 'YYYY-MM-DD'
}Examples
With Default Value
Pre-populate the input with a date range:
DD/MM/YYYY - DD/MM/YYYY
// - //
Selected: 2024-06-01 - 2024-06-15
const [value, setValue] = useState<DateRange | undefined>({
from: '2024-06-01',
to: '2024-06-15',
});
<DateRangeInput value={value} onChange={(v) => setValue(v ?? undefined)} />Date Formats
Choose between different date display formats:
DD/MM/YYYY (default)
DD/MM/YYYY - DD/MM/YYYY
// - //
MM/DD/YYYY
MM/DD/YYYY - MM/DD/YYYY
// - //
YYYY/MM/DD
YYYY/MM/DD - YYYY/MM/DD
// - //
<DateRangeInput format="DD/MM/YYYY" /> // 15/06/2024 - 30/06/2024
<DateRangeInput format="MM/DD/YYYY" /> // 06/15/2024 - 06/30/2024
<DateRangeInput format="YYYY/MM/DD" /> // 2024/06/15 - 2024/06/30Number of Months
Display one or two months in the calendar:
Single month
DD/MM/YYYY - DD/MM/YYYY
// - //
Dual months (default)
DD/MM/YYYY - DD/MM/YYYY
// - //
<DateRangeInput numberOfMonths={1} /> // Single month view
<DateRangeInput numberOfMonths={2} /> // Dual month view (default)Disabled and Read-only
Disabled
DD/MM/YYYY - DD/MM/YYYY
// - //
Read-only
DD/MM/YYYY - DD/MM/YYYY
// - //
<DateRangeInput value={value} disabled />
<DateRangeInput value={value} readOnly />Error State
DD/MM/YYYY - DD/MM/YYYY
// - //
Please select a valid date range
<DateRangeInput value={value} onChange={setValue} hasError />Styling
The component uses BEM-style class names:
GeckoUIDateRangeInputWrapper- Wrapper containerGeckoUIDateInput- Main input containerGeckoUIDateInput--disabled- Disabled stateGeckoUIDateInput--readOnly- Read-only stateGeckoUIDateInput--error- Error stateGeckoUIDateInput--focus- Focused stateGeckoUIDateInput--calendar-open- Calendar is openGeckoUIDateInput__segment- Date segment inputGeckoUIDateInput__separator- Separator between segments (/)GeckoUIDateRangeInput__range-separator- Separator between dates (-)GeckoUIDateRangeInput__calendar- Calendar containerGeckoUIDateInput__calendar-icon- Calendar iconGeckoUIDateInput__clear-button- Clear buttonGeckoUIDateInput__prefix- Prefix containerGeckoUIDateInput__suffix- Suffix container
Accessibility
- Segmented inputs for fine-grained keyboard control
- Tab navigation between date segments
- Arrow keys to move between segments
- Enter/Space to open calendar and submit
- Click outside to close calendar
- Clear button for resetting selection
Related Components
- Calendar - Standalone calendar component
- DateInput - Single date input with calendar
- RHFDateInput - React Hook Form date input