Advertisement
Guest User

index.jsx

a guest
Oct 11th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var React = require('react');
  2.  
  3. var TodoBox = React.createClass({
  4.     render: function() {
  5.         return (
  6.             <div className="todoBox">
  7.                 <h1>Todos</h1>
  8.                 <TodoList data= {this.props.data} />
  9.                 <TodoForm />
  10.             </div>
  11.         );
  12.     }
  13. });
  14.  
  15. var TodoList = React.createClass({
  16.     getInitialState: function() {
  17.         return {
  18.             data: this.props.data,
  19.             titleValue: "",
  20.             detailValue: ""
  21.         }
  22.     },
  23.  
  24.     changeTitle: function(event) {
  25.         this.setState({
  26.             titleValue: event.target.value
  27.         });
  28.     },
  29.  
  30.     changeDetail: function(event) {
  31.         this.setState({
  32.             detailValue: event.target.value
  33.         });
  34.     },
  35.  
  36.     addTodo: function() {
  37.         var newData = this.state.data;
  38.         newData.push({
  39.             title: this.state.titleValue,
  40.             detail: this.state.detailValue
  41.         });
  42.         this.setState({data: newData});
  43.         this.setState({titleValue: ""});
  44.         this.setState({detailValue: ""});
  45.     },
  46.  
  47.     render: function() {
  48.         var todo = this.props.data.map(function(item) {
  49.             return <Todo title={item.title} key={item.title}>{item.detail}</Todo>
  50.         });
  51.  
  52.         return (
  53.             <div>
  54.                 Title:<input type="text" value={this.state.titleValue} onChange={this.changeTitle} />
  55.                 Detail:<input type="text" value={this.state.detailValue} onChange={this.changeDetail} />
  56.                 <button onClick={this.addTodo}>Add</button>
  57.             </div>
  58.  
  59.             <div className = "todoList">
  60.                 <table style={{border: "2px solid black"}}>
  61.                   <tbody>
  62.                     {todo}
  63.                   </tbody>
  64.                 </table>
  65.             </div>
  66.         );
  67.     }
  68. });
  69.  
  70. var Todo = React.createClass({
  71.     propTypes: {
  72.         title: React.PropTypes.string.isRequired
  73.     },
  74.  
  75.     getInitialState: function() {
  76.         return {
  77.             checked: false,
  78.             TodoStyle: style.notCheckedTodo
  79.         };
  80.     },
  81.  
  82.     handleChange: function(event) {
  83.         this.setState({
  84.             checked: event.target.checked
  85.         });
  86.  
  87.         // set the style
  88.         if (event.target.checked) {
  89.             this.setState({
  90.                 TodoStyle: style.checkedTodo
  91.             });
  92.         }
  93.         else {
  94.             this.setState({
  95.                 TodoStyle: style.notCheckedTodo
  96.             });
  97.         }
  98.     },
  99.  
  100.     render: function() {
  101.         return (
  102.             <tr style={this.state.TodoStyle}>
  103.                 <td style={style.tableContent}><input type="checkbox" checked={this.state.checked} onChange={this.handleChange}/></td>
  104.                 <td style={style.tableContent}>{this.props.title}</td>
  105.                 <td style={style.tableContent}>{this.props.children}</td>
  106.             </tr>
  107.         );
  108.     }
  109. })
  110.  
  111. var TodoForm = React.createClass({
  112.   // Write code here
  113.   render: function() {
  114.     return (
  115.         <div className= "todoForm">
  116.           I am a TodoForm.
  117.         </div>
  118.     );
  119.   }
  120. });
  121.  
  122. var style = {
  123.     checkedTodo: {
  124.         textDecoration: "line-through"
  125.     },
  126.  
  127.     notCheckedTodo: {
  128.         textDecoration: "none"
  129.     },
  130.  
  131.     tableContent: {
  132.         border: "1px solid black"
  133.     }
  134. };
  135.  
  136.  
  137.  
  138. module.exports = TodoBox;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement