Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active August 29, 2015 14:20
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryanflorence/cfab4221074876433a49 to your computer and use it in GitHub Desktop.
Save ryanflorence/cfab4221074876433a49 to your computer and use it in GitHub Desktop.
// with static data, do filtering and sorting in the client
<Autocomplete
initialValue="Ma"
items={getUnitedStates()}
getItemValue={(item) => item.name}
shouldItemRender={(item, value) => (
state.name.toLowerCase().indexOf(value.toLowerCase()) !== -1 ||
state.abbr.toLowerCase().indexOf(value.toLowerCase()) !== -1
)}
sortItems={(a, b, value) => (
a.name.toLowerCase().indexOf(value.toLowerCase()) >
b.name.toLowerCase().indexOf(value.toLowerCase()) ? 1 : -1
)}
renderItem={(item, isHighlighted) => (
<div
style={isHighlighted ? styles.highlightedOption : styles.option}
key={item.abbr}
>{item.name}</div>
)}
renderEmpty={(value) => (
<div style={{padding: 10}}>No matches for {value}</div>
)}
/>
// or from server data that does sorting/filtering already and we just dump it all in
<Autocomplete
items={this.state.dynamicItems}
getItemValue={(item) => item.name}
onChange={(value) => {
makeRequest(value, (items) => {
this.setState({ dynamicItems: items })
})
}}
renderItem={(item, isHighlighted) => (
<div
style={isHighlighted ? styles.highlightedOption : styles.option}
key={item.abbr}
>{item.name}</div>
)}
renderEmpty={(value) => (
<div style={{padding: 10}}>No matches for {value}</div>
)}
/>
@iammerrick
Copy link

L33t

@misuba
Copy link

misuba commented May 5, 2015

Is this coming to react-autocomplete?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment