我目前在我的 React 项目中使用 Twitter Typeahead,我想显示基于 Typeahead 的建议,但我无法让它工作。
在我的代码下面:
var Search = React.createClass({
getInitialState: function () {
return {query: ''};
},
handleChange: function (e) {
this.setState({query: e.target.value});
},
componentDidMount(){
var suggestions = {
query: "d",
results: [
{name: "first"},
{name: "second"},
{name: "third"},
{name: "fourth"}
]
};
var suggests = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 10,
local: suggestions
});
var template = _.template('<span class="name"><%= name %></span>');
$(React.findDOMNode(this.refs.suggestion)).typeahead({
hint: true,
highlight: true,
},
{
name: 'suggests',
displayKey: 'name',
source: suggests.ttAdapter(),
templates: {
suggestion: function (data) {
return template(data);
}
}
});
},
render() {
<form action="myAction" method="GET">
<input name="q" id="query" ref="suggestion" className="form-control suggestions" type="text"
value={this.state.query}
onChange={this.handleChange} onBlur={this.handleChange}/>
</form>
}
});
根据我的代码,我需要添加或更改什么才能在我的项目上获得自动完成和建议。非常感谢您的宝贵建议和帮助。