νλ‘κ·Έλλ° μΈκ³λ₯Ό νꡬν©μλ€.
λμ select box ꡬννκΈ°
μ΄λ² νλ‘μ νΈμμ μ§μ, λμ΄λ μ νμ μμ°¨μ μΌλ‘ μ λ ₯ν μ μλ Select Boxλ₯Ό ꡬννμ΅λλ€.
μ ννλ μλμ λ°λΌ νμ μ£Όμ μ ν λͺ©λ‘μ΄ λ¬λΌμ§λ©°, λμ΄λ μ νμ λ³κ°λ‘ νμ λνλμΌ νμ΅λλ€.
μΌλ°μ μΈ μ€ν¬λ‘€ ννμ Select Boxκ° μλλΌ λͺ©λ‘μ΄ μ μλμ΄ μκ³ ν΄λ¦ν μ μλ κΈ°λ₯μ μνμ΅λλ€.
ꡬν νλ©΄
Console
μμ± μ½λ
ν΄λ¦ν΄μΌ λνλλ λͺ©λ‘μ΄ μλλΌ λͺ©λ‘μ΄ μ μλμ΄ μλ ννμ Select Boxλ₯Ό ꡬννκΈ° μν΄ λͺ©λ‘μ λ°°μ΄ ννλ‘ μμ±νμμ΅λλ€.
useStateλ₯Ό μ¬μ©νμ¬ μνλ₯Ό μ λ°μ΄νΈνλ ν¨μλ₯Ό μ μΈνμμ΅λλ€.
μ΄κΈ° μνλ‘λ 첫 λ²μ§Έ μλκ° μ νλκ³ , λλ¨Έμ§λ λΉ λ¬Έμμ΄λ‘ μ€μ λ©λλ€.
const [selectedProvince, setSelectedProvince] = useState(provinces[0].name);
μλ λ³κ²½ νΈλ€λ¬μμ μλ λΆλΆμ μλ μ ν μ μꡰꡬμ μλ©΄λμ μ νμ μ΄κΈ°ννλ λΆλΆμ λλ€.
setSelectedDistrict('');
setSelectedSubdistrict('');
Mselect.jsx
import style from './Mselect.module.css';
import React, { useState } from 'react';
function Mselect() {
const provinces = [
{ id: 1, name: 'μμΈνΉλ³μ' },
{ id: 2, name: 'κ²½κΈ°λ' },
{ id: 3, name: 'μΈμ²κ΄μμ' },
// λ€λ₯Έ μλ λ°μ΄ν° μΆκ°
];
const districts = {
μμΈνΉλ³μ: ['κ°λ¨κ΅¬', 'κ°μꡬ', 'μ‘νꡬ', /* ... */],
κ²½κΈ°λ: ['μμμ', 'μ©μΈμ', 'μ±λ¨μ', /* ... */],
μΈμ²κ΄μμ: ['μ€κ΅¬', 'λ¨κ΅¬', 'λꡬ', /* ... */],
// λ€λ₯Έ μλμ μꡰꡬ λ°μ΄ν° μΆκ°
};
const subdistrict = {
κ°λ¨κ΅¬: ['λ₯λ', 'μ²λ΄λ', 'λμΉ1λ', /* ... */],
μμμ: ['κ³Όμ²λ', 'μ μλ', 'μμλ', /* ... */],
μ€κ΅¬: ['κ°νμ', 'μ‘ν΄λ©΄', 'λ¬Ένλ', /* ... */],
}
const difficulties = ['μ¬μ', 'μ€κ°', 'μ΄λ €μ'];
// μνλ₯Ό μ μν©λλ€.
const [selectedProvince, setSelectedProvince] = useState(provinces[0].name);
const [selectedDistrict, setSelectedDistrict] = useState('');
const [selectedSubdistrict, setSelectedSubdistrict] = useState('');
const [difficulty, setDifficulty] = useState('');
// μλ λ³κ²½ νΈλ€λ¬
const handleProvinceChange = (provinceName) => {
setSelectedProvince(provinceName);
setSelectedDistrict('');
setSelectedSubdistrict('');
// νμ¬ λμ΄λ μνκ° μλ, μ§μ μ μ νλ λμ΄λλ₯Ό λ‘κΉ
console.log(`μλ: '${provinceName}', μꡰꡬ: '', μλ©΄λ: '', λμ΄λ: '${difficulty}'`);
};
// μꡰꡬ λ³κ²½ νΈλ€λ¬
const handleDistrictChange = (districtName) => {
setSelectedDistrict(districtName);
setSelectedSubdistrict('');
// νμ¬ λμ΄λ μνκ° μλ, μ§μ μ μ νλ λμ΄λλ₯Ό λ‘κΉ
console.log(`μλ: '${selectedProvince}', μꡰꡬ: '${districtName}', μλ©΄λ: '', λμ΄λ: '${difficulty}'`);
};
// μλ©΄λ λ³κ²½ νΈλ€λ¬
const handleSubdistrictChange = (subdistrictName) => {
setSelectedSubdistrict(subdistrictName);
// νμ¬ λμ΄λ μνκ° μλ, μ§μ μ μ νλ λμ΄λλ₯Ό λ‘κΉ
console.log(`μλ: '${selectedProvince}', μꡰꡬ: '${selectedDistrict}', μλ©΄λ: '${subdistrictName}', λμ΄λ: '${difficulty}'`);
};
// λμ΄λ λ³κ²½ νΈλ€λ¬
const handleDifficultyChange = (level) => {
setDifficulty(level);
// μλ‘κ² μ νλ λμ΄λλ₯Ό λ‘κΉ
console.log(`μλ: '${selectedProvince}', μꡰꡬ: '${selectedDistrict}', μλ©΄λ: '${selectedSubdistrict}', λμ΄λ: '${level}'`);
};
return (
<>
<div className={style.Mselectbox}>
<div className={style.Mselect_main}>
<h1 className={style.Mselect_title}>
<img src={`${process.env.PUBLIC_URL}/img/mountainpath.svg`} alt="λ±μ° μ 보 μμ΄μ½" className={style.icon1} />
λ±μ° μ 보</h1>
</div>
</div>
<div className={style.Mselectbox1}>
<h2>μλ μ ν</h2>
<div className={style.scrollableList}>
{provinces.map((province) => (
<div
key={province.id}
className={`${style.listItem} ${selectedProvince === province.name ? style.selected : ''}`}
onClick={() => handleProvinceChange(province.name)}
>
{province.name}
</div>
))}
</div>
{selectedProvince && (
<>
<h2>μꡰꡬ μ ν</h2>
<div className={style.scrollableList}>
{districts[selectedProvince]?.map((district, index) => (
<div
key={index}
className={`${style.listItem} ${selectedDistrict === district ? style.selected : ''}`}
onClick={() => handleDistrictChange(district)}
>
{district}
</div>
))}
{!districts[selectedProvince] && <div className={style.listItem}>μκ΅°κ΅¬κ° μμ΅λλ€.</div>}
</div>
<h2>μλ©΄λ μ ν</h2>
<div className={style.scrollableList}>
{selectedDistrict && subdistrict[selectedDistrict]?.map((sub, index) => (
<div
key={index}
className={`${style.listItem} ${selectedSubdistrict === sub ? style.selected : ''}`}
onClick={() => handleSubdistrictChange(sub)}
>
{sub}
</div>
))}
{!subdistrict[selectedDistrict] && <div className={style.listItem}>μλ©΄λμ΄ μμ΅λλ€.</div>}
</div>
<h2>λμ΄λ μ ν</h2>
<div className={style.scrollableList}>
{difficulties.map((level, index) => (
<div
key={index}
className={`${style.listItem} ${difficulty === level ? style.selected : ''}`}
onClick={() => handleDifficultyChange(level)}
>
{level}
</div>
))}
</div>
</>
)}
</div>
<div className={style.Mselect_result}>
<p>μ νλ μλ: {selectedProvince}</p>
<p>μ νλ μꡰꡬ: {selectedDistrict || 'μ νλμ§ μμ'}</p>
<p>μ νλ μλ©΄λ: {selectedSubdistrict || 'μ νλμ§ μμ'}</p>
<p>μ νλ λμ΄λ: {difficulty || 'μ νλμ§ μμ'}</p>
</div>
</>
);
}
export default Mselect;
Mselect.module.css
module.css ννλ₯Ό μ¬μ©ν λμ μ₯μ μ ν΄λμ€ μ΄λ¦μ΄ κ³ μ νκ² λ³κ²½λμ΄ μ€νμΌμ΄ λ€λ₯Έ μ»΄ν¬λνΈμ μν₯μ μ£Όμ§ μλλ€λ κ²μ λλ€.
@font-face {
font-family: 'GmarketSansLight';
src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2001@1.1/GmarketSansLight.woff') format('woff');
font-weight: normal;
font-style: normal;
}
.Mselectbox {
padding-top: 7.5rem;
justify-content: center;
align-items: center;
display: flex;
}
.Mselect_title {
font-size: 2.5rem;
display: flex;
font-family: 'GmarketSansLight';
}
.icon1 {
width: 3rem;
height: 3rem;
margin-right: 1rem;
}
.selector {
cursor: pointer;
/* μΆκ°μ μΈ μ€νμΌ */
}
.Mselectbox1 {
margin-top: 3rem;
display: flex;
justify-content: center;
width: 100vw;
}
.scrollableList {
max-height: 10rem;
max-width: 15rem;
overflow-y: auto;
/* μμ§ λ°©ν₯μΌλ‘λ§ μ€ν¬λ‘€ λ° νμ */
border: 0.1rem solid #ccc;
/* κ²½κ³μ μ€νμΌ */
margin-top: 5px;
/* μλ¨ μ¬λ°± */
margin-right: 2rem;
margin-left: 1rem;
width: 100%;
/* λλΉ μ€μ */
background: white;
/* λ°°κ²½μ */
z-index: 10;
}
.listItem {
padding: 10px;
/* ν¨λ© */
cursor: pointer;
/* λ§μ°μ€ ν¬μΈν° λ³κ²½ */
border-bottom: 1px solid #eee;
/* νλ¨ κ²½κ³μ μ€νμΌ */
}
.listItem:hover {
background-color: #f7f7f7;
/* νΈλ²μ λ°°κ²½μ λ³κ²½ */
}
.selected {
background-color: #e9ecef;
/* μμ μμ */
}
.Mselect_result {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-top: 2rem;
}
μμ κ°μ λ°©μμΌλ‘ reactλ₯Ό νμ©νμ¬ λμ Select Boxλ₯Ό ꡬνν μ μμ΅λλ€.