-
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: update vx/axis types #773
Conversation
|
||
/** A catch-all type for scales that are compatible with axis */ | ||
export type AxisScale<Output extends AxisScaleOutput = AxisScaleOutput> = | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any |
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 has to be any
. unknown
does not work for this case.
export type SharedAxisProps<ScaleInput> = { | ||
/** The class name applied to the outermost axis group element. */ | ||
axisClassName?: string; | ||
interface CommonProps<Scale extends AxisScale> { |
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 a new interface that is the shared props between Axis
and AxisRenderer
x: number; | ||
y: number; | ||
} | ||
|
||
export type ChildRenderProps<ScaleInput> = { | ||
export type AxisRendererProps<Scale extends AxisScale> = CommonProps<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.
Instead of accepting ScaleInput
generic, accept Scale
with the constraint that it must be a scale compatible for axis (compatible meaning scale output is AxisScaleOutput = number | NumberLike | undefined
regardless of input type) then infer ScaleInput
from Scale
* * "Type 'number' is not assignable to type 'DefaultThresholdInput'" | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type AnyD3Scale = D3Scale<any, any, any>; |
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 has to be any
. Cannot use unknown
.
|
||
export default function AxisBottom<ScaleInput>({ | ||
children, | ||
export default function AxisBottom<Scale extends AxisScale>({ |
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.
only destructure fields that are modified or used.
@@ -0,0 +1,109 @@ | |||
import React from 'react'; |
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.
Extracted from Axis.tsx
@@ -161,7 +161,7 @@ export default function Example({ | |||
} | |||
</BarStack> | |||
</Group> | |||
<AxisBottom<string> | |||
<AxisBottom |
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.
generic type can be inferred from the passed scale
const tickSign = isLeft || isTop ? -1 : 1; | ||
|
||
const position = center(scale.copy()); | ||
|
||
const axisFromPoint = new Point({ |
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 extracted the logic for flipping x
and y
into util function createPoint({x, y}, horizontal)
.
💥 Breaking Changes
🚀 Enhancements
vx/axis
vx/scale
andvx/axis
.Axis
vx/scale
AnyD3Scale
,InferD3ScaleOutput
,InferD3ScaleDiscreteInput
,InferD3ScaleThresholdInput
andScaleInput
.getTicks
,coerceNumber
andtoString
.📝 Documentation
🏠 Internal
vx/axis
SharedAxisProps
type is now based on generic typeScale
instead ofScaleInput
. We can inferScaleInput
fromScale
but not the other way around.Axis
. Separate the logic that were used when a child renderer prop (children
) was not provided into its own componentAxisRenderer
. ThisAxisRenderer
will receive the exact same props withchildren
so that enforces us to pass everything necessary.AxisRight
,AxisLeft
,AxisBottom
,AxisTop
to only list modified/used fields and pass the rest ofprops
through via...
.AxisOrientation
and constantOrientation
. Also refer to it asOrientation
consistently. (Replace allORIENT
andAxisOrientation
withOrientation
)center.ts
togetTickPosition.ts
,toNumberOrUndefined.ts
tocoerceNumber.ts
,labelTransform.ts
togetLabelTransform.ts
🏋️ Tests