| 1 |
import { useState } from 'react'; |
| 2 |
import { ProgressStepper, ProgressStep, Button, Stack, StackItem } from '@patternfly/react-core'; |
| 3 |
import accessibilityStyles from '@patternfly/react-styles/css/utilities/Accessibility/accessibility'; |
| 4 |
|
| 5 |
export const ProgressStepperBasic: React.FunctionComponent = () => { |
| 6 |
const [currentStep, setCurrentStep] = useState(0); |
| 7 |
|
| 8 |
const steps = [ |
| 9 |
{ title: 'First step', id: 'step1' }, |
| 10 |
{ title: 'Second step', id: 'step2' }, |
| 11 |
{ title: 'Third step', id: 'step3' }, |
| 12 |
{ title: 'Fourth step', id: 'step4' }, |
| 13 |
{ title: 'Fifth step', id: 'step5' } |
| 14 |
]; |
| 15 |
|
| 16 |
const onStepForward = (_event) => { |
| 17 |
const next = currentStep + 1; |
| 18 |
setCurrentStep(next <= 5 ? next : 4); |
| 19 |
}; |
| 20 |
|
| 21 |
const onStepBack = (_event) => { |
| 22 |
const next = currentStep - 1; |
| 23 |
setCurrentStep(next > 0 ? next : 0); |
| 24 |
}; |
| 25 |
|
| 26 |
return ( |
| 27 |
<Stack> |
| 28 |
<StackItem> |
| 29 |
<Button onClick={onStepBack} isDisabled={currentStep === 0}> |
| 30 |
Step back |
| 31 |
</Button>{' '} |
| 32 |
<Button onClick={onStepForward} isDisabled={currentStep === 5}> |
| 33 |
Step forward |
| 34 |
</Button> |
| 35 |
<br /> |
| 36 |
<br /> |
| 37 |
</StackItem> |
| 38 |
<StackItem> |
| 39 |
<div className={accessibilityStyles.screenReader} aria-live="polite"> |
| 40 |
{steps[currentStep] && `On ${steps[currentStep].title}.`} |
| 41 |
{steps[currentStep - 1] && `${steps[currentStep - 1].title} was successful.`} |
| 42 |
</div> |
| 43 |
<ProgressStepper> |
| 44 |
{steps.map((step, index) => { |
| 45 |
let variant = 'pending'; |
| 46 |
let ariaLabel = 'pending step'; |
| 47 |
if (index < currentStep) { |
| 48 |
variant = 'success'; |
| 49 |
ariaLabel = 'completed step, step with success'; |
| 50 |
} else if (index === currentStep) { |
| 51 |
variant = 'info'; |
| 52 |
ariaLabel = 'current step'; |
| 53 |
} |
| 54 |
|
| 55 |
return ( |
| 56 |
<ProgressStep |
| 57 |
id={index} |
| 58 |
titleId={step.id} |
| 59 |
key={index} |
| 60 |
variant={variant} |
| 61 |
isCurrent={index === currentStep} |
| 62 |
aria-label={ariaLabel} |
| 63 |
> |
| 64 |
{step.title} |
| 65 |
</ProgressStep> |
| 66 |
); |
| 67 |
})} |
| 68 |
</ProgressStepper> |
| 69 |
</StackItem> |
| 70 |
</Stack> |
| 71 |
); |
| 72 |
}; |