前端那些事之react篇--todomvc

react实现

目录结构

输入图片说明

index.js

var React = require('react'),
    ReactDOM = require('react-dom'),
    App = require('./App/index.js'),
    Todo = require('./to-do/index');

require('./index.css');


ReactDOM.render(
  <Todo />,
  document.getElementById('root')
);


todo文件下的index.js

import React from 'react'

import TodoList from './to-do-list'

function id() {
    return Math.random().toString().replace(/\./,'')+'-'+Math.random().toString().replace(/\./,'')
}

var TodoMVC = React.createClass({
    getInitialState:function () {
        return {
            items : [
                {text:'aaa',id:id(),type:'active'},
                {text:'bbb',id:id(),type:'no-active'},
                {text:'ccc',id:id()}
            ],
            value:'inp'
        }
    }
    ,
    render:function () {
        return (
            <div className="todo-mvc">
                <h3>todos</h3>

                <p>
                    <input value={this.state.value} onChange={this.handleChange}/>
                    <button onClick={this.handleAdd}>提交</button>
                </p>
                <TodoList
                    items={this.state.items}
                    onDelete={this.handleDelete}
                    onEdit={this.handleEdit}
                />
            </div>
        )
    },
    handleEdit:function (obj) {
        alert(obj.text)
        var items = this.state.items;


        items = items.map(function (o) {
            console.log(obj.id,o.id)
            if(o.id == obj.id){

                o.text = obj.text
            }
            return o
        })
        console.log(items)
        this.setState({items:items})
    },

    handleDelete:function (obj) {

        var items = this.state.items,
            json = []
        for(var i=0;i<items.length;i++){
            if(items[i].id != obj.id){
                json.push(items[i])
            }
        }
        this.setState({items:json})
    },
    handleAdd:function () {
        var items = this.state.items,
            text = this.state.value
        items.push({
            text:text,
            id:id()
        })
        this.setState({
            items:items,
            value : ''
        })
    },
    handleChange:function (e) {
        this.setState({
            value:e.target.value
        })
    }
})
module.exports = TodoMVC

todo文件下的to-do-list文件里的index

import React from 'react'


var TodoItem = React.createClass({
    getInitialState:function () {
        return {
            value : this.props.text
        }
    },
    render:function () {
        return (
            <li>
                {this.props.text}<button onClick={(e)=>this.props.delete(this.props.o)}>删除</button><br/>
                <input value={this.state.value} onChange={this.handleChange}/>
                <button onClick={this.handleEdit}>确定</button>
                <button onClick={this.handeCancel}>取消</button>
                <br/><br/>
            </li>
        )
    },
    handeCancel:function () {
        this.setState({
            value:this.props.text
        })
    },
    handleChange:function (e) {
        this.setState({
            value:e.target.value
        })
    },
    handleEdit:function () {
        var obj = {
            id:this.props.id,
            text:this.state.value
        }
        this.props.edit(obj)
    }
})

var TodoList = React.createClass({
    render:function () {
        var that = this
        var nodes = this.props.items.map(function (o) {
            return (
                <TodoItem id={o.id} edit={that.props.onEdit} o={o} key={o.id} text={o.text} delete={that.props.onDelete}/>
            )
        })
        return (
          <ul>{nodes}</ul>
        )
    }
})
module.exports = TodoList

转载于:https://my.oschina.net/yongxinke/blog/867791

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值