Skip to content

Commit 3d22446

Browse files
committed
feat: Update onStepChange prop to receive from and to step context.
1 parent af9d62c commit 3d22446

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/lib-ts/index.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,22 @@ export const StepsProvider: React.FC<React.PropsWithChildren> = ({
8686
);
8787
};
8888

89+
export interface StepChangeContext {
90+
from: number;
91+
to: number;
92+
}
93+
8994
export interface StepsProps {
9095
children: React.ReactNode;
91-
onStepChange?: () => void;
96+
onStepChange?: (context?: StepChangeContext) => void;
9297
startsFrom?: number;
9398
}
9499

95100
export const Steps: React.FC<StepsProps> = (props) => {
96101
const stepsContext = React.useContext(StepsContext);
97102
const { current, setCurrent, setSize } = stepsContext;
98103
const [isInitialRender, setIsInitialRender] = React.useState(true);
104+
const prevStepRef = React.useRef(current);
99105

100106
React.useEffect(() => {
101107
setIsInitialRender(false);
@@ -109,6 +115,7 @@ export const Steps: React.FC<StepsProps> = (props) => {
109115
} else {
110116
setCurrent(startsFrom);
111117
}
118+
prevStepRef.current = startsFrom;
112119
}, []);
113120

114121
React.useEffect(() => {
@@ -117,7 +124,10 @@ export const Steps: React.FC<StepsProps> = (props) => {
117124
}, [props.children]);
118125

119126
React.useEffect(() => {
120-
!isInitialRender && props.onStepChange?.();
127+
if (!isInitialRender) {
128+
props.onStepChange?.({ from: prevStepRef.current, to: current });
129+
}
130+
prevStepRef.current = current;
121131
}, [current]);
122132

123133
const steps = React.Children.map(props.children, (child, index) => {

0 commit comments

Comments
 (0)