useDatePickerState
Provides state management for a date picker component. A date picker combines a DateField and a Calendar popover to allow users to enter or select a date and time value.
install | yarn add react-stately |
---|---|
version | 3.33.0 |
usage | import {useDatePickerState} from 'react-stately' |
API#
useDatePickerState<T extends DateValue = DateValue>(
(props: DatePickerStateOptions<T>
)): DatePickerState
Interface#
Properties
Name | Type | Description |
value | DateValue | null | The currently selected date. |
dateValue | DateValue | The date portion of the value. This may be set prior to |
timeValue | TimeValue | The time portion of the value. This may be set prior to |
granularity | Granularity | The granularity for the field, based on the granularity prop and current value. |
hasTime | boolean | Whether the date picker supports selecting a time, according to the granularity prop and current value. |
isOpen | boolean | Whether the calendar popover is currently open. |
isInvalid | boolean | Whether the date picker is invalid, based on the isInvalid , minValue , and maxValue props. |
realtimeValidation | ValidationResult | Realtime validation results, updated as the user edits the value. |
displayValidation | ValidationResult | Currently displayed validation results, updated when the user commits their changes. |
Methods
Method | Description |
setValue(
(value: DateValue
| | null
)): void | Sets the selected date. |
setDateValue(
(value: CalendarDate
)): void | Sets the date portion of the value. |
setTimeValue(
(value: TimeValue
)): void | Sets the time portion of the value. |
setOpen(
(isOpen: boolean
)): void | Sets whether the calendar popover is open. |
formatValue(
(locale: string,
, fieldOptions: FieldOptions
)): string | Formats the selected value using the given options. |
getDateFormatter(
(locale: string,
, formatOptions: FormatterOptions
)): DateFormatter | Gets a formatter based on state's props. |
open(): void | Opens the overlay. |
close(): void | Closes the overlay. |
toggle(): void | Toggles the overlay's visibility. |
updateValidation(
(result: ValidationResult
)): void | Updates the current validation result. Not displayed to the user until commitValidation is called. |
resetValidation(): void | Resets the displayed validation state to valid when the user resets the form. |
commitValidation(): void | Commits the realtime validation so it is displayed to the user. |
Example#
See the docs for useDatePicker in react-aria for an example of useDatePickerState
.