[英]How to use DataSearch as controlled component from Reactivesearch?
我是 ReactiveSearch 庫的新手,我實現了帶有自動建議的 DataSearch 組件作為我的站點內搜索欄。 我添加了value
和onChange
以將輸入值存儲為狀態,但是一旦我添加了value
屬性,我就無法再在搜索欄中輸入。 怎么了?
我還想知道當我單擊其中一個建議來觸發某個事件時可以使用什么樣的回調函數,我嘗試了onClick
,但沒有奏效。 這是我的代碼,任何幫助表示贊賞!
import * as React from 'react';
import {
ReactiveBase,
DataSearch,
} from '@appbaseio/reactivesearch';
import './SearchBar.scss';
export default class SearchBar extends React.Component {
constructor(props) {
super(props);
this.state = { searchTerm: '' };
}
handleInputChange = (event) => {
this.setState({ searchTerm: event.target.value });
};
handleSearch = () => {
// do something...
};
public render() {
return (
<React.Fragment>
<ReactiveBase
app="rooms,floors,assets"
url="http://localhost:9200"
headers={{
'Access-Control-Allow-Origin': '*'
}}
style={{ display: 'inline' }}
>
<DataSearch
componentId="SearchRoom"
dataField={['roomName', 'roomNumber', 'floorName', 'assetName']}
placeholder="Search for rooms or assets"
style={{ width: 500, display: 'inline-block' }}
fuzziness={1}
value={this.state.searchTerm}
onChange={this.handleInputChange}
//how to trigger handleSearch when I click one of the suggestions?
/>
</ReactiveBase>
</React.Fragment>
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.