import { Children, type Dispatch, type SetStateAction, createContext, useContext, useState, } from "react"; export type TStepsProps = { children: any; initialPath?: string[]; initialState?: any; lastBack?: () => void; [key: string]: any; }; type TStepContext = { nextStep: (step: string) => () => void; stepBack: () => void; close: () => void; state: T; path: string[]; setState: Dispatch>; }; const StepContext = createContext(undefined as any); export function Steps({ children, initialPath = [], initialState = {}, lastBack }: TStepsProps) { const [state, setState] = useState(initialState); const [path, setPath] = useState(initialPath); const steps: any[] = Children.toArray(children).filter( (child: any) => child.props.disabled !== true, ); function stepBack() { if (path.length === 0) { lastBack?.(); } else { setPath((prev) => prev.slice(0, -1)); } } const nextStep = (step: string) => () => { setPath((prev) => [...prev, step]); }; const current = steps.find((step) => step.props.id === path[path.length - 1]) || steps[0]; return ( {current} ); } export function useStepContext(): TStepContext { return useContext(StepContext); } export function Step({ children, disabled = false, path = [], id, ...rest }: { children: React.ReactNode; disabled?: boolean; id: string; path?: string[] }) { return
{children}
; }