Date range picker
Preset ranges such as “Last 30 days”, with a calendar for any other range.
Preview
Date range
Usage
import { useState } from 'react';
import { ChartsProvider, DateRangePicker, dateRangePreset } from 'em-kit';
import type { TimeRange } from 'em-kit';
export const Example = () => {
const [range, setRange] = useState<TimeRange>(
dateRangePreset('Last 30 days'),
);
return (
<ChartsProvider>
<div style={{ width: 320 }}>
<DateRangePicker
title="Date range"
selectedValue={range}
onChange={setRange}
/>
</div>
</ChartsProvider>
);
};- A preset is named by
relativeTimeString; the picker fills in itsfromandtoand reports them throughonChange, in the provider’s time zone.
Install with npm install em-kit. Needs <ChartsProvider> once, and your own state for its value: Quick start.
Examples
Two months
The calendar shows two months side by side, for ranges that cross a month.
Show full code (21 lines)
import { useState } from 'react';
import { ChartsProvider, DateRangePicker, dateRangePreset } from 'em-kit';
import type { TimeRange } from 'em-kit';
export const Example = () => {
const [range, setRange] = useState<TimeRange>(
dateRangePreset('Last 30 days'),
);
return (
<ChartsProvider>
<div style={{ width: 320 }}>
<DateRangePicker
title="Date range"
selectedValue={range}
onChange={setRange}
showTwoMonths
/>
</div>
</ChartsProvider>
);
};<DateRangePicker
title="Date range"
selectedValue={range}
onChange={setRange}
showTwoMonths
/>Calendar only
No preset list: the picker opens straight on the calendar.
Show full code (19 lines)
import { useState } from 'react';
import { ChartsProvider, DateRangePicker } from 'em-kit';
import type { TimeRange } from 'em-kit';
export const Example = () => {
const [range, setRange] = useState<TimeRange>();
return (
<ChartsProvider>
<div style={{ width: 320 }}>
<DateRangePicker
title="Date range"
selectedValue={range}
onChange={setRange}
showCustomRangeOptions={false}
/>
</div>
</ChartsProvider>
);
};const [range, setRange] = useState<TimeRange>();
<DateRangePicker
title="Date range"
selectedValue={range}
onChange={setRange}
showCustomRangeOptions={false}
/>Props
Keyboard and screen readers: Accessibility.
| Prop | Type | Default | Description |
|---|---|---|---|
onChangerequired | ( | Called with the range the reader picks: a preset comes with its name and its dates, worked out in the provider’s time zone. A preset passed without dates is reported back with them at once. Called with undefined when the reader clears it. | |
placeholder | string | "Select a date-range" | Text shown while nothing is chosen. Translates like title. |
selectedValue | TimeRange | The chosen range: { from: Date, to: Date }, with an optional relativeTimeString naming a preset. dateRangePreset('Last 30 days') starts from a preset. | |
clearable | boolean | true | Let the reader clear the value. |
showCustomRangeOptions | boolean | true | List the presets, with the calendar behind a “Custom” entry. When false, the picker opens straight on the calendar. The presets are the provider’s theme.defaults.dateRangesOptions. |
showTwoMonths | boolean | false | Show two months side by side in the calendar. |
componentName | string | A name for this component, included in the interaction events it dispatches on the DOM, for your analytics. | |
trackingId | string | An id included in the same events, to tell two instances apart. | |
title | string | The title over the field. A "key|fallback" string is looked up in your translations. | |
description | string | Smaller text under the title. Translates like title. | |
tooltip | string | Text for an info icon beside the title. |
A blank default means the prop is unset unless you pass it.
| Type | Definition |
|---|---|
TimeRange | { from: Date |What a click’s TimeRange holds: Events. |
Field options
DateRangePicker reads only the common options in a field’s inputs.