-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
중복선택 버튼 컴포넌트 (체크박스) 작성
- Loading branch information
Showing
9 changed files
with
147 additions
and
19 deletions.
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} | ||
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.