Skip to content

Instantly share code, notes, and snippets.

@VictorCoding
Created September 17, 2017 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save VictorCoding/1a95414fd0e151c2c2782c1f1181d6e3 to your computer and use it in GitHub Desktop.
Save VictorCoding/1a95414fd0e151c2c2782c1f1181d6e3 to your computer and use it in GitHub Desktop.
working with search/filtering items
import { Div, css } from 'glamorous'
import React from 'react'
class Index extends React.Component {
constructor(props) {
super(props)
}
componentWillMount() {
this.setState({
posts: [
'first',
'second',
'third'
],
originalPosts: [
'first',
'second',
'third'
],
})
}
filterPosts(e) {
const filteredPosts = !e.target.value ? this.state.originalPosts :
this.state.originalPosts.filter(p => p.startsWith(e.target.value))
this.setState({
posts: filteredPosts,
})
}
render () {
return (
<Div css={{
display: 'flex',
flexDirection: 'column',
}}>
<Div css={{
fontSize: '2.2em',
display: 'flex',
justifyContent: 'center',
height: '10%',
fontWeight: '200',
}}>
VicAdventures
</Div>
<Div css={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
}}>
<input onChange={(e) => this.filterPosts(e)}/>
{this.state.posts.map((p, i) => <div key={i}>{p}</div>)}
</Div>
</Div>
)
}
}
export default Index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment