3

我目前在我的 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>
    }

});

根据我的代码,我需要添加或更改什么才能在我的项目上获得自动完成和建议。非常感谢您的宝贵建议和帮助。

4

1 回答 1

7

缺少以下内容

  1. 初始化猎犬
  2. 渲染方法缺少返回
  3. Bloodhound 本地选项应采用Suggestions.results 而不仅仅是建议

var Search = React.createClass({
  getInitialState: function() {
    return {
      query: ''
    };
  },
  handleChange: function(e) {
    this.setState({
      query: e.target.value
    });
  },
  componentDidMount: function() {
    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.results
    });

    suggests.initialize(); // <----- THIS ONE

    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: function() { // <---- MISSING A RETURN HERE
    return (<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>);
  }

});

这是一个演示http://jsbin.com/vubehe/edit?html ,输出

于 2015-07-08T22:15:06.690 回答