123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="content"></div>
            
          
!
            
              /** @jsx React.DOM */
    /**
     * 最外层父组件
     */
    var ProcuctFilterTable = React.createClass({
        getDefaultProps: function () {
            return {
                products: []
            }
        },
        getInitialState: function () {
            return {
                checkValue: false,
                searchValue: ''
            }
        },
        handleUserInput: function (inputParams) {
            this.setState({
                checkValue: inputParams.checkboxValue,
                searchValue: inputParams.searchValue
            });
        },
        render: function () {
            return (
                 <div>
                     <FilterBar checkValue={this.state.checkValue} searchValue={this.state.searchValue} userInputCallback={this.handleUserInput}/>
                     <ProcuctTable products={this.props.products} checkValue={this.state.checkValue} searchValue={this.state.searchValue}/>
                 </div>
            )
        }
    });

    /**
     * 子组件:负责显示类目和商品信息
     */
    var ProcuctTable = React.createClass({
        getDefaultProps: function () {
            return {
                products: [], //所有的商品信息
                checkValue: false,
                searchValue: ""
            }
        },
        render: function () {
            var rows = [];
            var lastCate;
            this.props.products.forEach(function(product, i) {
                //如果不使用bind则this指向window
                var curCate = product.category;
                var curState = product.stocked;
                var curName = product.name;
                //否则会报warning
                var keyForCat = i;
                var keyForRow = i + 'x';
                if (curCate != lastCate) {
                    rows.push(<CateRow category={curCate} key={keyForCat} />);
                    lastCate = curCate;
                }

                if (!this.props.checkValue || (this.props.checkValue && curState == true) ) {
                    if (curName.indexOf(this.props.searchValue) !== -1) {
                        rows.push(<ProcuctRow product={product} key={keyForRow} checkValue/>);
                    }
                }
            }.bind(this));
            return (<table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Price</th>
                    </tr>
                </thead>
                <tbody>{rows}</tbody>
            </table>)
        }
    });
    var ProcuctRow = React.createClass({
        getDefaultProps: function () {
            return {
                product: {
                    name: '',
                    price: '',
                    stocked: true
                }
            }
        },
        render: function () {
            var name = this.props.product.name;
            if (!this.props.product.stocked) {
                //注意样式对象的写法:第一重大括号表示这是 JavaScript 语法,第二重大括号表示样式对象。
                name = <span style={{color: 'red'}}>{name}</span>
            }
            return (<tr><td>{name}</td><td>{this.props.product.price}</td></tr>)
        }
    });
    var CateRow = React.createClass({
        getDefaultProps: function () {
            return {
                category: '' //商品类目名称
            }
        },
        render: function () {
            return (<tr><th colSpan="2">{this.props.category}</th></tr>)
        }
    });

    /**
     * 子组件:负责搜索和过滤功能
     */
    var FilterBar = React.createClass({
        getDefaultProps: function () {
            return {
                searchValue: "",
                checkValue: ""
            }
        },
        propTypes: {
            userInputCallback: React.PropTypes.func
        },
        handleSearchChange: function (event) {
            //form的处理。参见:https://facebook.github.io/react/docs/forms.html
                //event的方式 event.target
                //使用ref字段参见:https://facebook.github.io/react/docs/more-about-refs.html
                //findDOMNode方法:https://facebook.github.io/react/docs/working-with-the-browser.html
            var searchValue = React.findDOMNode(this.refs.filterNameInput).value;
            var checkboxValue = React.findDOMNode(this.refs.filterCheckbox).checked;
            this.props.userInputCallback({
                searchValue: searchValue,
                checkboxValue: checkboxValue
            });

        },
        render: function () {
            return (
                    <form>
                      <label>search:</label><input type="text" value={this.props.searchValue} onChange={this.handleSearchChange} ref="filterNameInput"/> <br/>
                      <label>in stock: </label><input type="checkbox" checked={this.props.checkValue} onChange={this.handleSearchChange} ref="filterCheckbox"/>
                    </form>
            );
        }
    });

    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'
    }];
    var productTable = React.render(
        <ProcuctFilterTable products={Products} />,
        document.querySelector('.content')
    );
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console