Skip to content

Instantly share code, notes, and snippets.

@rcdexta
Last active March 27, 2025 07:55
Show Gist options
  • Save rcdexta/4f341b6a43224296233911e928797378 to your computer and use it in GitHub Desktop.
Save rcdexta/4f341b6a43224296233911e928797378 to your computer and use it in GitHub Desktop.
import React from 'react'
import ReactDOM from 'react-dom'
import Autosuggest from 'react-autosuggest'
import axios from 'axios'
import { debounce } from 'throttle-debounce'
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 or short code',
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>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment