Skip to content

Commit

Permalink
Merge pull request #168 from hshoff/harry-refs
Browse files Browse the repository at this point in the history
[shape] add innerRef prop to shapes. fixes #140
  • Loading branch information
hshoff authored Oct 7, 2017
2 parents 8fdf8cc + ed1ee3b commit 0955af6
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 49 deletions.
2 changes: 2 additions & 0 deletions packages/vx-shape/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
"devDependencies": {
"babel-jest": "^20.0.3",
"d3-array": "^1.2.0",
"d3-hierarchy": "^1.1.5",
"enzyme": "^2.8.2",
"jest": "^20.0.3",
"react": "^15.0.0-0 || ^16.0.0-0",
"react-dom": "^15.0.0-0 || ^16.0.0-0",
"react-fatigue-dev": "github:tj/react-fatigue-dev",
"react-test-renderer": "^15.6.1",
"react-tools": "^0.10.0",
Expand Down
7 changes: 7 additions & 0 deletions packages/vx-shape/src/shapes/AreaClosed.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import React from 'react';
import cx from 'classnames';
import PropTypes from 'prop-types';
import { area } from 'd3-shape';
import additionalProps from '../util/additionalProps';

AreaClosed.propTypes = {
innerRef: PropTypes.func,
};

export default function AreaClosed({
x,
y,
Expand All @@ -16,6 +21,7 @@ export default function AreaClosed({
stroke = 'black',
fill = 'rgba(0,0,0,0.3)',
curve,
innerRef,
...restProps
}) {
const path = area()
Expand All @@ -27,6 +33,7 @@ export default function AreaClosed({
return (
<g>
<path
ref={innerRef}
className={cx('vx-area-closed', className)}
d={path(data)}
stroke={stroke}
Expand Down
11 changes: 9 additions & 2 deletions packages/vx-shape/src/shapes/Bar.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import React from 'react';
import cx from 'classnames';
import PropTypes from 'prop-types';
import additionalProps from '../util/additionalProps';

export default ({
Bar.propTypes = {
innerRef: PropTypes.func,
};

export default function Bar({
className,
innerRef,
data,
x = 0,
y = 0,
Expand All @@ -21,9 +27,10 @@ export default ({
strokeMiterlimit,
strokeOpacity,
...restProps,
}) => {
}) {
return (
<rect
ref={innerRef}
className={cx('vx-bar', className)}
x={x}
y={y}
Expand Down
7 changes: 7 additions & 0 deletions packages/vx-shape/src/shapes/Line.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import React from 'react';
import cx from 'classnames';
import PropTypes from 'prop-types';
import { Point } from '@vx/point';
import additionalProps from '../util/additionalProps';

Line.propTypes = {
innerRef: PropTypes.func,
};

export default function Line({
from = new Point({ x: 0, y: 0 }),
to = new Point({ x: 1, y: 1 }),
Expand All @@ -12,10 +17,12 @@ export default function Line({
transform = '',
className = '',
data,
innerRef,
...restProps
}) {
return (
<line
ref={innerRef}
className={cx('vx-line', className)}
x1={from.x}
y1={from.y}
Expand Down
14 changes: 10 additions & 4 deletions packages/vx-shape/src/shapes/LinePath.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import React from 'react';
import cx from 'classnames';
import PropTypes from 'prop-types';
import { line } from 'd3-shape';
import { curveLinear } from '@vx/curve';
import additionalProps from '../util/additionalProps';

LinePath.propTypes = {
innerRef: PropTypes.func,
};

export default function LinePath({
data,
xScale,
Expand All @@ -19,6 +24,7 @@ export default function LinePath({
fill = 'none',
curve = curveLinear,
glyph,
innerRef,
...restProps
}) {
const path = line()
Expand All @@ -29,6 +35,7 @@ export default function LinePath({
return (
<g>
<path
ref={innerRef}
className={cx('vx-linepath', className)}
d={path(data)}
stroke={stroke}
Expand All @@ -38,10 +45,9 @@ export default function LinePath({
fill={fill}
{...additionalProps(restProps, data)}
/>
{glyph &&
<g className="vx-linepath-glyphs">
{data.map(glyph)}
</g>}
{glyph && (
<g className="vx-linepath-glyphs">{data.map(glyph)}</g>
)}
</g>
);
}
9 changes: 8 additions & 1 deletion packages/vx-shape/src/shapes/LineRadial.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import React from 'react';
import cx from 'classnames';
import PropTypes from 'prop-types';
import { radialLine } from 'd3-shape';
import additionalProps from '../util/additionalProps';

LineRadial.propTypes = {
innerRef: PropTypes.func,
};

export default function LineRadial({
className = '',
angle,
radius,
defined,
curve,
data,
innerRef,
...restProps
}) {
const path = radialLine();
Expand All @@ -20,10 +26,11 @@ export default function LineRadial({
return (
<g>
<path
ref={innerRef}
className={cx('vx-line-radial', className)}
d={path(data)}
{...additionalProps(restProps, data)}
/>
</g>
);
}
}
11 changes: 9 additions & 2 deletions packages/vx-shape/src/shapes/LinkHorizontal.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import React from 'react';
import cx from 'classnames';
import PropTypes from 'prop-types';
import { linkHorizontal } from 'd3-shape';
import additionalProps from '../util/additionalProps';

LinkHorizontal.propTypes = {
innerRef: PropTypes.func,
};

export default function LinkHorizontal({
className,
innerRef,
data,
x = d => d.y,
y = d => d.x,
...restProps
}) {
const link = linkHorizontal()
const link = linkHorizontal();
link.x(x);
link.y(y);
return (
<path
ref={innerRef}
className={cx('vx-link-horizontal', className)}
d={link(data)}
{...additionalProps(restProps, data)}
/>
);
}
}
11 changes: 9 additions & 2 deletions packages/vx-shape/src/shapes/LinkVertical.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import React from 'react';
import cx from 'classnames';
import PropTypes from 'prop-types';
import { linkVertical } from 'd3-shape';
import additionalProps from '../util/additionalProps';

LinkVertical.propTypes = {
innerRef: PropTypes.func,
};

export default function LinkVertical({
className,
innerRef,
data,
x = d => d.x,
y = d => d.y,
...restProps
}) {
const link = linkVertical()
const link = linkVertical();
link.x(x);
link.y(y);
return (
<path
ref={innerRef}
className={cx('vx-link-vertical', className)}
d={link(data)}
{...additionalProps(restProps, data)}
/>
);
}
}
50 changes: 37 additions & 13 deletions packages/vx-shape/test/AreaClosed.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import React from "react";
import { shallow } from "enzyme";
import { extent, max } from "d3-array";
import { AreaClosed } from "../src";
import { appleStock } from "../../vx-mock-data";
import { scaleTime, scaleLinear } from "../../vx-scale";
import React from 'react';
import ReactDOM from 'react-dom';
import { shallow } from 'enzyme';
import { extent, max } from 'd3-array';
import { AreaClosed } from '../src';
import { appleStock } from '../../vx-mock-data';
import { scaleTime, scaleLinear } from '../../vx-scale';

const xStock = d => new Date(d.date);
const yStock = d => d.close;

const fakeXScale = scaleTime({
range: [0, 100],
domain: extent(appleStock, xStock)
domain: extent(appleStock, xStock),
});

const fakeYScale = scaleLinear({
range: [100, 0],
domain: [0, max(appleStock, yStock)],
nice: true
nice: true,
});

const AreaClosedWrapper = ({ ...restProps }) =>
Expand All @@ -28,15 +29,38 @@ const AreaClosedWrapper = ({ ...restProps }) =>
x={xStock}
y={yStock}
{...restProps}
/>
/>,
);

describe("<AreaClosed />", () => {
test("it should be defined", () => {
describe('<AreaClosed />', () => {
test('it should be defined', () => {
expect(AreaClosed).toBeDefined();
});

test("it should have the .vx-area-closed class", () => {
expect(AreaClosedWrapper().find('path').prop("className")).toBe("vx-area-closed");
test('it should have the .vx-area-closed class', () => {
expect(
AreaClosedWrapper()
.find('path')
.prop('className'),
).toBe('vx-area-closed');
});

test('it should expose its ref via an innerRef prop', done => {
const node = document.createElement('div');
const refCallback = n => {
expect(n.tagName).toEqual('PATH');
done();
};
ReactDOM.render(
<AreaClosed
data={appleStock}
xScale={fakeXScale}
yScale={fakeYScale}
x={xStock}
y={yStock}
innerRef={refCallback}
/>,
node,
);
});
});
17 changes: 14 additions & 3 deletions packages/vx-shape/test/Bar.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { Bar } from '../src';

describe('<Bar />', () => {
test('it should be defined', () => {
expect(Bar).toBeDefined()
})
})
expect(Bar).toBeDefined();
});

test('it should expose its ref via an innerRef prop', done => {
const node = document.createElement('div');
const refCallback = n => {
expect(n.tagName).toEqual('RECT');
done();
};
ReactDOM.render(<Bar innerRef={refCallback} />, node);
});
});
31 changes: 21 additions & 10 deletions packages/vx-shape/test/Line.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import React from "react";
import { shallow } from "enzyme";
import { Line } from "../src";
import React from 'react';
import ReactDOM from 'react-dom';
import { shallow } from 'enzyme';
import { Line } from '../src';

const LineWrapper = ({ ...restProps }) => shallow(<Line {...restProps} />);
const LineWrapper = ({ ...restProps }) =>
shallow(<Line {...restProps} />);

describe("<Line />", () => {
test("it should be defined", () => {
describe('<Line />', () => {
test('it should be defined', () => {
expect(Line).toBeDefined();
});

test("it should contain a <line/>", () => {
expect(LineWrapper().find("line").length).toBe(1);
test('it should contain a <line/>', () => {
expect(LineWrapper().find('line').length).toBe(1);
});

test("it should have the .vx-line class", () => {
expect(LineWrapper().prop("className")).toBe("vx-line");
test('it should have the .vx-line class', () => {
expect(LineWrapper().prop('className')).toBe('vx-line');
});

test('it should expose its ref via an innerRef prop', done => {
const node = document.createElement('div');
const refCallback = n => {
expect(n.tagName).toEqual('LINE');
done();
};
ReactDOM.render(<Line innerRef={refCallback} />, node);
});
});
Loading

0 comments on commit 0955af6

Please sign in to comment.