Skip to content

Commit

Permalink
fix(core): using type guard to check type of canvas children
Browse files Browse the repository at this point in the history
Checking props for id to allow external components as mentioned in react-financial#417.
  • Loading branch information
markmcdowell committed Aug 27, 2020
1 parent 8d718b0 commit 829ccfa
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions packages/core/src/utils/ChartDataUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ScaleContinuousNumeric, ScaleTime } from "d3-scale";
import flattenDeep from "lodash.flattendeep";
import * as React from "react";

import { Chart } from "../Chart";
import { Chart, ChartProps } from "../Chart";

import {
functor,
Expand Down Expand Up @@ -48,16 +48,31 @@ function isArraySize2AndNumber(yExtentsProp: any) {
const [a, b] = yExtentsProp;
return typeof a === "number" && typeof b === "number";
}

return false;
}

const isChartProps = (props: ChartProps | any | undefined): props is ChartProps => {
if (props === undefined) {
return false;
}

const chartProps = props as ChartProps;
if (chartProps.id === undefined) {
return false;
}

return true;
};

export function getNewChartConfig(innerDimension: any, children: any, existingChartConfig: any[] = []) {
return React.Children.map(children, (each) => {
if (each && each.type.toString() === Chart.toString()) {
if (each !== undefined && isChartProps(each.props)) {
const chartProps = {
...Chart.defaultProps,
...each.props,
};

const {
id,
origin,
Expand Down Expand Up @@ -110,18 +125,18 @@ export function getNewChartConfig(innerDimension: any, children: any, existingCh
yExtents,
yExtentsCalculator,
flipYScale,
// yScale: setRange(yScale.copy(), height, padding, flipYScale),
yScale,
yPan,
yPanEnabled,
// mouseCoordinates,
width,
height,
};
}

return undefined;
}).filter((each: any) => isDefined(each));
}).filter((each: any) => each !== undefined);
}

export function getCurrentCharts(chartConfig: any, mouseXY: number[]) {
const currentCharts = chartConfig
.filter((eachConfig: any) => {
Expand Down Expand Up @@ -205,7 +220,6 @@ export function getChartConfigWithUpdatedYScales(
};
});

// @ts-ignore
const updatedChartConfig = combine(chartConfig, yDomains);

return updatedChartConfig;
Expand Down

0 comments on commit 829ccfa

Please sign in to comment.