Skip to content

Instantly share code, notes, and snippets.

@yukoss
Created September 9, 2017 09:58
Show Gist options
  • Save yukoss/c2b38e398cd50fe6d052f36df07f4036 to your computer and use it in GitHub Desktop.
Save yukoss/c2b38e398cd50fe6d052f36df07f4036 to your computer and use it in GitHub Desktop.
Suggestions with a scrollbar
<h2>Suggestions with a scrollbar</h2>
<div id="app"></div>
function escapeRegexCharacters(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function getSuggestions(value) {
const escapedValue = escapeRegexCharacters(value.trim());
if (escapedValue === '') {
return [];
}
const regex = new RegExp('^' + escapedValue, 'i');
let request = fetch(`https://fnw-ml.herokuapp.com/us/stores/search?store_name=${value}`, {method:'GET'});
return request;
}
function getSuggestionValue(suggestion) {
return `${suggestion.store_name}, ${suggestion.city}, ${suggestion.state}`;
}
function renderSuggestion(suggestion) {
return (
<span>{suggestion.store_name}, {suggestion.city}, {suggestion.state}</span>
);
}
class App extends React.Component {
constructor() {
super();
this.state = {
value: '',
suggestions: []
};
}
onChange = (event, { newValue, method }) => {
this.setState({
value: newValue
});
};
onSuggestionsFetchRequested = ({ value }) => {
getSuggestions(value)
.then(stores => {
return stores.json();
})
.then(stores => {
this.setState({
suggestions: stores
});
})
.catch(err => console.error(err))
};
onSuggestionsClearRequested = () => {
this.setState({
suggestions: []
});
};
render() {
const { value, suggestions } = this.state;
const inputProps = {
placeholder: "Stores",
value,
onChange: this.onChange
};
return (
<Autosuggest
suggestions={suggestions}
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
onSuggestionsClearRequested={this.onSuggestionsClearRequested}
getSuggestionValue={getSuggestionValue}
renderSuggestion={renderSuggestion}
inputProps={inputProps} />
);
}
}
ReactDOM.render(<App />, document.getElementById('app'));
<script src="https://unpkg.com/react@15.4.2/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15.4.2/dist/react-dom.js"></script>
<script src="https://unpkg.com/react-autosuggest@9.0.0/dist/standalone/autosuggest.js"></script>
body {
font-family: Helvetica, sans-serif;
}
.react-autosuggest__container {
position: relative;
}
.react-autosuggest__input {
width: 240px;
height: 30px;
padding: 10px 20px;
font-family: Helvetica, sans-serif;
font-weight: 300;
font-size: 16px;
border: 1px solid #aaa;
border-radius: 4px;
}
.react-autosuggest__input--focused {
outline: none;
}
.react-autosuggest__input--open {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}
.react-autosuggest__suggestions-container {
display: none;
}
.react-autosuggest__suggestions-container--open {
display: block;
position: absolute;
top: 51px;
width: 280px;
border: 1px solid #aaa;
background-color: #fff;
font-family: Helvetica, sans-serif;
font-weight: 300;
font-size: 16px;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
max-height: 100px;
overflow-y: auto;
z-index: 2;
}
.react-autosuggest__suggestions-list {
margin: 0;
padding: 0;
list-style-type: none;
}
.react-autosuggest__suggestion {
cursor: pointer;
padding: 10px 20px;
}
.react-autosuggest__suggestion--highlighted {
background-color: #ddd;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment