我正在尝试在搜索输入上使用onSearch事件,如下所示:
render(){
return(
<input type="search" onSearch={ () => console.warn('search') } />
);
}
Run Code Online (Sandbox Code Playgroud)
但我收到未知的 prop 错误,有什么解决办法吗?
我想创建一个自动完成输入,当用户单击键盘中的“搜索”图标时触发事件。
编辑:
onSearch 不是一个标准属性,所以我想 React 不会支持它。由于 onSearch 与按 Enter 键相同,因此使用 onKeypress 即可完成此工作:
render() {
return (
<main>
<input onKeyPress={ this.onKeyPress } type="search" list="options"/>
<datalist id="options">
{ OPTS.map(item => <option value={ item.nm } key={ item.id } data-id={ item.id } />) }
</datalist>
</main>
);
}
onKeyPress(event) {
if (event.key === 'Enter') {
const id = document.querySelector(`#options option[value="${event.target.value}"]`).dataset.id;
if (id) {
console.warn('user search', …Run Code Online (Sandbox Code Playgroud)