How can I override ExpansionPanelSummary deep elements with styled-components?

后端 未结 1 1164
闹比i
闹比i 2020-12-19 17:02

Using the docs/examples for overriding Material UI styling with styled-components, I\'ve managed to style the root and \"deeper elements\" within an ExpansionPanel

相关标签:
1条回答
  • 2020-12-19 17:43

    This difficulty is independent of using styled-components and just has to do with wrapping ExpansionPanelSummary in another component.

    You can similarly reproduce this with the following wrapping of ExpansionPanelSummary:

    const MyCustomSummary = props => {
      return (
        <ExpansionPanelSummary {...props} expandIcon={<ExpandMoreIcon />}>
          <Typography>{props.text}</Typography>
        </ExpansionPanelSummary>
      );
    };
    
    

    There are several component groups like this where a Material-UI parent component looks for a particular type of child component and treats that child specially. For instance, you can find the following block in ExpansionPanel

          if (isMuiElement(child, ['ExpansionPanelSummary'])) {
            summary = React.cloneElement(child, {
              disabled,
              expanded,
              onChange: this.handleChange,
            });
            return null;
          }
    

    Fortunately, Material-UI has a straightforward way to tell it that your custom component should be treated the same as a particular Material-UI component via the muiName property:

    MyCustomSummary.muiName = "ExpansionPanelSummary";
    

    or in your case it would look like:

    export const ExpansionPanelSummary = styled(props => (
      <MUIExpansionPanelSummary
        {...props}
      />
    ))``;
    
    ExpansionPanelSummary.muiName = "ExpansionPanelSummary";
    

    0 讨论(0)
提交回复
热议问题