Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript(vx-voronoi): re-write package in TypeScript #512

Merged
merged 12 commits into from
Nov 14, 2019
Merged
4 changes: 2 additions & 2 deletions packages/vx-voronoi/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ const points = Array(n).fill(null).map(() => ({
// width + height set an extent on the voronoi
// x + y set relevant accessors depending on the shape of your data
const voronoiLayout = voronoi({
x: d => d.x, // maybe pass a scale here?
x: d => d.x,
y: d => d.y,
width,
height,
});

const voronoiDiagram = voronoiLayout(data);
const polygons = voronoiLayout.polygons(points); // equivalent to voronoiDiagram.polygons()
const polygons = voronoiDiagram.polygons(); // equivalent to voronoiLayout.polygons(points)

return (
<svg>
Expand Down
7 changes: 5 additions & 2 deletions packages/vx-voronoi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"sideEffects": false,
"main": "lib/index.js",
"module": "esm/index.js",
"types": "lib/index.d.ts",
"files": [
"lib",
"esm"
Expand Down Expand Up @@ -33,12 +34,14 @@
"access": "public"
},
"dependencies": {
"@vx/group": "0.0.192",
"@types/classnames": "^2.2.9",
"@types/d3-voronoi": "^1.1.9",
"@types/react": "*",
"classnames": "^2.2.5",
"d3-voronoi": "^1.1.2",
"prop-types": "^15.6.1"
},
"peerDependencies": {
"react": "^15.0.0-0 || ^16.0.0-0"
"react": "^16.3.0-0"
}
}
16 changes: 0 additions & 16 deletions packages/vx-voronoi/src/components/VoronoiPolygon.jsx

This file was deleted.

24 changes: 24 additions & 0 deletions packages/vx-voronoi/src/components/VoronoiPolygon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
import cx from 'classnames';

export type VoronoiPolygonProps = {
/** Override render function which is provided polygon and generated path. */
children?: ({ path, polygon }: { path: string; polygon: [number, number][] }) => React.ReactNode;
/** className to apply to path element. */
className?: string;
/** Array of coordinate arrays for the polygon (e.g., [[x,y], [x1,y1], ...]), used to generate polygon path. */
polygon?: [number, number][];
};

export default function VoronoiPolygon({
polygon,
className,
children,
...restProps
}: VoronoiPolygonProps & Omit<React.SVGProps<SVGPathElement>, keyof VoronoiPolygonProps>) {
if (!polygon) return null;
const path = `M${polygon.join('L')}Z`;
if (children) return <>{children({ path, polygon })}</>;

return <path className={cx('vx-voronoi-polygon', className)} d={path} {...restProps} />;
}
File renamed without changes.
17 changes: 0 additions & 17 deletions packages/vx-voronoi/src/voronoi.js

This file was deleted.

28 changes: 28 additions & 0 deletions packages/vx-voronoi/src/voronoi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { voronoi as d3Voronoi } from 'd3-voronoi';

const CLIP_PADDING = 1;

interface Config<Datum> {
width?: number;
height?: number;
x?: (d: Datum) => number;
y?: (d: Datum) => number;
}

/**
* Returns a configured d3 voronoi `layout`. calling `layout(data)` returns a voronoi *diagram*.
* Alternatively call `layout.polygons(data)`, `layout.triangles(data)`, `layout.links(data)`
*/
export default function getVoronoi<Datum>({ width = 0, height = 0, x, y }: Config<Datum>) {
const voronoi = d3Voronoi<Datum>();

if (x) voronoi.x(x);
if (y) voronoi.y(y);

voronoi.extent([
[-CLIP_PADDING, -CLIP_PADDING],
[width + CLIP_PADDING, height + CLIP_PADDING],
]);

return voronoi;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { shallow } from 'enzyme';
import { VoronoiPolygon } from '../src';

describe('<VoronoiPolygon />', () => {
const data = [1, 2];
const polygon = new Array(3).fill(null).map((_, i) => [i, i]);
polygon.data = data;
const polygon: [number, number][] = new Array(3).fill(null).map((_, i) => [i, i]);

const props = { polygon };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ describe('voronoi', () => {
});

test('x param should set voronoi x', () => {
const x = () => 'x!!!';
const x = () => 123;
const v = voronoi({ x });
expect(v.x()).toEqual(x);
});

test('y param should set voronoi y', () => {
const y = () => 'y!!!';
const y = () => 123;
const v = voronoi({ y });
expect(v.y()).toEqual(y);
});
Expand All @@ -21,9 +21,9 @@ describe('voronoi', () => {
const width = 17;
const height = 99;
const v = voronoi({ width, height });
const [, [x1, y1]] = v.extent();

expect(x1).toEqual(width + 1);
expect(y1).toEqual(height + 1);
const extent = v.extent();
const endCoord = extent![1];
expect(endCoord[0]).toEqual(width + 1);
expect(endCoord[1]).toEqual(height + 1);
});
});