Components · Controls

Comparison period select

Previous period, previous year and the like: what the current figures are compared with.

Open in playground

Preview

Compare to

Usage

tsx
import { useState } from 'react';
import { ChartsProvider, ComparisonPeriodSelect } from 'em-kit';

export const Example = () => {
  const [period, setPeriod] = useState<string | undefined>('Previous period');
  return (
    <ChartsProvider>
      <div style={{ width: 320 }}>
        <ComparisonPeriodSelect
          title="Compare to"
          comparisonPeriod={period}
          onChange={setPeriod}
        />
      </div>
    </ChartsProvider>
  );
};
  • The value is the option’s name, such as “Previous period”.

Install with npm install em-kit. Needs <ChartsProvider> once, and your own state for its value: Quick start.

Examples

With dates

Each option shows the dates it covers, worked out from the range being compared. primaryDateRange is the range your date picker holds: pass its state here.

tsxDiffers from Usage
Show full code (22 lines)
import { useState } from 'react';
import {
  ChartsProvider,
  ComparisonPeriodSelect,
  dateRangePreset,
} from 'em-kit';

export const Example = () => {
  const [period, setPeriod] = useState<string | undefined>('Previous period');
  return (
    <ChartsProvider>
      <div style={{ width: 320 }}>
        <ComparisonPeriodSelect
          title="Compare to"
          comparisonPeriod={period}
          onChange={setPeriod}
          primaryDateRange={dateRangePreset('Last 30 days')}
        />
      </div>
    </ChartsProvider>
  );
};
<ComparisonPeriodSelect
  title="Compare to"
  comparisonPeriod={period}
  onChange={setPeriod}
  primaryDateRange={dateRangePreset('Last 30 days')}
/>

Props

Keyboard and screen readers: Accessibility.

PropTypeDefaultDescription
onChangerequired(newComparisonPeriod: string) => voidCalled with the name of the comparison the reader picks, or with undefined when the reader clears it.
placeholderstring"Select a date-comparison"Text shown while nothing is chosen. Translates like title.
primaryDateRangeTimeRangeThe range being compared: { from: Date, to: Date }, with an optional relativeTimeString naming a preset. With it, each option shows the dates it covers.
comparisonPeriodstringThe chosen comparison, by name: one of "Previous period", "Previous week", "Previous month", "Previous quarter", "Previous year", "Same period last week", "Same period last month", "Same period last quarter", "Same period last year". The list, and how each works out its dates, is the provider’s theme.defaults.comparisonPeriodsOptions.
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

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