Last active
May 12, 2019 14:34
-
-
Save mohandere/24ad3ad78cfa5d5b77b0ef37a1b47e3b to your computer and use it in GitHub Desktop.
Higher-Order Component
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 products from './products.json' | |
class ProductsListWithSearch extends React.PureComponent { | |
state = { | |
searchTerm: '' | |
} | |
handleSearch = event => { | |
this.setState({ searchTerm: event.target.value }) | |
} | |
render() { | |
const { searchTerm } = this.state | |
let filteredProducts = filterProducts(searchTerm); | |
return ( | |
<> | |
<input onChange={this.handleSearch} value={this.state.searchTerm} type="text" placeholder="Search" /> | |
<ProductsList products={filteredProducts} /> | |
</> | |
) | |
} | |
const filterProducts = (searchTerm) => { | |
searchTerm = searchTerm.toUpperCase() | |
return products.filter(product => { | |
let str = `${product.title} ${product.style} ${product.sku}`.toUpperCase(); | |
return str.indexOf(searchTerm) >= 0; | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment