Skip to content

Commit

Permalink
[Tooltip] New warning message (#8988)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Nov 4, 2017
1 parent 560b99c commit 13731bb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Tooltip/Tooltip.js
Expand Up @@ -228,6 +228,22 @@ class Tooltip extends React.Component<ProvidedProps & Props, State> {
}
}

componentDidMount() {
warning(
!this.children ||
!this.children.disabled ||
// $FlowFixMe
!this.children.tagName.toLowerCase() === 'button',
[
'Material-UI: you are providing a disabled button children to the Tooltip component.',
'A disabled element do not fire events.',
'But the Tooltip needs to listen to the children element events to display the title.',
'',
'Place a `div` over top of the element.',
].join('\n'),
);
}

componentWillUnmount() {
clearTimeout(this.enterTimer);
clearTimeout(this.leaveTimer);
Expand All @@ -239,6 +255,7 @@ class Tooltip extends React.Component<ProvidedProps & Props, State> {
touchTimer = null;
isControlled = null;
popper = null;
children = null;
ignoreNonTouchEvents = false;

handleResize = debounce(() => {
Expand Down Expand Up @@ -421,7 +438,8 @@ class Tooltip extends React.Component<ProvidedProps & Props, State> {
: childrenProp
}
ref={node => {
targetProps.ref(findDOMNode(node));
this.children = findDOMNode(node);
targetProps.ref(this.children);
}}
/>
)}
Expand Down
24 changes: 24 additions & 0 deletions src/Tooltip/Tooltip.spec.js
Expand Up @@ -7,6 +7,7 @@ import { spy, useFakeTimers } from 'sinon';
import { Target, Popper } from 'react-popper';
import { ShallowWrapper } from 'enzyme';
import { createShallow, createMount, getClasses, unwrap } from '../test-utils';
import consoleErrorMock from '../../test/utils/consoleErrorMock';
import createMuiTheme from '../styles/createMuiTheme';
import Tooltip from './Tooltip';

Expand Down Expand Up @@ -315,4 +316,27 @@ describe('<Tooltip />', () => {
assert.strictEqual(handleUpdate.callCount, 1);
});
});

describe('disabled button warning', () => {
before(() => {
consoleErrorMock.spy();
});

after(() => {
consoleErrorMock.reset();
});

it('should raise a warning when we can listen to events', () => {
mount(
<Tooltip title="Hello World">
<button disabled>Hello World</button>
</Tooltip>,
);
assert.strictEqual(consoleErrorMock.callCount(), 1, 'should call console.error');
assert.match(
consoleErrorMock.args()[0][0],
/Material-UI: you are providing a disabled button children to the Tooltip component/,
);
});
});
});

0 comments on commit 13731bb

Please sign in to comment.