Created
September 17, 2017 02:11
-
-
Save VictorCoding/1a95414fd0e151c2c2782c1f1181d6e3 to your computer and use it in GitHub Desktop.
working with search/filtering items
This file contains hidden or 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 { 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