TimeFields allow users to enter and edit time values using a keyboard. Each part of the time is displayed in an individually editable segment.
Value
Use the value or defaultValue prop to set the time value, using objects in the @internationalized/date package. TimeField accepts plain Time, CalendarDateTime, or ZonedDateTime, but only displays the time.
Selected time: 11:45:00
import {Time} from '@internationalized/date';
import {TimeField} from '@react-spectrum/s2/TimeField';
import {useState} from 'react';
function Example() {
let [time, setTime] = useState<Time | null>(new Time(11, 45));
return (
<>
<TimeField
value={time}
onChange={setTime} />
<p>Selected time: {time ? time.toString() : '--'}</p>
</>
);
}
Format options
The time format is automatically determined based on the user's locale. TimeField supports several props to control how values are displayed.
Forms
Use the name prop to submit the selected date to the server as an ISO 8601 string. Set the isRequired, minValue, or maxValue props to validate the value, or implement custom client or server-side validation. See the Forms guide to learn more.
API
| Name | Type | Default |
|---|---|---|
size | 'S'
| 'M'
| 'L'
| 'XL' | Default: 'M'
|
The size of the TimeField. | ||
hourCycle | 12 | 24 | Default: — |
Whether to display the time in 12 or 24 hour format. By default, this is determined by the user's locale. | ||
granularity | 'hour'
| 'minute'
| 'second' | Default: 'minute'
|
Determines the smallest unit that is displayed in the time picker. | ||
hideTimeZone | boolean | Default: — |
Whether to hide the time zone abbreviation. | ||
shouldForceLeadingZeros | boolean | Default: — |
Whether to always show leading zeros in the hour field. By default, this is determined by the user's locale. | ||
placeholderValue | TimeValue | Default: — |
A placeholder time that influences the format of the placeholder shown when no value is selected. Defaults to 12:00 AM or 00:00 depending on the hour cycle. | ||
isDisabled | boolean | Default: — |
Whether the input is disabled. | ||
isReadOnly | boolean | Default: — |
Whether the input can be selected but not changed by the user. | ||
styles | StylesProp | Default: — |
Spectrum-defined styles, returned by the | ||
value | TimeValue | null | Default: — |
The current value (controlled). | ||
defaultValue | TimeValue | null | Default: — |
The default value (uncontrolled). | ||
onChange | | Default: — |
Handler that is called when the value changes. | ||
label | ReactNode | Default: — |
The content to display as the label. | ||
labelPosition | LabelPosition | Default: 'top'
|
The label's overall position relative to the element it is labeling. | ||
labelAlign | Alignment | Default: 'start'
|
The label's horizontal alignment relative to the element it is labeling. | ||
contextualHelp | ReactNode | Default: — |
A ContextualHelp element to place next to the label. | ||
minValue | TimeValue | null | Default: — |
The minimum allowed time that a user may select. | ||
maxValue | TimeValue | null | Default: — |
The maximum allowed time that a user may select. | ||
isRequired | boolean | Default: — |
Whether user input is required on the input before form submission. | ||
isInvalid | boolean | Default: — |
Whether the input value is invalid. | ||
validate | | Default: — |
A function that returns an error message if a given value is invalid.
Validation errors are displayed to the user when the form is submitted
if | ||
validationBehavior | 'native' | 'aria' | Default: 'native'
|
Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA. | ||
necessityIndicator | NecessityIndicator | Default: 'icon'
|
Whether the required state should be shown as an icon or text. | ||
description | ReactNode | Default: — |
A description for the field. Provides a hint such as specific requirements for what to choose. | ||
errorMessage | ReactNode | | Default: — |
An error message for the field. | ||
onFocus | | Default: — |
Handler that is called when the element receives focus. | ||
onBlur | | Default: — |
Handler that is called when the element loses focus. | ||
onFocusChange | | Default: — |
Handler that is called when the element's focus status changes. | ||
onKeyDown | | Default: — |
Handler that is called when a key is pressed. | ||
onKeyUp | | Default: — |
Handler that is called when a key is released. | ||
name | string | Default: — |
The name of the input element, used when submitting an HTML form. See MDN. | ||
form | string | Default: — |
The | ||
autoFocus | boolean | Default: — |
Whether the element should receive focus on render. | ||
id | string | Default: — |
The element's unique identifier. See MDN. | ||
aria-label | string | Default: — |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: — |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: — |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: — |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
UNSAFE_className | UnsafeClassName | Default: — |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | Default: — |
Sets inline style for the
element. Only use as a last resort. Use the | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||