Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="content"></div>
              
            
!

CSS

              
                
              
            
!

JS

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

Console