-
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
typescript(vx-tooltip): re-write package in TypeScript #504
Changes from all commits
5dd4a96
548f595
bccc5c8
90d2804
ac76afa
9789610
75b3ab4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 = {}>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks good, if the user specifies props on their component as |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re comment above I think this will become |
||
super(props); | ||
this.state = { | ||
tooltipOpen: false, | ||
|
@@ -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, | ||
|
@@ -43,7 +49,7 @@ export default function withTooltip( | |
tooltipData, | ||
})); | ||
} | ||
showTooltip({ tooltipLeft, tooltipTop, tooltipData }) { | ||
showTooltip({ tooltipLeft, tooltipTop, tooltipData }: ShowTooltipArgs) { | ||
this.updateTooltip({ | ||
tooltipOpen: true, | ||
tooltipLeft, | ||
|
@@ -72,6 +78,5 @@ export default function withTooltip( | |
</div> | ||
); | ||
} | ||
} | ||
return WrappedComponent; | ||
}; | ||
} |
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. may need a |
||
|
||
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, | ||
|
@@ -47,7 +44,7 @@ function TooltipWithBounds({ | |
children, | ||
style, | ||
...otherProps | ||
}) { | ||
}: Props) { | ||
let left = initialLeft; | ||
let top = initialTop; | ||
|
||
|
@@ -76,7 +73,4 @@ function TooltipWithBounds({ | |
); | ||
} | ||
|
||
TooltipWithBounds.propTypes = propTypes; | ||
TooltipWithBounds.defaultProps = defaultProps; | ||
|
||
export default withBoundingRects(TooltipWithBounds); |
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.
We actually would like to keep this around,
@airbnb/config-babel
addsbabel-plugin-typescript-to-proptypes
when used with@airbnb/config-typescript
which generatesPropTypes
forreact
componenttypes
/interfaces
, and requires us to keep theprop-types
dependency though it's no longer referenced in source code.