Time ranges
TimeRangePicker picks a start and an end time: opening hours, a shift, quiet hours. Both ends show their time as each platform does: Start and End rows on iOS, as in Calendar, with the wheels opening under the row you tap; large time boxes on Material and side-by-side cards on the web, with an arrow and the range's length between them. One TimePicker edits the end that's chosen, as wheels, a clock dial or a list, the same as TimePicker in each design.
import { TimeRangePicker, TimeRangePickerField, TimeRangePickerSheet } from '@elik56a/react-native-pickers';
<> <TimeRangePicker value={hours} onChange={setHours} minuteInterval={15} /> <Text style={{ textAlign: 'center', marginTop: 8 }}>{formatTimeRange(hours)}</Text> </>
Try the propsβ
displayformatminuteIntervalminDuration<TimeRangePicker
value={hours}
onChange={setHours}
minuteInterval={15}
minDuration={60}
/>
How the ends moveβ
- Moving the start carries the end along, keeping the length, as calendar apps do: a 9:00 β 17:00 shift moved to 10:00 becomes 10:00 β 18:00.
- Moving the end changes the length, within
minDurationandmaxDuration(in minutes). An end past either is pulled back to it. - An end before the start runs into the next day, as in 22:00 β 07:00. The
endis then aDateon the following day, soend - startis the length, and the picker and the field show it with +1. allowOvernight={false}keeps the range within the start's day: an end before the start is moved tominDurationafter it, and a start moved late in the day shortens the range to end by midnight.
<> <TimeRangePicker value={quiet} onChange={setQuiet} minuteInterval={30} minDuration={60} maxDuration={12 * 60} startLabel="From" endLabel="Until" /> <Text style={{ textAlign: 'center', marginTop: 8 }}> Do not disturb {formatTimeRange(quiet)} Β· {hours} h </Text> </>
getTimeRangeSeconds(range) gives the length, into the next day for an overnight range, and formatTimeRange(range, { format, locale }) the text, such as 9:00 AM β 5:00 PM or 22:00 β 07:00 +1.
In a sheet or a fieldβ
TimeRangePickerSheet keeps a draft and calls onConfirm with the range on Done. TimeRangePickerField is the form field: it shows the confirmed range and opens the sheet, a dropdown on the web, as the other picker fields do, with error, helperText, required and clearable.
presentationdisplayformat<TimeRangePickerField
label="Opening hours"
placeholder="Set hours"
minuteInterval={15}
value={hours}
onConfirm={setHours}
/>
<TimeRangePickerField
label="Opening hours"
placeholder="Set hours"
minuteInterval={15}
value={hours}
onConfirm={setHours}
/>
Accessibilityβ
The two ends are a radio group named "Start or end", and each end reads its label and time, such as "End 07:00 +1". The time picker below it has TimePicker's accessibility.
Propsβ
TimeRangePicker takes every TimePicker prop except value, defaultValue, onChange and onValueChanging, such as display, format, minuteInterval and minTime / maxTime, which apply to both ends. Plus:
| Prop | Type | Default | Description |
|---|---|---|---|
value | TimeRange ({ start: Date; end: Date }) | β | The range (controlled). The end is on the start's day, or the next day for an overnight range. |
defaultValue | TimeRange | from now, for an hour | Initial range when uncontrolled. The hour is kept within minDuration and maxDuration. |
onChange | (range: TimeRange) => void | β | Fires when the user moves either end. |
minDuration | number | 0 | Shortest range, in minutes. |
maxDuration | number | β | Longest range, in minutes. |
allowOvernight | boolean | true | Let the end run into the next day. |
startLabel / endLabel | string | 'Start' / 'End' | The ends' labels. |
TimeRangePickerSheet takes the same props, with onConfirm(range) in place of onChange and without defaultValue, plus the sheet props such as visible and onClose. TimeRangePickerField takes the sheet's props and the field props.
Relatedβ
TimePicker: one time of day- Date ranges: a range of days on a calendar
DurationPicker: a length of time, such as a timer