React、material-ui 和 stepper:如何在每个步骤中显示 html

use*_*525 5 reactjs material-ui

标题说明了一切。从Stepper它的例子来看 ,似乎只能显示一个文本。是否可以显示html?

如果我将 html 放在<StepContent />页面中看起来很奇怪: 在此处输入图片说明

我使用了以下代码:

<Stepper activeStep={0}>
    <Step key={1}>
        <StepLabel>FOO</StepLabel>
        <StepContent>
            <div>
                <FormControl component="fieldset" required>
                    <FormLabel component="legend">Gender</FormLabel>
                    <RadioGroup
                        aria-label="gender"
                        name="gender1"
                        value={this.state.value}>
                        <FormControlLabel value="female" control={<Radio/>} label="Female"/>
                        <FormControlLabel value="male" control={<Radio/>} label="Male"/>
                        <FormControlLabel value="other" control={<Radio/>} label="Other"/>
                        <FormControlLabel
                            value="disabled"
                            disabled
                            control={<Radio/>}
                            label="(Disabled option)"
                        />
                    </RadioGroup>
                </FormControl>
            </div>
        </StepContent>
    </Step>
    <Step key={2}>
        <StepLabel> bar </StepLabel>
    </Step>
</Stepper>
Run Code Online (Sandbox Code Playgroud)

Jul*_*ont 8

您放入的任何内容都StepContent将显示为Step它本身的一部分,而不显示在下面它自己的内容窗格中。

如果您想显示一些更大的内容,您应该跟踪您的 中的当前步骤索引state,并根据该索引显示内容。这将是更多的代码,但您将能够正确显示内容。顺便说一下,这就是文档的做法。

这是一个片段,展示了您如何实现这一目标:

class HorizontalLinearStepper extends React.Component {
  static propTypes = {
    classes: PropTypes.object,
  };

  // Track the active step to know what content to show
  state = {
    activeStep: 0,
  };

  getNumberOfSteps() {
    return 2;
  }

  // Get content based on which step is active
  getStepContent(step) {
    switch (step) {
      case 0:
        return (
          <div>
            <FormControl component="fieldset" required>
              <FormLabel component="legend">Gender</FormLabel>
              <RadioGroup
                aria-label="gender"
                name="gender1"
                value={this.state.value}>
                <FormControlLabel value="female" control={<Radio />} label="Female" />
                <FormControlLabel value="male" control={<Radio />} label="Male" />
                <FormControlLabel value="other" control={<Radio />} label="Other" />
                <FormControlLabel
                  value="disabled"
                  disabled
                  control={<Radio />}
                  label="(Disabled option)"
                />
              </RadioGroup>
            </FormControl>
          </div>
        );
      case 1:
        return (
          <div>
            <Typography>Some more arbitrary content.</Typography>
            <Button>And a big button for good measure</Button>
          </div>
        );
      default:
        return 'Unknown step';
    }
  }

  // Update the active state according to the next button press
  handleNext = () => {
    const { activeStep } = this.state;
    this.setState({
      activeStep: activeStep + 1
    });
  };

  // Similar for back and reset buttons
  handleBack = () => {
    const { activeStep } = this.state;
    this.setState({
      activeStep: activeStep - 1,
    });
  };

  handleReset = () => {
    this.setState({
      activeStep: 0,
    });
  };

  render() {
    const { classes } = this.props;
    const { activeStep } = this.state;

    // Notice that the content below isn't in the Stepper, it's in its own pane underneath
    return (
      <div className={classes.root}>
        <Stepper activeStep={activeStep}>
          <Step key={0}>
            <StepLabel>FOO</StepLabel>
          </Step>
          <Step key={1}>
            <StepLabel> bar </StepLabel>
          </Step>
        </Stepper>
        <div>
          {activeStep === this.getNumberOfSteps() ? (
            <div>
              <Typography className={classes.instructions}>
                All steps completed - you&quot;re finished
              </Typography>
              <Button onClick={this.handleReset} className={classes.button}>
                Reset
              </Button>
            </div>
          ) : (
            <div>
              {
                // Populate the content pane based on the active step
                this.getStepContent(activeStep)
              }
              <div>
                <Button
                  disabled={activeStep === 0}
                  onClick={this.handleBack}
                  className={classes.button}
                >
                  Back
                </Button>
                <Button
                  variant="raised"
                  color="primary"
                  onClick={this.handleNext}
                  className={classes.button}
                >
                    {activeStep === this.getNumberOfSteps() - 1 ? 'Finish' : 'Next'}
                </Button>
              </div>
            </div>
          )}
        </div>
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看完整的工作示例。

  • 更新了问题。 (2认同)
  • 完整的工作示例不再有效。它现在抱怨“在路径中找不到模块:相对于‘/node_modules/material-ui/styles/transitions.js’的‘@babel/runtime/core-js/object/keys’” (2认同)