-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.jsx
More file actions
111 lines (96 loc) · 2.42 KB
/
script.jsx
File metadata and controls
111 lines (96 loc) · 2.42 KB
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
var Product = React.createClass({
getInitialState: function() {
return {qty: 0};
},
buy: function() {
this.setState({qty: this.state.qty + 1});
this.props.handleTotal(this.props.price);
},
show: function() {
this.props.handleShow(this.props.name);
},
render: function() {
return (
<div>
<p>{this.props.name} - ${this.props.price}</p>
<button onClick={this.buy}>Buy</button>
<button onClick={this.show}>Show</button>
<h3>Qty: {this.state.qty} item(s)</h3>
<hr/>
</div>
);
}
});
var Total = React.createClass({
render: function() {
return (
<div>
<h3>Total Cash: {this.props.total}</h3>
</div>
);
}
});
var ProductForm = React.createClass({
submit: function(e) {
e.preventDefault();
var product = {
name: this.refs.name.value,
price: parseInt(this.refs.price.value)
}
this.props.handleCreate(product);
this.refs.name.value = "";
this.refs.price.value = "";
},
render: function() {
return(
<form onSubmit={this.submit}>
<input type="text" placeholder="Product Name" ref="name"/> -
<input type="text" placeholder="Product Price" ref="price"/>
<br/><br/>
<button>Create Product</button>
<hr/>
</form>
);
}
});
var ProductList = React.createClass({
getInitialState: function() {
return {
total: 0,
productList: [
{name: "Android", price: 123},
{name: "Apple", price: 321},
{name: "Nokia", price: 432}
]
};
},
createProduct: function(product) {
this.setState({
productList: this.state.productList.concat(product)
});
},
calculateTotal: function(price) {
this.setState({total: this.state.total + price});
},
showProduct : function(name) {
alert("You selected " + name);
},
render: function() {
var component = this;
var products = this.state.productList.map(function(product) {
return (
<Product name={product.name} price={product.price}
handleShow={component.showProduct}
handleTotal={component.calculateTotal}/>
);
});
return (
<div>
<ProductForm handleCreate={this.createProduct} />
{products}
<Total total={this.state.total}/>
</div>
);
}
});
React.render(<ProductList/>, document.getElementById("root"));