2.0.0-rc.4 • Published 9 months ago

@asphalt-react/time-picker v2.0.0-rc.4

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
9 months ago

TimePicker

npm

TimePicker lets you type and select time. TimePicker renders in 12 hour format by default but you can opt for the 24 hour format as well. It shows a picker inside a popover which has buttons to select a time. You can set the interval for the minutes field. It also has an input field for typing a time. A button with clock icon in the input field toggles the picker and clicking outside the picker or pressing Esc closes the picker, persisting the changes made through it. TimePicker also supports selection of time range.

For touch capable devices, TimePicker renders the native UI of the user agent which you can disable as well. The range feature will not work when the native picker is active. This is a restriction from the browser. You can disable the native picker to use the time range selection feature.

Usage

import TimePicker from "@asphalt-react/time-picker"

export default () => {
  const [time, setTime] = React.useState([new Date(2023, 7, 8, 15, 12, 12)])

  <TimePicker
    value={time}
    onChange={(time) => {
      setTime(time)
    }}
    onError={() => false}
  />
}

Supported formats

The input field accepts time following the ISO 8601 calendar date extended format. If a user types an invalid time in the field, the picker retains its previous state.

12-hour format

The input field in 12-hour format shows the time in hh:mm AM format. The picker allows the users to select the hour, minute and day period (AM/PM). Either of the two day period buttons is selected at all times. The hour and minute values roll over on reaching the end. For example: If the hour value is "10", clicking the next hour button rolls the value over to "11".

24-hour format

The input field in 24-hour format shows the time in hh:mm format. Set militaryTime prop to use this format. The picker allows the users to select the hour and the minute. The hour and minute values roll over on reaching the end. For example, if the the hour value is "22", clicking the next hour button rolls the value over to "23".

Configuring Minutes Steps

The minute values change on a particular interval using the minuteStep prop. The minute values are always a multiple of the interval. By default, the interval is set to "15". So, if the current value of minute is "30", clicking the next minute button changes the value to "45" and then to "00". Similarly, clicking the previous minute button changes the value to "30", then to "15" and so on.

Errors

TimePicker validates the input values and raises these errors:

  1. InvalidTime: If value prop receives an invalid date object; use the Date() constructor to create date objects.
  2. UserInvalidTime: When a user types a date which is either invalid or in incorrect format. For example:

By default,

  • 12:32 AM
  • 1:34 pm
  • 18:23 PM ❌ - invalid time

When meridiem is set to false which enables 12 hour format

  • 21:00
  • 01:00
  • 10:00 AM ❌ - invalid time

TimePicker with range,

  • 12:32 AM to 18:23 AM
  • 13:00 to 23:45
  • 5:00 AM to 21:00 ❌ - invalid range
  • 15:00 - 16:00 ❌ - invalid range

Accessibility

  1. The TimePicker is accessible via keyboard.
  2. The picker traps focus and rotates the focus among its elements.
  3. The next/previous buttons for the hour and minute labels in the picker have aria-label to assist screen readers.
  4. The time icon button has an aria-label attribute with value that change depending on whether a time is selected or not:
    • If no time is selected, the label is "Select a time"
    • If TimePicker already has a value, the label is "Selected time is TIME_STRING", where TIME_STRING is the selected time.
  5. When the picker closes, the focus shift to the time icon button. The screen reader then announces the selected time.
  6. If the end-user types an invalid date in the input field, TimePicker adds aria-invalid=”true” to the field.
  7. The day period buttons are semantic radio buttons. Use or keys to change selection.
  8. TimePicker accepts all aria attributes for role=”textbox”

Props

value

Selected time. Pass the start time followed by end time when it's a ranged TimePicker.

typerequireddefault
arrayOftrueN/A

onChange

Callback to handle time change.:

  • time: This is an array which contains selected time as a JS date object. It contains the start time as the first index followed by the end time (if range is true).
  • event: The react synthetic event.
const timeHandler = (time, event) => {
 // handle the selected time
}

Note: TimePicker will show the console warnings if value or onChange props are missing, as they are needed to update the time. Hence, we recommend you to pass both the props.

typerequireddefault
functrueN/A

minuteStep

Step interval for minute values in the picker. Possible values are 1, 5, 10, 15.

The value "1" is available from v1.23.2+

typerequireddefault
enumfalse15

invalid

Renders TimePicker in error state.

typerequireddefault
boolfalsefalse

onError

Callback to handle errors. The argument is an instance of JS Error.

function onError(error) {
  // handle the error
}
typerequireddefault
funcfalseN/A

size

Size of the time picker. Possible values are "m", "l" for medium & large.

typerequireddefault
enumfalse"m"

placeholder

Placeholder for the input field.

typerequireddefault
stringfalse"hh:mm"

disabled

Disables the input field.

typerequireddefault
boolfalsefalse

stretch

Enables the input field to take parent's width. The size of the picker does not change.

typerequireddefault
boolfalsefalse

timeIconLabel

accessibility

aria-label for time icon button.

typerequireddefault
stringfalseN/A

nextHourLabel

accessibility

aria-label for next hour button.

typerequireddefault
stringfalse"next hour"

previousHourLabel

accessibility

aria-label for previous hour button.

typerequireddefault
stringfalse"previous hour"

nextMinuteLabel

accessibility

aria-label for next minute button.

typerequireddefault
stringfalse"next minute"

previousMinuteLabel

accessibility

aria-label for previous minute button.

typerequireddefault
stringfalse"previous minute"

meridiem

Enables 12 hour time format.

typerequireddefault
boolfalsetrue

native

Enables native timepicker when on touch devices. You can change this behavior by setting it to false. Range does not work when this is enabled.

typerequireddefault
boolfalsetrue

range

Enables range time picker. This feature will not work when native is enabled.

typerequireddefault
boolfalsefalse

startSwitchCaption

Caption for start time in range time picker.

typerequireddefault
stringfalse"Start Time"

endSwitchCaption

Caption for end time in range time picker.

typerequireddefault
stringfalse"End Time"