group.jsx 1.1 KB
Newer Older
J
jljsj 已提交
1
var React = require('react');
J
jljsj 已提交
2
var Radio = require('./index');
J
jljsj 已提交
3 4 5 6

var AntRadioGroup = React.createClass({
  getDefaultProps: function () {
    return {
J
jljsj 已提交
7
      prefixCls: 'ant-radio-group'
J
jljsj 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    };
  },
  getInitialState: function () {
    var value = null;
    this.props.children.forEach(function (radio) {
      if (radio.props && radio.props.checked) {
        value = radio.props.value;
      }
      return false;
    });
    return {
      selectedValue: value
    };
  },
  render: function () {
    var self = this;
    var props = self.props;
    var children = props.children.map(function (radio) {
      if (radio.props) {
J
jljsj 已提交
27 28 29 30
        return <Radio {...radio.props}
          onChange={self.onRadioChange}
          checked = {self.state.selectedValue === radio.props.value}
        />;
J
jljsj 已提交
31 32 33 34 35
      }
      return radio;
    });
    return (
      <div className={props.prefixCls}>
A
afc163 已提交
36
        {children}
J
jljsj 已提交
37 38 39 40 41 42 43 44 45 46 47 48
      </div>
    );
  },
  onRadioChange: function (ev) {
    this.setState({
      selectedValue: ev.target.value
    });
    this.props.onChange(ev);
  }
});

module.exports = AntRadioGroup;