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

[#562] [모임 생성] 모임 이름 퍼널 #565

Merged
merged 3 commits into from
May 1, 2024
Merged
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
39 changes: 39 additions & 0 deletions src/stories/bookGroup/create/funnel/EnterTitleStep.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Meta, StoryObj } from '@storybook/react';
import { appLayoutMeta } from '@/stories/meta';
import { FormProvider, useForm } from 'react-hook-form';

import {
EnterTitleStep,
type EnterTitleStepValues,
} from '@/v1/bookGroup/create/steps/EnterTitleStep';

const meta: Meta<typeof EnterTitleStep> = {
title: 'bookGroup/create/steps/EnterTitleStep',
component: EnterTitleStep,
...appLayoutMeta,
};

export default meta;

type Story = StoryObj<typeof EnterTitleStep>;

const EnterTitleForm = () => {
const methods = useForm<EnterTitleStepValues>({
mode: 'all',
defaultValues: {
title: '',
},
});

return (
<FormProvider {...methods}>
<form>
<EnterTitleStep />
</form>
</FormProvider>
);
};

export const Default: Story = {
render: () => <EnterTitleForm />,
};
36 changes: 36 additions & 0 deletions src/v1/bookGroup/create/steps/EnterTitleStep/EnterTitleStep.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useFormContext } from 'react-hook-form';

import BottomActionButton from '@/v1/base/BottomActionButton';
import { TitleField } from './fields';

interface MoveFunnelStepProps {
onNextStep?: () => void;
}

export interface EnterTitleStepValues {
title: string;
}

const EnterTitleStep = ({ onNextStep }: MoveFunnelStepProps) => {
const { handleSubmit } = useFormContext<EnterTitleStepValues>();

return (
<article>
<section className="flex flex-col gap-[1.5rem]">
<h2 className="text-lg font-bold text-black-700">
독서모임 이름을 적어주세요
</h2>
<TitleField name="title" />
</section>

<BottomActionButton
type="submit"
onClick={handleSubmit(() => onNextStep?.())}
>
다음
</BottomActionButton>
</article>
);
};

export default EnterTitleStep;
47 changes: 47 additions & 0 deletions src/v1/bookGroup/create/steps/EnterTitleStep/fields/TitleField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useFormContext, useWatch } from 'react-hook-form';

import type { EnterTitleStepValues } from '../EnterTitleStep';

import ErrorMessage from '@/v1/base/ErrorMessage';
import Input from '@/v1/base/Input';
import InputLength from '@/v1/base/InputLength';

type DefaultFieldNameProps = {
name: keyof EnterTitleStepValues;
};

const TitleField = ({ name }: DefaultFieldNameProps) => {
const {
register,
control,
formState: { errors },
} = useFormContext<EnterTitleStepValues>();

const titleValue = useWatch({ control, name: name });
const titleErrors = errors[name];

return (
<label htmlFor={name} className="flex flex-col gap-[0.5rem]">
<Input
id={name}
placeholder="독서모임을 잘 표현할 수 있는 이름이면 좋아요."
error={!!titleErrors}
{...register(name, {
required: '독서모임 이름을 작성해 주세요',
minLength: { value: 2, message: '2글자 이상 입력해주세요' },
maxLength: { value: 20, message: '20글자 이하로 입력해주세요' },
})}
/>
<div className="flex flex-row-reverse justify-between">
<InputLength
currentLength={titleValue.length}
isError={!!titleErrors}
maxLength={20}
/>
<ErrorMessage>{titleErrors?.message}</ErrorMessage>
</div>
</label>
);
Comment on lines +22 to +44
Copy link
Member Author

Choose a reason for hiding this comment

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

comment;

useWatch()formState: { errors }가 사용되는 부분을
TitleField.tsx로 분리하여 리렌더링되는 컴포넌트 수를 줄였습니다.

};

export default TitleField;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as TitleField } from './TitleField';
2 changes: 2 additions & 0 deletions src/v1/bookGroup/create/steps/EnterTitleStep/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { EnterTitleStepValues } from './EnterTitleStep';
export { default as EnterTitleStep } from './EnterTitleStep';
Loading