Skip to content

Commit e9acd79

Browse files
authored
Merge pull request #10 from mohsinulhaq/refactor-tooltip-methods
refactor show/hide/toggle methods
2 parents 2d9985d + 82b873b commit e9acd79

File tree

2 files changed

+18
-29
lines changed

2 files changed

+18
-29
lines changed

src/Tooltip.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export default class Tooltip extends PureComponent {
2020
placement: T.string,
2121
trigger: T.string,
2222
clearScheduled: T.func,
23-
scheduleHide: T.func,
2423
tooltip: T.func,
2524
hideTooltip: T.func,
2625
parentOutsideClickHandler: T.func,
@@ -99,7 +98,7 @@ export default class Tooltip extends PureComponent {
9998
props.onMouseEnter
10099
),
101100
onMouseLeave: callAll(
102-
isHoverTriggered && this.props.scheduleHide,
101+
isHoverTriggered && this.props.hideTooltip,
103102
props.onMouseLeave
104103
)
105104
};

src/TooltipTrigger.js

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -89,36 +89,27 @@ export default class TooltipTrigger extends PureComponent {
8989
clearTimeout(this._showTimeout);
9090
};
9191

92-
showTooltip = () => {
92+
_showTooltip = (delay = this.props.delayShow) => {
9393
this._clearScheduled();
94-
this.setState({ tooltipShown: true });
95-
};
9694

97-
hideTooltip = () => {
98-
this._clearScheduled();
99-
this.setState({ tooltipShown: false });
100-
};
101-
102-
toggleTooltip = () => {
103-
this._clearScheduled();
104-
this.setState(prevState => ({
105-
tooltipShown: !prevState.tooltipShown
106-
}));
95+
this._showTimeout = setTimeout(
96+
() => this.setState({ tooltipShown: true }),
97+
delay
98+
);
10799
};
108100

109-
scheduleShow = () => {
101+
_hideTooltip = (delay = this.props.delayHide) => {
110102
this._clearScheduled();
111-
this._showTimeout = setTimeout(this.showTooltip, this.props.delayShow);
112-
};
113103

114-
scheduleHide = () => {
115-
this._clearScheduled();
116-
this._hideTimeout = setTimeout(this.hideTooltip, this.props.delayHide);
104+
this._hideTimeout = setTimeout(
105+
() => this.setState({ tooltipShown: false }),
106+
delay
107+
);
117108
};
118109

119-
scheduleToggle = () => {
120-
const action = this.state.tooltipShown ? 'scheduleHide' : 'scheduleShow';
121-
this[action]();
110+
_toggleTooltip = delay => {
111+
const action = this.state.tooltipShown ? '_hideTooltip' : '_showTooltip';
112+
this[action](delay);
122113
};
123114

124115
_contextMenuToggle = event => {
@@ -142,17 +133,17 @@ export default class TooltipTrigger extends PureComponent {
142133

143134
return {
144135
...props,
145-
onClick: callAll(isClickTriggered && this.scheduleToggle, props.onClick),
136+
onClick: callAll(isClickTriggered && this._toggleTooltip, props.onClick),
146137
onContextMenu: callAll(
147138
isRightClickTriggered && this._contextMenuToggle,
148139
props.onContextMenu
149140
),
150141
onMouseEnter: callAll(
151-
isHoverTriggered && this.scheduleShow,
142+
isHoverTriggered && this._showTooltip,
152143
props.onMouseEnter
153144
),
154145
onMouseLeave: callAll(
155-
isHoverTriggered && this.scheduleHide,
146+
isHoverTriggered && this._hideTooltip(),
156147
props.onMouseLeave
157148
)
158149
};
@@ -210,9 +201,8 @@ export default class TooltipTrigger extends PureComponent {
210201
scheduleUpdate
211202
}}
212203
innerRef={ref}
213-
hideTooltip={this.hideTooltip}
204+
hideTooltip={this._hideTooltip}
214205
clearScheduled={this._clearScheduled}
215-
scheduleHide={this.scheduleHide}
216206
/>
217207
)}
218208
</TooltipContext.Consumer>

0 commit comments

Comments
 (0)