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

feat: update vx/axis types #773

Merged
merged 23 commits into from
Aug 2, 2020
Merged

feat: update vx/axis types #773

merged 23 commits into from
Aug 2, 2020

Conversation

kristw
Copy link
Collaborator

@kristw kristw commented Jul 30, 2020

💥 Breaking Changes

  • N/A

🚀 Enhancements

vx/axis

  • Consistent and compatible typings across vx/scale and vx/axis.
  • More fields passed to child render props of Axis
  • Also export types

vx/scale

  • A few new types: AnyD3Scale, InferD3ScaleOutput, InferD3ScaleDiscreteInput, InferD3ScaleThresholdInput and ScaleInput.
  • A few new utilities functions: getTicks, coerceNumber and toString.

📝 Documentation

  • More field documentation

🏠 Internal

vx/axis

  • The SharedAxisProps type is now based on generic type Scale instead of ScaleInput. We can infer ScaleInput from Scale but not the other way around.
  • Refactor Axis. Separate the logic that were used when a child renderer prop (children) was not provided into its own component AxisRenderer. This AxisRenderer will receive the exact same props with children so that enforces us to pass everything necessary.
  • Refactor AxisRight, AxisLeft, AxisBottom, AxisTop to only list modified/used fields and pass the rest of props through via ....
  • Merge type AxisOrientation and constant Orientation. Also refer to it as Orientation consistently. (Replace all ORIENT and AxisOrientation with Orientation)
  • Rename center.ts to getTickPosition.ts, toNumberOrUndefined.ts to coerceNumber.ts, labelTransform.ts to getLabelTransform.ts

🏋️ Tests

  • Added unit tests and verified that the demo still look the same.


/** 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
Copy link
Collaborator Author

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> {
Copy link
Collaborator Author

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> & {
Copy link
Collaborator Author

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>;
Copy link
Collaborator Author

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.

@kristw kristw marked this pull request as ready for review July 30, 2020 19:22

export default function AxisBottom<ScaleInput>({
children,
export default function AxisBottom<Scale extends AxisScale>({
Copy link
Collaborator Author

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';
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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({
Copy link
Collaborator Author

@kristw kristw Jul 30, 2020

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).

@hshoff hshoff added this to the 0.0.199 milestone Aug 2, 2020
@hshoff hshoff merged commit 2a5e23e into airbnb:master Aug 2, 2020
@kristw kristw deleted the kristw--axis branch August 17, 2020 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants