Skip to content

Commit

Permalink
Add support for closeOnClick to WithTooltip (#19)
Browse files Browse the repository at this point in the history
Add support for closeOnClick to WithTooltip
  • Loading branch information
domyen authored Jun 18, 2019
2 parents 79a1e54 + e003439 commit fe0f9b3
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions src/components/tooltip/WithTooltip.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import TooltipTrigger from 'react-popper-tooltip';
import { withState } from 'recompose';

import { Tooltip } from './Tooltip';

Expand All @@ -16,6 +15,20 @@ const TargetSvgContainer = styled.g`
cursor: ${props => (props.mode === 'hover' ? 'default' : 'pointer')};
`;

const isDescendantOfAction = element => {
const { parentElement } = element;

if (parentElement.tagName === 'BODY') {
return false;
}

if (parentElement.tagName === 'A' || parentElement.tagName === 'BUTTON') {
return true;
}

return isDescendantOfAction(parentElement);
};

function WithTooltip({
svg,
trigger,
Expand All @@ -25,17 +38,26 @@ function WithTooltip({
hasChrome,
tooltip,
children,
tooltipShown,
onVisibilityChange,
startOpen,
...props
}) {
const Container = svg ? TargetSvgContainer : TargetContainer;
const [isTooltipShown, setTooltipShown] = useState(startOpen);
const toggleTooltipShown = () => setTooltipShown(!isTooltipShown);
const closeTooltipOnClick = event => {
if (!closeOnClick || !isDescendantOfAction(event.target)) {
return;
}

setTooltipShown(false);
};

return (
<TooltipTrigger
placement={placement}
trigger={trigger}
tooltipShown={tooltipShown}
onVisibilityChange={onVisibilityChange}
tooltipShown={isTooltipShown}
onVisibilityChange={toggleTooltipShown}
modifiers={modifiers}
tooltip={({
getTooltipProps,
Expand All @@ -50,16 +72,17 @@ function WithTooltip({
tooltipRef={tooltipRef}
arrowRef={arrowRef}
arrowProps={getArrowProps()}
onClick={closeTooltipOnClick}
{...getTooltipProps()}
>
{typeof tooltip === 'function'
? tooltip({ onHide: () => onVisibilityChange(false) })
? tooltip({ onHide: () => setTooltipShown(false) })
: tooltip}
</Tooltip>
)}
>
{({ getTriggerProps, triggerRef }) => (
<Container ref={triggerRef} {...getTriggerProps()} {...props}>
<Container ref={triggerRef} {...getTriggerProps()} {...props} onClick={toggleTooltipShown}>
{children}
</Container>
)}
Expand All @@ -76,8 +99,7 @@ WithTooltip.propTypes = {
hasChrome: PropTypes.bool,
tooltip: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
children: PropTypes.node.isRequired,
tooltipShown: PropTypes.bool,
onVisibilityChange: PropTypes.func.isRequired,
startOpen: PropTypes.bool,
};

WithTooltip.defaultProps = {
Expand All @@ -87,9 +109,7 @@ WithTooltip.defaultProps = {
placement: 'top',
modifiers: {},
hasChrome: true,
tooltipShown: false,
startOpen: false,
};

export default withState('tooltipShown', 'onVisibilityChange', ({ startOpen }) => startOpen)(
WithTooltip
);
export default WithTooltip;

0 comments on commit fe0f9b3

Please sign in to comment.