Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SMH-91] Funnel 컴포넌트 #32

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions packages/design-system/src/components/funnel/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Children, isValidElement } from 'react';
import type { ReactElement, ReactNode } from 'react';

export type NonEmptyArray<T> = readonly [T, ...T[]];

export interface FunnelProps<Steps extends NonEmptyArray<string>> {
steps: Steps;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string의 배열로 선언해도 되지 않나 생각했는데 string[]
빈 배열이 없다는 것을 타입으 보호하려고 의도한 것이 맞을까요? 실제로 빈 배열일 경우가 없나요?

step: Steps[number];
children:
| Array<ReactElement<StepProps<Steps>>>
| ReactElement<StepProps<Steps>>;
}

export interface StepProps<Steps extends NonEmptyArray<string>> {
name: Steps[number];
children: ReactNode;
}
export const Step = <T extends NonEmptyArray<string>>({
children,
}: StepProps<T>) => {
return <>{children}</>;
};

export const Funnel = <Steps extends NonEmptyArray<string>>({
steps,
step,
children,
}: FunnelProps<Steps>) => {
const validChildren = Children.toArray(children)
.filter(isValidElement)
.filter((i) =>
steps.includes((i.props as Partial<StepProps<Steps>>).name ?? ''),
) as Array<ReactElement<StepProps<Steps>>>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 타입 단언을 하는 이유는 무엇인가요?
타입 추론을 못하나요?


const targetStep = validChildren.find((child) => child.props.name === step);

if (targetStep == null) {
throw new Error(`${step} 스텝 컴포넌트를 찾지 못했습니다.`);
}

return <>{targetStep}</>;
};
39 changes: 39 additions & 0 deletions packages/design-system/src/hooks/useFunnel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
Funnel,
Step,
type FunnelProps,
type NonEmptyArray,
type StepProps,
} from '@components/funnel';
import { useMemo, useState } from 'react';

type RouteFunnelProps<Steps extends NonEmptyArray<string>> = Omit<
FunnelProps<Steps>,
'steps' | 'step'
>;

type FunnelComponent<Steps extends NonEmptyArray<string>> = ((
props: RouteFunnelProps<Steps>,
) => JSX.Element) & {
Step: (props: StepProps<Steps>) => JSX.Element;
};

export const useFunnel = <Steps extends NonEmptyArray<string>>(
steps: Steps,
options?: { initialStep?: Steps[number] },
): readonly [FunnelComponent<Steps>, (step: Steps[number]) => void] => {
const [step, setStep] = useState(options?.initialStep ?? steps[0]);

const FunnelComponent = useMemo(() => {
return Object.assign(
function RouteFunnel(props: RouteFunnelProps<Steps>) {
return <Funnel<Steps> steps={steps} step={step} {...props} />;
},
{
Step,
},
);
}, [step]);

return [FunnelComponent, setStep] as const;
};
Loading