reactjs - 用建议反应搜索组件,如何选择建议
问题描述
我有一个带有建议的组件搜索组件。我通过 javascript 创建建议。我可以打印所有建议,但是如何选择要放入占位符的建议元素?感谢您的答复。
搜索组件
import React, { useEffect, useState } from "react";
import api from "../../api/index";
import "./index.css";
export default function Search(query) {
const [searchSchool, setSearchShool] = useState("");
const [showSuggestions, setShowSuggestions] = useState(false);
const [allSchools, setAllSchools] = useState([]);
const inputRef = React.useRef(null);
useEffect(() => {
(async () => {
const data = await api.getSchools();
setAllSchools(data);
})();
}, []);
const resultsAllSchools = !searchSchool
? allSchools
: allSchools.filter(school => school.toLowerCase().includes(searchSchool));
const handleOnclick = e => {
e.preventDefault();
const lowerCase = e.target.value.toLowerCase();
setSearchShool(lowerCase);
setShowSuggestions(true);
if (e.target.value === "" || null) {
setShowSuggestions(false);
}
};
return (
<>
<form>
<input
className="select"
type="text"
placeholder="Cerca el teu centre educatiu"
value={searchSchool}
ref={inputRef}
onChange={handleOnclick}
></input>
<button>
</button>
<ul>
{showSuggestions &&
resultsAllSchools.map((item, i) => <li key={i}>{item}</li>)}
</ul>
</form>
</>
);
}
解决方案
推荐阅读
- python - 文件路径未正确读取
- javascript - 切换用户可以删除文本框中的文本与不删除文本的状态
- open-source - ibm inforsphere cdc 开源解决方案
- python - 如何将 blob 复制到存储桶中的特定文件夹?
- docker - 本地主机上的 docker 容器存储
- laravel - Laravel Forge - 过去部署日志的完整列表
- ctypes - IronPyton ctypes union 失败并显示“SystemError:对象引用未设置为对象的实例”。
- visual-studio-code - Dart 使用 12 GB 内存运行
- javascript - if 语句中的 Promise (Javascript)
- r - 使用采样功能过采样