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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/vx-tooltip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"sideEffects": false,
"main": "lib/index.js",
"module": "esm/index.js",
"types": "lib/index.d.ts",
"files": [
"lib",
"esm"
Expand All @@ -27,6 +28,8 @@
},
"homepage": "https://github.com/hshoff/vx#readme",
"dependencies": {
"@types/react": "*",
"@types/classnames": "^2.2.9",
"@vx/bounds": "0.0.192",
"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.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
import React from 'react';
import PropTypes from 'prop-types';

export const withTooltipPropTypes = {
tooltipOpen: PropTypes.bool,
tooltipLeft: PropTypes.number,
tooltipTop: PropTypes.number,
tooltipData: PropTypes.object,
updateTooltip: PropTypes.func,
showTooltip: PropTypes.func,
hideTooltip: PropTypes.func,
export type WithTooltipProvidedProps = {
tooltipOpen?: boolean;
tooltipLeft?: number;
tooltipTop?: number;
tooltipData?: object;
updateTooltip?: (args: UpdateTooltipArgs) => void;
showTooltip?: (args: ShowTooltipArgs) => void;
hideTooltip?: () => void;
};
type WithTooltipState = Pick<
WithTooltipProvidedProps,
'tooltipOpen' | 'tooltipLeft' | 'tooltipTop' | 'tooltipData'
>;
type ShowTooltipArgs = Omit<WithTooltipState, 'tooltipOpen'>;
type UpdateTooltipArgs = WithTooltipState;
type WithTooltipContainerProps = { style: React.CSSProperties };

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 👍

BaseComponent: React.ComponentType<Props>,
containerProps: WithTooltipContainerProps = {
style: {
position: 'relative',
width: 'inherit',
height: 'inherit',
},
} as const,
},
) {
class WrappedComponent extends React.PureComponent {
constructor(props) {
return class WrappedComponent extends React.PureComponent<Props, WithTooltipState> {
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

super(props);
this.state = {
tooltipOpen: false,
Expand All @@ -34,7 +40,7 @@ export default function withTooltip(
this.showTooltip = this.showTooltip.bind(this);
this.hideTooltip = this.hideTooltip.bind(this);
}
updateTooltip({ tooltipOpen, tooltipLeft, tooltipTop, tooltipData }) {
updateTooltip({ tooltipOpen, tooltipLeft, tooltipTop, tooltipData }: UpdateTooltipArgs) {
this.setState(prevState => ({
...prevState,
tooltipOpen,
Expand All @@ -43,7 +49,7 @@ export default function withTooltip(
tooltipData,
}));
}
showTooltip({ tooltipLeft, tooltipTop, tooltipData }) {
showTooltip({ tooltipLeft, tooltipTop, tooltipData }: ShowTooltipArgs) {
this.updateTooltip({
tooltipOpen: true,
tooltipLeft,
Expand Down Expand Up @@ -72,6 +78,5 @@ export default function withTooltip(
</div>
);
}
}
return WrappedComponent;
};
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import React from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';

Tooltip.propTypes = {
left: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
top: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
className: PropTypes.string,
style: PropTypes.object,
children: PropTypes.any,
export type TooltipProps = {
left?: number;
top?: number;
className?: string;
style?: React.CSSProperties;
children?: React.ReactNode;
};

export default function Tooltip({ className, top, left, style, children, ...restProps }) {
export default function Tooltip({
className,
top,
left,
style,
children,
...restProps
}: TooltipProps & JSX.IntrinsicElements['div']) {
return (
<div
className={cx('vx-tooltip-portal', className)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
import React from 'react';
import PropTypes from 'prop-types';
// @ts-ignore This line could be removed after bounds migration to the TS
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.


import Tooltip from './Tooltip';

const rectShape = PropTypes.shape({
top: PropTypes.number.isRequired,
right: PropTypes.number.isRequired,
bottom: PropTypes.number.isRequired,
left: PropTypes.number.isRequired,
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
});

const withBoundingRectsProps = {
getRects: PropTypes.func,
rect: rectShape,
parentRect: rectShape,
type RectShape = {
top: number;
right: number;
bottom: number;
left: number;
width: number;
height: number;
};

const tooltipProps = {
left: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
top: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
className: PropTypes.string,
style: PropTypes.object,
children: PropTypes.any,
type WithBoundingRectsProps = {
getRects?: () => RectShape;
rect?: RectShape;
parentRect?: RectShape;
};

const propTypes = {
...withBoundingRectsProps,
...tooltipProps,
offsetLeft: PropTypes.number,
offsetTop: PropTypes.number,
type TooltipProps = {
left?: number;
top?: number;
className?: string;
style?: React.CSSProperties;
children?: React.ReactNode;
};

const defaultProps = {};
type Props = {
offsetLeft?: number;
offsetTop?: number;
} & TooltipProps &
WithBoundingRectsProps;

function TooltipWithBounds({
left: initialLeft,
top: initialTop,
left: initialLeft = 0,
top: initialTop = 0,
offsetLeft = 10,
offsetTop = 10,
rect,
Expand All @@ -47,7 +44,7 @@ function TooltipWithBounds({
children,
style,
...otherProps
}) {
}: Props) {
let left = initialLeft;
let top = initialTop;

Expand Down Expand Up @@ -76,7 +73,4 @@ function TooltipWithBounds({
);
}

TooltipWithBounds.propTypes = propTypes;
TooltipWithBounds.defaultProps = defaultProps;

export default withBoundingRects(TooltipWithBounds);