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

new(Tooltip): Add onClose prop #355

Merged
merged 3 commits into from
Apr 20, 2020
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
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