首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >React JS搜索功能。显示搜索结果

React JS搜索功能。显示搜索结果
EN

Stack Overflow用户
提问于 2018-09-30 07:35:36
回答 1查看 0关注 0票数 0

我正在关注这个React-AuTocomplete教程

它按预期工作。但是,当我点击结果时,它会显示在搜索框内。如何在盒子外的某个地方展示它?

在此处输入图像描述
在此处输入图像描述

在此处输入图像描述
在此处输入图像描述
代码语言:javascript
复制
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import Autosuggest from 'react-autosuggest';
import axios from 'axios';
import { debounce } from 'throttle-debounce';
import { root } from 'postcss';
import '../styles.css';

class AutoComplete extends React.Component {
  state = {
    value: '',
    suggestions: []
  };

  componentWillMount() {
    this.onSuggestionsFetchRequested = debounce(500, this.onSuggestionsFetchRequested);
  }

  renderSuggestion = suggestion => {
    return (
      <div className="result">
        <div>{suggestion.fullName}</div>
        <div className="shortCode">{suggestion.shortCode}</div>
      </div>
    );
  };

  onChange = (event, { newValue }) => {
    this.setState({ value: newValue });
  };

  onSuggestionsFetchRequested = ({ value }) => {
    axios
      .post('http://localhost:9200/crm_app/customers/_search', {
        query: {
          multi_match: {
            query: value,
            fields: ['fullName', 'shortCode']
          }
        },
        sort: ['_score', { createdDate: 'desc' }]
      })
      .then(res => {
        const results = res.data.hits.hits.map(h => h._source);
        this.setState({ suggestions: results });
      });
  };

  onSuggestionsClearRequested = () => {
    this.setState({ suggestions: [] });
  };

  render() {
    const { value, suggestions } = this.state;

    const inputProps = {
      placeholder: 'customer name ',
      value,
      onChange: this.onChange
    };

    return (
      <div className="App">
        <h1>AutoComplete Demo</h1>
        <Autosuggest
          suggestions={suggestions}
          onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
          onSuggestionsClearRequested={this.onSuggestionsClearRequested}
          getSuggestionValue={suggestion => suggestion.fullName}
          renderSuggestion={this.renderSuggestion}
          inputProps={inputProps}
        />
      </div>
    );
  }
}

// const rootElement = document.getElementById('root');
// ReactDOM.render(<AutoComplete />, rootElement);
export default AutoComplete;
EN

回答 1

Stack Overflow用户

发布于 2018-09-30 17:14:00

该文档描述了onSuggestionSelected prop。当您选择建议时,这将回调。在你的回调处理程序中,你用一个建议值设置了一些状态,然后在render方法中渲染它:

代码语言:javascript
复制
onSuggestionSelected = ({suggestion}) => {
    this.setState({suggestion});
}

默认状态:

代码语言:javascript
复制
state = {
    value: '',
    suggestions: [],
    suggestion: ''
  };

在渲染中:

代码语言:javascript
复制
  render() {
    const { value, suggestions, suggestion} = this.state;

    const inputProps = {
      placeholder: 'customer name ',
      value,
      onChange: this.onChange
    };

    return (
      <div className="App">
        <h1>AutoComplete Demo</h1>
        <Autosuggest
          suggestions={suggestions}
          onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
          onSuggestionsClearRequested={this.onSuggestionsClearRequested}
          getSuggestionValue={suggestion => suggestion.fullName}
          renderSuggestion={this.renderSuggestion}
          inputProps={inputProps}
        />
      <div>{suggestion}</div>
      </div>
    );
  }
}

我没有测试过这个,但你应该明白这个想法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档