-
Notifications
You must be signed in to change notification settings - Fork 730
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #507 from hshoff/chris--typescript-vx-shape
typescript(vx-shape): Re-write package in TypeScript
- Loading branch information
Showing
82 changed files
with
2,056 additions
and
1,775 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* eslint-disable @typescript-eslint/unbound-method */ | ||
import React from 'react'; | ||
import cx from 'classnames'; | ||
import { arc as d3Arc, Arc as ArcType } from 'd3-shape'; | ||
import setNumOrAccessor, { NumberAccessor } from '../util/setNumberOrNumberAccessor'; | ||
import { $TSFIXME } from '../types'; | ||
|
||
export type ArcProps<Datum> = { | ||
/** className applied to path element. */ | ||
className?: string; | ||
/** A Datum for which to generate an arc. */ | ||
data?: Datum; | ||
/** Override render function which is passed the configured arc generator as input. */ | ||
children?: (args: { path: ArcType<$TSFIXME, Datum> }) => React.ReactNode; | ||
/** React ref to the path element. */ | ||
innerRef?: React.Ref<SVGPathElement>; | ||
/** Number or accessor function which returns a number, which defines the arc innerRadius. */ | ||
innerRadius?: NumberAccessor<Datum> | number; | ||
/** Number or accessor function which returns a number, which defines the arc outerRadius. */ | ||
outerRadius?: NumberAccessor<Datum> | number; | ||
/** Number or accessor function which returns a number, which defines the arc cornerRadius. */ | ||
cornerRadius?: NumberAccessor<Datum> | number; | ||
/** Number or accessor function which returns a number, which defines the arc startAngle. */ | ||
startAngle?: NumberAccessor<Datum> | number; | ||
/** Number or accessor function which returns a number, which defines the arc endAngle. */ | ||
endAngle?: NumberAccessor<Datum> | number; | ||
/** Number or accessor function which returns a number, which defines the arc padAngle. */ | ||
padAngle?: NumberAccessor<Datum> | number; | ||
/** Number or accessor function which returns a number, which defines the arc padRadius. */ | ||
padRadius?: NumberAccessor<Datum> | number; | ||
}; | ||
|
||
export default function Arc<Datum>({ | ||
className, | ||
data, | ||
innerRadius, | ||
outerRadius, | ||
cornerRadius, | ||
startAngle, | ||
endAngle, | ||
padAngle, | ||
padRadius, | ||
children, | ||
innerRef, | ||
...restProps | ||
}: ArcProps<Datum> & Omit<React.SVGProps<SVGPathElement>, keyof ArcProps<Datum>>) { | ||
const arc = d3Arc<Datum>(); | ||
if (innerRadius != null) setNumOrAccessor(arc.innerRadius, innerRadius); | ||
if (outerRadius != null) setNumOrAccessor(arc.outerRadius, outerRadius); | ||
if (cornerRadius != null) setNumOrAccessor(arc.cornerRadius, cornerRadius); | ||
if (startAngle != null) setNumOrAccessor(arc.startAngle, startAngle); | ||
if (endAngle != null) setNumOrAccessor(arc.endAngle, endAngle); | ||
if (padAngle != null) setNumOrAccessor(arc.padAngle, padAngle); | ||
if (padRadius != null) setNumOrAccessor(arc.padRadius, padRadius); | ||
|
||
if (children) return <>{children({ path: arc })}</>; | ||
if (!data) return null; | ||
|
||
return ( | ||
<path ref={innerRef} className={cx('vx-arc', className)} d={arc(data) || ''} {...restProps} /> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react'; | ||
import cx from 'classnames'; | ||
import { area, Area as AreaType, CurveFactory } from 'd3-shape'; | ||
import setNumOrAccessor from '../util/setNumberOrNumberAccessor'; | ||
|
||
type NumberAccessor<Datum> = (datum: Datum, index: number, data: Datum[]) => number; | ||
|
||
export type AreaProps<Datum> = { | ||
/** Override render function which is passed the configured area generator as input. */ | ||
children?: (args: { path: AreaType<Datum> }) => React.ReactNode; | ||
/** Classname applied to path element. */ | ||
className?: string; | ||
/** Array of data for which to generate an area shape. */ | ||
data?: Datum[]; | ||
/** The defined accessor for the shape. The final area shape includes all points for which this function returns true. By default all points are defined. */ | ||
defined?: (datum: Datum, index: number, data: Datum[]) => boolean; | ||
/** Sets the curve factory (from @vx/curve or d3-curve) for the area generator. Defaults to curveLinear. */ | ||
curve?: CurveFactory; | ||
/** React RefObject passed to the path element. */ | ||
innerRef?: React.Ref<SVGPathElement>; | ||
/** Sets the x0 accessor function, and sets x1 to null. */ | ||
x?: NumberAccessor<Datum> | number; | ||
/** Specifies the x0 accessor function which defaults to d => d[0]. */ | ||
x0?: NumberAccessor<Datum> | number; | ||
/** Specifies the x1 accessor function which defaults to null. */ | ||
x1?: NumberAccessor<Datum> | number; | ||
/** Sets the y0 accessor function, and sets y1 to null. */ | ||
y?: NumberAccessor<Datum> | number; | ||
/** Specifies the y0 accessor function which defaults to d => 0. */ | ||
y0?: NumberAccessor<Datum> | number; | ||
/** Specifies the y1 accessor function which defaults to d => d[1]. */ | ||
y1?: NumberAccessor<Datum> | number; | ||
}; | ||
|
||
export default function Area<Datum>({ | ||
children, | ||
x, | ||
x0, | ||
x1, | ||
y, | ||
y0, | ||
y1, | ||
data = [], | ||
defined = () => true, | ||
className, | ||
curve, | ||
innerRef, | ||
...restProps | ||
}: AreaProps<Datum> & Omit<React.SVGProps<SVGPathElement>, keyof AreaProps<Datum>>) { | ||
const path = area<Datum>(); | ||
if (x) setNumOrAccessor(path.x, x); | ||
if (x0) setNumOrAccessor(path.x0, x0); | ||
if (x1) setNumOrAccessor(path.x1, x1); | ||
if (y) setNumOrAccessor(path.y, y); | ||
if (y0) setNumOrAccessor(path.y0, y0); | ||
if (y1) setNumOrAccessor(path.y1, y1); | ||
if (defined) path.defined(defined); | ||
if (curve) path.curve(curve); | ||
if (children) return <>{children({ path })}</>; | ||
return ( | ||
<path ref={innerRef} className={cx('vx-area', className)} d={path(data) || ''} {...restProps} /> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* eslint-disable @typescript-eslint/unbound-method */ | ||
import React from 'react'; | ||
import cx from 'classnames'; | ||
import { area } from 'd3-shape'; | ||
import { AreaProps } from './Area'; | ||
import { ScaleType } from '../types'; | ||
import setNumOrAccessor from '../util/setNumberOrNumberAccessor'; | ||
|
||
export type AreaClosedProps<Datum> = { | ||
yScale: ScaleType; | ||
} & Pick< | ||
AreaProps<Datum>, | ||
| 'className' | ||
| 'innerRef' | ||
| 'children' | ||
| 'curve' | ||
| 'defined' | ||
| 'data' | ||
| 'x' | ||
| 'x0' | ||
| 'x1' | ||
| 'y' | ||
| 'y0' | ||
| 'y1' | ||
>; | ||
|
||
export default function AreaClosed<Datum>({ | ||
x, | ||
x0, | ||
x1, | ||
y, | ||
y1, | ||
y0, | ||
yScale, | ||
data = [], | ||
defined = () => true, | ||
className, | ||
curve, | ||
innerRef, | ||
children, | ||
...restProps | ||
}: AreaClosedProps<Datum> & Omit<React.SVGProps<SVGPathElement>, keyof AreaClosedProps<Datum>>) { | ||
const path = area<Datum>(); | ||
if (x) setNumOrAccessor(path.x, x); | ||
if (x0) setNumOrAccessor(path.x0, x0); | ||
if (x1) setNumOrAccessor(path.x1, x1); | ||
if (y0) { | ||
setNumOrAccessor(path.y0, y0); | ||
} else { | ||
/** | ||
* by default set the baseline to the first element of the yRange | ||
* @TODO take the minimum instead? | ||
*/ | ||
path.y0(yScale.range()[0]); | ||
} | ||
if (y && !y1) setNumOrAccessor(path.y1, y); | ||
if (y1 && !y) setNumOrAccessor(path.y1, y1); | ||
if (defined) path.defined(defined); | ||
if (curve) path.curve(curve); | ||
if (children) return <>{children({ path })}</>; | ||
return ( | ||
<path | ||
ref={innerRef} | ||
className={cx('vx-area-closed', className)} | ||
d={path(data) || ''} | ||
{...restProps} | ||
/> | ||
); | ||
} |
Oops, something went wrong.