Skip to content

Commit 6a32962

Browse files
let's merge
1 parent 72cca46 commit 6a32962

File tree

2 files changed

+31
-37
lines changed

2 files changed

+31
-37
lines changed

packages/material-ui/src/Tooltip/Tooltip.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,23 @@ export const styles = theme => ({
6767
});
6868

6969
class Tooltip extends React.Component {
70-
enterTimer = null;
70+
childrenRef = null;
7171

72-
leaveTimer = null;
72+
closeTimer = null;
7373

74-
touchTimer = null;
74+
defaultId = null;
7575

76-
closeTimer = null;
76+
enterTimer = null;
7777

78-
childrenRef = null;
78+
focusTimer = null;
79+
80+
ignoreNonTouchEvents = false;
7981

8082
isControlled = null;
8183

82-
ignoreNonTouchEvents = false;
84+
leaveTimer = null;
8385

84-
defaultId = null;
86+
touchTimer = null;
8587

8688
internalState = {
8789
hover: false,
@@ -126,16 +128,26 @@ class Tooltip extends React.Component {
126128
}
127129

128130
componentWillUnmount() {
131+
clearTimeout(this.closeTimer);
129132
clearTimeout(this.enterTimer);
133+
clearTimeout(this.focusTimer);
130134
clearTimeout(this.leaveTimer);
131135
clearTimeout(this.touchTimer);
132-
clearTimeout(this.closeTimer);
133136
}
134137

135138
onRootRef = ref => {
136139
this.childrenRef = ref;
137140
};
138141

142+
handleFocus = event => {
143+
event.persist();
144+
// The autoFocus of React might trigger the event before the componentDidMount.
145+
// We need to account for this eventuality.
146+
this.focusTimer = setTimeout(() => {
147+
this.handleEnter(event);
148+
});
149+
};
150+
139151
handleEnter = event => {
140152
const { children, enterDelay } = this.props;
141153
const childrenProps = children.props;
@@ -160,10 +172,6 @@ class Tooltip extends React.Component {
160172
return;
161173
}
162174

163-
if (!this.childrenRef) {
164-
return;
165-
}
166-
167175
// Remove the title ahead of time.
168176
// We don't want to wait for the next render commit.
169177
// We would risk displaying two tooltips at the same time (native + this one).
@@ -314,7 +322,7 @@ class Tooltip extends React.Component {
314322
}
315323

316324
if (!disableFocusListener) {
317-
childrenProps.onFocus = this.handleEnter;
325+
childrenProps.onFocus = this.handleFocus;
318326
childrenProps.onBlur = this.handleLeave;
319327
}
320328

packages/material-ui/src/Tooltip/Tooltip.test.js

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('<Tooltip />', () => {
1414
let shallow;
1515
let mount;
1616
let classes;
17+
let clock;
1718
const defaultProps = {
1819
title: 'Hello World',
1920
children: <span>Hello World</span>,
@@ -23,9 +24,11 @@ describe('<Tooltip />', () => {
2324
shallow = createShallow({ dive: true, disableLifecycleMethods: true });
2425
mount = createMount();
2526
classes = getClasses(<Tooltip {...defaultProps} />);
27+
clock = useFakeTimers();
2628
});
2729

2830
after(() => {
31+
clock.restore();
2932
mount.cleanUp();
3033
});
3134

@@ -92,7 +95,8 @@ describe('<Tooltip />', () => {
9295
const children = wrapper.childAt(0).childAt(0);
9396
assert.strictEqual(wrapper.state().open, false);
9497
children.simulate('mouseEnter', { type: 'mouseenter' });
95-
children.simulate('focus', { type: 'focus' });
98+
children.simulate('focus', { type: 'focus', persist });
99+
clock.tick(0);
96100
assert.strictEqual(wrapper.state().open, true);
97101
children.simulate('mouseLeave', { type: 'mouseleave' });
98102
assert.strictEqual(wrapper.state().open, true);
@@ -101,23 +105,13 @@ describe('<Tooltip />', () => {
101105
});
102106

103107
describe('touch screen', () => {
104-
let clock;
105-
106-
before(() => {
107-
clock = useFakeTimers();
108-
});
109-
110-
after(() => {
111-
clock.restore();
112-
});
113-
114108
it('should not respond to quick events', () => {
115109
const wrapper = shallow(<Tooltip {...defaultProps} />);
116110
wrapper.instance().childrenRef = document.createElement('div');
117111
const children = wrapper.childAt(0).childAt(0);
118112
children.simulate('touchStart', { type: 'touchstart', persist });
119113
children.simulate('touchEnd', { type: 'touchend', persist });
120-
children.simulate('focus', { type: 'focus' });
114+
children.simulate('focus', { type: 'focus', persist });
121115
children.simulate('mouseover', { type: 'mouseover' });
122116
assert.strictEqual(wrapper.state().open, false);
123117
});
@@ -127,7 +121,7 @@ describe('<Tooltip />', () => {
127121
wrapper.instance().childrenRef = document.createElement('div');
128122
const children = wrapper.childAt(0).childAt(0);
129123
children.simulate('touchStart', { type: 'touchstart', persist });
130-
children.simulate('focus', { type: 'focus' });
124+
children.simulate('focus', { type: 'focus', persist });
131125
children.simulate('mouseover', { type: 'mouseover' });
132126
clock.tick(1e3);
133127
assert.strictEqual(wrapper.state().open, true);
@@ -145,16 +139,6 @@ describe('<Tooltip />', () => {
145139
});
146140

147141
describe('prop: delay', () => {
148-
let clock;
149-
150-
before(() => {
151-
clock = useFakeTimers();
152-
});
153-
154-
after(() => {
155-
clock.restore();
156-
});
157-
158142
it('should take the enterDelay into account', () => {
159143
const wrapper = shallow(<Tooltip enterDelay={111} {...defaultProps} />);
160144
wrapper.instance().childrenRef = document.createElement('div');
@@ -169,7 +153,8 @@ describe('<Tooltip />', () => {
169153
const wrapper = shallow(<Tooltip leaveDelay={111} {...defaultProps} />);
170154
wrapper.instance().childrenRef = document.createElement('div');
171155
const children = wrapper.childAt(0).childAt(0);
172-
children.simulate('focus', { type: 'focus' });
156+
children.simulate('focus', { type: 'focus', persist });
157+
clock.tick(0);
173158
assert.strictEqual(wrapper.state().open, true);
174159
children.simulate('blur', { type: 'blur', persist });
175160
assert.strictEqual(wrapper.state().open, true);
@@ -194,6 +179,7 @@ describe('<Tooltip />', () => {
194179
const children = wrapper.childAt(0).childAt(0);
195180
const type = name.slice(2).toLowerCase();
196181
children.simulate(type, { type, persist });
182+
clock.tick(0);
197183
assert.strictEqual(handler.callCount, 1);
198184
});
199185
},

0 commit comments

Comments
 (0)