Skip to content

Commit 3625a8d

Browse files
committed
exploring state and events - part 6
1 parent 3e8921a commit 3625a8d

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

dropdown/src/dropdown.jsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,26 @@ module.exports = React.createClass({
1313
getInitialState: function(){
1414
return { open: false }
1515
},
16+
handleItemClick: function(item) {
17+
this.setState({
18+
open: false,
19+
itemTitle: item
20+
});
21+
},
1622
render: function() {
1723
var list = this.props.items.map(function(item){
18-
return <ListItem item={item} />
19-
});
24+
return <ListItem
25+
item={item}
26+
whenItemClicked={this.handleItemClick}
27+
className={this.state.itemTitle === item ? "active" : "" }
28+
/>
29+
}.bind(this));
2030

2131
return <div className="dropdown">
2232
<Button
2333
whenClicked={this.handleClick}
2434
className="btn-default"
25-
title={this.props.title}
35+
title={this.state.itemTitle || this.props.title}
2636
subTitleClassName="caret"
2737
/>
2838
<ul className={"dropdown-menu " + (this.state.open ? "show" : "") }>

dropdown/src/list-item.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
var React = require('react');
22

33
module.exports = React.createClass({
4+
handleClick: function() {
5+
this.props.whenItemClicked(this.props.item);
6+
},
47
render: function() {
5-
return <li><a>{this.props.item}</a></li>
8+
return <li className={this.props.className}>
9+
<a onClick={this.handleClick}>{this.props.item}</a>
10+
</li>
611
}
712
});

0 commit comments

Comments
 (0)