antd
Version:
An enterprise-class UI design language and React components implementation
137 lines (111 loc) • 5.79 kB
JavaScript
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import * as React from 'react';
import classNames from 'classnames';
import Radio from './radio';
import { ConfigContext } from '../config-provider';
import SizeContext from '../config-provider/SizeContext';
import { RadioGroupContextProvider } from './context';
var RadioGroup = function RadioGroup(props) {
var _React$useContext = React.useContext(ConfigContext),
getPrefixCls = _React$useContext.getPrefixCls,
direction = _React$useContext.direction;
var size = React.useContext(SizeContext);
var initValue;
if (props.value !== undefined) {
initValue = props.value;
} else if (props.defaultValue !== undefined) {
initValue = props.defaultValue;
}
var _React$useState = React.useState(initValue),
_React$useState2 = _slicedToArray(_React$useState, 2),
value = _React$useState2[0],
setValue = _React$useState2[1];
var _React$useState3 = React.useState(props.value),
_React$useState4 = _slicedToArray(_React$useState3, 2),
prevPropValue = _React$useState4[0],
setPrevPropValue = _React$useState4[1];
React.useEffect(function () {
setPrevPropValue(props.value);
if (props.value !== undefined || prevPropValue !== props.value) {
setValue(props.value);
}
}, [props.value]);
var onRadioChange = function onRadioChange(ev) {
var lastValue = value;
var val = ev.target.value;
if (!('value' in props)) {
setValue(val);
}
var onChange = props.onChange;
if (onChange && val !== lastValue) {
onChange(ev);
}
};
var renderGroup = function renderGroup() {
var _classNames;
var customizePrefixCls = props.prefixCls,
_props$className = props.className,
className = _props$className === void 0 ? '' : _props$className,
options = props.options,
buttonStyle = props.buttonStyle,
disabled = props.disabled,
children = props.children,
customizeSize = props.size,
style = props.style,
id = props.id,
onMouseEnter = props.onMouseEnter,
onMouseLeave = props.onMouseLeave;
var prefixCls = getPrefixCls('radio', customizePrefixCls);
var groupPrefixCls = "".concat(prefixCls, "-group");
var childrenToRender = children; // 如果存在 options, 优先使用
if (options && options.length > 0) {
childrenToRender = options.map(function (option) {
if (typeof option === 'string') {
// 此处类型自动推导为 string
return /*#__PURE__*/React.createElement(Radio, {
key: option,
prefixCls: prefixCls,
disabled: disabled,
value: option,
checked: value === option
}, option);
} // 此处类型自动推导为 { label: string value: string }
return /*#__PURE__*/React.createElement(Radio, {
key: "radio-group-value-options-".concat(option.value),
prefixCls: prefixCls,
disabled: option.disabled || disabled,
value: option.value,
checked: value === option.value,
style: option.style
}, option.label);
});
}
var mergedSize = customizeSize || size;
var classString = classNames(groupPrefixCls, "".concat(groupPrefixCls, "-").concat(buttonStyle), (_classNames = {}, _defineProperty(_classNames, "".concat(groupPrefixCls, "-").concat(mergedSize), mergedSize), _defineProperty(_classNames, "".concat(groupPrefixCls, "-rtl"), direction === 'rtl'), _classNames), className);
return /*#__PURE__*/React.createElement("div", {
className: classString,
style: style,
onMouseEnter: onMouseEnter,
onMouseLeave: onMouseLeave,
id: id
}, childrenToRender);
};
return /*#__PURE__*/React.createElement(RadioGroupContextProvider, {
value: {
onChange: onRadioChange,
value: value,
disabled: props.disabled,
name: props.name
}
}, renderGroup());
};
RadioGroup.defaultProps = {
buttonStyle: 'outline'
};
export default /*#__PURE__*/React.memo(RadioGroup);