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-glyph): re-write package in TypeScript #518

Merged
merged 7 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/vx-glyph/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,9 +34,12 @@
"access": "public"
},
"peerDependencies": {
"react": "^15.0.0-0 || ^16.0.0-0"
"react": "^16.3.0-0"
},
"dependencies": {
"@types/classnames": "^2.2.9",
"@types/d3-shape": "^1.3.1",
"@types/react": "*",
"@vx/group": "0.0.192",
"classnames": "^2.2.5",
"d3-shape": "^1.2.0",
Expand Down
19 changes: 0 additions & 19 deletions packages/vx-glyph/src/glyphs/Glyph.jsx

This file was deleted.

22 changes: 22 additions & 0 deletions packages/vx-glyph/src/glyphs/Glyph.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import cx from 'classnames';
import { Group } from '@vx/group';

export type GlyphProps = {
/** Top offset to apply to glyph g element container. */
top?: number;
/** Left offset to apply to glyph g element container. */
left?: number;
/** classname to apply to glyph g element container. */
className?: string;
/** Children to render. */
children?: React.ReactNode;
};

export default function Glyph({ top = 0, left = 0, className, children }: GlyphProps) {
return (
<Group className={cx('vx-glyph', className)} top={top} left={left}>
{children}
</Group>
);
}
25 changes: 0 additions & 25 deletions packages/vx-glyph/src/glyphs/GlyphCircle.jsx

This file was deleted.

41 changes: 41 additions & 0 deletions packages/vx-glyph/src/glyphs/GlyphCircle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import cx from 'classnames';
import { Symbol, symbol, symbolCircle } from 'd3-shape';
import Glyph from './Glyph';

export type GlyphCircleProps<Datum> = {
/** Render function override which is passed the configured path generator. */
children?: ({ path }: { path: Symbol<any, Datum> }) => React.ReactNode;
/** classname to apply to glyph path element. */
className?: string;
/** Top offset to apply to glyph g element container. */
top?: number;
/** Left offset to apply to glyph g element container. */
left?: number;
/** Size of circle in px, or an accessor which takes Datum as input and returns a size. */
size?: number | ((d: Datum) => number);
};

export default function GlyphCircle<Datum = any>({
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Datum generic here is not useful unless the consumer leverages the children override. but since d3s symbol takes it as an arg I think exposing it is good.

children,
className,
top,
left,
size,
...restProps
}: GlyphCircleProps<Datum> & Omit<React.SVGProps<SVGPathElement>, keyof GlyphCircleProps<Datum>>) {
const path = symbol<Datum>();
path.type(symbolCircle);

// TS can't differentiate the method overload here
if (typeof size === 'number') path.size(size);
else if (size) path.size(size);

if (children) return <>{children({ path })}</>;

return (
<Glyph top={top} left={left}>
<path className={cx('vx-glyph-circle', className)} d={path() || ''} {...restProps} />
</Glyph>
);
}
25 changes: 0 additions & 25 deletions packages/vx-glyph/src/glyphs/GlyphCross.jsx

This file was deleted.

41 changes: 41 additions & 0 deletions packages/vx-glyph/src/glyphs/GlyphCross.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import cx from 'classnames';
import { Symbol, symbol, symbolCross } from 'd3-shape';
import Glyph from './Glyph';

export type GlyphCrossProps<Datum> = {
/** Render function override which is passed the configured path generator. */
children?: ({ path }: { path: Symbol<any, Datum> }) => React.ReactNode;
/** classname to apply to glyph path element. */
className?: string;
/** Top offset to apply to glyph g element container. */
top?: number;
/** Left offset to apply to glyph g element container. */
left?: number;
/** Size of cross in px, or an accessor which takes Datum as input and returns a size. */
size?: number | ((d: Datum) => number);
};

export default function GlyphCross<Datum = any>({
children,
className,
top,
left,
size,
...restProps
}: GlyphCrossProps<Datum> & Omit<React.SVGProps<SVGPathElement>, keyof GlyphCrossProps<Datum>>) {
const path = symbol<Datum>();
path.type(symbolCross);

// TS can't differentiate the method overload here
if (typeof size === 'number') path.size(size);
else if (size) path.size(size);

if (children) return <>{children({ path })}</>;

return (
<Glyph top={top} left={left}>
<path className={cx('vx-glyph-cross', className)} d={path() || ''} {...restProps} />
</Glyph>
);
}
25 changes: 0 additions & 25 deletions packages/vx-glyph/src/glyphs/GlyphDiamond.jsx

This file was deleted.

42 changes: 42 additions & 0 deletions packages/vx-glyph/src/glyphs/GlyphDiamond.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react';
import cx from 'classnames';
import { Symbol, symbol, symbolDiamond } from 'd3-shape';
import Glyph from './Glyph';

export type GlyphDiamondProps<Datum> = {
/** Render function override which is passed the configured path generator. */
children?: ({ path }: { path: Symbol<any, Datum> }) => React.ReactNode;
/** classname to apply to glyph path element. */
className?: string;
/** Top offset to apply to glyph g element container. */
top?: number;
/** Left offset to apply to glyph g element container. */
left?: number;
/** Size of diamond in px, or an accessor which takes Datum as input and returns a size. */
size?: number | ((d: Datum) => number);
};

export default function GlyphDiamond<Datum = any>({
children,
className,
top,
left,
size,
...restProps
}: GlyphDiamondProps<Datum> &
Omit<React.SVGProps<SVGPathElement>, keyof GlyphDiamondProps<Datum>>) {
const path = symbol<Datum>();
path.type(symbolDiamond);

// TS can't differentiate the method overload here
if (typeof size === 'number') path.size(size);
else if (size) path.size(size);

if (children) return <>{children({ path })}</>;

return (
<Glyph top={top} left={left}>
<path className={cx('vx-glyph-diamond', className)} d={path() || ''} {...restProps} />
</Glyph>
);
}
19 changes: 0 additions & 19 deletions packages/vx-glyph/src/glyphs/GlyphDot.jsx

This file was deleted.

31 changes: 31 additions & 0 deletions packages/vx-glyph/src/glyphs/GlyphDot.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import cx from 'classnames';
import Glyph from './Glyph';

export type GlyphDotProps = {
/** classname to apply to glyph path element. */
className?: string;
/** Top offset to apply to glyph g element container. */
top?: number;
/** Left offset to apply to glyph g element container. */
left?: number;
/** Radius of dot. */
r?: number;
/** x coordinate of the center of the dot. */
cx?: number;
/** y coordinate of the center of the dot. */
cy?: number;
};

export default function GlyphDot({
top = 0,
left = 0,
className,
...restProps
}: GlyphDotProps & Omit<React.SVGProps<SVGCircleElement>, keyof GlyphDotProps>) {
return (
<Glyph top={top} left={left}>
<circle className={cx('vx-glyph-dot', className)} {...restProps} />
</Glyph>
);
}
25 changes: 0 additions & 25 deletions packages/vx-glyph/src/glyphs/GlyphSquare.jsx

This file was deleted.

41 changes: 41 additions & 0 deletions packages/vx-glyph/src/glyphs/GlyphSquare.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import cx from 'classnames';
import { Symbol, symbol, symbolSquare } from 'd3-shape';
import Glyph from './Glyph';

export type GlyphSquareProps<Datum> = {
/** Render function override which is passed the configured path generator. */
children?: ({ path }: { path: Symbol<any, Datum> }) => React.ReactNode;
/** classname to apply to glyph path element. */
className?: string;
/** Top offset to apply to glyph g element container. */
top?: number;
/** Left offset to apply to glyph g element container. */
left?: number;
/** Size of square in px, or an accessor which takes Datum as input and returns a size. */
size?: number | ((d: Datum) => number);
};

export default function GlyphSquare<Datum = any>({
children,
className,
top,
left,
size,
...restProps
}: GlyphSquareProps<Datum> & Omit<React.SVGProps<SVGPathElement>, keyof GlyphSquareProps<Datum>>) {
const path = symbol<Datum>();
path.type(symbolSquare);

// TS can't differentiate the method overload here
if (typeof size === 'number') path.size(size);
else if (size) path.size(size);

if (children) return <>{children({ path })}</>;

return (
<Glyph top={top} left={left}>
<path className={cx('vx-glyph-square', className)} d={path() || ''} {...restProps} />
</Glyph>
);
}
Loading