react template
HTML
<script src="http://fb.me/react-with-addons-0.10.0.min.js"></script>
<script src="http://fb.me/react-js-fiddle-integration.js"></script>
CSS
ul.Choices {
list-style-type: none;
margin: 0;
padding: 0;
}
ul.Choices li {
float: left;
margin: 5px;
padding: 5px;
}
ul.Choices li.Selected {
background-color: blue;
border-radius: 5px;
color: white;
}
ul.Choices a {
color: blue;
display: block;
}
ul.Choices li.Selected a {
color: white;
}
JavaScript 1.7
/**
* @jsx React.DOM
*/
var Chooser = React.createClass({
propTypes: {
onChange: React.PropTypes.func
},
getDefaultProps: function() {
return {
onUpdate: function() {},
choices: []
}
},
getInitialState: function() {
return {
choice: this.props.initialChoice || this.props.choices[0].value
}
},
onClick: function(value, e) {
e.preventDefault();
this.props.onChange({
choice: value
}, true);
this.setState({
choice: value
});
},
render: function() {
var self = this,
cx = React.addons.classSet,
choices = this.props.choices,
selected = this.state.choice;
return (
<ul className="Choices">
{choices.map(function(choice) {
return <li key={choice.value} className={cx({'Selected': (choice.value === selected)})}><a href='#' onClick={self.onClick.bind(null, choice.value)}>{choice.label}</a></li>;
})}
</ul>
);
}
});
var choices = [
{value: 'hot', label: 'Hot'},
{value: 'lukewarm', label: 'Lukewarm'},
{value: 'cold', label: 'Cold'}
];
var Panel = React.createClass({
getInitialState: function() {
return {
selected: 'cold'
};
},
choiceChange: function(e) {
this.setState({
selected: e.choice
});
},
render: function() {
return (
<div>
<div>{'Selected: ' + this.state.selected}</div>
<Chooser choices={choices} onChange={this.choiceChange} />
</div>
);
}
});
React.renderComponent(<Panel/>, document.body);