์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
- ํ ์คํธ ์ธํธ
- ๊นํ์๋ฌ
- ์์
- ํผ๊ณต๋จธ์
- ์ปค์คํ
- M1
- ๋งฅ๋ถ
- ios
- ์ฒดํฌ๋ฐ์ค์ปค์คํ
- ICTํ๊ธฐ
- java 11 ์ค์น
- ๊ณผ๋์ ํฉ๊ณผ ๊ณผ์์ ํฉ
- ๋จธ์ ๋ฌ๋
- ์ปด๊ณต์ ๋ฐฐ
- api๊ณตํตํ
- ICT์ธํด์ญ
- ํ๋ก ํธ์๋
- CSS
- ๊นํ์ฌ์ดํธ
- ICT์ง์
- XCode
- ํผ์ ๊ณต๋ถํ๋ ๋จธ์ ๋ฌ๋
- react
- ์๋ฆฟ์์ ํ
- svg์
- ๋ฅ๋ฌ๋
- ์์ด๋์ค๋ณต
- axios์ธ์คํด์ค
- styled-components
- ์ทํ๊ท๊ฑธ์ด
- Today
- Total
Hi-๋๐ High-๋โ๏ธ
[React] Checkbox ์ปค์คํ ํ๊ธฐ with styled-components (+์ ๋๋ฉ์ด์ ) ๋ณธ๋ฌธ
[React] Checkbox ์ปค์คํ ํ๊ธฐ with styled-components (+์ ๋๋ฉ์ด์ )
ํ์ด๋ 2023. 4. 6. 13:19ํ๋ก์ ํธ๋ฅผ ์งํํ๋ค ๋ณด๋ ์ฒดํฌ๋ฐ์ค๋ฅผ ๋ด๊ฐ ์ํ๋ ๋์์ธ์ผ๋ก ์ปค์คํ ์ ํด์ผ ํ๋ค.
๊ธฐ๋ณธ์ผ๋ก ์ ๊ณต๋๋ ์ฒดํฌ๋ฐ์ค๋ ๋ด๊ฐ ์ํ๋ ์คํ์ผ๊ณผ๋ ๊ฝค๋ ๋ค๋ฅด๊ฒ ์๊ฒผ๋ค.
๊ทธ๋์ ๋ค์๊ณผ ๊ฐ์ด Checkbox ์ปดํฌ๋ํธ๋ฅผ ์๋ก ๋ง๋ค๊ธฐ๋ก ํ๋ค.
์ฝ๋
Checkbox.js
import { CheckboxStyle, CheckboxWrapper, StyledLabel } from "./style";
const Checkbox = ({ id, label, checked, handler }) => {
return (
<CheckboxWrapper>
<CheckboxStyle
type="checkbox"
id={id}
name={id}
checked={checked}
onChange={handler}
></CheckboxStyle>
<StyledLabel htmlFor={id}>{label}</StyledLabel>
</CheckboxWrapper>
);
};
export default Checkbox;
๋จผ์ , <CheckboxWrapper />
๊ฐ ์ปค์คํ
ํ Checkbox์ธ <CheckboxStyle />
๊ณผ <StyledLabel />
์ ๊ฐ์ธ๋๋ก ํ๋ค. Checkbox๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด input type์ "checkbox"
๋ก ์ง์ ํด ์ฃผ๊ณ , ์๋ณํ๊ธฐ ์ํ id๊ฐ์ ์ง์ ํด ์ค๋ค. name์ ์ ๋ฌํ ๊ฐ์ ์ด๋ฆ์ด๊ณ , checked๋ฅผ ํตํด ์ ํ๋์๋์ง ๊ทธ ์ํ๋ฅผ ๊ด๋ฆฌํ๋ค. ์ฒดํฌ ์ฌ๋ถ๋ฅผ ๊ด๋ฆฌํด ์ฃผ๊ธฐ ์ํด onChange๋ ์ฌ์ฉํ๋ค.
<StyledLabel />
์ ํด๋ฆญํด๋ ์ฒดํฌ๋ฐ์ค๊ฐ ์ ํ๋๋๋ก ํ๊ธฐ ์ํด ์ฐ๊ฒฐํ๋ ์์
์ด ํ์ํ๋ค. ์ฒดํฌ๋ฐ์ค์ ๋ถ์ฌํ id๊ฐ์ label์ htmlFor๋ก ์ง์ ํ๋ฉด ๋๋ค.
โ๏ธ html์์๋ for๋ก ํด๋ ๋์์ด ๋์ง๋ง, react์์๋ for๋ฌธ์ด ๋ฐ๋ก ์๊ธฐ ๋๋ฌธ์ htmlFor๋ก ์ง์ ํด์ค์ผ ํ๋ค๋ ์ ์ ์ ์ํด์ผ ํ๋ค.
style.js
Checkbox ํด๋ ์์์ style์ ๋ฐ๋ก ๋ถ๋ฆฌํด์ ๊ด๋ฆฌํ๋๋ก ํ๋ค.
import styled from 'styled-components';
import IcGrayCheck from '../../images/ic_gray_check.svg';
export const CheckboxWrapper = styled.div`
display: flex;
align-items: center;
gap: 8px;
`;
export const CheckboxStyle = styled.input`
appearance: none;
width: 16px;
height: 16px;
border: 1px solid #CECECE;
border-radius: 3px;
&:checked {
border-color: transparent;
background-image: url(${IcGrayCheck});
background-size: 10px 7px;
background-position: 50%;
background-repeat: no-repeat;
background-color: #CECECE;
}
`;
export const StyledLabel = styled.div`
color: #A0A0A0;
font-size: 14px;
`;
index.js
๋ง์ง๋ง์ผ๋ก index.js๋ ์ปดํฌ๋ํธ๋ฅผ ๊น๋ํ๊ฒ ์ ๋ฆฌํ๊ธฐ ์ํด ์์ฑํ๋ค. ์ด๋ฆ์ ๋ฐ๊พธ๋ ๋ฑ์ ์ํฉ์์ ํธ๋ฆฌํ๊ฒ ์๋ํ๋๋ก!
import Checkbox from "./Checkbox";
export default Checkbox;
์ฌ์ฉ ๋ฐฉ๋ฒ
๋๋ ์์ ์ฒดํฌ๋ฐ์ค ์ปดํฌ๋ํธ๋ฅผ signup ํํธ์์ ์ด์ฉ์ฝ๊ด์ ํ์ํ๋ ์ฉ๋๋ก ์ฌ์ฉํ๋ค.
Signup.js
import Checkbox from "../components/Checkbox/Checkbox";
const Signup = () => {
const [tos1Checked, setTos1Checked] = useState(false);
const [tos2Checked, setTos2Checked] = useState(false);
const [tos3Checked, setTos3Checked] = useState(false);
function handleTos1Change(event) {
setTos1Checked(event.target.checked);
}
function handleTos2Change(event) {
setTos2Checked(event.target.checked);
}
function handleTos3Change(event) {
setTos3Checked(event.target.checked);
}
return (
<div>
<Checkbox
id="tos1"
label={"๋ง 14์ธ ์ด์์
๋๋ค."}
checked={tos1Checked}
handler={handleTos1Change}
/>
<Checkbox
id="tos2"
label={"์ด์ฉ์ฝ๊ด์ ๋ชจ๋ ํ์ธํ์์ผ๋ฉฐ ์ด์ ๋์ํฉ๋๋ค."}
checked={tos2Checked}
handler={handleTos2Change}
/>
<Checkbox
id="tos3"
label={"๊ฐ์ธ์ ๋ณด ์ฒ๋ฆฌ๋ฐฉ์นจ์ ๋ชจ๋ ํ์ธํ์์ผ๋ฉฐ ์ด์ ๋์ํฉ๋๋ค."}
checked={tos3Checked}
handler={handleTos3Change}
/>
</div>
);
};
export default Signup;
์ ๋๋ฉ์ด์ ์ถ๊ฐ
style.js
import styled from 'styled-components';
import IcGrayCheck from '../../images/ic_gray_check.svg';
export const CheckboxWrapper = styled.div`
display: flex;
align-items: center;
gap: 8px;
`;
export const CheckboxStyle = styled.input`
appearance: none;
width: 16px;
height: 16px;
border: 1px solid #CECECE;
border-radius: 3px;
transition: all 0.2s;
display: flex;
align-items: center;
justify-content: center;
background-image: url(${IcGrayCheck});
background-size: 0px 0px;
background-repeat: no-repeat;
background-position: 50%;
&:checked {
transition: all 0.2s;
border-color: transparent;
background-size: 10px 7px;
background-color: #CECECE;
}
`;
export const StyledLabel = styled.div`
color: #A0A0A0;
font-size: 14px;
`;
์ฒดํฌ๋ฐ์ค์ ์ ๋๋ฉ์ด์
์ ์ถ๊ฐํ๊ธฐ ์ํด stye.js๋ฅผ ์กฐ๊ธ ์์ ํ์๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฐํ๊ณผ check ์ด๋ฏธ์ง๊ฐ ๋ชจ๋ transition
์์ฑ์ ๊ฐ๊ฒ ๋์๊ณ , ๊ทธ ๊ณผ์ ์์ ๊นจ์ง๊ฑฐ๋ ์ด์ํ ๋์์ด ์๋๋ก background๊ด๋ จ ์ผ๋ถ ์์ฑ์ ์ด๋์์ผฐ๋ค.
'React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] ์๋ฌ ํด๊ฒฐ - "sh: react-scripts: command not found" (0) | 2023.07.04 |
---|---|
[React] API ์ค์ ๊ณตํตํ - ํ๊ฒฝ๋ณ์, axios ์ธ์คํด์ค ์ฌ์ฉํ๊ธฐ (0) | 2023.05.08 |
[React] API ์ฐ๋ํ๊ธฐ with axios (0) | 2023.05.08 |
[React] Inputํ๊ทธ numberํ์ ์๋ฆฟ์ ์ ํ (1) | 2023.04.10 |
[React] SVGํ์ผ ์์ ๋ณ๊ฒฝํ๊ธฐ (0) | 2023.03.18 |