import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
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';
var _jsxFileName = 'src/radio/RadioGroup.jsx';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

/**
 * RadioGroup
 */

var RadioGroup = function (_Component) {
    _inherits(RadioGroup, _Component);

    function RadioGroup(props) {
        _classCallCheck(this, RadioGroup);

        var _this = _possibleConstructorReturn(this, (RadioGroup.__proto__ || Object.getPrototypeOf(RadioGroup)).call(this, props));

        _this.onItemChange = function (itemValue) {
            return function (e) {
                if (!_this.isControlled) {
                    _this.setState({ value: itemValue });
                }
                if (_this.props.onChange) {
                    _this.props.onChange(e, itemValue);
                }
            };
        };

        _this.isControlled = props.value != null;
        if (!_this.isControlled) {
            _this.state = {
                value: props.defaultValue
            };
        }
        return _this;
    }

    _createClass(RadioGroup, [{
        key: 'componentWillReceiveProps',
        value: function componentWillReceiveProps(newProps) {
            if (!this.isControlled && newProps.value != null) {
                this.isControlled = true;
                this.setState({
                    value: null
                });
            }
            if (!this.isControlled && newProps.defaultValue !== this.props.defaultValue) {
                this.setState({
                    value: newProps.defaultValue
                });
            }
        }
    }, {
        key: 'render',
        value: function render() {
            var _this2 = this;

            var _props = this.props,
                prefixCls = _props.prefixCls,
                className = _props.className,
                propsValue = _props.value,
                defaultValue = _props.defaultValue,
                disabled = _props.disabled,
                name = _props.name,
                onChange = _props.onChange,
                children = _props.children,
                error = _props.error,
                inputProps = _props.inputProps,
                size = _props.size,
                other = _objectWithoutProperties(_props, ['prefixCls', 'className', 'value', 'defaultValue', 'disabled', 'name', 'onChange', 'children', 'error', 'inputProps', 'size']);

            var value = this.isControlled ? propsValue : this.state.value;
            var items = React.Children.map(children, function (child, index) {
                if (!React.isValidElement(child)) {
                    return null;
                }
                var checked = void 0;
                checked = child.props.value === value;

                return React.cloneElement(child, {
                    key: child.props.value || index,
                    checked: checked,
                    size: size,
                    name: name || child.props.name,
                    disabled: disabled || child.props.disabled,
                    onChange: _this2.onItemChange(child.props.value)
                });
            });
            var classes = classNames(prefixCls, _defineProperty({}, prefixCls + '-disabled', disabled), className);

            return React.createElement(
                'div',
                Object.assign({ className: classes }, other, {
                    __source: {
                        fileName: _jsxFileName,
                        lineNumber: 105
                    },
                    __self: this
                }),
                items
            );
        }
    }]);

    return RadioGroup;
}(Component);

RadioGroup.propTypes = {
    prefixCls: PropTypes.string,
    /** 子组件name */
    name: PropTypes.string,
    /** 是否禁用 */
    disabled: PropTypes.bool,
    /** 当前值（适用于受控组件） */
    value: PropTypes.oneOfType([PropTypes.bool, PropTypes.string, PropTypes.number]),
    /** 默认值 */
    defaultValue: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
    className: PropTypes.string,
    /** 选中状态改变时的回调 function(e, value) */
    onChange: PropTypes.func,
    /** 尺寸 */
    size: PropTypes.oneOf(['small', 'medium', 'large'])
};
RadioGroup.defaultProps = {
    prefixCls: 'ne-radio-group',
    disabled: false
};
export default RadioGroup;