Comparison period select
Previous period, previous year and the like: what the current figures are compared with.
Preview
Compare to
Usage
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.
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.
| Prop | Type | Default | Description |
|---|---|---|---|
onChangerequired | ( | Called with the name of the comparison the reader picks, or with undefined when the reader clears it. | |
placeholder | string | "Select a date-comparison" | Text shown while nothing is chosen. Translates like title. |
primaryDateRange | TimeRange | The range being compared: { from: Date, to: Date }, with an optional relativeTimeString naming a preset. With it, each option shows the dates it covers. | |
comparisonPeriod | string | The 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. | |
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
ComparisonPeriodSelect reads only the common options in a field’s inputs.