-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cars.jsx
53 lines (48 loc) · 1.29 KB
/
Cars.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
import React from 'react';
import Tracker from 'tracker-component';
import "/imports/models";
class Cars extends Tracker.Component {
constructor(props) {
super(props);
this.state = {
brand: this.props.brand
};
this.autorun(() => {
this.subscribe( 'models', this.state.brand );
});
this.autorun(() => {
this.setState({
ready: this.subscriptionsReady(),
cars: Models.find({ brand: this.state.brand }).fetch()
});
});
}
handleChange() {
this.setState({brand: this.refs.brand.value});
}
render() {
let {cars = []} = this.state;
let selectBrand = this.handleChange.bind(this);
let brands = ["Volvo", "Tesla", "DeLorean"];
return (
<div>
<select ref="brand" onChange={selectBrand} defaultValue={this.state.selected}>
{brands.map((brand, i) =>
<option value={brand} key={i}>{brand}</option>
)}
</select>
<ul className={["cars",
this.state.ready ? "ready" : ""].join(' ')}>
{cars.map((car, i) =>
<li className="car" key={i}>{`${car.brand} ${car.model}`}</li>
)}
</ul>
</div>
);
}
}
Cars.propTypes = {
brand: React.PropTypes.string
};
Cars.defaultProps = { brand: 'Volvo' };
export default Cars;