-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Stepper component to library. Add wrapper component to Stepper Add empty fragment to Stepper component. Refactor Stepper component to use Box instead of Wrap.
- Loading branch information
1 parent
6c588b7
commit d49e2e9
Showing
4 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { createContext, useContext, useState } from 'react'; | ||
import { | ||
Add, | ||
ExctractProps, | ||
StepperContextType, | ||
Steps, | ||
Subtract, | ||
} from './types'; | ||
import { Steppers } from './Steppers.component'; | ||
import { Box } from '../box/Box'; | ||
|
||
const StepperContext = createContext<StepperContextType | null>(null); | ||
|
||
export const useStepper = < | ||
T extends any[], | ||
StepIndex extends number, | ||
NextIndex = Add<StepIndex, 1>, | ||
PrevIndex = Subtract<StepIndex, 1>, | ||
>( | ||
index: StepIndex, | ||
steps: readonly [...Steps<T>], | ||
): (NextIndex extends number | ||
? { | ||
next: (props: ExctractProps<T[NextIndex]>) => void; | ||
} | ||
: Record<string, unknown>) & | ||
(PrevIndex extends -1 | ||
? Record<string, unknown> | ||
: PrevIndex extends number | ||
? { | ||
prev: (props: ExctractProps<T[PrevIndex]>) => void; | ||
} | ||
: Record<string, unknown>) => { | ||
const context = useContext(StepperContext); | ||
|
||
if (context === null) { | ||
throw new Error('Cannot use useStepper outside of Stepper'); | ||
} | ||
const { next, prev } = context; | ||
|
||
//@ts-expect-error generic type | ||
return { next, prev }; | ||
}; | ||
|
||
export const Stepper = <T extends any[]>({ | ||
steps, | ||
}: { | ||
steps: readonly [...Steps<T>]; | ||
}) => { | ||
const [currentStep, setCurrentStep] = useState<number>(0); | ||
const [stepProps, setStepProps] = useState<Record<string, unknown>>({}); | ||
|
||
const next = (props: Record<string, unknown>) => { | ||
setCurrentStep(currentStep + 1); | ||
setStepProps(props); | ||
}; | ||
|
||
const prev = (props: Record<string, unknown>) => { | ||
setCurrentStep(currentStep - 1); | ||
setStepProps(props); | ||
}; | ||
|
||
const { Component } = steps[currentStep]; | ||
|
||
return ( | ||
<StepperContext.Provider value={{ next, prev }}> | ||
<Box display={'flex'}> | ||
<Steppers | ||
activeStep={currentStep} | ||
steps={steps.map((step) => { | ||
return { | ||
title: step.label, | ||
}; | ||
})} | ||
/> | ||
<Component {...stepProps} /> | ||
</Box> | ||
</StepperContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { ReactNode } from 'react'; | ||
|
||
type MAXIMUM_DEPTH = 20; | ||
|
||
type GetResults<T> = T extends Step<infer Props> ? Step<Props> : never; | ||
|
||
type Length<T extends any[]> = T extends { length: infer L } ? L : never; | ||
|
||
type BuildTuple<L extends number, T extends any[] = []> = T extends { | ||
length: L; | ||
} | ||
? T | ||
: BuildTuple<L, [...T, any]>; | ||
|
||
export interface Step<T> { | ||
label: string; | ||
Component: (args: T) => ReactNode; | ||
} | ||
|
||
export interface StepperContextType { | ||
next: (props: Record<string, unknown>) => void; | ||
prev: (props: Record<string, unknown>) => void; | ||
} | ||
|
||
export declare type Steps< | ||
T extends any[], | ||
Result extends any[] = [], | ||
Depth extends ReadonlyArray<number> = [], | ||
> = Depth['length'] extends MAXIMUM_DEPTH | ||
? Step<unknown>[] | ||
: T extends [] | ||
? [] | ||
: T extends [infer Head] | ||
? [...Result, GetResults<Head>] | ||
: T extends [infer Head, ...infer Tail] | ||
? Steps<[...Tail], [...Result, GetResults<Head>], [...Depth, 1]> | ||
: unknown[] extends T | ||
? T | ||
: never; | ||
|
||
export type Add<A extends number, B extends number> = Length< | ||
[...BuildTuple<A>, ...BuildTuple<B>] | ||
>; | ||
|
||
export type Subtract<A extends number, B extends number> = | ||
BuildTuple<A> extends [...infer U, ...BuildTuple<B>] ? Length<U> : -1; | ||
|
||
export type ExctractProps<T> = T extends Step<infer Props> ? Props : never; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters