|
| 1 | +/** @jsx React.DOM */ |
| 2 | + |
| 3 | +var _ = require('lodash'); |
| 4 | +var React = require('./react'); |
| 5 | +var socketMixin = require('./socket-mixin')('http://carl.local:3000'); |
| 6 | + |
| 7 | +var person = React.createClass({ |
| 8 | + |
| 9 | + updateValue: function (e) { |
| 10 | + this.state.person.name = e.target.value; |
| 11 | + this.setState({ person: this.state.person }); |
| 12 | + this.props.onChange(this.state.person); |
| 13 | + }, |
| 14 | + |
| 15 | + componentWillReceiveProps: function (props) { |
| 16 | + this.setState({ |
| 17 | + person: props.person |
| 18 | + }); |
| 19 | + }, |
| 20 | + |
| 21 | + getInitialState: function () { |
| 22 | + return { |
| 23 | + person: this.props.person |
| 24 | + }; |
| 25 | + }, |
| 26 | + |
| 27 | + render: function () { |
| 28 | + return ( |
| 29 | + <div> |
| 30 | + <h2>{this.state.name}</h2> |
| 31 | + <input type='text' onChange={this.updateValue} value={this.state.person.name}/> |
| 32 | + </div> |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | +}); |
| 37 | + |
| 38 | +var people = React.createClass({ |
| 39 | + |
| 40 | + mixins: [socketMixin], |
| 41 | + |
| 42 | + updateValue: function (e) { |
| 43 | + this.setState({ name: e.target.value }); |
| 44 | + }, |
| 45 | + |
| 46 | + add: function () { |
| 47 | + this.state.people.push({ name: this.state.name }); |
| 48 | + this.setState({ people: this.state.people, name: '' }); |
| 49 | + }, |
| 50 | + |
| 51 | + remove: function (person) { |
| 52 | + this.state.people.splice(this.state.people.indexOf(person), 1) |
| 53 | + this.setState({ people: this.state.people }); |
| 54 | + }, |
| 55 | + |
| 56 | + change: function (person) { |
| 57 | + this.setState(this.state); |
| 58 | + }, |
| 59 | + |
| 60 | + getInitialState: function () { |
| 61 | + return { |
| 62 | + people: [{ id: 0, name: 'Jimi' }, { id: 1, name: 'Stevie' }, { id: 2, name: 'eric' } ], |
| 63 | + name: '' |
| 64 | + }; |
| 65 | + }, |
| 66 | + |
| 67 | + render: function () { |
| 68 | + |
| 69 | + var list = this.state.people.map(function (data) { |
| 70 | + return ( |
| 71 | + <li> |
| 72 | + <person person={data} onChange={this.change}></person> |
| 73 | + <button onClick={this.remove.bind(this, data)}>Remove</button> |
| 74 | + </li> |
| 75 | + ); |
| 76 | + }.bind(this)); |
| 77 | + |
| 78 | + return ( |
| 79 | + <div> |
| 80 | + <ul>{list}</ul> |
| 81 | + <input type='text' onChange={this.updateValue} value={this.state.name}/> |
| 82 | + <button onClick={this.add}>Add</button> |
| 83 | + </div> |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | +}); |
| 88 | + |
| 89 | +document.addEventListener('DOMContentLoaded', function () { |
| 90 | + React.renderComponent(<people></people>, document.getElementById('content')); |
| 91 | +}); |
0 commit comments