Time Picker
- Usage
- Styling
- Step
- Auto Open
- Validation
- Basic Features
- Read-Only & Disabled
- Style Variants
- Best Practices
- Related Components
Time Picker is an input field for used entering or selecting a specific time.
new tab
TimePicker timePicker = new TimePicker();
timePicker.setLabel("Alarm");
timePicker.setValue(LocalTime.of(7, 0));
add(timePicker);
The time can be entered directly using a keyboard, or by choosing a value from a set of predefined options presented in an overlay. The overlay opens when the field is clicked — or when any input is entered while the field is focused.
Step
Time Picker’s step parameter defines the interval in seconds between the items displayed in the overlay. It also specifies the amount by which the time increases or decreases using the Up and Down arrow keys — when the overlay is disabled.
The default step is one hour (i.e., 3600
seconds). Unlike Number Field, Time Picker accepts values that don’t align with the specified step.
new tab
TimePicker timePicker = new TimePicker();
timePicker.setLabel("Meeting time");
timePicker.setStep(Duration.ofMinutes(30));
timePicker.setValue(LocalTime.of(12, 30));
add(timePicker);
Note
|
Use Common Steps
A step must evenly divide an hour or day. For example, "15 minutes" and "30 minutes" are valid steps for an hour, and "2 hours" is a valid step for a day, whereas "42 minutes" isn’t valid for either.
|
The displayed time format changes based on the step.
new tab
TimePicker timePicker = new TimePicker();
timePicker.setLabel("Message received");
timePicker.setStep(Duration.ofSeconds(1));
timePicker.setValue(LocalTime.of(15, 45, 8));
add(timePicker);
Step | Format |
---|---|
Less than 60 seconds. | HH:MM:SS |
Less than 1 second. | HH:MM:SS:FFF |
Note
|
Limit Number of Steps
The overlay doesn’t appear for steps less than 900 seconds (i.e., 15 minutes), to avoid showing an impractical number of choices.
|
Auto Open
The overlay opens automatically when the field is focused using a pointer (i.e., a mouse or touch), or when the user types in the field. You can disable this so that the overlay opens only when the toggle button — or the Up or Down arrow keys — is pressed.
new tab
TimePicker timePicker = new TimePicker();
timePicker.setLabel("Alarm");
timePicker.setStep(Duration.ofMinutes(30));
timePicker.setValue(LocalTime.of(5, 30));
timePicker.setAutoOpen(false);
add(timePicker);
Validation
Time Picker provides a validation mechanism based on constraints. Constraints allow you to define criteria that the time must meet to be considered valid. Validation occurs typically when the user initiates a time change, for example by selecting a time from the overlay or through text input followed by Enter. If the time is invalid, the field is highlighted in red, and an error message appears underneath the input.
Below is a list of supported constraints with more detailed information:
Bad Input
Bad input refers to any input that cannot be parsed into a value of the field type. When an unparsable value is entered, the field resets the value to empty and becomes invalid. This constraint is non-configurable and enabled by default.
Required
Required fields are marked with an indicator next to the label, and become invalid if their value is first entered and then cleared.
An instruction text at the top of the form explaining the required indicator is recommended. The indicator itself can be customized with the --lumo-required-field-indicator
style property.
Min & Max Values
The valid input range of Time Picker can be restricted by defining min
and max
values. Times before the min
and after the max
aren’t displayed in the overlay. Helper text can be used to inform the user about the accepted range.
The following example demonstrates how to specify these constraints and provide error messages:
new tab
TimePicker timePicker = new TimePicker();
timePicker.setRequiredIndicatorVisible(true);
timePicker.setLabel("Appointment time");
timePicker.setHelperText("Open 8:00-16:00");
timePicker.setStep(Duration.ofMinutes(30));
timePicker.setValue(LocalTime.of(8, 30));
timePicker.setMin(LocalTime.of(8, 0));
timePicker.setMax(LocalTime.of(16, 0));
timePicker.setI18n(new TimePickerI18n()
.setBadInputErrorMessage("Invalid time format")
.setRequiredErrorMessage("Field is required")
.setMinErrorMessage("Too early, choose another time")
.setMaxErrorMessage("Too late, choose another time"));
add(timePicker);
It’s important to ensure an appropriate error message is configured for each constraint violation to provide users with clear feedback.
Custom Validation
For more complex cases where constraint validation isn’t enough, Flow and Hilla offer the Binder API that allows you to define custom validation rules. This is useful, for example, when you want to allow time selection only within separate periods, like morning and afternoon slots. In the following example, try selecting a time that is between opening hours to see a custom validation message.
new tab
TimePicker timePicker = new TimePicker();
timePicker.setLabel("Appointment time");
timePicker.setHelperText("Open 8:00-12:00, 13:00-16:00");
timePicker.setStep(Duration.ofMinutes(30));
timePicker.setMin(LocalTime.of(8, 0));
timePicker.setMax(LocalTime.of(16, 0));
timePicker.setI18n(new TimePickerI18n()
.setBadInputErrorMessage("Invalid time format"));
add(timePicker);
Binder<Appointment> binder = new Binder<>(Appointment.class);
binder.forField(timePicker).withValidator(startTime -> {
return !(LocalTime.of(8, 0).isAfter(startTime)
|| (LocalTime.of(12, 0).isBefore(startTime)
&& LocalTime.of(13, 0).isAfter(startTime))
|| LocalTime.of(16, 0).isBefore(startTime));
}, "The selected time is not available").bind(Appointment::getStartTime,
Appointment::setStartTime);
Basic Features
The following features, common to most input field components, are supported:
Label
The label is used to identify the input field. It supports plain-text content, and its length is limited to the width of the field. Helpers and Tooltips can be used to provide additional information that doesn’t fit into the label.
Visible labels are strongly recommended for all input fields. In cases where the built-in label cannot be used, an external element can be associated as the field’s label through the aria-labelledby
attribute. Fields without any visible label should include an invisible label for assistive technologies with the aria-label
attribute.
Helper
Helpers are used to provide additional information that the user may need to enter in the field, such as format requirements or explanations of the field’s purpose below the field.
A style variant is available for rendering the helper above the field.
In addition to plain text, helpers can contain components and HTML elements. However, complex and interactive content is likely to have accessibility issues.
Placeholder
The placeholder is text that’s displayed when the field is empty. Its primary purpose is to provide a short input hint (e.g., the expected format) in situations where a Helper cannot be used.
Placeholders should not be used as a replacement for a visible label. They can be mistaken for a manually entered value. See Label for alternatives to the built-in field label.
Tooltip
Tooltips are small text pop-ups displayed on hover, and on keyboard-focus. They can be used to provide additional information about a field. This can be useful in situations where an always visible Helper is not appropriate. Helpers are generally recommended in favor of tooltips, though, as they provide much better discoverability and mobile support. See the Tooltip documentation for more information.
Prefix
A prefix element — rendered at the start of the field — can be used to display units, icons, and similar visual cues to the field’s purpose or format.
External & Invisible Labels (ARIA)
Visible labels are strongly recommended for all input fields. In situations where the built-in label cannot be used, an external element can be associated as the field’s label through its element id
. Fields without any visible label should be provided an invisible label for assistive technologies like screen readers.
<!-- Associates external element as label: -->
<label id="external-label">This is the label</label>
<vaadin-time-picker accessible-name-ref="external-label">...
<!-- Invisible label for screen readers: -->
<vaadin-time-picker accessible-name="This is the label">...
new tab
TimePicker field = new TimePicker();
field.setLabel("Label");
field.setHelperText("Helper text");
field.setPlaceholder("Placeholder");
field.setTooltipText("Tooltip text");
field.setClearButtonVisible(true);
field.setPrefixComponent(VaadinIcon.VAADIN_H.create());
Read-Only & Disabled
Fields used to display values should be set to read-only
mode to prevent editing. Read-only fields are focusable and visible to screen readers. They can display tooltips. Their values can be selected and copied.
Fields that are currently unavailable should be disabled
. The reduced contrast of disabled fields makes them inappropriate for displaying information. They can’t be focused or display tooltips. They’re invisible to screen readers, and their values cannot be selected and copied.
Disabled fields can be useful in situations where they can become enabled based on some user action. Consider hiding fields entirely if there’s nothing the user can do to make them editable.
new tab
TimePicker readonlyField = new TimePicker();
readonlyField.setReadOnly(true);
readonlyField.setLabel("Read-only");
readonlyField.setValue(LocalTime.of(7, 0));
TimePicker disabledField = new TimePicker();
disabledField.setEnabled(false);
disabledField.setLabel("Disabled");
Style Variants
The following style variants can be applied:
Text Alignment
Three different text alignments are supported: left
, which is the default; center
; and right
.
Right-alignment is recommended for numerical values when presented in vertical groups. This tends to aid interpretation and comparison of values.
Small Variant
The small variant can be used to make individual fields more compact. The default size of fields can be customized with style properties.
Helper Above Field
The helper can be rendered above the field, and below the label.
Borders
Borders can be applied to the field surface by providing a value (e.g., 1px
) to the --vaadin-input-field-border-width
CSS property. This can be applied globally to all input fields using the html
selector, or to individual component instances. Borders are required to achieve WCAG 2.1 level AA conformant color contrast with the default Lumo styling of fields.
You can override the default border color with the --vaadin-input-field-border-color
property.
new tab
TimePicker field = new TimePicker();
field.addThemeVariants(TimePickerVariant.LUMO_SMALL,
TimePickerVariant.LUMO_ALIGN_RIGHT,
TimePickerVariant.LUMO_HELPER_ABOVE_FIELD);
field.getStyle().set("--vaadin-input-field-border-width", "1px");
Best Practices
Use Time Picker when the user needs to choose a time of day. Don’t use it for durations, such as for a stopwatch or timer.