Created
July 10, 2014 08:25
-
-
Save n1k0/e752b674d2af672bb76f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="A sample react dropdown menu component" /> | |
<script src="//fb.me/react-with-addons-0.10.0.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<div id="menu"></div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @jsx React.DOM */ | |
var MenuItem = React.createClass({ | |
handleClick: function(event) { | |
this.props.action(this.props.option); | |
}, | |
render: function() { | |
return ( | |
<button onClick={this.handleClick}> | |
{this.props.option.name} | |
</button> | |
); | |
} | |
}); | |
var Menu = React.createClass({ | |
getInitialState: function() { | |
return { | |
opened: false, | |
options: this.props.options || [], | |
selected: this.props.selected | |
}; | |
}, | |
open: function(event) { | |
this.setState({open: true}); | |
}, | |
select: function(option) { | |
this.setState({open: false, selected: option}); | |
}, | |
render: function() { | |
if (!this.state.open) { | |
return ( | |
<MenuItem option={this.state.selected} action={this.open} /> | |
); | |
} | |
return ( | |
<ul> | |
{this.state.options.map(function(option, key) { | |
return ( | |
<li key={key}> | |
<MenuItem option={option} action={this.select} /> | |
</li> | |
); | |
}.bind(this))} | |
</ul> | |
); | |
} | |
}); | |
var options = [{name: "a", value: 1}, {name: "b", value: 2}]; | |
React.renderComponent( | |
<Menu options={options} selected={options[1]} select={console.log} />, | |
document.querySelector('#menu') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment