Antd Steps步骤条结合路由跳转

Steps步骤条

定义一个home目录,
index.js
step.js
StepOne.js
StepTwo.js
StepThree.js

// index.js
import React, { Component } from 'react'
import Stepper from './step'
import { Route } from 'react-router-dom'
import StepOne from './StepOne'
import StepTwo from './StepTwo'
import StepThree from './StepThree'

export default class Home extends Component {
  render() {
    return (
      <div>
        <Stepper {...this.props}/>  //传递路由属性
        <Route path='/home/step1' component={StepOne} />
        <Route path='/home/step2' component={StepTwo} />
        <Route path='/home/step3'component={StepThree} />
      </div>
    )
  }
}

step.js引入antd steps组件

import React from 'react'
import { Steps } from 'antd';

const { Step } = Steps;
class Stepper extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      current: 0
    };
  }
  //点击跳转
  onChange = (current) => {
    const { push } = this.props.history
    switch (current) {
      case 0:
        push('/home/step1')
        break;
      case 1:
        push('/home/step2')
        break;
      default:
        push('/home/step3')
        break;
    }
    this.setState({ current })
  }

  componentDidMount() {
    this.checkStep()
  }
  UNSAFE_componentWillReceiveProps(nextProps){
    this.checkStep(nextProps)
  }
  //通过路由判断
  checkStep = (nextProps) => {
    const { pathname } = nextProps ? nextProps.location : this.props.location
    switch (pathname) {
      case '/home/step1':
        this.setState({ current: 0 })
        break;
      case '/home/step2':
        this.setState({ current: 1 })
        break;
      default:
        this.setState({ current: 2 })
        break;
    }
  }
  render() {
    const { current } = this.state
    return (
      <div>
        <Steps
          type="navigation"
          current={current}
          onChange={this.onChange}
        >
          <Step
            title="第一步"
          // status="finish"
          // description="This is a description."
          />
          <Step
            title="第二步"
          // status="process"
          // description="This is a description."
          />
          <Step
            title="第三步"
          // status="wait"
          // description="This is a description."
          />
        </Steps>
      </div>
    );
  }
}
export default Stepper
//StepOne.js
import React, { Component } from 'react'
import { Button } from 'antd'
export default class StepOne extends Component {
  nextHandler=()=>{
    const {push}=this.props.history
    push('/home/step2')
  }
  render() {
    return (
      <div>
        第一步
        <Button onClick = { this.nextHandler }> 下一步 </Button>
      </div>
    )
  }
}
//StepTwo.js
import React, { Component } from 'react'
import { Button } from 'antd'
export default class StepTwo extends Component {
  nextHandler=()=>{
    const {push}=this.props.history
    push('/home/step3')
  }
  render() {
    return (
      <div>
        第二步
        <Button onClick = { this.nextHandler }> 下一步 </Button>
      </div>
    )
  }
}

//StepThree.js
import React, { Component } from 'react'
export default class StepThree extends Component {
  render() {
    return (
      <div>
        第三步
      </div>
    )
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值