-
Notifications
You must be signed in to change notification settings - Fork 5
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
중복선택 버튼 컴포넌트 (체크박스) 작성 #27
Conversation
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.
수고하셨습니다!!
<RadioButtonGroup isChecked={isRadioChecked} onChange={onChange} radioName={"프로젝트 주기"}> | ||
<Radio value={"0"}>주 1회</Radio> | ||
<Radio value={"1"}>월 1회</Radio> | ||
<Radio value={"2"}>분기별</Radio> | ||
<Radio value={"3"}>프로젝트 끝난 후</Radio> | ||
</RadioButtonGroup> | ||
|
||
<br /> | ||
<h3>체크박스</h3> | ||
<CheckBoxGroup isChecked={isCheckBoxChecked} onChange={toggle}> | ||
<CheckBox value={"00"}>주 1회</CheckBox> | ||
<CheckBox value={"10"}>월 1회</CheckBox> | ||
<CheckBox value={"20"}>분기별</CheckBox> | ||
<CheckBox value={"30"}>프로젝트 끝난 후</CheckBox> | ||
</CheckBoxGroup> |
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.
사용방법이 직관적이어서 좋네요!!
return ( | ||
<ListItemCard variant={checkboxContext?.isChecked(value) ? "theme" : "default"}> | ||
<label | ||
htmlFor={value} |
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.
htmlFor
이라는 속성도 있군요!! 하나 또 알아갑니다.
src/hooks/useRadioButton.ts
Outdated
import { RadioContextState } from "@/store/context/RadioContext"; | ||
|
||
type UseRadioButtonReturn = [RadioContextState["isChecked"], RadioContextState["onChange"], string | undefined]; | ||
|
||
export const useRadioButton = (): UseRadioButtonReturn => { | ||
const [selectedValue, setSelectedValue] = useState<string>(); | ||
const isChecked = (value: string) => selectedValue === value; | ||
const onChange = (value: string) => setSelectedValue(value); | ||
|
||
return [isChecked, onChange, selectedValue]; |
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.
import { RadioContextState } from "@/store/context/RadioContext"; | |
type UseRadioButtonReturn = [RadioContextState["isChecked"], RadioContextState["onChange"], string | undefined]; | |
export const useRadioButton = (): UseRadioButtonReturn => { | |
const [selectedValue, setSelectedValue] = useState<string>(); | |
const isChecked = (value: string) => selectedValue === value; | |
const onChange = (value: string) => setSelectedValue(value); | |
return [isChecked, onChange, selectedValue]; | |
export const useRadioButton = () => { | |
const [selectedValue, setSelectedValue] = useState<string>(); | |
const isChecked = (value: string) => selectedValue === value; | |
const onChange = (value: string) => setSelectedValue(value); | |
return [isChecked, onChange, selectedValue] as const; | |
} |
저는 custom hook에서 배열로 반환 시 return type을 설정해줄 때 as const로 명시해주는게 깔끔하더라구요~!!
별개로 object return 이 아닌 array return을 채택하신 이유도 궁금해요~!!
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.
아!! 맞네요 as const로 수정하겠습니다! 감사합니다 🙇♀️
+) Staging.tsx 파일에 예시 작성하면서 라디오버튼과 체크박스를 같이 사용할 때 useRadioButton
, useCheckBox
훅이 같은 이름(isChecked)을 반환하다보니 충돌이 나더라구요! 저희 프로젝트에서는 아직까지 그럴 일은 없겠지만, 그래도 이름을 사용하는 쪽에서 바꿔서 사용하면 좋겠다는 생각으로 이렇게 작성했습니다!
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.
자세한 설명 감사합니다👍👍
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.
수고하셨습니다👏👏
🏄🏼♂️ Summary (요약)
🫨 Describe your Change (변경사항)
useRadioButton
,useCheckBox
훅 작성selectedValue
,setSelectedValues
(튜플의 세 번째 값) 값이 선택된 value 정보를 담고 있습니다.radioName
props를 추가로 전달해야 한다는 점 외에는 라디오버튼, 체크박스 사용 방법이 같습니다.🧐 Issue number and link (참고)
📚 Reference (참조)
Staging.tsx
에 사용 예시를 참고해주세요!