-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: 퍼널 컴포넌트 리팩토링 * refactor: RoomNewPage의 퍼널 부분 리팩토링 * docs: Funnel 스토리북 수정 * refactor: initialStep 기능 추가
- Loading branch information
1 parent
df8dc5e
commit 1f61f9f
Showing
11 changed files
with
142 additions
and
122 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
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
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 |
---|---|---|
@@ -1,32 +1,31 @@ | ||
import React, { PropsWithChildren } from 'react'; | ||
import { StepNames } from '../types/funnel'; | ||
import Step from './Step'; | ||
import React from 'react'; | ||
import Step, { StepProps } from './Step'; | ||
|
||
interface FunnelProps<T extends StepNames> { | ||
current: T[number]; | ||
interface FunnelProps<T extends readonly string[]> { | ||
step: T[number]; | ||
children: React.ReactNode; | ||
} | ||
|
||
const Funnel = <T extends StepNames>({ | ||
current, | ||
/** 하위 노드 중에서 렌더링 해야 할 Step 컴포넌트를 그리는 컴포넌트입니다. */ | ||
const Funnel = <T extends readonly string[]>({ | ||
step, | ||
children | ||
}: PropsWithChildren<FunnelProps<T>>) => { | ||
}: FunnelProps<T>) => { | ||
const validChildren = React.Children.toArray(children) | ||
.filter<React.ReactElement>(React.isValidElement) | ||
.filter((child) => child.type === Step); | ||
.filter(React.isValidElement) | ||
.filter((child) => child.type === Step) as React.ReactElement< | ||
StepProps<T> | ||
>[]; | ||
|
||
const currentStepIndex = validChildren.findIndex( | ||
(step) => step.props.name === current | ||
); | ||
const currentStep = validChildren.find((child) => child.props.name === step); | ||
|
||
if (currentStepIndex === -1) { | ||
if (!currentStep) { | ||
throw new Error( | ||
'보여주려는 current 스텝이 Funnel의 children 중에 존재하지 않습니다.' | ||
`Funnel의 children 중에서 ${step} 스텝이 존재하지 않습니다.` | ||
); | ||
} | ||
|
||
return <>{validChildren[currentStepIndex]}</>; | ||
return <>{currentStep}</>; | ||
}; | ||
|
||
Funnel.Step = Step; | ||
|
||
export default Funnel; |
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,28 @@ | ||
import { useState } from 'react'; | ||
|
||
const useFunnel = <T extends readonly string[]>( | ||
steps: T, | ||
initialStep: T[number] = steps[0] | ||
) => { | ||
const [step, setStep] = useState<T[number]>(initialStep); | ||
const currentIdx = steps.indexOf(step); | ||
|
||
const hasPrev = currentIdx > 0; | ||
const hasNext = currentIdx < steps.length - 1; | ||
|
||
const toPrev = () => { | ||
if (!hasPrev) return; | ||
|
||
setStep(steps[currentIdx - 1]); | ||
}; | ||
|
||
const toNext = () => { | ||
if (!hasNext) return; | ||
|
||
setStep(steps[currentIdx + 1]); | ||
}; | ||
|
||
return { step, setStep, hasPrev, toPrev, hasNext, toNext }; | ||
}; | ||
|
||
export default useFunnel; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { default as Funnel } from './components/Funnel'; | ||
export { default as createFunnel } from './utils/createFunnel'; | ||
export { default as useFunnel } from './hooks/useFunnel'; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.