Skip to main content

Date & Time Input Components

Date and Time input components provide comprehensive date selection and time input functionality built on top of react-datepicker and integrated with React Hook Form for validation and form management.

Component Overview

Core Components

Version Support

The components support multiple versions:

  • V2 - Updated components with improved styling
  • Legacy - Original components with basic functionality

WPDatepicker

Basic date picker component with validation and form integration.

Component Location

import { WPDatepicker } from 'components/Forms/WPDatepicker';

Props

PropTypeDefaultDescription
namestring-Field name for form registration
labelstring-Input label text
controlControl-React Hook Form control object
rulesRegisterOptions-Validation rules
defaultValueDate-Default date value
isRequiredbooleanfalseWhether field is required
isLoadingbooleanfalseLoading state
isClearablebooleanfalseWhether date can be cleared
helperTextstring-Helper text to display
popperModifiersbooleanfalseEnable popper modifiers
errorobject-Error object from validation

Usage Examples

Basic Date Picker

import { WPDatepicker } from 'components/Forms/WPDatepicker';
import { useForm } from 'react-hook-form';

function DateForm() {
const { control } = useForm();

return (
<WPDatepicker
name='birthDate'
label='Birth Date'
control={control}
rules={{
required: 'Birth date is required',
}}
isClearable
/>
);
}

Date Picker with Custom Validation

import { WPDatepicker } from 'components/Forms/WPDatepicker';

function CustomDateForm() {
const { control } = useForm();

return (
<WPDatepicker
name='startDate'
label='Start Date'
control={control}
rules={{
required: 'Start date is required',
validate: value => {
const today = new Date();
return value >= today || 'Start date must be today or later';
},
}}
helperText='Select a start date for the project'
popperModifiers
/>
);
}

Date Picker with Default Value

import { WPDatepicker } from 'components/Forms/WPDatepicker';

function DefaultDateForm() {
const { control } = useForm();

return (
<WPDatepicker
name='dueDate'
label='Due Date'
control={control}
defaultValue={new Date()}
rules={{
required: 'Due date is required',
}}
/>
);
}

WPDateRangePicker

Date range selection component for selecting start and end dates.

Component Location

import WPDateRangePicker from 'components/DatePicker/WPDatePickers';
import V2WPDateRangePicker from 'components/DatePicker/V2WPDatePickers'; // V2 version

Props

PropTypeDefaultDescription
startDateDatenullStart date value
endDateDatenullEnd date value
onChangefunction-Change handler for date range
placeholderTextstring'Filter by date'Placeholder text
highlightbooleanfalseWhether to highlight the input
onResetfunction-Reset handler
popperModifiersbooleanfalseEnable popper modifiers
minimalbooleanfalseUse minimal styling
minimalStylesobject{}Custom minimal styles
monthRangebooleanfalseShow month range format

Usage Examples

Basic Date Range Picker

import WPDateRangePicker from 'components/DatePicker/WPDatePickers';
import { useState } from 'react';

function DateRangeForm() {
const [startDate, setStartDate] = useState(null);
const [endDate, setEndDate] = useState(null);

const handleDateChange = dates => {
const [start, end] = dates;
setStartDate(start);
setEndDate(end);
};

return (
<WPDateRangePicker
startDate={startDate}
endDate={endDate}
onChange={handleDateChange}
placeholderText='Select date range'
/>
);
}

Date Range Picker with Reset

import WPDateRangePicker from 'components/DatePicker/WPDatePickers';

function DateRangeWithReset() {
const [startDate, setStartDate] = useState(null);
const [endDate, setEndDate] = useState(null);

const handleDateChange = dates => {
const [start, end] = dates;
setStartDate(start);
setEndDate(end);
};

const handleReset = () => {
setStartDate(null);
setEndDate(null);
};

return (
<WPDateRangePicker
startDate={startDate}
endDate={endDate}
onChange={handleDateChange}
onReset={handleReset}
placeholderText='Select date range'
highlight={startDate && endDate}
/>
);
}

Minimal Date Range Picker

import WPDateRangePicker from 'components/DatePicker/WPDatePickers';

function MinimalDateRange() {
const [startDate, setStartDate] = useState(null);
const [endDate, setEndDate] = useState(null);

return (
<WPDateRangePicker
startDate={startDate}
endDate={endDate}
onChange={([start, end]) => {
setStartDate(start);
setEndDate(end);
}}
minimal
minimalStyles={{
fontSize: '12px',
padding: '4px 8px',
}}
/>
);
}

WPMonthPicker

Month selection component for selecting months and years.

Component Location

import { WPMonthPicker } from 'components/DatePicker/WPDatePickers';
import { V2WPMonthPicker } from 'components/DatePicker/V2WPDatePickers'; // V2 version

Props

PropTypeDefaultDescription
selectedDateDate-Selected date value
onChangefunction-Change handler
placeholderstring'Select Month'Placeholder text
sizestring'md'Input size
inputHeightstring-Custom input height

Usage Examples

Basic Month Picker

