antd
Version:
An enterprise-class UI design language and React-based implementation
188 lines (154 loc) • 6.81 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _defineProperty2 = require('babel-runtime/helpers/defineProperty');
var _defineProperty3 = _interopRequireDefault(_defineProperty2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _react = require('react');
var React = _interopRequireWildcard(_react);
var _propTypes = require('prop-types');
var PropTypes = _interopRequireWildcard(_propTypes);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _shallowequal = require('shallowequal');
var _shallowequal2 = _interopRequireDefault(_shallowequal);
var _radio = require('./radio');
var _radio2 = _interopRequireDefault(_radio);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function getCheckedValue(children) {
var value = null;
var matched = false;
React.Children.forEach(children, function (radio) {
if (radio && radio.props && radio.props.checked) {
value = radio.props.value;
matched = true;
}
});
return matched ? { value: value } : undefined;
}
var RadioGroup = function (_React$Component) {
(0, _inherits3['default'])(RadioGroup, _React$Component);
function RadioGroup(props) {
(0, _classCallCheck3['default'])(this, RadioGroup);
var _this = (0, _possibleConstructorReturn3['default'])(this, (RadioGroup.__proto__ || Object.getPrototypeOf(RadioGroup)).call(this, props));
_this.onRadioChange = function (ev) {
var lastValue = _this.state.value;
var value = ev.target.value;
if (!('value' in _this.props)) {
_this.setState({
value: value
});
}
var onChange = _this.props.onChange;
if (onChange && value !== lastValue) {
onChange(ev);
}
};
var value = void 0;
if ('value' in props) {
value = props.value;
} else if ('defaultValue' in props) {
value = props.defaultValue;
} else {
var checkedValue = getCheckedValue(props.children);
value = checkedValue && checkedValue.value;
}
_this.state = {
value: value
};
return _this;
}
(0, _createClass3['default'])(RadioGroup, [{
key: 'getChildContext',
value: function getChildContext() {
return {
radioGroup: {
onChange: this.onRadioChange,
value: this.state.value,
disabled: this.props.disabled,
name: this.props.name
}
};
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
this.setState({
value: nextProps.value
});
} else {
var checkedValue = getCheckedValue(nextProps.children);
if (checkedValue) {
this.setState({
value: checkedValue.value
});
}
}
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(nextProps, nextState) {
return !(0, _shallowequal2['default'])(this.props, nextProps) || !(0, _shallowequal2['default'])(this.state, nextState);
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var props = this.props;
var prefixCls = props.prefixCls,
_props$className = props.className,
className = _props$className === undefined ? '' : _props$className,
options = props.options,
buttonStyle = props.buttonStyle;
var groupPrefixCls = prefixCls + '-group';
var classString = (0, _classnames2['default'])(groupPrefixCls, groupPrefixCls + '-' + buttonStyle, (0, _defineProperty3['default'])({}, groupPrefixCls + '-' + props.size, props.size), className);
var children = props.children;
// 如果存在 options, 优先使用
if (options && options.length > 0) {
children = options.map(function (option, index) {
if (typeof option === 'string') {
// 此处类型自动推导为 string
return React.createElement(
_radio2['default'],
{ key: index, prefixCls: prefixCls, disabled: _this2.props.disabled, value: option, onChange: _this2.onRadioChange, checked: _this2.state.value === option },
option
);
} else {
// 此处类型自动推导为 { label: string value: string }
return React.createElement(
_radio2['default'],
{ key: index, prefixCls: prefixCls, disabled: option.disabled || _this2.props.disabled, value: option.value, onChange: _this2.onRadioChange, checked: _this2.state.value === option.value },
option.label
);
}
});
}
return React.createElement(
'div',
{ className: classString, style: props.style, onMouseEnter: props.onMouseEnter, onMouseLeave: props.onMouseLeave, id: props.id },
children
);
}
}]);
return RadioGroup;
}(React.Component);
exports['default'] = RadioGroup;
RadioGroup.defaultProps = {
disabled: false,
prefixCls: 'ant-radio',
buttonStyle: 'outline'
};
RadioGroup.childContextTypes = {
radioGroup: PropTypes.any
};
module.exports = exports['default'];
;