πΆ 벨λ‘νΌνΈμ ν¨κ»νλ λͺ¨λ 리μ‘νΈ
νμ₯μ : .module.css
ν΄λμ€ μ΄λ¦μ λνμ¬ κ³ μ ν μ΄λ¦λ€μ΄ λ§λ€μ΄μ§κΈ° λλ¬Έμ,
μ€μλ‘ CSS ν΄λμ€ μ΄λ¦μ΄ λ€λ₯Έ κ΄κ³ μλ κ³³μμ μ¬μ©ν CSS ν΄λμ€ μ΄λ¦κ³Ό μ€λ³΅λλ μΌμ λνμ¬
κ±±μ ν νμκ° μλ€.
<components/CheckBox.js>
* classnames μ¬μ© μ ν μ
import React from 'react';
import { MdCheckBox, MdCheckBoxOutlineBlank } from 'react-icons/md';
import styles from './CheckBox.module.css';
function CheckBox({ children, checked, ...rest }) {
return (
<div className={styles.checkbox}>
<label>
<input type="checkbox" checked={checked} {...rest} />
<div className={styles.icon}>
{checked ? (
<MdCheckBox className={styles.checked} />
) : (
<MdCheckBoxOutlineBlank />
)}
</div>
</label>
<span>{children}</span>
</div>
);
}
export default CheckBox;
* classnames μ¬μ©ν μ
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={cx('checked')} />
) : (
<MdCheckBoxOutlineBlank />
)}
</div>
</label>
<span>{children}</span>
</div>
);
}
export default CheckBox;
(μ΄κ±° κ°λ₯)
cx('one', 'two') // μ¬λ¬κ° CSS ν΄λμ€ μ μ©
cx('my-component', {
condition: true
})
cx('my-component', ['another', 'classnames'])
<components/CheckBox.module.css>
.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;
}
<App.js>
import React, { useState } from 'react';
import CheckBox from './components/CheckBox';
function App() {
const [check, setCheck] = useState(false);
const onChange = e => {
setCheck(e.target.checked);
};
return (
<div>
<CheckBox onChange={onChange} checked={check}>
λ€μ μ½κ΄μ λͺ¨λ λμ
</CheckBox>
<p>
<b>check: </b>
{check ? 'true' : 'false'}
</p>
</div>
);
}
export default App;