Skip to content

Commit

Permalink
[IconButton] Warn when providing onClick to a child of a button
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Jan 12, 2019
1 parent 598132f commit a3ed62d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -42,7 +42,7 @@
"size:why": "size-limit --why packages/material-ui/build/index.js",
"size:overhead:why": "size-limit --why ./test/size/overhead.js",
"test": "yarn lint && yarn typescript && yarn test:coverage",
"test:unit": "cross-env NODE_ENV=test mocha 'packages/**/*.test.js' 'docs/**/*.test.js'",
"test:unit": "cross-env NODE_ENV=test mocha 'packages/**/*.test.js' 'docs/**/*.test.js' --exclude '**/node_modules/**'",
"test:watch": "yarn test:unit --watch",
"test:coverage": "cross-env NODE_ENV=test BABEL_ENV=coverage nyc mocha 'packages/material-ui/**/*.test.js' 'packages/material-ui-utils/**/*.test.js' 'packages/material-ui-styles/**/*.test.js' && nyc report -r lcovonly",
"test:coverage:html": "cross-env NODE_ENV=test BABEL_ENV=coverage nyc mocha 'packages/material-ui/**/*.test.js' 'packages/material-ui-utils/**/*.test.js' 'packages/material-ui-styles/**/*.test.js' && nyc report --reporter=html",
Expand Down
17 changes: 16 additions & 1 deletion packages/material-ui/src/IconButton/IconButton.js
Expand Up @@ -3,6 +3,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { chainPropTypes } from '@material-ui/utils';
import withStyles from '../styles/withStyles';
import { fade } from '../styles/colorManipulator';
import ButtonBase from '../ButtonBase';
Expand Down Expand Up @@ -103,7 +104,21 @@ IconButton.propTypes = {
/**
* The icon element.
*/
children: PropTypes.node,
children: chainPropTypes(PropTypes.node, props => {
React.Children.toArray(props.children).forEach(child => {
if (React.isValidElement(child) && child.props.onClick) {
throw new Error(
[
'Material-UI: you are providing a onClick event listener ' +
'to a child of a button element.',
'Firefox will never trigger the event.',
'https://github.com/mui-org/material-ui/issues/13957',
'You should move the onClick listener to the parent button element.',
].join('\n'),
);
}
});
}),
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css-api) below for more details.
Expand Down

0 comments on commit a3ed62d

Please sign in to comment.