Skip to content

Commit

Permalink
Make content's type more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbieTheWagner committed Aug 7, 2024
1 parent 7afe1be commit 3c6409c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
14 changes: 8 additions & 6 deletions ember-highcharts/src/components/high-charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const CHART_TYPES = {
undefined: 'chart',
} as const;

interface HighChartsSignature {
interface HighChartsSignature<Content extends Highcharts.Options['series']> {
Element: HTMLDivElement;
Args: {
/**
Expand All @@ -36,7 +36,7 @@ interface HighChartsSignature {
* The `content` argument matches up with the `series` option in the Highcharts/Highstock/Highmaps API.
* Use this option to set the series data for your chart.
*/
content?: Highcharts.Options['series'];
content?: Content;
/**
* The `chartOptions` argument is a generic object for setting different options with Highcharts/Highstock/Highmaps.
* Use this option to set things like the chart title and axis settings.
Expand All @@ -56,7 +56,9 @@ interface HighChartsSignature {
};
}

export default class HighCharts extends Component<HighChartsSignature> {
export default class HighCharts<
Content extends Highcharts.Options['series'],
> extends Component<HighChartsSignature<Content>> {
get content() {
return this.args.content ?? undefined;
}
Expand Down Expand Up @@ -121,9 +123,9 @@ export default class HighCharts extends Component<HighChartsSignature> {
onDidUpdate(
_elem: unknown,
[content, chartOptions, mode]: [
HighChartsSignature['Args']['content'],
HighChartsSignature['Args']['chartOptions'],
HighChartsSignature['Args']['mode'],
HighChartsSignature<Content>['Args']['content'],
HighChartsSignature<Content>['Args']['chartOptions'],
HighChartsSignature<Content>['Args']['mode'],
],
) {
const { chart } = this;
Expand Down
6 changes: 4 additions & 2 deletions ember-highcharts/src/utils/build-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ export const EMPTY_CHART_CONTENT = [
},
] as const;

export default function buildOptions(
export default function buildOptions<
Content extends Highcharts.Options['series'],
>(
theme: Highcharts.Options = {},
options: Highcharts.Options = {},
content?: Highcharts.Options['series'],
content?: Content,
) {
// if 'no-data-to-display' module has been imported, keep empty series
// and leave it to highcharts to show no data label.
Expand Down
6 changes: 4 additions & 2 deletions ember-highcharts/src/utils/chart-data.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type Highcharts from 'highcharts';

export function getSeriesMap(seriesGroup: Array<Highcharts.SeriesOptionsType>) {
export function getSeriesMap<Content extends Highcharts.SeriesOptionsType>(
seriesGroup: Array<Content>,
) {
const seriesMap = seriesGroup.reduce(
(seriesMap, seriesItem) => {
seriesMap[seriesItem.name as string] = seriesItem;
return seriesMap;
},
{} as { [key: string]: Highcharts.SeriesOptionsType },
{} as { [key: string]: Content },
);

return seriesMap;
Expand Down

0 comments on commit 3c6409c

Please sign in to comment.