|
1 | | -/* eslint-disable @typescript-eslint/no-var-requires */ |
2 | | -import React, { useState, useEffect } from 'react'; |
3 | | -import './styles.css'; |
4 | | -import { Istep, IstepperProps } from './types'; |
5 | | -import whiteTick from '../assets/white-tick.svg'; |
| 1 | +import React, { useState, useEffect, ReactElement, FC } from 'react'; |
| 2 | +import styles from './styles'; |
| 3 | +import { IStep, IStepperProps } from './types'; |
| 4 | +import Bubble from '../bubble'; |
| 5 | +import { LABEL_POSITION } from '../constants'; |
6 | 6 |
|
7 | | -const Stepper = (props: IstepperProps): any => { |
8 | | - const { steps, currentActiveStepIndex } = props; |
| 7 | +const Stepper: FC<IStepperProps> = (props) => { |
| 8 | + const { |
| 9 | + steps, |
| 10 | + currentActiveStepIndex, |
| 11 | + enableStepClick = false, |
| 12 | + onStepClick, |
| 13 | + renderAdornment, |
| 14 | + stylesOverride = {}, |
| 15 | + labelPosition = LABEL_POSITION.RIGHT |
| 16 | + } = props; |
9 | 17 |
|
10 | | - // const whiteTick = require('../assets/white-tick.svg') as string; |
11 | | - const [stepsVal, setSteps] = useState<Istep[]>([]); |
12 | | - const [currentActiveStepIndexVal, setCurrentActiveStepIndexVal] = useState<number>() |
| 18 | + const { |
| 19 | + getLabelDescriptionStyles, |
| 20 | + getLabelTitleStyles, |
| 21 | + getActiveLabelTitleStyles, |
| 22 | + getActiveLabelDescriptionStyles, |
| 23 | + getLineSeparatorStyles, |
| 24 | + getInactiveLineSeparatorStyles, |
| 25 | + getBubbleStyles, |
| 26 | + getActiveBubbleStyles, |
| 27 | + getInActiveBubbleStyles |
| 28 | + } = stylesOverride; |
13 | 29 |
|
14 | | - useEffect(() => { |
15 | | - setSteps(steps); |
16 | | - }, [steps]); |
| 30 | + const [currentActiveStepIndexVal, setCurrentActiveStepIndexVal] = useState<number>(0); |
17 | 31 |
|
18 | 32 | useEffect(() => { |
19 | | - setCurrentActiveStepIndexVal(currentActiveStepIndex ?? 0); |
| 33 | + setCurrentActiveStepIndexVal((currentActiveStepIndex ?? 0) as number); |
20 | 34 | }, [currentActiveStepIndex]); |
21 | 35 |
|
| 36 | + const handleStepClick = (stepIndex: number): void => { |
| 37 | + if (enableStepClick) { |
| 38 | + if (onStepClick) onStepClick(stepIndex); |
| 39 | + else setCurrentActiveStepIndexVal(stepIndex); |
| 40 | + } |
| 41 | + } |
| 42 | + |
22 | 43 | return ( |
23 | | - <div className="stepperContainer"> |
24 | | - {stepsVal?.map((step: Istep, index: number) => ( |
25 | | - <div key={index} className="eachStep"> |
26 | | - <div className='bubbleLineWrapper'> |
| 44 | + <div style={styles.stepperContainer}> |
| 45 | + {steps?.map((step: IStep, index: number): ReactElement => ( |
| 46 | + <div key={index} style={styles.eachStep}> |
| 47 | + <div style={styles.bubbleLineWrapper}> |
| 48 | + <Bubble |
| 49 | + step={step} |
| 50 | + index={index} |
| 51 | + enableStepClick={enableStepClick} |
| 52 | + currentActiveStepIndexVal= {currentActiveStepIndexVal} |
| 53 | + handleStepClick={handleStepClick} |
| 54 | + renderAdornment={renderAdornment} |
| 55 | + getBubbleStyles={getBubbleStyles} |
| 56 | + getActiveBubbleStyles={getActiveBubbleStyles} |
| 57 | + getInActiveBubbleStyles={getInActiveBubbleStyles} |
| 58 | + /> |
27 | 59 | <div |
28 | | - className={`eachBubble |
29 | | - ${index === currentActiveStepIndexVal && 'activeStepBubble'} |
30 | | - ${index > currentActiveStepIndexVal && 'inactiveStepBubble'}`} |
| 60 | + style={{ |
| 61 | + ...styles.labelContainer, |
| 62 | + ...styles[`labelContainer__${labelPosition}`] ?? {} |
| 63 | + }} |
31 | 64 | > |
32 | | - {currentActiveStepIndexVal > index && ( |
33 | | - <img |
34 | | - src={whiteTick} |
35 | | - className="whiteTickImg" |
36 | | - alt="" |
37 | | - /> |
38 | | - ) |
39 | | - || index + 1} |
40 | | - </div> |
41 | | - <div className='eachLabel'> |
42 | | - {step.label && ( |
43 | | - <span className='labelTitle'> |
44 | | - {step?.label} |
| 65 | + {step?.label && ( |
| 66 | + <span |
| 67 | + style={{...styles.labelTitle, |
| 68 | + ...((enableStepClick && styles.cursorPointer) || {}), |
| 69 | + ...((getLabelTitleStyles && getLabelTitleStyles(step, index)) || {}), |
| 70 | + ...((index === currentActiveStepIndexVal && styles.activeLabelTitle) || {}), |
| 71 | + ...((index === currentActiveStepIndexVal && getActiveLabelTitleStyles && getActiveLabelTitleStyles(step, index)) || {}) |
| 72 | + }} |
| 73 | + onClick={(): void => handleStepClick(index)} |
| 74 | + role="presentation" |
| 75 | + > |
| 76 | + {step.label} |
45 | 77 | </span> |
46 | 78 | )} |
47 | 79 | {step?.description && ( |
48 | | - <span className='labelDescription'> |
49 | | - {step?.description} |
| 80 | + <span style={{...styles.labelDescription, |
| 81 | + ...((enableStepClick && styles.cursorPointer) || {}), |
| 82 | + ...((getLabelDescriptionStyles && getLabelDescriptionStyles(step, index)) || {}), |
| 83 | + ...((index === currentActiveStepIndexVal) && styles.activeLabelDescription|| {}), |
| 84 | + ...((index === currentActiveStepIndexVal && getActiveLabelDescriptionStyles && getActiveLabelDescriptionStyles(step, index)) || {}) |
| 85 | + }} |
| 86 | + onClick={(): void => handleStepClick(index)} |
| 87 | + role="presentation" |
| 88 | + > |
| 89 | + {step.description} |
50 | 90 | </span> |
51 | 91 | )} |
52 | 92 | </div> |
53 | | - {index < stepsVal?.length - 1 && ( |
54 | | - <div className={`lineSeparator ${(index > currentActiveStepIndexVal - 1) && 'activeStepLineSeparator'}`} /> |
| 93 | + {index < steps?.length - 1 && ( |
| 94 | + <div style={{...styles.lineSeparator, |
| 95 | + ...((getLineSeparatorStyles && getLineSeparatorStyles(step, index)) || {}), |
| 96 | + ...((index > currentActiveStepIndexVal - 1) && styles.inactiveStepLineSeparator || {}), |
| 97 | + ...((index > currentActiveStepIndexVal - 1 |
| 98 | + && getInactiveLineSeparatorStyles && getInactiveLineSeparatorStyles(step, index)) || {}) |
| 99 | + }} /> |
55 | 100 | )} |
56 | 101 | </div> |
57 | 102 | </div> |
|
0 commit comments