import { WPMonthPicker } from 'components/DatePicker/WPDatePickers';
import { useState } from 'react';

function MonthForm() {
const [selectedMonth, setSelectedMonth] = useState(new Date());

return (
<WPMonthPicker
selectedDate={selectedMonth}
onChange={setSelectedMonth}
placeholder='Select a month'
/>
);
}

Month Picker with Custom Size

import { WPMonthPicker } from 'components/DatePicker/WPDatePickers';

function CustomMonthPicker() {
const [selectedMonth, setSelectedMonth] = useState(new Date());

return (
<WPMonthPicker
selectedDate={selectedMonth}
onChange={setSelectedMonth}
size='lg'
inputHeight='48px'
placeholder='Select month and year'
/>
);
}

WPBareDatePicker

Bare date picker component for quarter and custom date selections.

Component Location

import { WPBareDatePicker } from 'components/DatePicker/WPDatePickers';
import { V2WPBareDatePicker } from 'components/DatePicker/V2WPDatePickers'; // V2 version

Props

PropTypeDefaultDescription
selectedDateDate-Selected date value
onChangefunction-Change handler
dateFormatstring-Custom date format
sizestring'md'Input size

Usage Examples

Basic Bare Date Picker

import { WPBareDatePicker } from 'components/DatePicker/WPDatePickers';
import { useState } from 'react';

function BareDateForm() {
const [selectedDate, setSelectedDate] = useState(new Date());

return (
<WPBareDatePicker
selectedDate={selectedDate}
onChange={setSelectedDate}
dateFormat='yyyy-MM-dd'
/>
);
}

Quarter Picker

import { WPBareDatePicker } from 'components/DatePicker/WPDatePickers';

function QuarterPicker() {
const [selectedDate, setSelectedDate] = useState(new Date());

return (
<WPBareDatePicker
selectedDate={selectedDate}
onChange={setSelectedDate}
dateFormat='QQQ yyyy'
showQuarterYearPicker
/>
);
}

CustomInput

Custom input component used internally by date pickers for consistent styling.

Component Location

import { CustomInput } from 'components/DatePicker/CustomDatePicker';
import { CustomInput } from 'components/DatePicker/V2CustomDatePicker'; // V2 version

Props

PropTypeDefaultDescription
valuestring-Input value
onClickfunction-Click handler
type'date' | 'clock''date'Input type for icon
placeholderstring-Placeholder text
highlightbooleanfalseWhether to highlight input
errorobject-Error object
isClearablebooleanfalseWhether input is clearable
widthstring'full'Input width

Usage Examples

Basic Custom Input

import { CustomInput } from 'components/DatePicker/CustomDatePicker';
import DatePicker from 'react-datepicker';

function CustomDateInput() {
const [selectedDate, setSelectedDate] = useState(new Date());

return (
<DatePicker
selected={selectedDate}
onChange={setSelectedDate}
customInput={
<CustomInput placeholder='Select date' highlight={!!selectedDate} />
}
/>
);
}

Time Input with Clock Icon

import { CustomInput } from 'components/DatePicker/CustomDatePicker';
import DatePicker from 'react-datepicker';

function TimeInput() {
const [selectedTime, setSelectedTime] = useState(new Date());

return (
<DatePicker
selected={selectedTime}
onChange={setSelectedTime}
showTimeSelect
showTimeSelectOnly
timeIntervals={15}
timeCaption='Time'
dateFormat='h:mm aa'
customInput={<CustomInput type='clock' placeholder='Select time' />}
/>
);
}

DateWrapper

Styled wrapper component that provides consistent styling for all date picker components.

Component Location

import { DateWrapper } from 'components/DatePicker/CustomDatePicker';
import { DateWrapper } from 'components/DatePicker/V2CustomDatePicker'; // V2 version
import { DateWrapperForDrawer } from 'components/DatePicker/CustomDatePicker'; // For modal usage

Usage Examples

Basic Date Wrapper

import { DateWrapper } from 'components/DatePicker/CustomDatePicker';
import DatePicker from 'react-datepicker';

function WrappedDatePicker() {
const [selectedDate, setSelectedDate] = useState(new Date());

return (
&lt;DateWrapper&gt;
<DatePicker
selected={selectedDate}
onChange={setSelectedDate}
customInput={<CustomInput />}
/>
</DateWrapper>
);
}

Date Wrapper for Drawer/Modal

import { DateWrapperForDrawer } from 'components/DatePicker/CustomDatePicker';
import DatePicker from 'react-datepicker';

function ModalDatePicker() {
const [selectedDate, setSelectedDate] = useState(new Date());

return (
&lt;DateWrapperForDrawer&gt;
<DatePicker
selected={selectedDate}
onChange={setSelectedDate}
customInput={<CustomInput />}
withPortal
/>
</DateWrapperForDrawer>
);
}

Form Integration

With React Hook Form

import { WPDatepicker } from 'components/Forms/WPDatepicker';
import { useForm } from 'react-hook-form';

