TimePicker
Captures time value from the user
Source
Docs
Package
Usage
TimePicker component is an alternative to TimeInput that offers more
features, it supports 24-hour and 12-hour formats, dropdown with hours, minutes and seconds, and
more.
Controlled
TimePicker component value is a string in hh:mm:ss 24-hour format (for example 18:34:55).
Empty string is used to represent no value. onChange function is called only when the entered value is valid.
The input value is considered valid in the following cases:
- All inputs are empty. In this case
onChangeis called with an empty string. - All inputs are filled. For example if
withSecondsprop is set and the user entered12:34:56,onChangewill be called with12:34:56. But if the user entered12:34,onChangewill not be called because seconds value is missing.
With seconds
Set withSeconds prop to enable seconds input. Note that if this prop is used,
onChange is not called until all inputs are filled – it is not possible
to enter only hours and minutes.
12-hour format
Set format="12h" to use 12-hour format. Note that onChange is called only when all inputs are filled
including am/pm input.
Change am/pm labels
To change am/pm labels use amPmLabels prop. Example of changing labels to Hindi:
Min and max values
Set min and max props to limit available time range:
With dropdown
Set withDropdown prop to display the dropdown with hours, minutes, seconds and am/pm selects.
By default, the dropdown is visible when one of the inputs is focused.
Hours/minutes/seconds step
Use hoursStep, minutesStep and secondsStep props to control step for each input.
These props are used to control value by which the input is incremented or decremented when
up/down arrow keys are pressed and to generate corresponding values range in the dropdown.
Control dropdown opened state
Use popoverProps to pass props down to the underlying Popover component:
Time presets
You can define time presets with presets prop. Presets are displayed in the dropdown and can be selected by clicking on them.
Time values for presets should be in hh:mm:ss or hh:mm 24-hour time format. Presets
display value is generated based on format, amPmLabels and withSeconds props.
Time presets groups
To group presets use an array of objects with label and values keys:
Time presets range
If you need to generate a range of time values, use getTimeRange function exported from
@mantine/dates package. The function accepts start, end time and interval in hh:mm:ss format.
Dropdown position
By default, the dropdown is displayed below the input if there is enough space; otherwise it is displayed above the input.
You can change this behavior by setting position and middlewares props, which are passed down to the
underlying Popover component.
Example of dropdown that is always displayed above the input:
Dropdown width
To change dropdown width, set width prop in comboboxProps. By default,
dropdown width is adjusted to fit all content. Example of dropdown width set
to the input width:
Paste events
By default, TimePicker handles only time in 24-hour format (for example 17:33:43 or 19:22) for paste events.
With pasteSplit prop you can create a custom paste time parser:
Try pasting time in 12h format in any input. For example, try pasting 12:34 PM or 8:56:45 AM
Clearable
Set clearable prop to display a clear button in the right section of the input.
The clear button is visible when at least one of the fields has value.
Disabled
Read only
Input props
TimePicker component supports Input and Input.Wrapper components features and all div element props. TimePicker documentation does not include all features supported by the component – see Input documentation to learn about all available features.
Input description
Get refs of inner inputs
Use hoursRef, minutesRef, secondsRef and amPmRef props to get refs of inner inputs:
onFocus and onBlur events
onFocus and onBlur events are called when the first input is focused and the last input is blurred:
Accessibility
Set aria labels for hours, minutes, seconds and am/pm inputs and clear button with corresponding props:
Keyboard interactions: