码迷,mamicode.com
首页 > 其他好文 > 详细

学习react-简单小案例(5)--综合案例

时间:2017-12-05 20:08:45      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:react   ret   border   div   dom   create   nbsp   demo   browser   

<html>
    <head>
        <title></title>
        <meta charset="UTF-8"/>
        <script src="js/react.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="js/react-dom.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="js/browser.min.js" type="text/javascript" charset="utf-8"></script>
        <style type="text/css">
            .bodystyle{
                width: 100%;
                height: 100%;
                background: brown;
            }
            .borderstyle{
                border: 1px solid #ccc;
                background: #ffffff;
                width: 200px;
                margin: 10px;
                float: left;
                height: 80px;
            }
            textarea{
                width: 100%;
            }
        </style>
    </head>
    <body>
        <div id="container"></div>
    </body>
    <script type="text/babel">
        //父级DIV
        var Demo = React.createClass({
            //初始化
            getInitialState:function(){
                return ({
                    datas:[]
                });
            },
            updateComment:function(newText,i){
                var arr = this.state.datas;
                arr[i] = newText;
                this.setState({datas:arr});
            },
            delComment:function(i){
                var arr = this.state.datas;
                arr.splice(i,1);
                this.setState({datas:arr});
            },
            eachComment:function(text,i){
                return (
                    <Crud delCommentText={this.delComment} updateCommentText={this.updateComment} key={i} index={i}>
                        {text}
                    </Crud>
                    )
            },
            add:function(text){
                var arr = this.state.datas;
                arr.push(text);
                this.setState({datas:arr});
            },
            render:function(){
                return(
                    <div>
                        <div>
                            <button onClick={this.add.bind(null,"Hello world")}>添加</button>
                        </div>
                        <div className="bodystyle">
                            {this.state.datas.map(this.eachComment)}
                        </div>
                    </div>
                );
            }
        });
        //子级DIV
        var Crud = React.createClass({
            getInitialState:function(){
                return ({
                    edit:false
                });
            },
            edit:function(){
                this.setState({edit:true})
            },
            del:function(){
                this.props.delCommentText(this.props.index)
            },
            save:function(){
                var val = this.refs.newText.value;
                this.props.updateCommentText(val,this.props.index)
                this.setState({edit:false})
            },
            rederNormal:function(){
                return (
                    <div className="borderstyle">
                        <div>{this.props.children}</div>
                        <button onClick={this.edit}>编辑</button>
                        <button onClick={this.del}>删除</button>
                    </div>
                );
            },
            rederForm:function(){
                return (
                    <div className="borderstyle">
                        <textarea ref="newText" defaultValue={this.props.children}></textarea>
                        <button onClick={this.save}>更新</button>
                    </div>
                );
            },
            render:function(){
                if(this.state.edit){
                    return this.rederForm()
                }else{
                    return this.rederNormal()
                }
                
            }
        });
        ReactDOM.render(
            <Demo />,
            document.getElementById(container)
        );
    </script>
</html>

 

学习react-简单小案例(5)--综合案例

标签:react   ret   border   div   dom   create   nbsp   demo   browser   

原文地址:http://www.cnblogs.com/phper8/p/7988911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!