-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact.js
88 lines (72 loc) · 2.04 KB
/
react.js
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
function Car(props) {
const classes = ['car']
if(props.car.active) {
classes.push('active')
}
return (
<div className={classes.join(' ')} onClick={props.onActive}>
<img className="car__img" src={props.car.img} />
<span className="car__name">{props.car.name}</span>
<span className="car__price">{props.car.price}</span>
</div>
)
}
class App extends React.Component {
state = {
cars: [
{ active: false, name: 'BMW', price: '2000', img: 'http://lorempixel.com/200/200/' },
{ active: false, name: 'Audi', price: '1500', img: 'http://lorempixel.com/201/200/' },
{ active: false, name: 'Mersedes', price: '2500', img: 'http://lorempixel.com/199/200/' },
{ active: false, name: 'Jaguar', price: '3000', img: 'http://lorempixel.com/200/201/ '}
],
visible: true,
titleApp: 'Title Application'
}
handleActive(name) {
const cars = this.state.cars.concat()
const car = cars.find(c => c.name === name)
car.active = !car.active
this.setState({ cars })
}
toggleHandler() {
this.setState({ visible: !this.state.visible })
}
changeTitleHandler(value) {
this.setState({
titleApp: value
})
}
renderCars() {
if (!this.state.visible) {
return null
}
return this.state.cars.map(car => {
return (
<Car
car={car}
key={car.name + Math.random()}
onActive={this.handleActive.bind(this, car.name)}
/>
)
})
}
render() {
return (
<div className="app">
<h1>{this.state.titleApp}</h1>
<button style={{ marginRight: 15 }} onClick={() => this.toggleHandler()}>Toggle</button>
<input
type="text"
placeholder="Change Title"
onChange={(event) => this.changeTitleHandler(event.target.value)}
/>
<hr />
<div className="car-wrapper">
{ this.renderCars() }
</div>
</div>
)
}
}
const domContainer = document.querySelector('#root');
ReactDOM.render(<App />, domContainer);