UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

202 lines (176 loc) 6.2 kB
import _extends from "@babel/runtime/helpers/extends"; 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 _createSuper from "@babel/runtime/helpers/createSuper"; import React, { Children, Component } from 'react'; import classNames from 'classnames'; import shallowEqual from 'shallowequal'; import Radio from './radio'; import ConfigContext from '../config-provider/ConfigContext'; import { RadioContextProvider } from './RadioContext'; 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, context) { var _this; _classCallCheck(this, RadioGroup); _this = _super.call(this, props, context); _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: "getContextValue", value: function getContextValue() { 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 _this2 = this; var props = this.props; var customizePrefixCls = props.prefixCls, _props$className = props.className, className = _props$className === void 0 ? '' : _props$className, options = props.options, buttonStyle = props.buttonStyle, size = props.size, label = props.label, disabled = props.disabled; var value = this.state.value; var getPrefixCls = this.context.getPrefixCls; var prefixCls = getPrefixCls('radio', customizePrefixCls); var groupPrefixCls = "".concat(prefixCls, "-group"); var classString = classNames(groupPrefixCls, "".concat(groupPrefixCls, "-").concat(buttonStyle), _defineProperty({}, "".concat(groupPrefixCls, "-").concat(size), size), className); var children = props.children; // 如果存在 options, 优先使用 if (options && options.length > 0) { children = options.map(function (option, index) { if (typeof option === 'string') { // 此处类型自动推导为 string return /*#__PURE__*/React.createElement(Radio, { key: String(index), prefixCls: prefixCls, disabled: disabled, value: option, onChange: _this2.onRadioChange, checked: value === option }, option); } // 此处类型自动推导为 { label: string value: string } return /*#__PURE__*/React.createElement(Radio, { key: String(index), prefixCls: prefixCls, disabled: option.disabled || disabled, value: option.value, onChange: _this2.onRadioChange, checked: value === option.value }, option.label); }); } var group = /*#__PURE__*/React.createElement("div", { className: classString, style: props.style, onMouseEnter: props.onMouseEnter, onMouseLeave: props.onMouseLeave, id: props.id }, /*#__PURE__*/React.createElement(RadioContextProvider, _extends({}, this.getContextValue(), { getPrefixCls: getPrefixCls }), children)); if (label) { var _classNames2; var wrapperClassString = classNames((_classNames2 = {}, _defineProperty(_classNames2, "".concat(groupPrefixCls, "-wrapper"), true), _defineProperty(_classNames2, "".concat(groupPrefixCls, "-has-label"), label), _classNames2)); var labelClassString = classNames("".concat(groupPrefixCls, "-label"), { 'label-disabled': disabled }); return /*#__PURE__*/React.createElement("div", { className: wrapperClassString }, /*#__PURE__*/React.createElement("span", { className: labelClassString }, label), group); } return group; } }], [{ key: "contextType", get: function get() { return ConfigContext; } }]); return RadioGroup; }(Component); export { RadioGroup as default }; RadioGroup.displayName = 'RadioGroup'; RadioGroup.defaultProps = { disabled: false, buttonStyle: 'outline' }; //# sourceMappingURL=group.js.map