Skip to content

Commit

Permalink
[Tooltip] Fix children focus detection (#14496)
Browse files Browse the repository at this point in the history
  • Loading branch information
codeheroics authored and oliviertassinari committed Feb 12, 2019
1 parent 8d0e344 commit bf8514d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
13 changes: 6 additions & 7 deletions packages/material-ui/src/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,14 @@ class Tooltip extends React.Component {
};

handleFocus = event => {
event.persist();
// Workaround for https://github.com/facebook/react/issues/7769
// The autoFocus of React might trigger the event before the componentDidMount.
// We need to account for this eventuality.
this.focusTimer = setTimeout(() => {
// We need to make sure the focus hasn't moved since the event was triggered.
if (this.childrenRef === document.activeElement) {
this.handleEnter(event);
}
});
if (!this.childrenRef) {
this.childrenRef = event.currentTarget;
}

this.handleEnter(event);

const childrenProps = this.props.children.props;
if (childrenProps.onFocus) {
Expand Down
24 changes: 24 additions & 0 deletions packages/material-ui/src/Tooltip/Tooltip.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from 'react';
import { assert } from 'chai';
import PropTypes from 'prop-types';
import { spy, useFakeTimers } from 'sinon';
import consoleErrorMock from 'test/utils/consoleErrorMock';
import { createShallow, createMount, getClasses, unwrap } from '@material-ui/core/test-utils';
import Popper from '../Popper';
import Tooltip from './Tooltip';
import Input from '../Input';
import createMuiTheme from '../styles/createMuiTheme';

function persist() {}
Expand Down Expand Up @@ -170,6 +172,28 @@ describe('<Tooltip />', () => {
it('should mount without any issue', () => {
mount(<Tooltip {...defaultProps} open />);
});

it('should handle autoFocus + onFocus forwarding', () => {
const AutoFocus = props => (
<div>
{props.open ? (
<Tooltip title="Title">
<Input value="value" autoFocus />
</Tooltip>
) : null}
</div>
);
AutoFocus.propTypes = {
open: PropTypes.bool,
};

const wrapper = mount(<AutoFocus />);
wrapper.setProps({ open: true });
assert.strictEqual(wrapper.find(Popper).props().open, false);
clock.tick(0);
wrapper.update();
assert.strictEqual(wrapper.find(Popper).props().open, true);
});
});

describe('prop: delay', () => {
Expand Down

0 comments on commit bf8514d

Please sign in to comment.