-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.jsx
More file actions
101 lines (97 loc) · 2.4 KB
/
script.jsx
File metadata and controls
101 lines (97 loc) · 2.4 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
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>
<p>Quantity: {this.state.qty} item{this.state.qty>1 ? 's': ''}</p>
</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.handleAddProduct(product);
this.refs.name.value = "";
this.refs.price.value = 0;
},
render: function() {
return (
<form onSubmit={this.submit}>
<input type="text" placeholder="Product Name" ref="name" /> -
<input type="text" placeholder="Product Price" ref="price" />
<br/>
<button>Create Product</button>
</form>
);
}
});
var ProductList = React.createClass({
getInitialState: function() {
return {
total: 0,
productList: [
{name: "Samsung", price: 189.99},
{name: "iPhone", price: 199.99},
{name: "Nokia", price: 200.99}
]
};
},
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 handleAddProduct={this.createProduct} />
{products}
<Total total={this.state.total} />
</div>
);
}
});
React.render(<ProductList/>, document.getElementById("root"));