Skip to content

Commit

Permalink
new(Tooltip): Add onClose prop (#355)
Browse files Browse the repository at this point in the history
* update(Tooltip): Add  prop

* add test
  • Loading branch information
Austin Piel authored Apr 20, 2020
1 parent 5816442 commit 3eab555
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/components/Tooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export type TooltipProps = {
horizontalAlign?: 'center' | 'left' | 'right';
/** True to use a light background with dark text. */
inverted?: boolean;
/** Callback fired when the tooltip is closed. */
onClose?: () => void;
/** Callback fired when the tooltip is shown. */
onShow?: () => void;
/** True to prevent dismissmal on mouse down. */
Expand Down Expand Up @@ -69,6 +71,7 @@ export class Tooltip extends React.Component<TooltipProps & WithStylesProps, Too
static defaultProps = {
disabled: false,
inverted: false,
onClose() {},
onShow() {},
remainOnMouseDown: false,
toggleOnClick: false,
Expand Down Expand Up @@ -186,6 +189,7 @@ export class Tooltip extends React.Component<TooltipProps & WithStylesProps, Too

private handleClose = () => {
this.setState({ open: false });
this.props.onClose!();
};

private handleMouseEnter = () => {
Expand Down
42 changes: 42 additions & 0 deletions packages/core/src/components/Tooltip/story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@ class TooltipDemo extends React.Component<{}, { text: string; clicked: boolean }
}
}

class TooltipOnCloseDemo extends React.Component<{}> {
state = { text: 'Closed' };

handleOnShow = () => {
this.setState({
text: 'Open',
});
};

handleOnClose = () => {
this.setState({
text: 'Closed',
});
};

render() {
return (
<div style={{ textAlign: 'center' }}>
<Spacing top={10}>
<Tooltip
content="Tooltip"
verticalAlign="above"
onClose={this.handleOnClose}
onShow={this.handleOnShow}
>
<Button>Hover me</Button>
</Tooltip>
<Text>The tooltip is {this.state.text}</Text>
</Spacing>
</div>
);
}
}

class TooltipOnShowDemo extends React.Component<{}, { text: string; count: number }> {
state = { text: 'Hovered 0 times', count: 0 };

Expand Down Expand Up @@ -215,6 +249,14 @@ toggleWithClick.story = {
name: 'Toggle tooltip on click',
};

export function handleOnClose() {
return <TooltipOnCloseDemo />;
}

handleOnClose.story = {
name: 'Callback fired when tooltip is shown and closed',
};

export function withAccessibilityLabel() {
return (
<>
Expand Down
7 changes: 7 additions & 0 deletions packages/core/test/components/Tooltip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ describe('<Tooltip />', () => {
expect(wrapper).toMatchSnapshot();
});

it('fires onClose callback when closed', () => {
const onCloseSpy = jest.fn();
wrapper.setProps({ onClose: onCloseSpy });
childContainer.simulate('mouseleave');
expect(onCloseSpy).toHaveBeenCalled();
});

describe('with remainOnMouseDown', () => {
beforeEach(() => {
wrapper.setProps({ remainOnMouseDown: true });
Expand Down

0 comments on commit 3eab555

Please sign in to comment.