No results found

Date Input

A customizable date entry field that allows users to input dates in various formats.

My date input
//

Selected date:

Note: This component relies on field-sizing: content; to set the width of the inputs based on the content of the input or its placeholder. This is supported in Chromium-based browsers, but may not work properly in other browsers. If you need to support other or older browsers, you can manage the width of the inputs in your own CSS.

Features

  • Date and time segment UI for structured input
  • Keyboard tab and arrow navigation between segments
  • Composable format (mm/dd/yyyy, dd.mm.yyyy, yyyy-mm-dd, etc.)
  • Automatic segment validation and constraints
  • Dynamic date value construction from segments
  • Smart segment focus management
  • Month-aware day validation (adjusts max days based on month/year)
  • Placeholder text and visual state handling
  • Accessibility features including ARIA attributes
  • Form integration with hidden input

Examples

Basic Usage

The Date Input component provides a structured way to enter dates with separate segments for year, month, and day. The basic implementation includes a root component that manages state, a label for accessibility, date entry container, and individual segments.

Enter your date of birth:
--

In this example:

Visual Features

Custom Date Formats

You can customize the date format by arranging the segments in any order and using different separators.

European-style date entry:
..

This example shows a European-style date format (DD.MM.YYYY) with dots as separators. The showLeadingZero prop ensures single-digit values are displayed with a leading zero.

Leading Zeros

The showLeadingZero prop can be applied to month or day segments to ensure consistent formatting with leading zeros for single-digit values.

Choose your date:
//

Selected date: 2021-01-01

In this example, both the month and day segments display leading zeros, creating a consistent visual appearance.

Separators

The Entry component accepts a separator prop, which then gets automatically inserted between the segments of the input.

<DateInput.Entry separator="/">
  <DateInput.Month />
  <DateInput.Day />
  <DateInput.Year />
</DateInput.Entry>

You may instead use normal markup to insert separators between the segments.

<DateInput.Root>
  <DateInput.Label>Enter your date of birth:</DateInput.Label>
  <DateInput.Entry>
    <DateInput.Year />
    <span>-</span>
    <DateInput.Month />
    <span>-</span>
    <DateInput.Day />
  </DateInput.Entry>
</DateInput.Root>

Example using a custom separator icon inserted via the separator prop:

When's your next pickleball match?

Advanced Usage

Form Integration

The Date Input can be integrated with forms using the <DateInput.HiddenInput> component.

Appointment Date
//

The name attribute on the <DateInput.HiddenInput> component ensures the date value is included when the form is submitted.

Reactive Binding

You can bind the date value to a signal for two-way data binding.

Party like it's:
//

Bound date: 1999-12-31

This example demonstrates binding the date to a signal with bind:date. Changes made in the input are reflected in the external state, and external changes update the input.

Value-Based One-Way Data Binding

For more granular control, you can use the Date Input with date and onChange$ props.

My date input
//

Selected date:

Times changed: 0

The onChange$ handler is called whenever the date changes, allowing you to track changes and update external state.

Observing Changes at the Root Level

You can also use the onChange$ handler on the <DateInput.Root> component to observe changes at the root level. The event fires whenever any date changes in the Date Input context, providing the date values as an array. This can be useful when you have two or more Date Entry components under the same root.

I'm thinking of a date. I'll give you two guesses, and tell you which is closer.

Two guesses
--
||
--
Dates: []
Result:

Disabled State

The Date Input can be disabled to prevent user interaction.

Permanently Disabled
//

When the disabled prop is set to true, all segments become non-editable while still displaying their values.

Toggling Disabled State

You can dynamically toggle the disabled state of the Date Input.

My date input
//

This example shows how to toggle the disabled state using a signal, allowing you to enable or disable the input based on application logic.

Component State

Using Component State

The Date Input component provides several ways to control its state through props.

Initial Values

When no initial value is provided, all segments will display their placeholder text.

To set an initial date value, use the date prop on the <DateInput.Root> component. The simplest way to provide an initial value is to provide a string value in ISO format (YYYY-MM-DD).

<DateInput.Root date="2021-01-01">
  {/* ... */}
</DateInput.Root>

Controlling the Date Value

You can also use a variable such that any updates to your date value will be reflected in the input.

<DateInput.Root  date={selectedDate.value} >
  {/* ... */}
</DateInput.Root>

Alternatively, you can supply a Signal to the bind:date prop to create a two-way binding, where changes in the input are automatically reflected in the external state, and vice versa.

<DateInput.Root bind:date={selectedDate}>
  {/* ... */}
</DateInput.Root>

Disabled State

To prevent user interaction with the Date Input, use the disabled prop:

<DateInput.Root disabled={true}>
  {/* ... */}
</DateInput.Root>

The Disabled example demonstrates this feature, showing a Date Input with a fixed date that users cannot modify.

State Interactions

Responding to Date Changes

The Date Input provides an onChange$ handler that is called whenever the date changes, allowing you to respond to user input:

const handleChange$ = $((date: DateInput.ISODate | null) => {
  // Update external state or perform actions based on the new date
  selectedDate.value = date;
});

As shown in the Change example, you can use this handler to track changes and update external state.

