.Box {
background: black;
color: white;
padding: 2rem;
}
.Box2 {
background: black;
color: white;
padding: 2rem;
}
import React from 'react'
import styles from './Box.module.css'
function Box() {
return (
<>
<div className={styles.Box}>{styles.Box}</div>
<div className={styles.Box2}>{styles.Box2}</div>
</>
)
}
export default Box
import React from 'react'
function CheckBox({ children, checked, ...rest }) {
return (
<div>
<label>
<input type="checkbox" checked={checked} {...rest} />
<div>{checked ? '체크됨' : '체크 안됨'}</div>
</label>
<span>{children}</span>
</div>
)
}
export default CheckBox
<span>{children}</span>
에서 children은 App.js의 CheckBox 안에 있는 '다음 약관에 모두 동의' 내용 반환import { useState } from 'react'
import './App.scss'
// import Button from './components/Button'
import Box from './components/Box'
import CheckBox from './components/CheckBox'
function App() {
const [check, setCheck] = useState(false)
const onChange = (e) => {
setCheck(e.target.checked)
}
return (
<div className="App">
<CheckBox onChange={onChange} checked={check}>
다음 약관에 모두 동의
</CheckBox>
<p>
<b>check: </b>
{check ? 'true' : 'false'}
</p>
</div>
)
}
export default App
import React from 'react'
import { MdCheckBox, MdCheckBoxOutlineBlank } from 'react-icons/md'
function CheckBox({ children, checked, ...rest }) {
return (
<div>
<label>
<input type="checkbox" checked={checked} {...rest} />
<div>{checked ? <MdCheckBox /> : <MdCheckBoxOutlineBlank />}</div>
</label>
<span>{children}</span>
</div>
)
}
export default CheckBox
.checkbox {
display: flex;
align-items: center;
}
.checkbox label {
cursor: pointer;
}
/* 실제 input을 숨기기 위한 코드 */
.checkbox input {
width: 0;
height: 0;
position: absolute;
opacity: 0;
}
.checkbox span {
font-size: 1.125rem;
font-weight: bold;
}
.icon {
display: flex;
align-items: center;
/* 아이콘의 크기는 폰트 사이즈로 조정 가능 */
font-size: 2rem;
margin-right: 0.25rem;
color: #adb5bd;
}
.checked {
color: #339af0;
}
import React from 'react'
import { MdCheckBox, MdCheckBoxOutlineBlank } from 'react-icons/md'
import styles from './CheckBox.module.css'
import classNames from 'classnames/bind'
const cx = classNames.bind(styles)
function CheckBox({ children, checked, ...rest }) {
return (
<div className={cx('checkbox')}>
<label>
<input type="checkbox" checked={checked} {...rest} />
<div className={cx('icon')}>{checked ? <MdCheckBox className={styles.checked} /> : <MdCheckBoxOutlineBlank />}</div>
</label>
<span>{children}</span>
</div>
)
}
export default CheckBox
cx('one', 'two')
cx('my-component', {
condition: true
})
cx('my-component', ['another', 'classnames'])