[英]Building an autocomplete textbox with ReactJS
My idea is to mantain the list of filtered users (suggestions) as state on the component, when the input changes, the state is updated.我的想法是将过滤后的用户列表(建议)作为组件的状态进行维护,当输入发生变化时,状态会更新。
How can I display the filtered list below the text box?如何在文本框下方显示过滤后的列表? One option is 'datalist' tag (HTML5), but the list is already filtered, and part of the functionality of this tag is filtering.
一个选项是“datalist”标签(HTML5),但列表已经被过滤,这个标签的部分功能是过滤。 I can't use any library or framework.
我不能使用任何库或框架。
English is not my native language, sorry if you find some mistake.英语不是我的母语,如果您发现错误,请见谅。
Thanks.谢谢。
Try a component from a design library, like the Material-UI autocomplete component http://www.material-ui.com/#/components/auto-complete尝试设计库中的组件,例如 Material-UI 自动完成组件http://www.material-ui.com/#/components/auto-complete
The dataSource
attribute represents the array of autocomplete options. dataSource
属性表示自动完成选项的数组。
How I did it was to pass in the dataList
array as a prop and filterByField
prop so that you can change what to filter, then add an event listener to the input ( onChange
) that passes the value to a function that filters the dataList.我的做法是将
dataList
数组作为 prop 和filterByField
prop 传递,以便您可以更改要过滤的内容,然后将事件侦听器添加到输入 ( onChange
),将值传递给过滤 dataList 的函数。
onChangeInput(e) {
const { dataList, filterByField } = this.props;
const filteredDataList = dataList.filter(items => items[filterByField].toLowerCase().startsWith(e.target.value.toLowerCase()) );
// update internal component state to trigger render of dropdown list
this.setState({filteredList: filteredDataList});
}
I also added a check for no matches found so I can show a message:我还添加了一个未找到匹配项的检查,以便我可以显示一条消息:
if (filteredDataList.length === 0) {
this.setState({noMatchFound: true});
}
Then in my render() I simply check if filteredList isn't null and show an unordered list that I use css to display below the input.然后在我的 render() 中,我只是检查 filteredList 是否不为 null 并显示一个无序列表,我使用 css 在输入下方显示。
{this.state.filteredList !== null
<ul className="autocomplete-list">
{this.filteredListMarkup()}
</ul>
}
filteredListMarkup()
then uses map to return an <li>
for each item with the necessary event handlers to update the selected item into the input and close the autocomplete-list
by this.setState({filteredList: null});
filteredListMarkup()
然后使用 map 为每个项目返回一个<li>
并使用必要的事件处理程序将所选项目更新到输入中并通过this.setState({filteredList: null});
关闭autocomplete-list
;
You might also find this one useful:您可能还会发现这个有用:
https://github.com/reactjs/react-autocomplete https://github.com/reactjs/react-autocomplete
Even if you could use dependencies, I tried a bunch of the top current ones and personally wasn't happy with any of them (added dependencies like jQuery, not lightweight to use/understand/customize, css challenges, etc).即使您可以使用依赖项,我也尝试了一些当前最流行的依赖项,但个人对其中任何一个都不满意(添加了 jQuery 等依赖项,使用/理解/自定义不轻,css 挑战等)。
In then end, I found this lightweight vanilla React typeahead tutorial (no, I didn't write the tutorial).最后,我找到了这个轻量级的 vanilla React typeahead 教程(不,我没有写教程)。 It's quick, simple, and three's no added dependency tree weight (eg: jQuery) or dependency maintenance.
它快速、简单,并且没有添加依赖树权重(例如:jQuery)或依赖维护。 This solution also easily adjusted to the newer React patterns & the libraries I was using, and I'm guessing the same would be true of the patterns/libraries you may be using.
这个解决方案也很容易适应较新的 React 模式和我正在使用的库,我猜你可能正在使用的模式/库也是如此。 Maybe this will help you or someone else like it did me.
也许这会像对我一样帮助您或其他人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.