From d13c76d7e4c45b9484d4da09b458af5067095511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=81=8F=E5=8F=B3?= Date: Tue, 26 May 2020 22:26:51 +0800 Subject: [PATCH] refactor components/radio/group.tsx (#24480) --- components/radio/__tests__/group.test.js | 24 +++++----- components/radio/group.tsx | 56 +++++++++--------------- 2 files changed, 31 insertions(+), 49 deletions(-) diff --git a/components/radio/__tests__/group.test.js b/components/radio/__tests__/group.test.js index 5aa9adad82..f34b3543de 100644 --- a/components/radio/__tests__/group.test.js +++ b/components/radio/__tests__/group.test.js @@ -35,16 +35,10 @@ describe('Radio Group', () => { , ); - wrapper - .find('div') - .at(0) - .simulate('mouseenter'); + wrapper.find('div').at(0).simulate('mouseenter'); expect(onMouseEnter).toHaveBeenCalled(); - wrapper - .find('div') - .at(0) - .simulate('mouseleave'); + wrapper.find('div').at(0).simulate('mouseleave'); expect(onMouseLeave).toHaveBeenCalled(); }); @@ -186,18 +180,22 @@ describe('Radio Group', () => { const wrapper = mount( , ); - - expect(wrapper.state().value).toEqual('bamboo'); + expect(wrapper.find('.ant-radio-wrapper').at(0).hasClass('ant-radio-wrapper-checked')).toBe( + true, + ); }); [undefined, null].forEach(newValue => { it(`should set value back when value change back to ${newValue}`, () => { const options = [{ label: 'Bamboo', value: 'bamboo' }]; const wrapper = mount(); - expect(wrapper.state().value).toEqual('bamboo'); - + expect(wrapper.find('.ant-radio-wrapper').at(0).hasClass('ant-radio-wrapper-checked')).toBe( + true, + ); wrapper.setProps({ value: newValue }); - expect(wrapper.state().value).toEqual(newValue); + expect(wrapper.find('.ant-radio-wrapper').at(0).hasClass('ant-radio-wrapper-checked')).toBe( + false, + ); }); }); }); diff --git a/components/radio/group.tsx b/components/radio/group.tsx index 3eca001100..4b45f52ae3 100644 --- a/components/radio/group.tsx +++ b/components/radio/group.tsx @@ -11,18 +11,6 @@ import { ConfigConsumer, ConfigConsumerProps } from '../config-provider'; import SizeContext from '../config-provider/SizeContext'; import { RadioGroupContextProvider } from './context'; -function getCheckedValue(children: React.ReactNode) { - let value = null; - let matched = false; - React.Children.forEach(children, (radio: any) => { - if (radio && radio.props && radio.props.checked) { - value = radio.props.value; - matched = true; - } - }); - return matched ? { value } : undefined; -} - class RadioGroup extends React.PureComponent { static defaultProps = { buttonStyle: 'outline' as RadioGroupButtonStyle, @@ -35,11 +23,6 @@ class RadioGroup extends React.PureComponent { if (nextProps.value !== undefined || prevState.prevPropValue !== nextProps.value) { newState.value = nextProps.value; - } else { - const checkedValue = getCheckedValue(nextProps.children); - if (checkedValue) { - newState.value = checkedValue.value; - } } return newState; @@ -52,9 +35,6 @@ class RadioGroup extends React.PureComponent { value = props.value; } else if (props.defaultValue !== undefined) { value = props.defaultValue; - } else { - const checkedValue = getCheckedValue(props.children); - value = checkedValue && checkedValue.value; } this.state = { value, @@ -63,7 +43,7 @@ class RadioGroup extends React.PureComponent { } onRadioChange = (ev: RadioChangeEvent) => { - const lastValue = this.state.value; + const { value: lastValue } = this.state; const { value } = ev.target; if (!('value' in this.props)) { this.setState({ @@ -78,30 +58,35 @@ class RadioGroup extends React.PureComponent { }; renderGroup = ({ getPrefixCls, direction }: ConfigConsumerProps) => { - const { props } = this; const { prefixCls: customizePrefixCls, className = '', options, buttonStyle, + disabled, + children, size: customizeSize, - } = props; + style, + id, + onMouseEnter, + onMouseLeave, + } = this.props; + const { value } = this.state; const prefixCls = getPrefixCls('radio', customizePrefixCls); const groupPrefixCls = `${prefixCls}-group`; - let { children } = props; - + let childrenToRender = children; // 如果存在 options, 优先使用 if (options && options.length > 0) { - children = options.map(option => { + childrenToRender = options.map(option => { if (typeof option === 'string') { // 此处类型自动推导为 string return ( {option} @@ -112,9 +97,9 @@ class RadioGroup extends React.PureComponent { {option.label} @@ -136,16 +121,15 @@ class RadioGroup extends React.PureComponent { }, className, ); - return (
- {children} + {childrenToRender}
); }}