choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
215 lines (185 loc) • 6.23 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
function _createSuper(Derived) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
return function () {
var Super = _getPrototypeOf(Derived),
result;
if (isNativeReflectConstruct()) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
import React, { Children, Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import shallowEqual from 'lodash/isEqual';
import Radio from './radio';
import { getPrefixCls } from '../configure';
function getCheckedValue(children) {
var value = null;
var matched = false;
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 =
/*#__PURE__*/
function (_Component) {
_inherits(RadioGroup, _Component);
var _super = _createSuper(RadioGroup);
function RadioGroup(props) {
var _this;
_classCallCheck(this, RadioGroup);
_this = _super.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;
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;
}
_createClass(RadioGroup, [{
key: "getChildContext",
value: function getChildContext() {
var _this$props = this.props,
disabled = _this$props.disabled,
name = _this$props.name;
var value = this.state.value;
return {
radioGroup: {
onChange: this.onRadioChange,
value: value,
disabled: disabled,
name: 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 !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState);
}
}, {
key: "render",
value: function render() {
var _classNames2;
var props = this.props;
var customizePrefixCls = props.prefixCls,
_props$className = props.className,
className = _props$className === void 0 ? '' : _props$className,
options = props.options,
size = props.size,
label = props.label,
disabled = props.disabled;
var value = this.state.value;
var prefixCls = getPrefixCls('radio-group', customizePrefixCls);
var classString = classNames(prefixCls, _defineProperty({}, "".concat(prefixCls, "-").concat(size), size), className);
var wrapperClassString = classNames((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-wrapper"), true), _defineProperty(_classNames2, "".concat(prefixCls, "-has-label"), label), _classNames2));
var labelClassString = classNames("".concat(prefixCls, "-label"), {
'label-disabled': disabled
});
var children = props.children; // 如果存在 options, 优先使用
if (options && options.length > 0) {
children = options.map(function (option, index) {
if (typeof option === 'string') {
// 此处类型自动推导为 string
return React.createElement(Radio, {
key: String(index),
disabled: disabled,
value: option,
checked: value === option
}, option);
} // 此处类型自动推导为 { label: string value: string }
return React.createElement(Radio, {
key: String(index),
disabled: option.disabled || disabled,
value: option.value,
checked: value === option.value
}, option.label);
});
}
return React.createElement("div", {
className: wrapperClassString
}, props.label ? React.createElement("span", {
className: labelClassString
}, props.label) : null, React.createElement("div", {
className: classString,
style: props.style,
onMouseEnter: props.onMouseEnter,
onMouseLeave: props.onMouseLeave,
id: props.id
}, children));
}
}]);
return RadioGroup;
}(Component);
export { RadioGroup as default };
RadioGroup.displayName = 'RadioGroup';
RadioGroup.defaultProps = {
disabled: false
};
RadioGroup.childContextTypes = {
radioGroup: PropTypes.any
};
//# sourceMappingURL=group.js.map