123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- let ProductCategoryRow = React.createClass({
- name: "ProductCategoryRow",
- componentDidMount: function () {
- console.log("Component did mountDM", this.name, "State", this.state, "Props", this.props);
- },
- getInitialState: function () {
- console.log("Get Initial State", this.name);
- return null;
- },
- render: function () {
- return (<tr><th colSpan="2">{this.props.category}</th></tr>);
- }
- });
- let ProductRow = React.createClass({
- name: "ProductRow",
- componentDidMount: function () {
- console.log("Component did mountDM", this.name, "State", this.state, "Props", this.props);
- },
- getInitialState: function () {
- console.log("Get Initial State", this.name);
- return null;
- },
- render: function () {
- let 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>
- );
- }
- });
- let ProductTable = React.createClass({
- name: "ProductTable",
- componentDidMount: function () {
- console.log("Component did mountDM", this.name, "State", this.state, "Props", this.props);
- },
- getInitialState: function () {
- console.log("Get Initial State", this.name);
- return null;
- },
- render: function () {
- let rows = [];
- let lastCategory = null;
- this.props.products.forEach(function (product) {
- // Apply name and stock filters.
- if (product.name.indexOf(this.props.filterText) === -1 || (!product.stocked && this.props.inStockOnly)) {
- return;
- }
- if (product.category !== lastCategory) {
- rows.push(<ProductCategoryRow category={product.category} key={product.category} />);
- }
- rows.push(<ProductRow product={product} key={product.name} />);
- lastCategory = product.category;
- }.bind(this));
- return (
- <table>
- <thead>
- <tr>
- <th>Name</th>
- <th>Price</th>
- </tr>
- </thead>
- <tbody>
- {rows}
- </tbody>
- </table>
- );
- }
- });
- let SearchBar = React.createClass({
- name: "SearchBar",
- componentDidMount: function () {
- console.log("Component did mountDM", this.name, "State", this.state, "Props", this.props);
- },
- getInitialState: function () {
- console.log("Get Initial State", this.name, "Props", this.props);
- return null;
- },
- render: function () {
- return (
- <form>
- <input type="text" placeholder="Search..." value={this.props.filterText} />
- <p>
- <input type="checkbox" checked={this.props.inStockOnly} />
- {' '}
- Only show products in stock
- </p>
- </form>
- );
- }
- });
- let FilterableProductTable = React.createClass({
- name: "FilterableProductTable",
- componentDidMount: function () {
- console.log("Component did mountDM", this.name, this.name, "State", this.state, "Props", this.props);
- },
- getInitialState: function () {
- let state = {
- filterText: "ball",
- inStockOnly: true
- };
- console.log("Get Initial State", this.name, state, "Props", this.props);
- return state;
- },
- render: function () {
- return (
- <div>
- <SearchBar
- filterText={this.state.filterText}
- inStockOnly={this.state.inStockOnly}
- />
- <ProductTable
- products={this.props.products}
- filterText={this.state.filterText}
- inStockOnly={this.state.inStockOnly}
- />
- </div>
- );
- }
- });
- let 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("content")
- );
|