|
1 | | -import { FC, useMemo } from 'react'; |
| 1 | +import { FC, KeyboardEvent, useMemo } from 'react'; |
2 | 2 |
|
3 | 3 | import { Typography } from '@equinor/eds-core-react'; |
4 | 4 | import { TypographyVariants } from '@equinor/eds-core-react/dist/types/components/Typography/Typography.tokens'; |
@@ -43,44 +43,70 @@ interface StepProps { |
43 | 43 | index: number; |
44 | 44 | onlyShowCurrentStepLabel?: boolean; |
45 | 45 | children?: string; |
| 46 | + allowJumpingAhead?: boolean; |
46 | 47 | } |
47 | 48 |
|
48 | 49 | export const Step: FC<StepProps> = ({ |
49 | 50 | index, |
50 | 51 | onlyShowCurrentStepLabel = false, |
51 | 52 | children, |
| 53 | + allowJumpingAhead = false, |
52 | 54 | }) => { |
53 | 55 | const { currentStep, setCurrentStep, isStepAtIndexDisabled } = useStepper(); |
54 | 56 |
|
55 | 57 | const isDisabled = isStepAtIndexDisabled(index); |
56 | 58 |
|
57 | 59 | const textVariant = useMemo((): TypographyVariants => { |
58 | 60 | if (index < currentStep) return 'body_short'; |
| 61 | + if (allowJumpingAhead && index > currentStep) return 'body_short_bold'; |
59 | 62 | return 'body_short_bold'; |
60 | | - }, [currentStep, index]); |
| 63 | + }, [currentStep, index, allowJumpingAhead]); |
61 | 64 |
|
62 | 65 | const textColor = useMemo((): string | undefined => { |
63 | | - if (index > currentStep || isDisabled) |
| 66 | + if (isDisabled) return colors.interactive.disabled__text.rgba; |
| 67 | + if (index > currentStep && !allowJumpingAhead) |
64 | 68 | return colors.interactive.disabled__text.rgba; |
65 | 69 | return colors.text.static_icons__default.rgba; |
66 | | - }, [currentStep, index, isDisabled]); |
| 70 | + }, [currentStep, index, isDisabled, allowJumpingAhead]); |
| 71 | + |
| 72 | + const isClickable = useMemo((): boolean => { |
| 73 | + if (isDisabled) return false; |
| 74 | + if (index < currentStep) return true; |
| 75 | + if (allowJumpingAhead && index > currentStep) return true; |
| 76 | + return false; |
| 77 | + }, [index, currentStep, isDisabled, allowJumpingAhead]); |
67 | 78 |
|
68 | 79 | const handleOnClick = () => { |
69 | | - if (index < currentStep && !isDisabled) { |
| 80 | + if (isClickable) { |
| 81 | + setCurrentStep(index); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + const handleOnKeyDown = (event: KeyboardEvent<HTMLDivElement>) => { |
| 86 | + if (!isClickable) return; |
| 87 | + |
| 88 | + if (event.key === 'Enter' || event.key === ' ') { |
| 89 | + event.preventDefault(); |
70 | 90 | setCurrentStep(index); |
71 | 91 | } |
72 | 92 | }; |
73 | 93 |
|
74 | 94 | return ( |
75 | 95 | <Container |
76 | 96 | data-testid="step" |
77 | | - $clickable={index < currentStep} |
| 97 | + $clickable={isClickable} |
78 | 98 | onClick={handleOnClick} |
| 99 | + onKeyDown={handleOnKeyDown} |
79 | 100 | $disabled={isDisabled} |
80 | | - aria-disabled={isDisabled} |
| 101 | + aria-disabled={!isClickable} |
81 | 102 | role="button" |
| 103 | + tabIndex={isClickable ? 0 : -1} |
82 | 104 | > |
83 | | - <StepIcon index={index} disabled={isDisabled} /> |
| 105 | + <StepIcon |
| 106 | + index={index} |
| 107 | + disabled={isDisabled} |
| 108 | + allowJumpingAhead={allowJumpingAhead} |
| 109 | + /> |
84 | 110 | {(!onlyShowCurrentStepLabel || currentStep === index) && ( |
85 | 111 | <Typography variant={textVariant} color={textColor}> |
86 | 112 | {children} |
|
0 commit comments