我正在构建一个多选下拉列表,并希望在用户选择一个选项时隐藏选项,但我无法在selectedCategoriesData
状态更新后隐藏下拉列表,因为setShowCategories(false);
更新会停止selectedCategoriesData
状态更新。
如何确保先更新selectedCategoriesData
状态,然后再更新setShowCategories
。
- 请阅读我的问题评论 *
以下是文件:
Filter.jsx:
import classes from "./Filter.module.css";
const Filter = ({ categories }) => {
const [selectedCategoriesData, setSelectedCategoriesData] = useState([]);
const [showCategories, setShowCategories] = useState(false);
const handleCategoryInput = (e) => {};
const handleCategorySelect = (categoryChosen) => {
setSelectedCategoriesData([...selectedCategoriesData, categoryChosen]);
// setShowCategories(false); <---- This is causing not to update the selectedCategoriesData state, why ? I also want to hide the dropdown of options, so I need to set setShowCategories to false AFTER the selectedCategoriesData state is updated.
};
return (
<div className={classes.filter_container}>
<div className="category_filter">
<label htmlFor="ctgry">Category</label>
<input
className={classes.category_select}
onChange={handleCategoryInput}
onFocus={() => setShowCategories(true)}
onBlur={() => setShowCategories(false)}
></input>
{showCategories && (
<ul className={classes.categories_data}>
{categories.map((category, index) => (
<li tabIndex={0} key={index} onClick={() => handleCategorySelect(category)}>
<span>{category}</span>
</li>
))}
</ul>
)}
</div>
</div>
);
};
export default Filter;
Filter.module.css:
.filter_container {
margin-left: 100px;
}
.filter_container label {
font-size: 1.6rem;
font-weight: 600;
}
.category_select {
min-width: 20rem;
border: 1px solid #494949;
padding: 1rem;
font-size: 1.4rem;
display: block;
}
.categories_data {
border: 1px solid gray;
border-top: none;
font-size: 1.4rem;
max-height: 20rem;
width: 20rem;
overflow-x: hidden;
overflow-y: hidden;
}
.categories_data:hover {
overflow-y: scroll;
overflow-x: hidden;
}
.categories_data li span {
display: inline-block;
padding: 0.65rem 0.8rem;
}
.categories_data li:hover {
background-color: #e2e2e2;
cursor: pointer;
}
.categories_data::-webkit-scrollbar {
width: 1rem; /* width of the entire scrollbar */
}
.categories_data::-webkit-scrollbar-track {
background: transparent; /* color of the tracking area */
}
.categories_data::-webkit-scrollbar-thumb {
background-color: #4b4b4b; /* color of the scroll thumb */
border-radius: 20px; /* roundness of the scroll thumb */
border: 3px solid #fff;
/* creates padding around scroll thumb */
}
类别 prop 是['category-1', 'category-2', 'category-3', 'category-4', 'category-5', 'category-6']
数组
1条答案
按热度按时间niwlg2el1#
在仔细阅读了您的问题并运行了您的代码之后,我意识到该问题是由于输入语句中的
onBlur={() => setShowCategories(false)}
功能引起的。这是在更新SelectedCategoriesData
状态之前触发setShowCategories(false)
。要获得在输入组件外部单击时关闭下拉菜单的功能,可以使用
ref
和eventlistener
而不是onBlur
。下面是这个实现的代码:我希望这个答案对你有帮助。以下是sandbox链接:Sandbox Link