|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import React, { useState, useRef } from 'react'; |
| 4 | +import ProgressBar from '~components/progress-bar'; |
| 5 | +import SpaceBetween from '~components/space-between'; |
| 6 | +import Header from '~components/header'; |
| 7 | +import Button from '~components/button'; |
| 8 | +import Box from '~components/box'; |
| 9 | + |
| 10 | +export default function ProgressBarWithUpdates() { |
| 11 | + const [progressStep1, setProgressStep1] = useState(0); |
| 12 | + const [progressStep10, setProgressStep10] = useState(0); |
| 13 | + const timeoutRef1 = useRef<NodeJS.Timeout | number>(); |
| 14 | + const timeoutRef10 = useRef<NodeJS.Timeout | number>(); |
| 15 | + |
| 16 | + const activateTimerStep1 = () => { |
| 17 | + resetTimeoutStep1(); |
| 18 | + function step(i: number) { |
| 19 | + setProgressStep1(i + 1); |
| 20 | + timeoutRef1.current = setTimeout(() => i < 99 && step(i + 1), 100); |
| 21 | + } |
| 22 | + step(0); |
| 23 | + }; |
| 24 | + const resetTimeoutStep1 = () => { |
| 25 | + setProgressStep1(0); |
| 26 | + if (timeoutRef1.current !== undefined) { |
| 27 | + clearTimeout(timeoutRef1.current); |
| 28 | + timeoutRef1.current = undefined; |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + const activateTimerStep10 = () => { |
| 33 | + resetTimeoutStep10(); |
| 34 | + function step(i: number) { |
| 35 | + setProgressStep10(i * 10); |
| 36 | + timeoutRef10.current = setTimeout(() => i < 10 && step(i + 1), 500); |
| 37 | + } |
| 38 | + step(0); |
| 39 | + }; |
| 40 | + |
| 41 | + const resetTimeoutStep10 = () => { |
| 42 | + setProgressStep10(0); |
| 43 | + if (timeoutRef10.current !== undefined) { |
| 44 | + clearTimeout(timeoutRef10.current); |
| 45 | + timeoutRef10.current = undefined; |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + return ( |
| 50 | + <div> |
| 51 | + <Header variant={'h1'}>Dynamic progress bar</Header> |
| 52 | + <SpaceBetween direction={'vertical'} size={'s'}> |
| 53 | + <div> |
| 54 | + <Box variant={'div'} fontWeight={'bold'}> |
| 55 | + High granularity (step == 1) |
| 56 | + </Box> |
| 57 | + <ProgressBar |
| 58 | + status={progressStep1 < 100 ? 'in-progress' : 'success'} |
| 59 | + value={progressStep1} |
| 60 | + variant={'standalone'} |
| 61 | + label={'Tea'} |
| 62 | + description={'We will make a nice cup of tea ...'} |
| 63 | + additionalInfo={'Take some cookie as a desert'} |
| 64 | + resultText={'Your tea is ready!'} |
| 65 | + /> |
| 66 | + <div style={{ display: 'flex' }}> |
| 67 | + <Button onClick={activateTimerStep1}>Start</Button> |
| 68 | + <Button onClick={resetTimeoutStep1}>Reset</Button> |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + <div> |
| 72 | + <Box variant={'div'} fontWeight={'bold'}> |
| 73 | + Low granularity (step == 10) |
| 74 | + </Box> |
| 75 | + <ProgressBar |
| 76 | + status={progressStep10 < 100 ? 'in-progress' : 'success'} |
| 77 | + value={progressStep10} |
| 78 | + variant={'standalone'} |
| 79 | + label={'Tea'} |
| 80 | + description={'We will make a nice cup of tea ...'} |
| 81 | + additionalInfo={'Take some cookie as a desert'} |
| 82 | + resultText={'Your tea is ready!'} |
| 83 | + /> |
| 84 | + <div style={{ display: 'flex' }}> |
| 85 | + <Button onClick={activateTimerStep10}>Start</Button> |
| 86 | + <Button onClick={resetTimeoutStep10}>Reset</Button> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + </SpaceBetween> |
| 90 | + </div> |
| 91 | + ); |
| 92 | +} |
0 commit comments