cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              /** @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
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console