import React, { Component } from 'react'; import './search-box.css'; /** * Search for location */ class SearchBox extends Component { constructor(props) { super(props); this.state = { q: '', results: [], fetching: false } this.handleChange = this.handleChange.bind(this); this.search = this.search.bind(this); this.handleKeyPress = this.handleKeyPress.bind(this); this.clearResults = this.clearResults.bind(this); this.clearQuery = this.clearQuery.bind(this); } // Update search term handleChange(e) { this.setState({ q: e.target.value }); // If the ‘clear’ icon has been clicked, clear results list as well if(e.target.value === '') { this.clearResults(); } } // Clear search results on ESC handleKeyPress(e){ if(e.keyCode === 27) { //ESC is pressed this.clearQuery(); this.clearResults(); } } clearResults(){ this.setState({ results: [] }); } clearQuery(){ this.setState({ q: '' }); } // Query search endpoint search(e) { e.preventDefault(); this.setState({ fetching: true }) fetch( '/search?q='+this.state.q ).then( (res) => res.json() ).then((data) => { if (data && data.results){ this.setState({ results: data.results, fetching: false }) } else { console.error(data); this.setState({ results: [], fetching: false }) } }).catch((err) => { console.error(err) this.setState({ results: [], fetching: false }) }) } render() { const resultsList = this.state.results.length?