React-radio-group
This is a fork of https://github.com/chenglou/react-radio-group updated to work with npm and commonjs.
This is your average radios group:
Apple Orange Watermelon
Repetitive, hard to manipulate and easily desynchronized.
Lift up name
, give the group an initial checked value, and optionally remove the form tag:
Apple Orange Watermelon
Listen for changes, get the new value as intuitively as possible:
// further... this.refs.fruitsGroup.getCheckedValue(); // => whatever's currently checked// handleChange is also passed the native onChange event, whose value// resides in event.target.value (see example below)
That's it for the API! See below for a complete example.
Install
bower install react-radio-group
Simply drop the script somewhere on your page (after React of course):
Example
Demo's almost as long as the whole source code.
/*** @jsx React.DOM*/var Demo = React.createClass({ getInitialState: function() { return {value: 'celery'}; }, componentDidMount: function() { // change the selected radio to "Potato" in one second setTimeout(function() { this.setState({value: 'potato'}); }.bind(this), 1000); }, render: function() { // the radios can be arbitrarily deep. They will always be fetched and // attached the `name` attribute correctly. `value` is optional return ( Celery Potato Broccoli ); }, handleChange: function(event) { // will return the currently selected radio's value, or null if none // alternatively, use the passed parameter `event` var selectedVeggy = this.refs.veggiesGroup.getCheckedValue(); var sameVeggy = event.target.value; }}); React.renderComponent(, document.body);
License
MIT.