Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
fix(Tooltip): Add Reference Type checking and documentation comments
Browse files Browse the repository at this point in the history
The <Reference> component of the render method will check for the type props and return a usable
component accordingly.
  • Loading branch information
Greg-Hamel committed May 30, 2018
1 parent 62487d5 commit 268744d
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/components/Tooltip/Tooltip.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@ import type { PopperChildrenProps, ReferenceChildrenProps } from "react-popper";
import styles from "./Tooltip.css";

type Props = {|
+content: string,
+children?: React.Node,
/**
* The reference element which the Tooltip will be based on.
*/
+children?: React.Element<any>,
/**
* Any additional classNames for the Tooltip.
*/
+className?: string,
/**
* This is the text content of the Tooltip.
*/
+content: string,
/**
* This is the placement of the Tooltip (top, bottom, left, right).
*/
+placement?: Placement,
+type?: "link",
|};

type State = {
Expand Down Expand Up @@ -52,15 +65,22 @@ class Tooltip extends React.Component<Props, State> {
return (
<Manager>
<Reference>
{({ ref }: ReferenceChildrenProps) => (
<div
ref={ref}
onMouseEnter={this._handleTriggerOnMouseEnter}
onMouseLeave={this._handleTriggerOnMouseLeave}
>
{children}
</div>
)}
{({ ref }: ReferenceChildrenProps) =>
typeof children === "undefined"
? // Type checking, undefined cannot be passed to React.cloneElement
console.error("undefined children provided to Tooltip.")
: this.props.type
? React.cloneElement(children, {
ref: ref,
onMouseEnter: this._handleTriggerOnMouseEnter,
onMouseLeave: this._handleTriggerOnMouseLeave,
})
: React.cloneElement(children, {
rootRef: ref,
onMouseEnter: this._handleTriggerOnMouseEnter,
onMouseLeave: this._handleTriggerOnMouseLeave,
})
}
</Reference>
{this.state.isShown && (
<Popper placement={placement} eventsEnabled={true}>
Expand Down

0 comments on commit 268744d

Please sign in to comment.