This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constructor(props) { | |
super(props); | |
this.state = { | |
// ... more code ... | |
}; | |
this.autocomplete = this.autocomplete.bind(this); | |
this.hanldeKeyup = this.hanldeKeyup.bind(this); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
autocomplete(evt) { | |
// ... more code ... | |
} | |
hanldeKeyup(evt) { | |
// event code 27 means escape key | |
if (evt.keyCode === 27) { | |
this.setState({ searchItems: [] }); | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SearchItemsList = ({ | |
searchItems, | |
}) => { | |
return ( | |
<ul className="list-group"> | |
{searchItems.map((item, idx) => ( | |
<li | |
className="list-group-item" | |
key={idx} | |
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input | |
type="text" | |
id="autocomplete" | |
onChange={this.autocomplete} | |
/> | |
{searchItems.length > 0 && ( | |
<SearchItemsList | |
searchItems={searchItems} | |
/> | |
)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
| |
const SearchItemsList = () => { | |
return ( | |
<div> | |
hello from search item list | |
</div> | |
) | |
} | |
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<label htmlFor="autocomplete">Item Name</label> | |
<input | |
type="text" | |
id="autocomplete" | |
onChange={this.autocomplete} | |
value={name} | |
className="custom-input form-control" | |
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constructor(props) { | |
super(props); | |
// ... more code ... | |
this.autocomplete = this.autocomplete.bind(this); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{// ... more code ...} | |
<input | |
type="text" | |
id="autocomplete" | |
onChange={this.autocomplete.bind(this)} | |
value={name} | |
className="custom-input form-control" | |
/> | |
{// ... more code ...} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constructor(props){ | |
// ... more code ... | |
} | |
autocomplete(evt) { | |
let text = evt.target.value; | |
fetch(`http://localhost:3004/items?name_like=${text}&_limit=6`) | |
.then(res => res.json()) | |
.then(data => { | |
this.setState({ searchItems: data }); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return ( | |
<div className="container mt-3"> | |
<h1 className="h2 text-center">Autocomplete Example</h1> | |
<div className="form-group"> | |
<label htmlFor="autocomplete">Item Name</label> | |
<input | |
type="text" | |
id="autocomplete" | |
value={name} | |
className="custom-input form-control" |