Skip to content

Commit 4870a6a

Browse files
feat: Add custom styles for current step and completed steps
feat: Add custom styles for current step and completed steps
1 parent 91827e8 commit 4870a6a

File tree

6 files changed

+210
-160
lines changed

6 files changed

+210
-160
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ Props that can be passed to the component are listed below:
136136
</td>
137137
<td><code>undefined</code></td>
138138
</tr>
139+
<tr>
140+
<td><code><b>completedNodeStyle?:</b> object</code></td>
141+
<td>Custom styles for completed nodes</td>
142+
<td><code>undefined</code></td>
143+
</tr>
144+
<tr>
145+
<td><code><b>currentNodeStyle?:</b> object</code></td>
146+
<td>Custom styles for current nodes</td>
147+
<td><code>undefined</code></td>
148+
</tr>
139149
</tbody>
140150
</table>
141151

@@ -159,7 +169,8 @@ function App() {
159169
}),
160170
CompletedNode: () => ({
161171
backgroundColor: "#028A0F",
162-
};
172+
})
173+
};
163174

164175
return (
165176
<Stepper

src/node/node.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,38 @@ const Node: FC<INodeProps> = (props) => {
1919
currentStepIndex,
2020
handleStepClick,
2121
showCursor,
22-
getStyles
22+
getStyles,
23+
nodeStyle
2324
} = props;
2425

26+
const isCompleted = step.completed;
27+
const isActive = index === currentStepIndex;
28+
2529
return (
2630
<div
2731
className={`${styles.eachNode}
2832
${showCursor && styles.cursorPointer}
29-
${index === currentStepIndex && styles.activeStepNode}
30-
${!step.completed && currentStepIndex !== index && styles.inactiveStepNode}
31-
${step.completed && currentStepIndex !== index && styles.completedStepNode}
33+
${isActive && styles.activeStepNode}
34+
${!isCompleted && !isActive && styles.inactiveStepNode}
35+
${isCompleted && !isActive && styles.completedStepNode}
3236
`}
33-
style={{
34-
...((getStyles(Elements.Node)) || {}),
35-
...((index === currentStepIndex && getStyles(Elements.ActiveNode)) || {}),
36-
...((step.completed && getStyles(Elements.CompletedNode)) || {}),
37-
...((!step.completed && currentStepIndex !== index
38-
&& getStyles(Elements.InActiveNode)) || {})
39-
}}
37+
style={nodeStyle}
4038
onClick={(): void | null => handleStepClick && handleStepClick()}
4139
role="presentation"
4240
id="stepper-node"
4341
>
4442
{(renderNode && renderNode(step, index))
4543
|| (
4644
<>
47-
{step?.completed && (
45+
{isCompleted ? (
4846
<img
4947
src={whiteTick}
5048
className={styles.whiteTickImg}
5149
alt=""
52-
/>)
53-
|| index + 1}
50+
/>
51+
) : (
52+
index + 1
53+
)}
5454
</>
5555
)}
5656
</div>

src/node/types.d.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { ReactElement } from "react";
2-
import { IStep } from "../stepper/types";
1+
import { ReactElement, CSSProperties } from "react";
32
import { Elements } from "../constants";
3+
import type { IStep } from "../stepper/types";
44

5-
export type INodeProps = {
5+
export interface INodeProps {
66
step: IStep;
7-
renderNode?(step: IStep, index: number): ReactElement;
87
index: number;
9-
currentStepIndex?: number;
10-
handleStepClick(): void;
8+
currentStepIndex: number;
9+
handleStepClick?: () => void;
1110
showCursor: boolean;
12-
getStyles(element: Elements): object;
13-
};
11+
renderNode?: (step: IStep, stepIndex: number) => ReactElement;
12+
getStyles: (element: Elements) => object;
13+
nodeStyle?: CSSProperties;
14+
}

src/stepper/step.tsx

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useRef, useEffect, useState } from "react";
22
import "./styles.scss";
3-
import type { IStepProps } from "../stepper/types";
3+
import type { IStepProps } from "./types";
44
import { LABEL_POSITION, ORIENTATION } from "../constants";
55
import StepContent from "./stepContent";
66
import StepInfo from "./stepInfo";
@@ -26,7 +26,9 @@ const Step: (props: IStepProps) => JSX.Element = ({
2626
showDescriptionsForAllSteps = false,
2727
stepContent,
2828
onStepClick,
29-
renderNode
29+
renderNode,
30+
completedNodeStyle,
31+
currentNodeStyle
3032
} = stepperProps;
3133
const [nodeWidth, setNodeWidth] = useState(0);
3234

@@ -68,51 +70,39 @@ const Step: (props: IStepProps) => JSX.Element = ({
6870
currentStepIndex > index ? "activeConnector" : ""
6971
} ${index === steps.length - 1 ? "hiddenConnector" : ""}`;
7072

73+
const stepInfoProps = {
74+
orientation,
75+
labelPosition,
76+
isVertical,
77+
isInlineLabelsAndSteps,
78+
index,
79+
currentStepIndex,
80+
step,
81+
showDescriptionsForAllSteps,
82+
onStepClick,
83+
renderNode,
84+
styles,
85+
nodeRef,
86+
prevConnectorClassName,
87+
nextConnectorClassName,
88+
steps,
89+
completedNodeStyle,
90+
currentNodeStyle
91+
};
92+
7193
return orientation !== ORIENTATION.VERTICAL &&
7294
labelPosition === LABEL_POSITION.TOP ? (
73-
<StepInfo
74-
orientation={orientation}
75-
labelPosition={labelPosition}
76-
isVertical={isVertical}
77-
isInlineLabelsAndSteps={isInlineLabelsAndSteps}
78-
index={index}
79-
currentStepIndex={currentStepIndex}
80-
step={step}
81-
showDescriptionsForAllSteps={showDescriptionsForAllSteps}
82-
onStepClick={onStepClick}
83-
renderNode={renderNode}
84-
styles={styles}
85-
nodeRef={nodeRef}
86-
prevConnectorClassName={prevConnectorClassName}
87-
nextConnectorClassName={nextConnectorClassName}
88-
steps={steps}
89-
/>
95+
<StepInfo {...stepInfoProps} />
9096
) : (
9197
<div
9298
className={
9399
orientation === ORIENTATION.VERTICAL &&
94-
labelPosition === LABEL_POSITION.LEFT
100+
labelPosition === LABEL_POSITION.LEFT
95101
? "verticalTextLeftContainer"
96102
: ""
97103
}
98104
>
99-
<StepInfo
100-
orientation={orientation}
101-
labelPosition={labelPosition}
102-
isVertical={isVertical}
103-
isInlineLabelsAndSteps={isInlineLabelsAndSteps}
104-
index={index}
105-
currentStepIndex={currentStepIndex}
106-
step={step}
107-
showDescriptionsForAllSteps={showDescriptionsForAllSteps}
108-
onStepClick={onStepClick}
109-
renderNode={renderNode}
110-
styles={styles}
111-
nodeRef={nodeRef}
112-
prevConnectorClassName={prevConnectorClassName}
113-
nextConnectorClassName={nextConnectorClassName}
114-
steps={steps}
115-
/>
105+
<StepInfo {...stepInfoProps} />
116106
<StepContent
117107
labelPosition={labelPosition}
118108
isVertical={isVertical}

0 commit comments

Comments
 (0)