Skip to content

Instantly share code, notes, and snippets.

@n1k0
Created July 10, 2014 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n1k0/e752b674d2af672bb76f to your computer and use it in GitHub Desktop.
Save n1k0/e752b674d2af672bb76f to your computer and use it in GitHub Desktop.
<!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>
/** @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