function DateForm() {
const { control, handleSubmit, errors } = useForm();

const onSubmit = data => {
console.log(data);
};

return (
<form onSubmit={handleSubmit(onSubmit)}>
<WPDatepicker
name='eventDate'
label='Event Date'
control={control}
rules={{
required: 'Event date is required',
validate: value => {
const today = new Date();
return value >= today || 'Event date must be in the future';
},
}}
helperText='Select the date for your event'
/>

<button type='submit'>Submit</button>
</form>
);
}

Date Range Form

import WPDateRangePicker from 'components/DatePicker/WPDatePickers';
import { useForm } from 'react-hook-form';

function DateRangeForm() {
const { register, handleSubmit, setValue, watch } = useForm();
const startDate = watch('startDate');
const endDate = watch('endDate');

const handleDateChange = dates => {
const [start, end] = dates;
setValue('startDate', start);
setValue('endDate', end);
};

return (
<form onSubmit={handleSubmit(console.log)}>
<WPDateRangePicker
startDate={startDate}
endDate={endDate}
onChange={handleDateChange}
placeholderText='Select date range'
/>

<button type='submit'>Submit</button>
</form>
);
}

Validation Patterns

Common Validation Rules

const dateValidationRules = {
required: 'Date is required',

// Future date validation
futureDate: value => {
const today = new Date();
return value >= today || 'Date must be in the future';
},

// Past date validation
pastDate: value => {
const today = new Date();
return value <= today || 'Date must be in the past';
},

// Date range validation
dateRange: (value, { startDate }) => {
if (!startDate) return true;
return value >= startDate || 'End date must be after start date';
},

// Business days validation
businessDay: value => {
const day = value.getDay();
return (day !== 0 && day !== 6) || 'Please select a weekday';
},
};

Custom Validation Example

import { WPDatepicker } from 'components/Forms/WPDatepicker';

function ValidationExample() {
const { control } = useForm();

return (
<WPDatepicker
name='projectDeadline'
label='Project Deadline'
control={control}
rules={{
required: 'Deadline is required',
validate: {
futureDate: value => {
const today = new Date();
return value >= today || 'Deadline must be in the future';
},
businessDay: value => {
const day = value.getDay();
return (day !== 0 && day !== 6) || 'Please select a weekday';
},
withinRange: value => {
const maxDate = new Date();
maxDate.setFullYear(maxDate.getFullYear() + 2);
return value <= maxDate || 'Deadline must be within 2 years';
},
},
}}
/>
);
}

Styling Customization

Custom Styling

import styled from '@emotion/styled';
import { DateWrapper } from 'components/DatePicker/CustomDatePicker';

const CustomDateWrapper = styled(DateWrapper)`
.react-datepicker {
border: 2px solid #62a446;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.react-datepicker__day--selected {
background-color: #62a446;
color: white;
}

.react-datepicker__header {
background-color: #f8f9fa;
border-bottom: 1px solid #dee2e6;
}
`;

Theme Integration

import { useTheme } from '@chakra-ui/react';
import { DateWrapper } from 'components/DatePicker/CustomDatePicker';

function ThemedDatePicker() {
const theme = useTheme();

return (
&lt;DateWrapper&gt;
<DatePicker
selected={selectedDate}
onChange={setSelectedDate}
customInput={<CustomInput highlight={!!selectedDate} />}
/>
</DateWrapper>
);
}

Accessibility Features

  • Keyboard Navigation: Full keyboard support for date selection
  • Screen Reader Support: Proper ARIA labels and descriptions
  • Focus Management: Logical focus order within calendar
  • Date Announcements: Screen readers announce selected dates
  • High Contrast: Support for high contrast themes

Performance Optimization

Lazy Loading

import { lazy, Suspense } from 'react';
import { Spinner } from '@chakra-ui/react';

const WPDatepicker = lazy(() => import('components/Forms/WPDatepicker'));

function LazyDatePicker() {
return (
<Suspense fallback={<Spinner />}>
<WPDatepicker name='date' label='Date' control={control} />
</Suspense>
);
}

Memoization

import { memo } from 'react';
import { WPDatepicker } from 'components/Forms/WPDatepicker';

const MemoizedDatePicker = memo(WPDatepicker);

function OptimizedForm() {
return <MemoizedDatePicker name='date' label='Date' control={control} />;
}

Best Practices

  1. Validation: Always validate date inputs on both client and server
  2. Accessibility: Provide clear labels and help text
  3. Error Handling: Display clear error messages for invalid dates
  4. Performance: Use memoization for complex date calculations
  5. Time Zones: Consider time zone implications for date values
  6. Format Consistency: Use consistent date formats across the application
  7. Mobile Experience: Ensure date pickers work well on mobile devices

Migration Notes

From Legacy to V2

  1. Styling: V2 components have updated styling and theming
  2. Props: Some prop names may have changed
  3. Validation: Improved validation integration
  4. Performance: Better performance optimizations

Common Migration Steps

  1. Update import paths to V2 components
  2. Review and update prop usage
  3. Test date validation and error handling
  4. Verify styling and theming integration