Skip to content

Commit

Permalink
[#398] RadioButton 컴포넌트 (#400)
Browse files Browse the repository at this point in the history
* feat: RadioButton 컴포넌트 작성

* feat: RadioButton 스토리북 작성

* fix: 대문자 수정
  • Loading branch information
hanyugeon authored Oct 17, 2023
1 parent 14184d3 commit 4f57252
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/stories/Base/RadioButton.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Meta, StoryObj } from '@storybook/react';
import { SubmitHandler, useForm } from 'react-hook-form';

import RadioButton from '@/ui/Base/RadioButton';
import Button from '@/ui/Base/Button';

const meta: Meta<typeof RadioButton> = {
title: 'Base/RadioButton',
component: RadioButton,
tags: ['autodocs'],
};

export default meta;

type Story = StoryObj<typeof RadioButton>;

type FormValues = {
radio: string;
};

const RadioButtonWithUseForm = () => {
const { register, handleSubmit, watch } = useForm<FormValues>({
mode: 'all',
defaultValues: { radio: '라디오 A' },
});

const handleSubmitForm: SubmitHandler<FormValues> = ({ radio }) => {
alert(`Submit as: ${radio}`);
};

return (
<>
<form
onSubmit={handleSubmit(handleSubmitForm)}
className="flex w-[43rem] flex-col gap-[1.6rem]"
>
<div className="flex justify-between">
<RadioButton {...register('radio')} value="라디오 A" />
<RadioButton {...register('radio')} value="라디오 B" />
<RadioButton {...register('radio')} value="라디오 C" />
</div>
<Button size="large" type="submit">
Submit
</Button>
</form>
<pre>{JSON.stringify(watch(), null, 2)}</pre>
</>
);
};

export const Default: Story = {
render: args => <RadioButton {...args} value="라디오 버튼" />,
};

export const RadioButtonForm: Story = {
render: RadioButtonWithUseForm,
};
30 changes: 30 additions & 0 deletions src/ui/Base/RadioButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ComponentPropsWithoutRef, forwardRef, Ref } from 'react';

const BASE_RADIO_BUTTON_CLASSES =
'px-[1.2rem] py-[0.5rem] bg-main-600/[0.18] text-main-900 text-sm font-normal rounded-[2rem] cursor-pointer w-full h-full peer-checked:bg-main-900 peer-checked:text-white';

const RadioButton = (
{
name,
value,
...props
}: Omit<ComponentPropsWithoutRef<'input'>, 'id' | 'type' | 'className'>,
ref: Ref<HTMLInputElement>
) => {
return (
<label htmlFor={`id-${value}`}>
<input
id={`id-${value}`}
name={name}
className="peer hidden"
type="radio"
value={value}
ref={ref}
{...props}
/>
<span className={BASE_RADIO_BUTTON_CLASSES}>{value}</span>
</label>
);
};

export default forwardRef(RadioButton);

0 comments on commit 4f57252

Please sign in to comment.