A Pen by Kevin Lacker on CodePen.
Created
February 7, 2017 21:37
-
-
Save benjaminapetersen/59cd9d6954220186038462cb6de42889 to your computer and use it in GitHub Desktop.
Thinking In React: Step 2
This file contains 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
<div id="container"> | |
<!-- This element's contents will be replaced with your component. --> | |
</div> |
This file contains 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
class ProductCategoryRow extends React.Component { | |
render() { | |
return <tr><th colSpan="2">{this.props.category}</th></tr>; | |
} | |
} | |
class ProductRow extends React.Component { | |
render() { | |
var name = this.props.product.stocked ? | |
this.props.product.name : | |
<span style={{color: 'red'}}> | |
{this.props.product.name} | |
</span>; | |
return ( | |
<tr> | |
<td>{name}</td> | |
<td>{this.props.product.price}</td> | |
</tr> | |
); | |
} | |
} | |
class ProductTable extends React.Component { | |
render() { | |
var rows = []; | |
var lastCategory = null; | |
this.props.products.forEach(function(product) { | |
if (product.category !== lastCategory) { | |
rows.push(<ProductCategoryRow category={product.category} key={product.category} />); | |
} | |
rows.push(<ProductRow product={product} key={product.name} />); | |
lastCategory = product.category; | |
}); | |
return ( | |
<table> | |
<thead> | |
<tr> | |
<th>Name</th> | |
<th>Price</th> | |
</tr> | |
</thead> | |
<tbody>{rows}</tbody> | |
</table> | |
); | |
} | |
} | |
class SearchBar extends React.Component { | |
render() { | |
return ( | |
<form> | |
<input type="text" placeholder="Search..." /> | |
<p> | |
<input type="checkbox" /> | |
{' '} | |
Only show products in stock | |
</p> | |
</form> | |
); | |
} | |
} | |
class FilterableProductTable extends React.Component { | |
render() { | |
return ( | |
<div> | |
<SearchBar /> | |
<ProductTable products={this.props.products} /> | |
</div> | |
); | |
} | |
} | |
var PRODUCTS = [ | |
{category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'}, | |
{category: 'Sporting Goods', price: '$9.99', stocked: true, name: 'Baseball'}, | |
{category: 'Sporting Goods', price: '$29.99', stocked: false, name: 'Basketball'}, | |
{category: 'Electronics', price: '$99.99', stocked: true, name: 'iPod Touch'}, | |
{category: 'Electronics', price: '$399.99', stocked: false, name: 'iPhone 5'}, | |
{category: 'Electronics', price: '$199.99', stocked: true, name: 'Nexus 7'} | |
]; | |
ReactDOM.render( | |
<FilterableProductTable products={PRODUCTS} />, | |
document.getElementById('container') | |
); |
This file contains 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
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-dom.js"></script> |
This file contains 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
body { | |
padding: 5px | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment