【问题标题】:MUI Stepper Icon not showingMUI 步进器图标未显示
【发布时间】:2021-12-13 00:55:31
【问题描述】:

我正在尝试让 MUI 步进器组件在每个步骤节点内实际呈现一些图标。但是,当我尝试这样做时,它只会呈现一个没有任何内容的空白屏幕。我想要这个外观(如文档中所示)。

如您所见,所需的外观是内部带有图标,MUI 提供了下面的示例代码。但是当我尝试实现它时,我得到一个空白屏幕

const ColorlibStepIconRoot = styled('div')(({ theme, ownerState }) => ({
  zIndex: 1,
  color: '#fff',
  width: 50,
  height: 50,
  display: 'flex',
  borderRadius: '50%',
  justifyContent: 'center',
  alignItems: 'center',
  ...(ownerState.active && {
    backgroundImage:
      'linear-gradient( 136deg, rgb(242,113,33) 0%, rgb(233,64,87) 50%, rgb(138,35,135) 100%)',
    boxShadow: '0 4px 10px 0 rgba(0,0,0,.25)',
  }),
  ...(ownerState.completed && {
    backgroundImage:
      'linear-gradient( 136deg, rgb(242,113,33) 0%, rgb(233,64,87) 50%, rgb(138,35,135) 100%)',
  }),
}));

function ColorlibStepIcon(props) {
  const { active, completed, className } = props;

  const icons = {
    1: <Icon>star</Icon>,
    2: <Icon>star</Icon>,
    3: <Icon>star</Icon>,
  };

  return (
    <ColorlibStepIconRoot ownerState={{ completed, active }} className={className}>
      {icons[String(props.icon)]}
    </ColorlibStepIconRoot>
  );
}


<Stepper alternativeLabel activeStep={2} style={{ background: 'none' }}>
        {steps.map(label => (
          <Step key={label}>
            <StepLabel StepIconComponent={ColorlibStepIcon}>{label}</StepLabel>
          </Step>
        ))}
      </Stepper>

【问题讨论】:

    标签: javascript css reactjs bootstrap-4 material-ui


    【解决方案1】:

    当步骤图标未激活或未完成时,您没有在ColorlibStepIconRoot 中设置背景颜色,因此背景颜色是透明的,图标颜色是白色的,而您在白色背景上看不到任何东西:

    const ColorlibStepIconRoot = styled('div')(({ theme, ownerState }) => ({
      backgroundColor:
        theme.palette.mode === 'dark' ? theme.palette.grey[700] : '#ccc',
      ...
    

    【讨论】:

    • 对不起,实际上没有。我认为这实际上是一个未定义样式化组件 api 的问题。我使用时它不存在: import { styled } from '@material-ui/core/styles'
    • @KennyQuach @material-ui/core/styles 是 v4 包,样式化 API 仅在来自 @mui/material/styles 的 v5 中可用。 v4 中的等效项是 withStyles,但除了错误的导入路径之外,您还缺少 backgroundColor 属性。
    猜你喜欢
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    • 2016-08-09
    • 2022-11-22
    • 2022-01-02
    • 2019-06-06
    相关资源
    最近更新 更多