Components · Controls

Date range picker

Preset ranges such as “Last 30 days”, with a calendar for any other range.

Open in playground

Preview

Date range

Usage

tsx
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 its from and to and reports them through onChange, 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.

tsxDiffers from Usage
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.

tsxDiffers from Usage
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.

PropTypeDefaultDescription
onChangerequired(newDateRange: TimeRange) => voidCalled 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.
placeholderstring"Select a date-range"Text shown while nothing is chosen. Translates like title.
selectedValueTimeRangeThe chosen range: { from: Date, to: Date }, with an optional relativeTimeString naming a preset. dateRangePreset('Last 30 days') starts from a preset.
clearablebooleantrueLet the reader clear the value.
showCustomRangeOptionsbooleantrueList 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.
showTwoMonthsbooleanfalseShow two months side by side in the calendar.
componentNamestringA name for this component, included in the interaction events it dispatches on the DOM, for your analytics.
trackingIdstringAn id included in the same events, to tell two instances apart.
titlestringThe title over the field. A "key|fallback" string is looked up in your translations.
descriptionstringSmaller text under the title. Translates like title.
tooltipstringText for an info icon beside the title.

A blank default means the prop is unset unless you pass it.

TypeDefinition
TimeRange{ from: Date | undefined; to: Date | undefined; relativeTimeString?: string; } | undefinedWhat a click’s TimeRange holds: Events.

Field options

DateRangePicker reads only the common options in a field’s inputs.