1
votes

Im looking for a way to force the Expansion panel to expanded=false when the expansion panel is disabled. Imagine an expanded Expansion panel. An event occurs and the Expansion Panel switch to disabled. My problem is that it's still expanded. I want to force expanded=false but I don't know how to do.

Here is my code :

return (


<ExpansionPanel disabled={props.body==null} >
  <ExpansionPanelSummary
  expandIcon={<ExpandMoreIcon />}
  id="panel1a-header"
  classes={{
    content: classes.spaced,
  }}
  >
    <Box fontWeight="fontWeightBold">{props.name}</Box>
    <Box fontWeight="fontWeightBold" className={props.hasError ? classes.error : classes.ok}>
      {props.hasError ? 'DOWN' : 'UP'}
    </Box>
  </ExpansionPanelSummary>
  <ExpansionPanelDetails 
  classes={{
    root: classes.rowSpaced
    }}>
      {
      props.body != null && props.body.checks != null ?

      <Box>
        <List
      component="nav"
      aria-labelledby="nested-list-subheader"
      subheader={
        <ListSubheader component="div" id="nested-list-subheader">Liveness</ListSubheader>
      }
      className={classes.root}
      >
        <ListItem>
          <ListItemText>
            <Box display="flex" flexDirection="row" className={classes.spaced}>
              <Typography key={props.body.checks[0].name}>{props.body.checks[0].name}</Typography>
              <Typography key={props.body.checks[0].name + props.body.checks[0].status} className={props.body.checks[0].status === 'UP' ? classes.ok : classes.error}>
                {props.body.checks[0].status}
              </Typography>
            </Box>
          </ListItemText>
        </ListItem>
      </List>

      <Divider />
      <List
      component="nav"
      aria-labelledby="nested-list-subheader"
      subheader={
        <ListSubheader component="div" id="nested-list-subheader">
          Readyness
        </ListSubheader>
      }
      className={classes.root}
      >
        {
        props.body != null && props.body.checks != null ? props.body.checks.slice(1).sort((a, b) => (a.name > b.name) ? 1 : -1).map((check, i) => (

        <ListItem key={check.name + check.status}>
          <ListItemText>
            <Box display="flex" flexDirection="row" className={classes.spaced}>
              <Typography key={check.name}>{check.name}</Typography>
              <Typography key={check.name + check.status}  className={check.status === 'UP' ? classes.ok : classes.error}>
               {check.status}
              </Typography>
            </Box>
          </ListItemText>
        </ListItem>
        )) : null
        }
      </List>

      </Box>

      : null
      }


    </ExpansionPanelDetails>
  </ExpansionPanel>
  );

If props.body==null then the Expansion Panel has to be disabled=true AND expanded=false because I don't want to keep it expanded when disabled... If this.props.body!=null then I want to let the user click on it to expand as he wants.

Notice the props.body != null && props.body.checks != null ? condition because an async call is made. See react this.props undefined or empty object

How to do ?

Thank you !

2

2 Answers

2
votes

Since you want to have the ability to disable/enable the panel, and also the expand/collapse settings - you will need to move to a controlled panel. This means that you need to have a variable that saves the status of the expand/collapse (and probably also the status of the panel (enabled/disabled).

Once the user you have a panel that is disabled - make sure that this change will also set the value of the expand to false:

const MyControlledPanel = props => {
  const { children, disabled } = props;
  const [isExpanded, setIsExpanded] = React.useState(false);

  const handleChange = () => {
    setIsExpanded(!isExpanded);
  };

  React.useEffect(() => {
    if (disabled) {
      setIsExpanded(false);
    }
  }, [disabled]);

  return (
    <ExpansionPanel
      disabled={disabled}
      expanded={isExpanded}
      onChange={handleChange}
    >
      {children}
    </ExpansionPanel>
  );
};

By setting the isExpanded to false once you get a disabled=true value in the prop - you make sure that even if you get disabled=false - the panel is still collapsed (and the user will now be able to expand it again).

You can find here a working version of this: https://codesandbox.io/s/controlled-panel-collapse-on-disable-1ejtt?file=/demo.js

0
votes
const [isExpanded, setIsExpanded] = React.useState(false);
const isDisabled = (props.body === null);

React.useEffect(() => {
  if(isExpanded && isDisabled) {
    setIsExpanded(false);
  }
}, [isExpanded, isDisabled]);
<ExpansionPanel disabled={isDisabled} expanded={isExpanded && !isDisabled} onChange={() => setIsExpanded(!isExpanded)}>
....
</ExpansionPanel>