Clearing the Date Value

You can clear the date value by setting it to null, which will reset all segments to their placeholder state:

<button onClick$={() => (selectedDate.value = null)}>
  Clear
</button>

This is demonstrated in both the Reactive and Value-Based examples, where a button is provided to clear the date.

Configuration Options

Date Format Configuration

Custom Date Formats

The Date Input component allows you to customize the date format by arranging the segments in any order and using different separators. This flexibility enables you to create date formats that match regional preferences or specific application requirements. As shown in the Format example above, you can create a European-style date format (DD.MM.YYYY) by arranging the segments in day-month-year order and using dots as separators:

<DateInput.Entry>
  <DateInput.Day showLeadingZero={true} />
  <DateInput.Separator separator="." />
  <DateInput.Month showLeadingZero={true} />
  <DateInput.Separator separator="." />
  <DateInput.Year />
</DateInput.Entry>

The component supports various date formats including:

Segment Formatting

Leading Zeros

Each segment component (<DateInput.Day />, <DateInput.Month />, and <DateInput.Year />) accepts a showLeadingZero prop that controls whether single-digit values are displayed with a leading zero. This can be useful for maintaining consistent visual width across all possible values. As shown in the Value-Based example, you can apply this prop to both month and day segments:

<DateInput.Month showLeadingZero />
<DateInput.Day showLeadingZero />

When showLeadingZero is enabled:

Currently showLeadingZero has no effect on the year display.

Placeholder Text

Each segment component accepts a placeholder prop that allows you to customize the placeholder text shown when no value is entered:

<DateInput.Day placeholder="Day" />
<DateInput.Month placeholder="Month" />
<DateInput.Year placeholder="Year" />

The default placeholder values are:

Multiple dates

The Date Input can be composed with multiple <DateInput.Entry> components to create a multi-date input. You could use multiple <DateInput.Root> components, but if you have multiple dates that are related, with a single label, you can use a single <DateInput.Root> component with multiple <DateInput.Entry> components, as in the following Date Range example:

Travel dates
--
to
--

Departure date:
Return date:

Technical Constraints

Date Range Limitations

The Date Input component enforces the following constraints on segment values:

ISO Date Format

The component uses the ISO date format (YYYY-MM-DD) for its internal value representation. When providing a date via the date prop, it must be in this format:

<DateInput.Root date="2023-04-15">
  {/* ... */}
</DateInput.Root>

If an invalid format is provided, the component will throw an error.

Keyboard Navigation

The Date Input component provides comprehensive keyboard navigation between segments:

Form Integration

The Date Input component provides seamless integration with forms through the <DateInput.HiddenInput> component.

Basic Form Integration

To use the Date Input in a form, include the <DateInput.HiddenInput> component within the <DateInput.Entry> component.

<DateInput.Root>
  {/* ... */}
  <DateInput.Entry>
    {/* ... date segments ... */}
    <DateInput.HiddenInput name="appointment-date" />
  </DateInput.Entry>
</DateInput.Root>

This creates a hidden native input field that will be included in the form data when the form is submitted.

Appointment Date
//

In this example:

Note that the <DateInput.HiddenInput> must be slotted within a <DateInput.Entry> component in order to properly access the date value from the entry's context.

Required Fields

The required attribute can be applied to make the date field mandatory in form submissions:

<DateInput.HiddenInput name="appointment-date" required={true} />

Form Value Handling

The <DateInput.HiddenInput> component automatically uses the current date value from the Date Input context. When the form is submitted, the date is included in the ISO format (YYYY-MM-DD). As shown in the Form example above, the submitted data includes the date value with the specified field name:

{ "appointment-date": "2022-02-14" }

Custom Form Handling

For more control over form submission, you can combine the Date Input with custom form handling logic:

<form
  preventdefault:submit
  onSubmit$={(e) => {
    const form = e.target as HTMLFormElement;
    const formData = Object.fromEntries(new FormData(form));
    // Process form data
  }}
>
  <DateInput.Root>
    {/* ... */}
    <DateInput.Entry>
      {/* ... date segments ... */}
      <DateInput.HiddenInput name="date-field" />
    </DateInput.Entry>
  </DateInput.Root>
  <button type="submit">Submit</button>
</form>

This approach allows you to intercept the form submission and process the data before sending it to a server or performing other actions.

Environment Considerations

The Date Input component is designed to work seamlessly in both server-side rendering (SSR) and client-side rendering (CSR) environments, as is standard for Qwik components.

API Reference

Accessibility

Keyboard Interactions

KeyDescription
ArrowUpWhen focus is on a segment, increments the value of that segment
ArrowDownWhen focus is on a segment, decrements the value of that segment
ArrowLeftMoves cursor left within the current segment or to the previous segment as appropriate
ArrowRightMoves cursor right within the current segment or to the next segment as appropriate
TabMoves focus to the next segment or element
Shift + TabMoves focus to the previous segment or element
Digits (0-9)When focus is on a segment, inputs numeric values into that segment
BackspaceWhen focus is on a segment, allows deleting characters
DeleteWhen focus is on a segment, allows deleting characters