Muscardinus

Progress Bar 본문

FrontEnd/Basic Skills

Progress Bar

Muscardinus 2022. 3. 20. 01:20
728x90
import { useState, useRef } from 'react';
import ProgressBar from './ProgressBar';
import Button from './Button';

function App() {

  const [ current , setCurrent ] = useState(0);
  const limit = 4;
  const range = 100 / limit;
  const animationSpeed = 500;
  const isLoading = useRef(false);

  const delay = (delay) => {
      isLoading.current = true;
      return new Promise(() => {
        setTimeout(() => {
            isLoading.current = false;
        }, delay);
      })
  }

  const handleNext = async () => {
    // Do Something Here!
    // 다음 스텝으로 이동
    if (isLoading.current) return;
    if (current === limit) return;
    setCurrent((prev) => prev + 1);
    await delay(animationSpeed);
  }

  const handlePrev = async () => {
    // Do Something Here!
    // 이전 스텝으로 이동
    if (isLoading.current) return;
    if (current === 0) return;
    setCurrent((prev) => prev - 1);
    await delay(animationSpeed);
  }



  return (
    <div>

      <ProgressBar width={ current * range } animationSpeed={animationSpeed}/>

      <br />
      <br />


      <Button text={'이전'} onClick={handlePrev}/>
      <Button text={'다음'} onClick={handleNext}/>

    </div>
  );
}

export default App;
728x90

'FrontEnd > Basic Skills' 카테고리의 다른 글

Toggle Button  (0) 2022.03.20
Chips UI  (0) 2022.03.20
Loading  (0) 2022.03.20
Scroll Indicator  (0) 2022.03.20
Modal Window  (0) 2022.03.19
Comments