#5-0 ๋ค์ด๊ฐ๋ ๋ง
ํ์๊ฐ์
ํ์ด์ง์ ๋ค์ด๊ฐ ์ฝ๊ด๋์ ์ฒดํฌ๋ฐ์ค ๊ธฐ๋ฅ์ ๊ตฌํํ๋ค. ์ฒ์์ ์ ์ง ์กฐ๊ธ ์ด๋ ค์ธ๊บผ ๊ฐ์์ ์ผ๋จ ํผ์ ์๋ํด๋ณด๊ณ ์๋๋ฉด ๊ตฌ๊ธ๋ง ํด๋ณด์ ์ถ์๋ค. ๊ทผ๋ฐ ์ฌ๊ฑธ ์ฑ๊ณตํ๋ค. ์ฝฉ๋๋ฌผ ์ฒ๋ผ ๋๋ ์กฐ๊ธ์ฉ ์๋ผ๊ณ ์๋๋ณด๋ค. ๋ฟ๋ฏ!
#5-1 ์ฝ๊ด๋์ ์ฒดํฌ๋ฐ์ค ๊ธฐ๋ฅ ์ค๊ณ

1) ์ ์ฒด๋์ ์ฒดํฌ ์ ์ธ๋ถํญ๋ชฉ 3๊ฐ ๋ชจ๋ ์๋์ฒดํฌ
2) ์ ์ฒด๋์ ์ฒดํฌํด์ ์ ์ธ๋ถํญ๋ชฉ 3๊ฐ ๋ชจ๋ ์๋ ์ฒดํฌํด์
3) ์ธ๋ถํญ๋ชฉ 3๊ฐ ๋ชจ๋ ์ฒดํฌ ์ ์ ์ฒด๋์ ์๋์ฒดํฌ
4) ์ ์ฒด๋์ + ์ธ๋ถํญ๋ชฉ 3๊ฐ ๋ชจ๋ ์ฒดํฌ๋ ์ํ์์ ์ธ๋ถํญ๋ชฉ 1๊ฐ
์ฒดํฌํด์ ์ ์ ์ฒด๋์ ์๋ ์ฒดํฌํด์
#5-2 ์ฝ๊ด๋์ ์ฒดํฌ๋ฐ์ค ๊ธฐ๋ฅ ๊ตฌํ
1) ์ฒดํฌ๋ฐ์ค์ checked ์์ฑ๊ฐ์ธ true / false ๋ฅผ useState๋ฅผ ์ด์ฉํ์ฌ ์ ์ฅ ๋ฐ ๋ณ๊ฒฝํจ.
2) ๊ธฐ๋ฅ 1๋ฒ๊ณผ 2๋ฒ์ ํด๋น ์ฒดํฌ๋ฐ์ค์ onChange ์ด๋ฒคํธ๋ฅผ ํตํด ๊ธฐ๋ฅ ๊ตฌํํจ. (์ด๋ onClick์ผ๋ก ํด๋ ์๋๋๊ธด ํจ)
3) ๊ธฐ๋ฅ 3๋ฒ๊ณผ 4๋ฒ์ useEffect๋ฅผ ์ด์ฉํด์ ๊ธฐ๋ฅ ๊ตฌํํจ. ์์ onchange ์ด๋ฒคํธ๋ก๋ ์ธ๋ถํญ๋ชฉ 3๊ฐ๊ฐ ๋ชจ๋ ์ฒดํฌ๋ ์ฆ์ ์ ์ฒด๋์๊ฐ ์๋์ฒดํฌ๋์ง ์์ผ๋, useEffect๋ฅผ ์ฌ์ฉํ๋ฉด ๋ชจ๋ ์ฒดํฌ๋ ๋(์ ๋ฐ์ดํธ)๋ฅผ ์บ์นํด ์ฆ์ ์ ์ฒด๋์๊ฐ ์๋์ฒดํฌ๋๊ฒ ๋๋ค.
import React, {useState, useEffect} from 'react';
import styles from './Join.module.scss';
function JoinPresenter(){
const [allCheck, setAllCheck] = useState(false);
const [ageCheck, setAgeCheck] = useState(false);
const [useCheck, setUseCheck] = useState(false);
const [marketingCheck, setMarketingCheck] = useState(false);
const allBtnEvent =()=>{
if(allCheck === false) {
setAllCheck(true);
setAgeCheck(true);
setUseCheck(true);
setMarketingCheck(true);
}else {
setAllCheck(false);
setAgeCheck(false);
setUseCheck(false);
setMarketingCheck(false);
}
};
const ageBtnEvent =()=>{
if(ageCheck === false) {
setAgeCheck(true)
}else {
setAgeCheck(false)
}
};
const useBtnEvent =()=>{
if(useCheck === false) {
setUseCheck(true)
}else {
setUseCheck(false)
}
};
const marketingBtnEvent =()=>{
if(marketingCheck === false) {
setMarketingCheck(true)
}else {
setMarketingCheck(false)
}
};
useEffect(()=>{
if(ageCheck===true && useCheck===true && marketingCheck===true){
setAllCheck(true)
} else {
setAllCheck(false)
}
}, [ageCheck,useCheck, marketingCheck])
return (
<form method="post" action="" className={styles.form}>
<div className={styles.form_agreement}>
<label className={styles.form_agreement_title}>
์ฝ๊ด๋์
</label>
<div className={styles.form_agreement_box}>
<div className={styles.form_agreement_all}>
<input type="checkbox" id="all-check" checked={allCheck} onChange={allBtnEvent}/>
<label for="all-check">์ ์ฒด๋์</label>
</div>
<div className={styles.form_agreement_item}>
<input type="checkbox" id="check1" checked={ageCheck} onChange={ageBtnEvent}/>
<label for="check1">๋ง 14์ธ ์ด์์
๋๋ค <span className={styles.blue}>(ํ์)</span></label>
</div>
<div className={styles.form_agreement_item}>
<input type="checkbox" id="check2" checked={useCheck} onChange={useBtnEvent}/>
<label for="check2">์ด์ฉ์ฝ๊ด <span className={styles.blue}>(ํ์)</span></label>
</div>
<div className={styles.form_agreement_item}>
<input type="checkbox" id="check3" checked={marketingCheck} onChange={marketingBtnEvent}/>
<label for="check3">๋ง์ผํ
๋์ <span className={styles.gray}>(์ ํ)</span></label>
</div>
</div>
</div>
<button type="submit" className={`${styles.btn_primary} ${styles.btn_55} ${styles.btn_submit}`}>
ํ์๊ฐ์
ํ๊ธฐ
</button>
</form>
)
}
export default JoinPresenter;
'[PROJECT] ๋ด์ผ์ ์ง' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
#7. ํ์๊ฐ์ ์ ํจ์ฑ ๊ฒ์ฌ (react-hook-form) (0) | 2021.10.17 |
---|---|
#6. ๋ก๊ทธ์ธ ๊ธฐ๋ฅ ๊ตฌํ (1์ฐจ) (1) | 2021.10.04 |
#4. ์บ๋ก์ (Carousel) ๊ตฌํํ๊ธฐ (0) | 2021.09.25 |
#3. ๋ชจ๋ฌ์ฐฝ ๊ตฌํ( feat. ์์ญ ๋ฐ ํด๋ฆญ ์ ๋ซ๊ธฐ) (0) | 2021.09.15 |
#2. ๋ก๊ทธ์ธ ๊ธฐ๋ฅ ๊ฐ๋ ์ก๊ธฐ (0) | 2021.09.13 |