[IconButton] Improve onClick warning
See original GitHub issueNot sure where to put this, but I’m getting the warning:
Failed prop type: Material-UI: You are providing an onClick event listener to a child of a button element.
Firefox will never trigger the event.
You should move the onClick listener to the parent button element.
- The issue is present in the latest release.
- I have searched the issues of this repository and believe that this is not a duplicate.
Current Behavior 😯
Warning thrown
Expected Behavior 🤔
Warning is a non-issue (in Firefox, from what I gather, in this situation), so this warning shouldn’t be there.
I get why this is being thrown, the expand icon is wrapped in a button base (IconButton
), but I feel like the warning is misleading.
Also, curious to know if you guys had a conversation about ever giving users the ability to pass a function to the IconButton
component? If not, I wouldn’t mind making a PR. Not sure how it would look like yet, but what would be wrong with something like,
type TAccordion = {
. . . . .
onExpandIconClick: (...params: any[]) => any;
}
const AccordionSummary = React.forwardRef(function AccordionSummary(props, ref) {
const {
onExpandIconClick,
classes,
expandIcon,
IconButtonProps = {},
...other
} = props;
. . . . .
return {
. . . . .
{expandIcon && (
<IconButton
onClick={onExpandIconClick}
edge='end'
component='div'
tabIndex={null}
role={null}
aria-hidden
{...IconButtonProps}
className={clsx(
classes.expandIcon,
{
[classes.expanded]: expanded,
},
IconButtonProps.className,
)}
>
{expandIcon}
</IconButton>
)}
Steps to Reproduce 🕹
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (12 by maintainers)
Top Results From Across the Web
[IconButton] Improve onClick warning · Issue #22804 - GitHub
Not sure where to put this, but I'm getting the warning: Failed prop type: Material-UI: You are providing an onClick event listener to...
Read more >react Material-ui, How do I know I can use onClick for button?
Props that are not cited in the doc are passed down into their internal <EnhancedButton /> component which will handle onClick just fine....
Read more >Button icon with possibility of passing parameters and get ...
I have a IconButton , but to pass parameters to the JS, I use regular Icon with span like this : <span onclick="{!c....
Read more >IconButton API - Material UI - MUI
API reference docs for the React IconButton component. ... MuiIconButton-colorWarning, Styles applied to the root element if color="warning" .
Read more >Icon Button API - React Magma
The onClick event will fire when the button is clicked, or when the enter or spacebar key is pressed. Events do not fire...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I will make this PR tomorrow (if I dont forget) so the others can also comment if it makes sense. @mkhoussid the conclusion for you is as per said before. You shouldn’t use
expandIcon
for this, but do it by appropriately laying out the children ofAccordionSummary
.@pranjanpr yes.
Also there is an unrelated second issue that I’m doing a PR for (you can see the PR link slightly above), which is slightly more advanced.