This repository has been archived by the owner on Dec 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.jsx
119 lines (111 loc) · 2.46 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var React = require('react');
var BookForm = React.createClass({
propTypes: {
onBook: React.PropTypes.func.isRequired
},
getInitialState: function() {
return {
title: '',
read: false
};
},
changeTitle: function(ev) {
this.setState({
title: ev.target.value
});
},
changeRead: function() {
this.setState({
read: !this.state.read
});
},
addBook: function(ev) {
ev.preventDefault();
this.props.onBook({
title: this.state.title,
read: this.state.read
});
this.setState({
title: '',
read: false
});
},
render: function() {
return (
<form onSubmit={this.addBook}>
<div>
<label htmlFor='title'>Title</label>
<div><input type='text' id='title' value={this.state.title} onChange={this.changeTitle} placeholder='Title' /></div>
</div>
<div>
<label htmlFor='title'>Read</label>
<div><input type='checkbox' id='read' checked={this.state.read} onChange={this.changeRead} /></div>
</div>
<div>
<button type='submit'>Add Book</button>
</div>
</form>
);
}
});
var Books = React.createClass({
propTypes: {
books: React.PropTypes.array
},
getInitialState: function() {
return {
books: (this.props.books || [])
};
},
onBook: function(book) {
this.state.books.push(book);
this.setState({
books: this.state.books
});
},
render: function() {
var books = this.state.books.map(function(book) {
return <Book title={book.title} read={book.read}></Book>;
});
return (
<div>
<BookForm onBook={this.onBook}></BookForm>
<table>
<thead>
<tr>
<th>Title</th>
<th>Read</th>
</tr>
</thead>
<tbody>{books}</tbody>
</table>
</div>
);
}
});
var Book = React.createClass({
propTypes: {
title: React.PropTypes.string.isRequired,
read: React.PropTypes.bool.isRequired
},
getInitialState: function() {
return {
title: this.props.title,
read: this.props.read
};
},
handleChange: function(ev) {
this.setState({
read: !this.state.read
});
},
render: function() {
return (
<tr>
<td>{this.props.title}</td>
<td><input type='checkbox' checked={this.state.read} onChange={this.handleChange} /></td>
</tr>
);
}
});
module.exports = Books;