Skip to content

Commit 221463e

Browse files
committed
integrated step click handler
1 parent 3bda0ad commit 221463e

5 files changed

Lines changed: 45 additions & 19 deletions

File tree

src/stepper-component/constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const VISITED = 'visited';
2+
const UNVISITED = 'unvisited';
3+
const COMPLETED = 'completed';
4+
5+
export const STEP_STATUSES = {
6+
VISITED,
7+
UNVISITED,
8+
COMPLETED
9+
};

src/stepper-component/stepperComponent.tsx

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
1-
/* eslint-disable @typescript-eslint/no-var-requires */
2-
import React, { useState, useEffect } from 'react';
1+
import React, { useState, useEffect, ReactElement } from 'react';
32
import './styles.css';
43
import { Istep, IstepperProps } from './types';
54
import whiteTick from '../assets/white-tick.svg';
5+
import { STEP_STATUSES } from './constants';
66

7-
const Stepper = (props: IstepperProps): any => {
8-
const { steps, currentActiveStepIndex } = props;
7+
const Stepper = (props: IstepperProps): ReactElement => {
8+
const { steps, currentActiveStepIndex, onStepClick, enableStepClick = false } = props;
99

10-
// const whiteTick = require('../assets/white-tick.svg') as string;
11-
const [stepsVal, setSteps] = useState<Istep[]>([]);
1210
const [currentActiveStepIndexVal, setCurrentActiveStepIndexVal] = useState<number>()
1311

14-
useEffect(() => {
15-
setSteps(steps);
16-
}, [steps]);
17-
1812
useEffect(() => {
1913
setCurrentActiveStepIndexVal(currentActiveStepIndex ?? 0);
2014
}, [currentActiveStepIndex]);
2115

16+
const handleStepClick = (stepIndex: number): void => {
17+
if (enableStepClick) {
18+
if (onStepClick !== undefined ) onStepClick(stepIndex);
19+
else setCurrentActiveStepIndexVal(stepIndex);
20+
}
21+
}
22+
2223
return (
2324
<div className="stepperContainer">
24-
{stepsVal?.map((step: Istep, index: number) => (
25+
{steps?.map((step: Istep, index: number): ReactElement => (
2526
<div key={index} className="eachStep">
2627
<div className='bubbleLineWrapper'>
2728
<div
2829
className={`eachBubble
30+
${enableStepClick && 'cursorPointer'}
2931
${index === currentActiveStepIndexVal && 'activeStepBubble'}
30-
${index > currentActiveStepIndexVal && 'inactiveStepBubble'}`}
32+
${(step.status === STEP_STATUSES.UNVISITED && currentActiveStepIndexVal !== index) && 'inactiveStepBubble'}`}
33+
onClick={(): void => handleStepClick(index)}
3134
>
32-
{currentActiveStepIndexVal > index && (
35+
{step?.status === STEP_STATUSES.COMPLETED && (
3336
<img
3437
src={whiteTick}
3538
className="whiteTickImg"
@@ -40,17 +43,17 @@ const Stepper = (props: IstepperProps): any => {
4043
</div>
4144
<div className='eachLabel'>
4245
{step.label && (
43-
<span className='labelTitle'>
46+
<span className={`labelTitle ${index === currentActiveStepIndexVal && 'activeLabelTitle'}`}>
4447
{step?.label}
4548
</span>
4649
)}
4750
{step?.description && (
48-
<span className='labelDescription'>
51+
<span className={`labelDescription ${index === currentActiveStepIndexVal && 'activeLabelDescription'}`}>
4952
{step?.description}
5053
</span>
5154
)}
5255
</div>
53-
{index < stepsVal?.length - 1 && (
56+
{index < steps?.length - 1 && (
5457
<div className={`lineSeparator ${(index > currentActiveStepIndexVal - 1) && 'activeStepLineSeparator'}`} />
5558
)}
5659
</div>

src/stepper-component/styles.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
.inactiveStepBubble {
3232
opacity: 0.4;
3333
}
34+
.cursorPointer {
35+
cursor: pointer;
36+
}
3437
.whiteTickImg {
3538
object-fit: cover;
3639
width: 10px;
@@ -61,13 +64,19 @@
6164
.labelTitle {
6265
color: #4F4F4F;
6366
}
67+
.activeLabelTitle {
68+
font-weight: 700;
69+
}
6470
.labelDescription {
6571
font-weight: 400;
6672
font-size: 12px;
6773
line-height: 21px;
6874
letter-spacing: 0.02em;
6975
color: #929292;
7076
}
77+
.activeLabelDescription {
78+
color: #676767;
79+
}
7180
.lineSeparator {
7281
height: 22px;
7382
width: 1px;

src/stepper-component/types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
export interface Istep {
22
label: string,
3-
description?: string
3+
description?: string,
4+
status: string
45
}
56

67
export interface IstepperProps {
78
steps: Istep[],
8-
currentActiveStepIndex: number
9+
currentActiveStepIndex: number,
10+
onStepClick?(stepIndex: number): void,
11+
enableStepClick: boolean
912
}

src/stories/StepperComponent.stories.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ LoggedIn.args = {
3232
label: 'Step 4',
3333
description: 'The quick brown fox jumps over the lazy dog'
3434
}],
35-
currentActiveStepIndex: 2
35+
currentActiveStepIndex: 2,
36+
enableStepClick: true,
37+
// onStepClick: (stepIndex: number) => console.log("🚀 ~ file: StepperComponent.stories.tsx:37 ~ stepIndex", stepIndex)
3638
};

0 commit comments

Comments
 (0)