Think in React

2019-05-05  本文已影响0人  YC____
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' }
];
class FilterableProductTable extends Component {
    constructor(props) {
        super(props);
        this.state = {
            filterText: '',
            inStockOnly: false
        };
    }
    handleFilterTextInput = filterText => {
        this.setState({
            filterText: filterText
        });
    };

    handleInStockInput = inStockOnly => {
        this.setState({
            inStockOnly: inStockOnly
        });
    };
    render() {
        return (
            <div>
                <SearchBar
                    filterText={this.state.filterText}
                    inStockOnly={this.state.inStockOnly}
                    onFilterTextInput={this.handleFilterTextInput}
                    onInStockInput={this.handleInStockInput}
                />
                <ProductTable
                    products={PRODUCTS}
                    filterText={this.state.filterText}
                    inStockOnly={this.state.inStockOnly}
                    onFilterTextInput={this.handleFilterTextInput}
                    onInStockInput={this.handleInStockInput}
                />
            </div>
        );
    }
}
class SearchBar extends Component {
    handleFilterTextInputChange = e => {
        this.props.onFilterTextInput(e.target.value);
    };
    handleInStockInputChange = e => {
        this.props.onInStockInput(e.target.checked);
    };

    render() {
        return (
            <form>
                <input type="text" placeholder="search ..." value={this.props.filterText} onChange={this.handleFilterTextInputChange} />
                <p>
                    <input type="checkbox" value={this.props.inStockOnly} onChange={this.handleInStockInputChange} />
                    only show products in stocked
                </p>
            </form>
        );
    }
}
class ProductTable extends Component {
    render() {
        console.log(this.props);
        var rows = [];
        var lastCategory = null;
        this.props.products.forEach(i => {
            if (i.name.indexOf(this.props.filterText) === -1 || (!i.stocked && this.props.inStockOnly)) {
                return;
            }
            if (i.category !== lastCategory) {
                rows.push(<ProductCategoryRow category={i.category} key={i.category} />);
            }
            rows.push(<ProductRow product={i} key={i.name} />);
            lastCategory = i.category;
        });
        return (
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Price</th>
                    </tr>
                </thead>
                <tbody>{rows}</tbody>
            </table>
        );
    }
}
class ProductCategoryRow extends Component {
    render() {
        return (
            <tr>
                <th colSpan="2">{this.props.category}</th>
            </tr>
        );
    }
}
class ProductRow extends Component {
    render() {
        return (
            <tr>
                <td>{this.props.product.name}</td>
                <td>{this.props.product.price}</td>
            </tr>
        );
    }
}

export default FilterableProductTable;
上一篇 下一篇

猜你喜欢

热点阅读