首页 > 解决方案 > 如何在material-ui中同时激活所有步骤?

问题描述

我在我的 React 项目中使用 material-ui,现在我想展示一个步进器。但我不想要按钮来逐步前进和前进。我希望所有步骤一起活跃。

import React from 'react';
import Stepper from '@material-ui/core/Stepper';
import Step from '@material-ui/core/Step';
import StepLabel from '@material-ui/core/StepLabel';
import StepContent from '@material-ui/core/StepContent';
import Typography from '@material-ui/core/Typography';

function getSteps() {
  return ['Title 1', 'Title 2', 'Title 3'];
}

function getStepContent(step) {
  switch (step) {
    case 0:
      return `For each ad campaign that you create, you can control how much
              you're willing to spend on clicks and conversions, which networks
              and geographical locations you want your ads to show on, and more.`;
    case 1:
      return 'An ad group contains one or more ads which target a shared set of keywords.';
    case 2:
      return `Try out different ad text to see what brings in the most customers,
              and learn how to enhance your ads using features like ad extensions.
              If you run into any problems with your ads, find out how to tell if
              they're running and how to resolve approval issues.`;
    default:
      return 'Unknown step';
  }
}

class VerticalLinearStepper extends React.Component {

  render() {
    const steps = getSteps();

    return (
      <div className={classes.root}>
        <Stepper orientation="vertical">
          {steps.map((label, index) => {
            return (
              <Step key={label} active={true}>
                <StepLabel>{label}</StepLabel>
                <StepContent>
                  <Typography>{getStepContent(index)}</Typography>
                </StepContent>
              </Step>
            );
          })}
        </Stepper>
      </div>
    );
  }
}

我想要的是在没有任何按钮的情况下一起查看所有步骤的内容来前进和后退。但我只能看到第一步的内容,因为activestep'Stepper'的prop的默认值为0。

标签: reactjsmaterial-ui

解决方案


将 active = {true} 添加到 Step 组件解决了这个问题。


推荐阅读