Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling collapse or expand in accordion of material-ui

I have an accordion of MaterialUI where I also added few icons but on click of those two particular icons, I don't want the accordion to get expanded or collapsed. I want other onClick event to happen for the click but not expand or collapse. Here is the code I am using.

<ExpansionPanel>
  <ExpansionPanelSummary expandIcon={<ExpandMoreIcon />}>
    <Typography  className={classes.heading}>
      {name}
    </Typography>


  <ListItem>
      <ListItemText  />
      <IconButton color="primary" aria-label="Edit" onClick={onClickEdit}>
          <Edit />
      </IconButton>
      <IconButton onClick={onClickDelete} color="secondary" aria-label="Delete">
          <Delete />
      </IconButton>
  </ListItem>
</ExpansionPanelSummary>
           

For click of two icons, I don't want accordion to expand or collapse. Is this anyway related?

like image 794
jammy Avatar asked Aug 17 '18 18:08

jammy


People also ask

How do you remove expanded accordion gap?

You can do that by setting the Accordion margin to auto which should be the same as setting it to 0 . Make sure the style is applied to every Accordion s on the screen. See this example here. The Accordion is being moved when expanding is just a side effect of positive margin plus the transition effect when expanded.

How do you keep accordion open by default in react?

To keep single pane open always in React Accordion component By default, all Accordion panels are collapsible. You can customize the Accordion to keep one panel as expanded state always. This is applicable for Single expand mode.

What is accordion MUI?

The accordion component allows the user to show and hide sections of related content on a page. An accordion is a lightweight container that may either be used standalone, or be connected to a larger surface, such as a card. Feedback. WAI-ARIA.


2 Answers

You could stop the event from bubbling up to the parent in your onClickDelete or onClickEdit function:

function onClickDelete(event) {
    event.stopPropagation();
    // Handle click here
}

Here's a rough example: https://codesandbox.io/s/54vypx6k9n

like image 148
evan_schmevan Avatar answered Oct 19 '22 09:10

evan_schmevan


Well, stopPropagation may create many interesting unexpected behaviors:

  1. Like body click listeners will not be fired.
  2. Besides, you need to restyle the accordion header inner content to make sure it takes the whole inner space to catch the click event

Why not a simpler way, pure CSS:

.MuiAccordionSummary-root  {
  pointer-events: none;
}
like image 43
Paul Dev Avatar answered Oct 19 '22 11:10

Paul Dev