UNPKG

antd

Version:

An enterprise-class UI design language and React components implementation

104 lines 4.08 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import classNames from 'classnames'; import useMergedState from "rc-util/es/hooks/useMergedState"; import * as React from 'react'; import { ConfigContext } from '../config-provider'; import SizeContext from '../config-provider/SizeContext'; import getDataOrAriaProps from '../_util/getDataOrAriaProps'; import { RadioGroupContextProvider } from './context'; import Radio from './radio'; import useStyle from './style'; var RadioGroup = /*#__PURE__*/React.forwardRef(function (props, ref) { var _classNames; var _React$useContext = React.useContext(ConfigContext), getPrefixCls = _React$useContext.getPrefixCls, direction = _React$useContext.direction; var size = React.useContext(SizeContext); var _useMergedState = useMergedState(props.defaultValue, { value: props.value }), _useMergedState2 = _slicedToArray(_useMergedState, 2), value = _useMergedState2[0], setValue = _useMergedState2[1]; 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 customizePrefixCls = props.prefixCls, _props$className = props.className, className = _props$className === void 0 ? '' : _props$className, options = props.options, _props$buttonStyle = props.buttonStyle, buttonStyle = _props$buttonStyle === void 0 ? 'outline' : _props$buttonStyle, disabled = props.disabled, children = props.children, customizeSize = props.size, style = props.style, id = props.id, onMouseEnter = props.onMouseEnter, onMouseLeave = props.onMouseLeave, onFocus = props.onFocus, onBlur = props.onBlur; var prefixCls = getPrefixCls('radio', customizePrefixCls); var groupPrefixCls = prefixCls + "-group"; // Style var _useStyle = useStyle(prefixCls), _useStyle2 = _slicedToArray(_useStyle, 2), wrapSSR = _useStyle2[0], hashId = _useStyle2[1]; var childrenToRender = children; // 如果存在 options, 优先使用 if (options && options.length > 0) { childrenToRender = options.map(function (option) { if (typeof option === 'string' || typeof option === 'number') { // 此处类型自动推导为 string return /*#__PURE__*/React.createElement(Radio, { key: option.toString(), prefixCls: prefixCls, disabled: disabled, value: option, checked: value === option }, option); } // 此处类型自动推导为 { label: string value: string } return /*#__PURE__*/React.createElement(Radio, { key: "radio-group-value-options-" + 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, groupPrefixCls + "-" + buttonStyle, (_classNames = {}, _defineProperty(_classNames, groupPrefixCls + "-" + mergedSize, mergedSize), _defineProperty(_classNames, groupPrefixCls + "-rtl", direction === 'rtl'), _classNames), className, hashId); return wrapSSR( /*#__PURE__*/React.createElement("div", _extends({}, getDataOrAriaProps(props), { className: classString, style: style, onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave, onFocus: onFocus, onBlur: onBlur, id: id, ref: ref }), /*#__PURE__*/React.createElement(RadioGroupContextProvider, { value: { onChange: onRadioChange, value: value, disabled: props.disabled, name: props.name, optionType: props.optionType } }, childrenToRender))); }); export default /*#__PURE__*/React.memo(RadioGroup);