import _defineProperty from"babel-runtime/helpers/defineProperty";import _classCallCheck from"babel-runtime/helpers/classCallCheck";import _createClass from"babel-runtime/helpers/createClass";import _possibleConstructorReturn from"babel-runtime/helpers/possibleConstructorReturn";import _inherits from"babel-runtime/helpers/inherits";import*as React from"react";import*as PropTypes from"prop-types";import classNames from"classnames";import shallowEqual from"shallowequal";import Radio from"./radio";function getCheckedValue(e){var a=null,t=!1;return React.Children.forEach(e,function(e){e&&e.props&&e.props.checked&&(a=e.props.value,t=!0)}),t?{value:a}:void 0}var RadioGroup=function(e){function a(e){_classCallCheck(this,a);var t=_possibleConstructorReturn(this,(a.__proto__||Object.getPrototypeOf(a)).call(this,e));t.onRadioChange=function(e){var a=t.state.value,o=e.target.value;"value"in t.props||t.setState({value:o});var r=t.props.onChange;r&&o!==a&&r(e)};var o=void 0;if("value"in e)o=e.value;else if("defaultValue"in e)o=e.defaultValue;else{var r=getCheckedValue(e.children);o=r&&r.value}return t.state={value:o},t}return _inherits(a,e),_createClass(a,[{key:"getChildContext",value:function(){return{radioGroup:{onChange:this.onRadioChange,value:this.state.value,disabled:this.props.disabled,name:this.props.name}}}},{key:"componentWillReceiveProps",value:function(e){if("value"in e)this.setState({value:e.value});else{var a=getCheckedValue(e.children);a&&this.setState({value:a.value})}}},{key:"shouldComponentUpdate",value:function(e,a){return!shallowEqual(this.props,e)||!shallowEqual(this.state,a)}},{key:"render",value:function(){var e=this,a=this.props,t=a.prefixCls,o=void 0===t?"dt-radio-group":t,r=a.className,l=void 0===r?"":r,s=a.options,i=classNames(o,_defineProperty({},o+"-"+a.size,a.size),l),n=a.children;return s&&s.length>0&&(n=s.map(function(a,t){return"string"==typeof a?React.createElement(Radio,{key:t,disabled:e.props.disabled,value:a,onChange:e.onRadioChange,checked:e.state.value===a},a):React.createElement(Radio,{key:t,disabled:a.disabled||e.props.disabled,value:a.value,onChange:e.onRadioChange,checked:e.state.value===a.value},a.label)})),React.createElement("div",{className:i,style:a.style,onMouseEnter:a.onMouseEnter,onMouseLeave:a.onMouseLeave,id:a.id},n)}}]),a}(React.Component);export default RadioGroup;RadioGroup.defaultProps={disabled:!1},RadioGroup.childContextTypes={radioGroup:PropTypes.any};