fix: Radio.Group forward child element ref (#26555)

Co-authored-by: William Cai <williamcai@easyops.cn>
This commit is contained in:
willc001 2020-09-03 15:31:08 +08:00 committed by GitHub
parent 25f2bd66c8
commit b0c70ab348
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -153,6 +153,19 @@ describe('Radio Group', () => {
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
}); });
it('should forward ref', () => {
let radioGroupRef;
const wrapper = mount(
createRadioGroupByOption({
ref: ref => {
radioGroupRef = ref;
},
}),
);
expect(radioGroupRef).toBe(wrapper.children().getDOMNode());
});
describe('value is null or undefined', () => { describe('value is null or undefined', () => {
it('use `defaultValue` when `value` is undefined', () => { it('use `defaultValue` when `value` is undefined', () => {
const options = [{ label: 'Bamboo', value: 'bamboo' }]; const options = [{ label: 'Bamboo', value: 'bamboo' }];

View File

@ -7,7 +7,7 @@ import { ConfigContext } from '../config-provider';
import SizeContext from '../config-provider/SizeContext'; import SizeContext from '../config-provider/SizeContext';
import { RadioGroupContextProvider } from './context'; import { RadioGroupContextProvider } from './context';
const RadioGroup: React.FC<RadioGroupProps> = props => { const RadioGroup = React.forwardRef<HTMLDivElement, RadioGroupProps>((props, ref) => {
const { getPrefixCls, direction } = React.useContext(ConfigContext); const { getPrefixCls, direction } = React.useContext(ConfigContext);
const size = React.useContext(SizeContext); const size = React.useContext(SizeContext);
@ -96,6 +96,7 @@ const RadioGroup: React.FC<RadioGroupProps> = props => {
onMouseEnter={onMouseEnter} onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave} onMouseLeave={onMouseLeave}
id={id} id={id}
ref={ref}
> >
{childrenToRender} {childrenToRender}
</div> </div>
@ -114,7 +115,7 @@ const RadioGroup: React.FC<RadioGroupProps> = props => {
{renderGroup()} {renderGroup()}
</RadioGroupContextProvider> </RadioGroupContextProvider>
); );
}; });
RadioGroup.defaultProps = { RadioGroup.defaultProps = {
buttonStyle: 'outline' as RadioGroupButtonStyle, buttonStyle: 'outline' as RadioGroupButtonStyle,