Skip to content

Commit

Permalink
[#367] Switch 컴포넌트 (#386)
Browse files Browse the repository at this point in the history
* feat: switch 컴포넌트 공유를 위한 커밋

* style: Switch 컴포넌트 마크업 변경

* feat: Switch 컴포넌트 구현 및 사용하지 않는 컴포넌트 제거

* chore: 사용하지 않는 stories/Switch 컴포넌트 제거

* fix: FormValues 타입 boolean으로 수정
  • Loading branch information
gxxrxn committed Jun 17, 2024
1 parent 182def7 commit e1b1ae9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 41 deletions.
57 changes: 57 additions & 0 deletions src/stories/Base/Switch.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 Switch from '@/ui/Base/Switch';
import Button from '@/ui/Base/Button';

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

export default meta;

type Story = StoryObj<typeof Switch>;

type FormValues = {
comment: boolean;
};

const SwitchWithUseForm = () => {
const { register, handleSubmit } = useForm<FormValues>({
mode: 'all',
defaultValues: { comment: true },
});

const handleSubmitForm: SubmitHandler<FormValues> = ({ comment }) => {
alert(`comment: ${comment ? 'public' : 'private'}`);
};

return (
<form
onSubmit={handleSubmit(handleSubmitForm)}
className="flex w-[43rem] flex-col gap-[1.6rem]"
>
<div className="flex justify-between">
<span className="text-md">댓글 공개</span>
<Switch {...register('comment')} />
</div>
<Button
size="large"
type="submit"
onClick={handleSubmit(handleSubmitForm)}
>
Submit
</Button>
</form>
);
};

export const Default: Story = {
args: { name: 'switch' },
};

export const WithForm: Story = {
render: SwitchWithUseForm,
};
16 changes: 0 additions & 16 deletions src/stories/Switch.stories.ts

This file was deleted.

25 changes: 0 additions & 25 deletions src/stories/Switch.tsx

This file was deleted.

30 changes: 30 additions & 0 deletions src/ui/Base/Switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ComponentPropsWithoutRef, forwardRef, Ref } from 'react';

const Switch = (
{
name,
...props
}: Omit<ComponentPropsWithoutRef<'input'>, 'id' | 'type' | 'className'>,
ref: Ref<HTMLInputElement>
) => {
return (
<label
className="relative inline-flex h-[2.4rem] w-[4.2rem] cursor-pointer items-center"
htmlFor={name}
>
<input
id={name}
name={name}
className="peer hidden"
type="checkbox"
ref={ref}
{...props}
/>
<div className="absolute bottom-0 left-0 right-0 top-0 rounded-full bg-cancel peer-checked:bg-main-900" />
<span className="absolute left-0 h-[2rem] w-[2rem] translate-x-[2rem] transform rounded-full bg-white transition peer-checked:translate-x-[0.25rem]" />
<span className="sr-only">{`Enable ${name}`}</span>
</label>
);
};

export default forwardRef(Switch);

0 comments on commit e1b1ae9

Please sign in to comment.