GithubHelp home page GithubHelp logo

react-radio-group's Introduction

React-radio-group

This is your average radios group:

<form>
  <input type="radio" name="fruit" value="apple" />Apple
  <input type="radio" name="fruit" value="orange" />Orange
  <input type="radio" name="fruit" value="watermelon" />Watermelon
</form>

Repetitive, hard to manipulate and easily desynchronized. Lift up name, give the group an initial checked value, and optionally remove the form tag:

<RadioGroup name="fruit" value="orange">
  <input type="radio" value="apple" />Apple
  <input type="radio" value="orange" />Orange
  <input type="radio" value="watermelon" />Watermelon
</RadioGroup>

Listen for changes, get the new value as intuitively as possible:

<RadioGroup name="fruit" value="orange" ref="fruitsGroup" onChange={this.handleChange}>
// 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):

<script src="path/to/react-radio-group.js"></script>

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 (
      <RadioGroup
        name="veggy"
        value={this.state.value}
        ref="veggiesGroup"
        onChange={this.handleChange}
      >
        <div>
          <label>
            <input type="radio" value="celery"/>Celery
          </label>
          <label>
            <input type="radio" value="potato"/>Potato
          </label>
          <label>
            <input type="radio" value="broccoli"/>Broccoli
          </label>
        </div>
      </RadioGroup>
    );
  },

  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(<Demo/>, document.body);

License

MIT.

react-radio-group's People

Contributors

chenglou avatar chantastic avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.