-
Notifications
You must be signed in to change notification settings - Fork 715
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
feat(shape): update typings and add factory functions #776
Conversation
020d5b1
to
a0fb156
Compare
@@ -45,3 +45,7 @@ export { default as LinkVerticalStep, pathVerticalStep } from './shapes/link/ste | |||
export { default as LinkRadialStep, pathRadialStep } from './shapes/link/step/LinkRadialStep'; | |||
export { default as Polygon, getPoints, getPoint } from './shapes/Polygon'; | |||
export { default as Circle } from './shapes/Circle'; | |||
|
|||
// Export factory functions | |||
export * from './types/D3ShapeConfig'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 Also export the factories
if (padAngle != null) setNumOrAccessor(arc.padAngle, padAngle); | ||
if (padRadius != null) setNumOrAccessor(arc.padRadius, padRadius); | ||
}: AddSVGProps<ArcProps<Datum>, SVGPathElement>) { | ||
const path = arc({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 Use the factory to create path
if (endAngle != null) setNumOrAccessor(arc.endAngle, endAngle); | ||
if (padAngle != null) setNumOrAccessor(arc.padAngle, padAngle); | ||
if (padRadius != null) setNumOrAccessor(arc.padRadius, padRadius); | ||
}: AddSVGProps<ArcProps<Datum>, SVGPathElement>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 Use helper type
const x1Range = x1Scale.range(); | ||
const x1Domain = x1Scale.domain(); | ||
|
||
const barWidth = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic appears in a few files so it was extracted to util/getBandwidth.ts
@@ -75,7 +66,7 @@ export default function BarStackComponent<Datum, Key extends StackKey = StackKey | |||
const barHeight = (yScale(y0(bar)) || 0) - (yScale(y1(bar)) || 0); | |||
const barY = yScale(y1(bar)); | |||
const barX = | |||
'bandwidth' in xScale && typeof xScale.bandwidth === 'function' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bandwidth is always a function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm v excited that we are enforcing proper usage of band scales here versus anything before with potential to break 😅 not sure why the function check was there before 🤔
@@ -0,0 +1,9 @@ | |||
export type Accessor<Datum, Output> = (d: Datum) => Output; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 All accessor types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for making these more accurate/pulling them out 💯
@@ -0,0 +1,25 @@ | |||
import { $TSFIXME } from '../types'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 Simple default accessors that are used in many places
@@ -55,7 +55,7 @@ describe('<Arc />', () => { | |||
ArcChildren({ children: fn }); | |||
const args = fn.mock.calls[0][0]; | |||
const keys = Object.keys(args); | |||
expect(keys.includes('path')).toEqual(true); | |||
expect(keys).toContain('path'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 eslint
auto-replace this
@@ -64,7 +65,7 @@ describe('<Area />', () => { | |||
AreaChildren({ children: fn }); | |||
const args = fn.mock.calls[0][0]; | |||
const keys = Object.keys(args); | |||
expect(keys.includes('path')).toEqual(true); | |||
expect(keys).toContain('path'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 eslint
auto-replace this
xScale.range = () => [0, 100]; | ||
xScale.domain = () => [0, 100]; | ||
xScale.copy = () => xScale; | ||
const yScale = scaleLinear({ domain: [0, 100], range: [100, 0] }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 replace with real scale
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is another epic improvement, thanks for going through all of @vx/shape
, it's a beast 👹
had a couple comments, overall it looks great!
@@ -75,7 +66,7 @@ export default function BarStackComponent<Datum, Key extends StackKey = StackKey | |||
const barHeight = (yScale(y0(bar)) || 0) - (yScale(y1(bar)) || 0); | |||
const barY = yScale(y1(bar)); | |||
const barX = | |||
'bandwidth' in xScale && typeof xScale.bandwidth === 'function' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm v excited that we are enforcing proper usage of band scales here versus anything before with potential to break 😅 not sure why the function check was there before 🤔
@@ -50,11 +50,6 @@ const PieChildren = ({ children, ...restProps }: Partial<PieProps<Datum>>) => | |||
); | |||
|
|||
describe('<Pie />', () => { | |||
beforeEach(() => { | |||
// eslint-disable-next-line @typescript-eslint/unbound-method | |||
global.console.error = jest.fn(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this because nimbus
handles this for us?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This console.error
is one test I am not sure what is going on. It still throw but console.error
was not called. I also question the need to check if console.error
is called.
Spent half a day trying to dig into issue. Could not figure out why. I checked the demo and verify that Pie
is still working fine, so I would like to drop this.
💥 Breaking Changes
🚀 Enhancements
Extract and export new shape Factories
Create new factory functions for
d3-shape
and export as part ofvx/shape
(arc
,area
,line
,pie
,radialLine
),similar to
vx/scale
has factories ford3-scale
.Example
These factories are used to simplify and reduce duplicate implementation in
Arc
,Pie
, etc.Improve typings
Scale types
Replace custom-defined scale types in
vx/shape
with types fromvx/scale
.Shared types
Previously there is a pattern of component
A
pick props fromB
andB
pick fromC
. Chain picking like this can be hard to trace so the common props are extracted asBaseXXXProps
and declared intypes
directory for the component to import and extends.Consistent Accessor types
Previously each file declare its own accessor types. We had several
NumberAccessor
which meant different things. This PR refactors and unifies them intypes/accessor.ts
Type helpers
Most of the React shape components in this package augment the props with native SVG Props. This pattern was extracted as a helper type and used in all the React shape components.
Default accessors
Default accessors for x, y, source, target are extracted to
utils/accessors.ts
Reduce $TSFIXME type
Replace with strict typings when possible.