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

Merged
merged 7 commits into from
Oct 9, 2019

Conversation

Rudeg
Copy link

@Rudeg Rudeg commented Sep 18, 2019

🚀 Enhancements

This PR builds off of #488 which added TypeScript build support, and converts the @vx-tooltip package to TypeScript.

tooltip component depends on vx/bounds, so CI might be failing because of this. Closes #505.

Testing

  • CI
  • Generates .d.ts files

@williaster @hshoff @schillerk @milesj @kristw

Copy link
Collaborator

@williaster williaster left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! Overall looks good, had a couple thoughts.

"@vx/bounds": "0.0.189",
"classnames": "^2.2.5",
"prop-types": "^15.5.10"
Copy link
Collaborator

Choose a reason for hiding this comment

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

We actually would like to keep this around, @airbnb/config-babel adds babel-plugin-typescript-to-proptypes when used with @airbnb/config-typescript which generates PropTypes for react component types/interfaces, and requires us to keep the prop-types dependency though it's no longer referenced in source code.

className: PropTypes.string,
style: PropTypes.object,
children: PropTypes.any,
type Props = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

could we export these for use by consumers, and (optionally) update to TooltipProps? (there's been back and forth on this vs Props internally but have been trying to use the more specific name variant in @vx)

updateTooltip: PropTypes.func,
showTooltip: PropTypes.func,
hideTooltip: PropTypes.func,
type Props = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we export these for use by consumers (especially important since it's an HOC) and also change to WithTooltipProvidedProps so it's clear that they are props provided by the HOC, not those of the BaseComponent which is wrapped?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Will also need to define a type for WithTooltipState

Copy link
Author

Choose a reason for hiding this comment

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

the types of the state would be the same as part of the WithTooltipProvidedProps, so I defined the state types by using it. If you think it would be better to have it separately - I'll fix it :)

hideTooltip?: () => void;
};

type ShowTooltipArgs = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

could update to Pick<WithTooltipProvidedProps, 'tooltipLeft' | 'tooltipTop' | 'tooltipData'>

tooltipData?: Props['tooltipData'];
};

type UpdateTooltipArgs = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

could update to Pick<WithTooltipProvidedProps, 'tooltipOpen' | 'tooltipLeft' | 'tooltipTop' | 'tooltipData'>

tooltipOpen?: Props['tooltipOpen'];
tooltipLeft?: Props['tooltipLeft'];
tooltipTop?: Props['tooltipTop'];
tooltipData?: Props['tooltipData'];
};

export default function withTooltip(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the props aren't quite right for this. For this to be compatible with any BaseComponent there will need to be a Generic here for the BaseComponents own props. Something like this:

function withTooltip<Props extends object = {}>(
  BaseComponent: (React.Component | React.PureComponent | React.FC)<ProvidedProps & Props>,
  containerProps: WithTooltipContainerProps = ...,
) {
  class WrappedComponent extends React.PureComponent<Props, WithTooltipState> {
     ...
  }
}

@milesj does this look right?

},
},
) {
class WrappedComponent extends React.PureComponent {
constructor(props) {
constructor(props: Props) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Re comment above I think this will become BaseProps

@@ -1,44 +1,40 @@
import React from 'react';
import PropTypes from 'prop-types';
import { withBoundingRects } from '@vx/bounds';
Copy link
Collaborator

Choose a reason for hiding this comment

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

may need a @ts-ignore here as you mentioned because @vx/bounds currently has no types.

'tooltipOpen' | 'tooltipLeft' | 'tooltipTop' | 'tooltipData'
>;
type ShowTooltipArgs = Pick<WithTooltipProvidedProps, 'tooltipLeft' | 'tooltipTop' | 'tooltipData'>;
type UpdateTooltipArgs = Pick<
Copy link
Collaborator

Choose a reason for hiding this comment

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

same type?

type UpdateTooltipArgs = WithTooltipState;

WithTooltipProvidedProps,
'tooltipOpen' | 'tooltipLeft' | 'tooltipTop' | 'tooltipData'
>;
type ShowTooltipArgs = Pick<WithTooltipProvidedProps, 'tooltipLeft' | 'tooltipTop' | 'tooltipData'>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

type ShowTooltipArgs = Omit<WithTooltipState, 'tooltipOpen'>;

Copy link
Collaborator

@williaster williaster left a comment

Choose a reason for hiding this comment

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

Looks great, I had one other small comment on TooltipProps I missed in the first pass. Thanks again!

export default function withTooltip(
BaseComponent,
containerProps = {
export default function withTooltip<Props extends object = {}>(
Copy link
Collaborator

Choose a reason for hiding this comment

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

looks good, if the user specifies props on their component as OwnProps & WithTooltipProvidedProps, this should have everything on it 👍

style,
children,
...restProps
}: TooltipProps) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

missed this one earlier, but we'd like to preserve the ability to pass additional restProps here so we don't make breaking changes. the pattern I think we've aligned on is updating this line to TooltipProps & JSX.IntrinsicElements.div so that TooltipProps can still be imported separately but the component accepts any div props as well.

Copy link
Author

Choose a reason for hiding this comment

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

oh, you are right! It's a great point, thanks!

Copy link
Collaborator

@williaster williaster left a comment

Choose a reason for hiding this comment

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

LGTM 🎉 will hold off on merging until we land a couple upstream PRs 👍

height: 'inherit',
position: 'relative' as const,
width: 'inherit' as const,
height: 'inherit' as const,
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

could do

style: {
  position: 'relative',
  width: 'inherit',
  height: 'inherit'
} as const

@williaster williaster changed the base branch from chris--typescript to master October 4, 2019 20:39
@williaster
Copy link
Collaborator

@Rudeg FYI I updated the base branch to master, and fixed a conflict.

@hshoff hshoff merged commit 131b1d5 into airbnb:master Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Re-write @vx/tooltip in TypeScript
4 participants