-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: RadioGroup (RadioButton) 컴포넌트 구현 #147
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a389652
feat: RadioGroup 구현 완료
fecapark c5c4486
docs: 임시저장
fecapark 86aa6c0
Merge branch 'develop' into feat/#142-radio-button
fecapark 38209a6
docs: RadioGroup 문서 완료
fecapark 164a566
docs: 제네릭 누락 수정
fecapark 2fb3851
feat: index.ts 에 모듈 export 추가
fecapark d1f310b
fix: 배경 색상 투명으로 변경
fecapark c256978
feat: radio focus시 outline 스타일 임의 추가
fecapark cbf2b92
feat: 현재 선택된 radio를 저장하는 context 추가
fecapark fc82e42
feat: radiogroup focus 기능 개선
fecapark e857187
feat: disabled시 radio focus 막는 기능 추가
fecapark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createContext } from 'react'; | ||
|
||
import { | ||
RadioGroupSizeType, | ||
RadioGroupOrientationType, | ||
} from '@/components/RadioGroup/RadioGroup.type'; | ||
|
||
interface RadioGroupContextProps { | ||
orientation?: RadioGroupOrientationType; | ||
size?: RadioGroupSizeType; | ||
currentRadioValue?: string; | ||
} | ||
|
||
export const RadioGroupContext = createContext<RadioGroupContextProps>({ | ||
size: undefined, | ||
orientation: undefined, | ||
currentRadioValue: undefined, | ||
}); |
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,144 @@ | ||
import { Canvas, Meta, Controls } from '@storybook/blocks'; | ||
import * as RadioGroupStories from './RadioGroup.stories'; | ||
|
||
<Meta of={RadioGroupStories} /> | ||
|
||
# RadioGroup | ||
|
||
RadioGroup은 단일 선택을 나타낼 수 있는 요소인 Radio Button을 그룹화하여 사용할 수 있도록 도와주는 컴포넌트입니다. | ||
|
||
<Canvas of={RadioGroupStories.Control} /> | ||
<Controls /> | ||
|
||
<br /> | ||
<br /> | ||
|
||
## 개발 시 참고하면 좋을 내용 | ||
|
||
RadioGroup은 단일 선택을 제공할 때에만 사용합니다. | ||
다중 선택을 제공해야한다면 Checkbox 혹은 Switch를 사용합니다. | ||
|
||
<br /> | ||
<br /> | ||
|
||
## 사용법 | ||
|
||
RadioGroup 컴포넌트는 `useRadioGroup` 훅을 불러와 사용합니다. | ||
|
||
1. 안전한 typing을 위해 RadioGroup의 value를 나열한 타입을 정의합니다. | ||
|
||
```tsx | ||
type RadioGroupValues = '한국어' | '영어' | '일본어'; | ||
``` | ||
|
||
2. `useRadioGroup` 훅을 호출합니다. | ||
|
||
```tsx | ||
import { useRadioGroup } from '@yourssu/design-system-react'; | ||
|
||
const RadioGroup = useRadioGroup<RadioGroupValues>(); | ||
``` | ||
|
||
3. 반환된 RadioGroup 컴포넌트를 통해 원하는 콘텐츠를 구성합니다. | ||
|
||
```tsx | ||
<RadioGroup size="medium"> | ||
<RadioGroup.Item value="한국어">한국어</RadioGroup.Item> | ||
<RadioGroup.Item value="영어">영어</RadioGroup.Item> | ||
<RadioGroup.Item value="일본어">일본어</RadioGroup.Item> | ||
</RadioGroup> | ||
``` | ||
|
||
<Canvas of={RadioGroupStories.Usage} /> | ||
|
||
4. 필요하다면 `useRadioGroup` 훅에 기본으로 선택될 value를 전달합니다. | ||
|
||
```tsx | ||
const RadioGroup = useRadioGroup<RadioGroupValues>('한국어'); | ||
``` | ||
|
||
<Canvas of={RadioGroupStories.UsageDefault} /> | ||
|
||
<br /> | ||
<br /> | ||
|
||
### RadioGroup: size (필수) | ||
|
||
RadioGroup.Item 컴포넌트의 크기를 정합니다. | ||
가능한 값은 `small`, `medium`, `large` 입니다. | ||
|
||
```tsx | ||
<RadioGroup size="small">...</RadioGroup> | ||
<RadioGroup size="medium">...</RadioGroup> | ||
<RadioGroup size="large">...</RadioGroup> | ||
``` | ||
|
||
<Canvas of={RadioGroupStories.Size} /> | ||
|
||
<br /> | ||
<br /> | ||
|
||
### RadioGroup: orientation (선택) | ||
|
||
RadioGroup.Item 컴포넌트들의 정렬 방향을 정합니다. | ||
가능한 값은 `vertical`, `horizontal` 이며, 기본값은 `vertical` 입니다. | ||
|
||
```tsx | ||
<RadioGroup size="medium">...</RadioGroup> | ||
<RadioGroup size="medium" orientation="vertical">...</RadioGroup> | ||
<RadioGroup size="medium" orientation="horizontal">...</RadioGroup> | ||
``` | ||
|
||
<Canvas of={RadioGroupStories.Orientation} /> | ||
|
||
<br /> | ||
<br /> | ||
|
||
## 예시 | ||
|
||
### RadioGroup.Item: disabled | ||
|
||
RadioGroup.Item 컴포넌트를 비활성화 상태로 만듭니다. | ||
|
||
```tsx | ||
<RadioGroup size="medium"> | ||
<RadioGroup.Item value="한국어">한국어</RadioGroup.Item> | ||
<RadioGroup.Item value="영어" disabled> | ||
영어 | ||
</RadioGroup.Item> | ||
<RadioGroup.Item value="일본어">일본어</RadioGroup.Item> | ||
</RadioGroup> | ||
``` | ||
|
||
<Canvas of={RadioGroupStories.Disabled} /> | ||
|
||
<br /> | ||
<br /> | ||
|
||
### 변경 감지 이벤트 할당 | ||
|
||
RadioGroup 컴포넌트에 `onValueChange` 이벤트를 할당하여 변경 감지 이벤트를 처리할 수 있습니다. | ||
|
||
```tsx | ||
import { RadioGroupValueChangeEvent } from '@yourssu/design-system-react'; | ||
``` | ||
|
||
```tsx | ||
const RadioGroup = useRadioGroup<RadioGroupValues>('한국어'); | ||
|
||
const onValueChange = (e: RadioGroupValueChangeEvent<RadioGroupValues>) => { | ||
const { value, event } = e; | ||
alert(`선택된 값: ${value}`); | ||
console.log(event); | ||
}; | ||
|
||
return ( | ||
<RadioGroup size="medium" onValueChange={onValueChange}> | ||
<RadioGroup.Item value="한국어">한국어입니다</RadioGroup.Item> | ||
<RadioGroup.Item value="영어">영어입니다</RadioGroup.Item> | ||
<RadioGroup.Item value="일본어">일본어입니다</RadioGroup.Item> | ||
</RadioGroup> | ||
); | ||
``` | ||
|
||
<Canvas of={RadioGroupStories.Event} /> |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 설문조사처럼 하나의 페이지 안에 n개의 radiogroup이 있어야 하는 상황을 위해 const LangRadioGroup = useRadioGroup<'한국어' | '영어' | '일본어'>();
const GenderRadioGroup = useRadioGroup<'남성' | '여성'>();
<LangRadioGroup size="medium">
<LangRadioGroup.Item value="한국어">한국어</LangRadioGroup.Item>
<LangRadioGroup.Item value="영어">영어</LangRadioGroup.Item>
<LangRadioGroup.Item value="일본어">일본어</LangRadioGroup.Item>
</LangRadioGroup>
<GenderRadioGroup size="medium">
<GenderRadioGroup.Item value="남성">남성</GenderRadioGroup.Item>
<GenderRadioGroup.Item value="여성">여성</GenderRadioGroup.Item>
</GenderRadioGroup> |
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,190 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { | ||
RadioGroupItemProps, | ||
RadioGroupProps, | ||
RadioGroupValueChangeEvent, | ||
} from '@/components/RadioGroup/RadioGroup.type'; | ||
import { useRadioGroup } from '@/components/RadioGroup/hooks/useRadioGroup'; | ||
|
||
const meta: Meta<RadioGroupProps<string> & RadioGroupItemProps<string>> = { | ||
title: 'Components/RadioGroup', | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
args: { | ||
size: 'medium', | ||
orientation: 'vertical', | ||
}, | ||
argTypes: { | ||
size: { | ||
description: 'RadioGroup.Item의 크기를 정합니다.', | ||
control: { | ||
type: 'radio', | ||
}, | ||
options: ['small', 'medium', 'large'], | ||
}, | ||
onValueChange: { | ||
description: '선택된 RadioGroup.Item이 바뀔 때 호출되는 함수입니다.', | ||
}, | ||
orientation: { | ||
description: 'RadioGroup.Item이 나열되는 방향을 정합니다.', | ||
control: { | ||
type: 'radio', | ||
}, | ||
table: { | ||
defaultValue: { summary: 'vertical' }, | ||
}, | ||
options: ['vertical', 'horizontal'], | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
const ControlComponent = (args: object) => { | ||
const RadioGroup = useRadioGroup<'item-1' | 'item-2' | 'item-3'>('item-1'); | ||
|
||
return ( | ||
<RadioGroup orientation="vertical" size="large" {...args}> | ||
<RadioGroup.Item value="item-1">Item1</RadioGroup.Item> | ||
<RadioGroup.Item value="item-2">Item2</RadioGroup.Item> | ||
<RadioGroup.Item value="item-3">Item3</RadioGroup.Item> | ||
</RadioGroup> | ||
); | ||
}; | ||
|
||
const UsageComponent = () => { | ||
const RadioGroup = useRadioGroup<'한국어' | '영어' | '일본어'>(); | ||
|
||
return ( | ||
<RadioGroup size="medium"> | ||
<RadioGroup.Item value="한국어">한국어</RadioGroup.Item> | ||
<RadioGroup.Item value="영어">영어</RadioGroup.Item> | ||
<RadioGroup.Item value="일본어">일본어</RadioGroup.Item> | ||
</RadioGroup> | ||
); | ||
}; | ||
|
||
const UsageDefaultComponent = () => { | ||
const RadioGroup = useRadioGroup<'한국어' | '영어' | '일본어'>('한국어'); | ||
|
||
return ( | ||
<RadioGroup size="medium"> | ||
<RadioGroup.Item value="한국어">한국어</RadioGroup.Item> | ||
<RadioGroup.Item value="영어">영어</RadioGroup.Item> | ||
<RadioGroup.Item value="일본어">일본어</RadioGroup.Item> | ||
</RadioGroup> | ||
); | ||
}; | ||
|
||
const SizeComponent = () => { | ||
const RadioGroup = useRadioGroup<'한국어' | '영어' | '일본어'>(); | ||
|
||
return ( | ||
<div style={{ display: 'flex', gap: 48 }}> | ||
<RadioGroup size="small"> | ||
<div>- small</div> | ||
<RadioGroup.Item value="한국어">한국어</RadioGroup.Item> | ||
<RadioGroup.Item value="영어">영어</RadioGroup.Item> | ||
<RadioGroup.Item value="일본어">일본어</RadioGroup.Item> | ||
</RadioGroup> | ||
<RadioGroup size="medium"> | ||
<div>- medium</div> | ||
<RadioGroup.Item value="한국어">한국어</RadioGroup.Item> | ||
<RadioGroup.Item value="영어">영어</RadioGroup.Item> | ||
<RadioGroup.Item value="일본어">일본어</RadioGroup.Item> | ||
</RadioGroup> | ||
<RadioGroup size="large"> | ||
<div>- large</div> | ||
<RadioGroup.Item value="한국어">한국어</RadioGroup.Item> | ||
<RadioGroup.Item value="영어">영어</RadioGroup.Item> | ||
<RadioGroup.Item value="일본어">일본어</RadioGroup.Item> | ||
</RadioGroup> | ||
</div> | ||
); | ||
}; | ||
|
||
const OrientationComponent = () => { | ||
const RadioGroup = useRadioGroup<'한국어' | '영어' | '일본어'>(); | ||
|
||
return ( | ||
<div style={{ display: 'flex', gap: 80 }}> | ||
<RadioGroup size="small"> | ||
<RadioGroup.Item value="한국어">한국어</RadioGroup.Item> | ||
<RadioGroup.Item value="영어">영어</RadioGroup.Item> | ||
<RadioGroup.Item value="일본어">일본어</RadioGroup.Item> | ||
</RadioGroup> | ||
<RadioGroup size="small" orientation="vertical"> | ||
<RadioGroup.Item value="한국어">한국어</RadioGroup.Item> | ||
<RadioGroup.Item value="영어">영어</RadioGroup.Item> | ||
<RadioGroup.Item value="일본어">일본어</RadioGroup.Item> | ||
</RadioGroup> | ||
<RadioGroup size="small" orientation="horizontal"> | ||
<RadioGroup.Item value="한국어">한국어</RadioGroup.Item> | ||
<RadioGroup.Item value="영어">영어</RadioGroup.Item> | ||
<RadioGroup.Item value="일본어">일본어</RadioGroup.Item> | ||
</RadioGroup> | ||
</div> | ||
); | ||
}; | ||
|
||
const EventComponent = () => { | ||
const RadioGroup = useRadioGroup<'한국어' | '영어' | '일본어'>('한국어'); | ||
|
||
const onValueChange = (e: RadioGroupValueChangeEvent<'한국어' | '영어' | '일본어'>) => { | ||
const { value, event } = e; | ||
alert(`선택된 값: ${value}`); | ||
console.log(event); | ||
}; | ||
|
||
return ( | ||
<RadioGroup size="medium" onValueChange={onValueChange}> | ||
<RadioGroup.Item value="한국어">한국어입니다</RadioGroup.Item> | ||
<RadioGroup.Item value="영어">영어입니다</RadioGroup.Item> | ||
<RadioGroup.Item value="일본어">일본어입니다</RadioGroup.Item> | ||
</RadioGroup> | ||
); | ||
}; | ||
|
||
const DisabledComponent = () => { | ||
const RadioGroup = useRadioGroup<'한국어' | '영어' | '일본어'>(); | ||
|
||
return ( | ||
<RadioGroup size="medium"> | ||
<RadioGroup.Item value="한국어">한국어</RadioGroup.Item> | ||
<RadioGroup.Item value="영어" disabled> | ||
영어 | ||
</RadioGroup.Item> | ||
<RadioGroup.Item value="일본어">일본어</RadioGroup.Item> | ||
</RadioGroup> | ||
); | ||
}; | ||
|
||
export const Control: StoryObj = { | ||
render: ControlComponent, | ||
}; | ||
|
||
export const Usage: StoryObj = { | ||
render: UsageComponent, | ||
}; | ||
|
||
export const UsageDefault: StoryObj = { | ||
render: UsageDefaultComponent, | ||
}; | ||
|
||
export const Size: StoryObj = { | ||
render: SizeComponent, | ||
}; | ||
|
||
export const Orientation: StoryObj = { | ||
render: OrientationComponent, | ||
}; | ||
|
||
export const Disabled: StoryObj = { | ||
render: DisabledComponent, | ||
}; | ||
|
||
export const Event: StoryObj = { | ||
render: EventComponent, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tabs
에서는 아예 여길undefined
로 넣어버려서 사용할 때 코드가 더러웠는데 ^^.............. 여기처럼 수정하겠읍니다