简体   繁体   English

Material UI StepIcon 具有实际图标但保留背景圆圈

[英]Material UI StepIcon to have actual icon but keep background circle

I've tried searching stack overflow and haven't found a specific question with this attempt.我试过搜索堆栈溢出,但没有找到具体的问题。 I am using the styled way to style a Material UI Stepper component.我正在使用样式化方式来设置 Material UI Stepper 组件的样式。 All the examples I see use withStyles, makeStyles, everything but styled for changing colors. But, I also want to have a real icon, not text, in the step labels like below.我看到的所有示例都使用 withStyles、makeStyles,除了用于更改 colors 的样式之外的所有内容。但是,我还希望在如下步骤标签中有一个真正的图标,而不是文本。

带图像的步进器

Every time I add an icon component to the icon properties, it just shows the icon and chucks the circle that would normally encompass the text.每次我将图标组件添加到图标属性时,它只会显示图标并删除通常包含文本的圆圈。 I'd like to keep the circle and add the icon along with add text to the top.我想保留圆圈并添加图标以及在顶部添加文本。 The active/completed would be the gold color and grey for disabled/inactive.活动/完成的颜色为金色,禁用/不活动的颜色为灰色。

Here's my coding attempts, I'd tried various combinations, stuck the examples from the demos on Material UI website and still haven't had much luck getting icon to show inside a circle.这是我的编码尝试,我尝试了各种组合,将演示中的示例粘贴在 Material UI 网站上,但仍然没有太多运气让图标显示在圆圈内。 Do I need to just give up on the StepIcon component and wrap the icon in an Avatar component or something?我是否需要放弃 StepIcon 组件并将图标包装在 Avatar 组件或其他组件中?

  const TimelineIcon = styled(StepIcon)(({ theme }) => ({
        root: {
          color: theme.palette.primary.light,
          display: "flex",
          height: 22,
          alignItems: "center",
          "&$active": {
            color: theme.palette.success.main,
          },
          "&$completed": {
            backgroundColor: theme.palette.primary.light,
            color: theme.palette.success.main,
            zIndex: 1,
            fontSize: 18,
          },
        },
      }));

      const stepIconComponent = () => {
        return (
          <div>
            <TimelineIcon icon={<Check />} />
          </div>
        );
      };

    <Stepper
      orientation={"horizontal"}
      alternativeLabel
      style={{ width: "100%" }}
    >
      {stepper.steps.map((step: TimelineStepProps) => {
        return (
          <Step key={step.title}>
            <StepLabel StepIconComponent={stepIconComponent}>
              {step.title}
            </StepLabel>
          </Step>
        );
      })}
    </Stepper>

Due to the design of the StepIcon, it does not apply classes if an actual icon is passed rather than text or number.由于 StepIcon 的设计,如果传递的是实际图标而不是文本或数字,则它不会应用类。 This forces it to only show the icon and not the circle background with it.这会强制它只显示图标而不是圆圈背景。 Here is the component: implementation of StepIcon这是组件: StepIcon 的实现

The code that forces the StepIcon to apply classes only if the "icon" is a number or string is this:仅当“图标”是数字或字符串时才强制 StepIcon 应用类的代码如下:

if (typeof icon === 'number' || typeof icon === 'string') {
    const className = clsx(classes.root, {
      [classes.active]: active,
      [classes.error]: error,
      [classes.completed]: completed,
    });

So what I had to do was recreate the icon with the circle and have two different looks for active or disabled.所以我必须做的是用圆圈重新创建图标,并有两种不同的外观来表示活动或禁用。 I also had to style the connector to push it down a little if there was text up top (in the example a number above the circle) or not (since that could be optional in this case)如果顶部有文本(在示例中圆圈上方的数字)或没有文本(因为在这种情况下可能是可选的),我还必须设置连接器的样式以将其向下推一点

const ActiveSvgIcon = styled(SvgIcon)(({ theme }) => ({
    color: theme.palette.success.main,
    "&.MuiSvgIcon-root": {
      width: "1.2em",
      height: "1.2em",
    },
  }));

  const DisabledSvgIcon = styled(SvgIcon)(({ theme }) => ({
    color: theme.palette.primary.light,
    "&.MuiSvgIcon-root": {
      width: "1.2em",
      height: "1.2em",
    },
  }));

  const getIcon = (step: TimelineStepProps) => {
    return step.active ? (
      <ActiveSvgIcon shapeRendering="circle">
        <circle cx="12" cy="12" r="12" />
        {step.icon}
      </ActiveSvgIcon>
    ) : (
      <DisabledSvgIcon shapeRendering="circle">
        <circle cx="12" cy="12" r="12" />
        {step.icon}
      </DisabledSvgIcon>
    );
  };

  const iconStepComponent = (step: TimelineStepProps) => {
    return (
      <div style={{ marginTop: step.superScript === "" ? "20px" : 0 }}>
        <StepperText>{step.superScript}</StepperText>
        {getIcon(step)}
        <StepperTitle>{step.title}</StepperTitle>
        <StepperText>{step.subTitle}</StepperText>
      </div>
    );
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM