【问题标题】:Material UI Stepper using custom icon deletes the step number. how to add the number or random text back with a custom icon?Material UI Stepper 使用自定义图标删除步骤编号。如何使用自定义图标添加数字或随机文本?
【发布时间】:2016-11-24 01:28:19
【问题描述】:

<Stepper linear={false} activeStep={1}>
        <Step key={1}>
          <StepButton
            icon={<LensIcon color={grey300} children={<p>2</p>}/>}
            onClick={() => console.log('Clicked')}
          />
        </Step>
        <Step key={2}>
          <StepButton
            onClick={() => console.log('Clicked')}
          />
        </Step>
</Stepper>

嘿,如果我添加一个自定义图标,我会丢失步骤的数量,我希望能够添加颜色来指示步骤的状态。但如果我介绍我自己的图标,我会丢失数字。 请告知如何同时拥有两者?谢谢。

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    您可以将 LensIcon 和步数标签放在 div 内,并使用绝对定位将标签浮动在顶部。这是一个完成这项工作的小“StepIcon”组件:

    const StepIcon = ({ label, color = colors.grey300, textColor = colors.lightBlack }) => (
        <div style={{ position: 'relative' }}>
          <LensIcon color={color} />
          <div style={{ color: textColor, position: 'absolute', top: 0, left: 0, width: '100%', height: '100%', textAlign: 'center', lineHeight: '24px' }}>{label}</div>
        </div>
    );
    

    用法(可以使用数字或字符串标签):

    ...
    <Step key={1}>
      <StepButton
        icon={<StepIcon label={1} />}
        onClick={() => console.log('Clicked') }
        />
    </Step>
    <Step key={3}>
      <StepButton
        icon={<StepIcon label={'A'} color={colors.green500} textColor={colors.white} />}
        onClick={() => console.log('Clicked') }
      />
    </Step>
    

    这是一个 jsFiddle 的实际操作:https://jsfiddle.net/67p1w0mk/2/

    【讨论】:

    • 不再需要这样做,您可以使用道具 stepIconComponent 与 StepLabel 一起指定图标。请看这个codesandbox.io/s/material-demo-forked-c0nvt?file=/…
    • 如果您使用 MUI 的图标,这是正确的答案。如果你使用自己的,不要太多-_-
    • @Abidh 你不太正确。 OP 要求提供一个接收 StepIcon 并保留步骤索引的组件。在您的情况下,图标完全覆盖了数字。
    猜你喜欢
    • 2019-07-10
    • 2017-09-22
    • 2023-03-07
    • 2020-07-03
    • 2021-12-13
    • 2021-06-21
    • 2022-01-02
    • 2020-07-16
    • 1970-01-01
    相关资源
    最近更新 更多