From c20dc79538a1ec44655c92512fa2df5215caf153 Mon Sep 17 00:00:00 2001 From: Marco Vettorello Date: Tue, 2 Apr 2019 01:57:12 +0800 Subject: [PATCH] refactor: move bars props out of components --- .../react_canvas/bar_geometries.test.tsx | 35 ++++++ .../react_canvas/bar_geometries.tsx | 103 ++++++++++++------ .../react_canvas/line_geometries.test.tsx | 1 + 3 files changed, 106 insertions(+), 33 deletions(-) create mode 100644 src/components/react_canvas/bar_geometries.test.tsx diff --git a/src/components/react_canvas/bar_geometries.test.tsx b/src/components/react_canvas/bar_geometries.test.tsx new file mode 100644 index 0000000000..dcddcf2d78 --- /dev/null +++ b/src/components/react_canvas/bar_geometries.test.tsx @@ -0,0 +1,35 @@ +import { buildBarProps } from './bar_geometries'; + +describe('[canvas] Bar Geometries', () => { + test('can build bar props', () => { + const props = buildBarProps({ + index: 1, + x: 10, + y: 20, + width: 30, + height: 40, + fill: 'red', + stroke: 'blue', + strokeWidth: 1, + borderEnabled: true, + geometryStyle: { + opacity: 0.5, + }, + }); + expect(props).toEqual({ + key: `bar-1`, + x: 10, + y: 20, + width: 30, + height: 40, + fill: 'red', + stroke: 'blue', + strokeWidth: 1, + strokeEnabled: true, + strokeHitEnabled: false, + perfectDrawEnabled: false, + listening: false, + opacity: 0.5, + }); + }); +}); diff --git a/src/components/react_canvas/bar_geometries.tsx b/src/components/react_canvas/bar_geometries.tsx index 4dcac43202..bfefab2e2e 100644 --- a/src/components/react_canvas/bar_geometries.tsx +++ b/src/components/react_canvas/bar_geometries.tsx @@ -3,7 +3,7 @@ import React from 'react'; import { Group, Rect } from 'react-konva'; import { animated, Spring } from 'react-spring/renderprops-konva.cjs'; import { LegendItem } from '../../lib/series/legend'; -import { BarGeometry, getGeometryStyle } from '../../lib/series/rendering'; +import { BarGeometry, GeometryStyle, getGeometryStyle } from '../../lib/series/rendering'; import { BarSeriesStyle, SharedGeometryStyle } from '../../lib/themes/theme'; import { GlobalKonvaElementProps } from './globals'; @@ -47,7 +47,7 @@ export class BarGeometries extends React.PureComponent< style: { border }, sharedStyle, } = this.props; - return bars.map((bar, i) => { + return bars.map((bar, index) => { const { x, y, width, height, color } = bar; // Properties to determine if we need to highlight individual bars depending on hover state @@ -69,43 +69,80 @@ export class BarGeometries extends React.PureComponent< const borderEnabled = border.visible && width > border.strokeWidth * 7; if (this.props.animated) { return ( - + - {(props: { y: number; height: number }) => ( - - )} + {(props: { y: number; height: number }) => { + const barProps = buildBarProps({ + index, + x, + y: props.y, + width, + height: props.height, + fill: color, + stroke: border.stroke, + strokeWidth: border.strokeWidth, + borderEnabled, + geometryStyle, + }); + + return ; + }} ); } else { - return ( - - ); + const barProps = buildBarProps({ + index, + x, + y, + width, + height, + fill: color, + stroke: border.stroke, + strokeWidth: border.strokeWidth, + borderEnabled, + geometryStyle, + }); + return ; } }); } } + +export function buildBarProps({ + index, + x, + y, + width, + height, + fill, + stroke, + strokeWidth, + borderEnabled, + geometryStyle, +}: { + index: number; + x: number; + y: number; + width: number; + height: number; + fill: string; + stroke: string; + strokeWidth: number; + borderEnabled: boolean; + geometryStyle: GeometryStyle; +}) { + return { + key: `bar-${index}`, + x, + y, + width, + height, + fill, + strokeWidth, + stroke, + strokeEnabled: borderEnabled, + ...GlobalKonvaElementProps, + ...geometryStyle, + }; +} diff --git a/src/components/react_canvas/line_geometries.test.tsx b/src/components/react_canvas/line_geometries.test.tsx index 114558a624..bad42d42b7 100644 --- a/src/components/react_canvas/line_geometries.test.tsx +++ b/src/components/react_canvas/line_geometries.test.tsx @@ -75,5 +75,6 @@ describe('[canvas] Line Geometries', () => { perfectDrawEnabled: false, listening: false, }); + expect(props.fill).toBeFalsy(); }); });