-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
83ec37b
chore: #11 라디오버튼 사용 예시
leeminhee119 6124c7a
style: #11 라디오 버튼 cursor pointer
leeminhee119 4691b4c
feat: #11 다중 선택 버튼
leeminhee119 71ef0c0
refactor: #11 라디오버튼 훅 작성, 체크박스와 사용 방식 통일
leeminhee119 32d081e
refactor: #11 공통 타입 정의, 재사용
leeminhee119 e7bb5ab
chore: #11 불필요한 useState 제거
leeminhee119 4f3e5ba
chore: #11 rest -> props
leeminhee119 b11f3a8
chore: #11 context 사용하는 곳으로 이동, 파일 제거
leeminhee119 775bfc8
chore: #11 불필요한 타입 선언 제거
leeminhee119 c356813
fix: #11 import 경로 수정
leeminhee119 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
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,46 @@ | ||
import { css } from "@emotion/react"; | ||
import { useContext } from "react"; | ||
|
||
import { CheckBoxContext } from "./CheckBoxGroup"; | ||
|
||
import ListItemCard from "@/component/common/Card/ListItemCard"; | ||
|
||
type CheckBoxProps = { | ||
value: string; | ||
children: React.ReactNode; | ||
}; | ||
|
||
const CheckBox = ({ value, children }: CheckBoxProps) => { | ||
const checkboxContext = useContext(CheckBoxContext); | ||
return ( | ||
<ListItemCard variant={checkboxContext?.isChecked(value) ? "theme" : "default"}> | ||
<label | ||
htmlFor={value} | ||
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.
|
||
css={css` | ||
font-weight: 600; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100%; | ||
width: 100%; | ||
cursor: pointer; | ||
`} | ||
> | ||
{children} | ||
</label> | ||
<input | ||
type="checkbox" | ||
id={value} | ||
value={value} | ||
onChange={(e) => { | ||
checkboxContext?.onChange && checkboxContext.onChange(e.target.value); | ||
}} | ||
css={css` | ||
display: none; | ||
`} | ||
/> | ||
</ListItemCard> | ||
); | ||
}; | ||
|
||
export default CheckBox; |
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,29 @@ | ||
import { css } from "@emotion/react"; | ||
import { createContext } from "react"; | ||
|
||
export type CheckBoxContextState = { | ||
isChecked: (value: string) => boolean; | ||
onChange: (value: string) => void; | ||
}; | ||
|
||
export const CheckBoxContext = createContext<CheckBoxContextState | undefined>(undefined); | ||
|
||
type CheckBoxGroupProps = { | ||
children: React.ReactNode; | ||
} & CheckBoxContextState; | ||
|
||
const CheckBoxGroup = ({ children, ...props }: CheckBoxGroupProps) => { | ||
return ( | ||
<div | ||
css={css` | ||
display: flex; | ||
flex-direction: column; | ||
gap: 1rem; | ||
`} | ||
> | ||
<CheckBoxContext.Provider value={props}>{children}</CheckBoxContext.Provider> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CheckBoxGroup; |
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
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
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,10 @@ | ||
import { useState } from "react"; | ||
|
||
export const useCheckBox = () => { | ||
const [checkedStates, setCheckedStates] = useState<Record<string, boolean>>({}); | ||
const isChecked = (value: string) => checkedStates[value]; | ||
const toggle = (value: string) => setCheckedStates((prev) => ({ ...prev, [value]: !prev[value] })); | ||
const selectedValues = Object.keys(checkedStates).filter((key) => checkedStates[key]); | ||
|
||
return [isChecked, toggle, selectedValues] as const; | ||
}; |
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,9 @@ | ||
import { useState } from "react"; | ||
|
||
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; | ||
}; |
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
This file was deleted.
Oops, something went wrong.
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.
사용방법이 직관적이어서 좋네요!!