useDateField

Provides the behavior and accessibility implementation for a date field component. A date field allows users to enter and edit date and time values using a keyboard. Each part of a date value is displayed in an individually editable segment.

mmddyyyy
Example
DateField.tsx
DateField.css
Form.css
import {DateField} from './DateField';

<DateField label="Event date" />

API

Shows a date field with labels pointing to its parts, including the label, field, and segments.Event date8 / 15 / 2022SegmentFieldLabel
<DateField>
  <Label />
  <DateInput>
    {segment => <DateSegment segment={segment} />}
  </DateInput>
  <Text slot="description" />
  <FieldError />
</DateField>
useDateFieldState<T extends = >(props: <T>): useDateField<T extends >( props: <T>, state: , ref: <Elementnull> ): useDateSegment( segment: , state: , ref: <HTMLElementnull> ):

DateFieldState

Properties

NameType
dateValueDate

The current value, converted to a native JavaScript Date object.

calendar

The calendar system currently in use.

segments[]

A list of segments for the current value.

dateFormatter

A date formatter configured for the current locale and format.

granularity

The granularity for the field, based on the granularity prop and current value.

maxGranularity'year''month'

The maximum date or time unit that is displayed in the field.

isDisabledboolean

Whether the field is disabled.

isReadOnlyboolean

Whether the field is read only.

realtimeValidation

Realtime validation results, updated as the user edits the value.

displayValidation

Currently displayed validation results, updated when the user commits their changes.

valuenull

The current field value.

defaultValuenull

The default field value.

Methods

setValue(value: null): void
Sets the field's value.
increment(type: ): void
Increments the given segment. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
decrement(type: ): void
Decrements the given segment. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
incrementPage(type: ): void
Increments the given segment by a larger amount, rounding it to the nearest increment. The amount to increment by depends on the field, for example 15 minutes, 7 days, and 5 years. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
decrementPage(type: ): void
Decrements the given segment by a larger amount, rounding it to the nearest increment. The amount to decrement by depends on the field, for example 15 minutes, 7 days, and 5 years. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
incrementToMax(type: ): void
Increments the given segment to its maxiumum value.
decrementToMin(type: ): void
Decrements the given segment to its minimum value.
setSegment(type: , value: number): void
Sets the value of the given segment.
confirmPlaceholder(): void
Updates the remaining unfilled segments with the placeholder value.
clearSegment(type: ): void
Clears the value of the given segment, reverting it to the placeholder.
formatValue(fieldOptions: ): string
Formats the current date value using the given options.
getDateFormatter(locale: string, formatOptions: ):
Gets a formatter based on state's props.
updateValidation(result: ): 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.

AriaDateFieldProps

NameTypeDefault
isDateUnavailable(date: ) => booleanDefault:

Callback that is called for each date of the calendar. If it returns true, then the date is unavailable.

placeholderValuenullDefault:

A placeholder date that influences the format of the placeholder shown when no value is selected. Defaults to today's date at midnight.

hourCycle1224Default:

Whether to display the time in 12 or 24 hour format. By default, this is determined by the user's locale.

granularityDefault:

Determines the smallest unit that is displayed in the date picker. By default, this is "day" for dates, and "minute" for times.

hideTimeZonebooleanDefault: false

Whether to hide the time zone abbreviation.

shouldForceLeadingZerosbooleanDefault:

Whether to always show leading zeros in the month, day, and hour fields. By default, this is determined by the user's locale.

isDisabledbooleanDefault:

Whether the input is disabled.

isReadOnlybooleanDefault:

Whether the input can be selected but not changed by the user.

valuenullDefault:

The current value (controlled).

defaultValuenullDefault:

The default value (uncontrolled).

onChange(value: <>null) => voidDefault:

Handler that is called when the value changes.

DateFieldAria

NameType
labelPropsDOMAttributes

Props for the field's visible label element, if any.

fieldProps

Props for the field grouping element.

inputPropsInputHTMLAttributes<HTMLInputElement>

Props for the hidden input element for HTML form submission.

descriptionPropsDOMAttributes

Props for the description element, if any.

errorMessagePropsDOMAttributes

Props for the error message element, if any.

validationDetailsValidityState

The native validation details for the input.