iT邦幫忙

2022 iThome 鐵人賽

DAY 23
0
自我挑戰組

React 面 項目 看起來很好吃吧--佐material 調料系列 第 23

React side project--Stepper(步驟條)

  • 分享至 

  • xImage
  •  

在很多問卷調查中,下面會有個Stepper 來顯示你做到第幾步

或是說明文件下方 讓使用者可以用來切換步驟

首先 引入

import * as React from 'react';
import Box from '@mui/material/Box';
import Stepper from '@mui/material/Stepper';
import Step from '@mui/material/Step';
import StepButton from '@mui/material/StepButton';
import Button from '@mui/material/Button';
import Typography from '@mui/material/Typography';

接下來 加入應用的函式,其中不免俗地說 React mui 果然是基於JS下面的做法

會用js的邏輯操控step

 const [activeStep, setActiveStep] = React.useState(0);
  const [completed, setCompleted] = React.useState({});

  const totalSteps = () => {
    return steps.length;
  };

  const completedSteps = () => {
    return Object.keys(completed).length;
  };

  const isLastStep = () => {
    return activeStep === totalSteps() - 1;
  };

  const allStepsCompleted = () => {
    return completedSteps() === totalSteps();
  };

  const handleNext = () => {
    const newActiveStep =
      isLastStep() && !allStepsCompleted()
        ? // It's the last step, but not all steps have been completed,
          // find the first step that has been completed
          steps.findIndex((step, i) => !(i in completed))
        : activeStep + 1;
    setActiveStep(newActiveStep);
  };

  const handleBack = () => {
    setActiveStep((prevActiveStep) => prevActiveStep - 1);
  };

  const handleStep = (step) => () => {
    setActiveStep(step);
  };

  const handleComplete = () => {
    const newCompleted = completed;
    newCompleted[activeStep] = true;
    setCompleted(newCompleted);
    handleNext();
  };

  const handleReset = () => {
    setActiveStep(0);
    setCompleted({});
  };

Day 23


上一篇
React side project--Image list (圖片拼貼)
下一篇
React side project--Tabs (選項卡)
系列文
React 面 項目 看起來很好吃吧--佐material 調料30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言