mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-11-30 11:08:45 +08:00
e4c5ab9c69
* Add a name property for RadioGroup * Add document for RadioGroup's name property * Add test case for RadioGroup name property
70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
import React from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import RcCheckbox from 'rc-checkbox';
|
|
import classNames from 'classnames';
|
|
import shallowEqual from 'shallowequal';
|
|
import { AbstractCheckboxProps } from '../checkbox/Checkbox';
|
|
import RadioGroup from './group';
|
|
import RadioButton from './radioButton';
|
|
|
|
export type RadioProps = AbstractCheckboxProps;
|
|
|
|
export default class Radio extends React.Component<RadioProps, any> {
|
|
static Group: typeof RadioGroup;
|
|
static Button: typeof RadioButton;
|
|
|
|
static defaultProps = {
|
|
prefixCls: 'ant-radio',
|
|
type: 'radio',
|
|
};
|
|
|
|
static contextTypes = {
|
|
radioGroup: PropTypes.any,
|
|
};
|
|
|
|
shouldComponentUpdate(nextProps, nextState, nextContext) {
|
|
return !shallowEqual(this.props, nextProps) ||
|
|
!shallowEqual(this.state, nextState) ||
|
|
!shallowEqual(this.context.radioGroup, nextContext.radioGroup);
|
|
}
|
|
|
|
render() {
|
|
const { props, context } = this;
|
|
const {
|
|
prefixCls,
|
|
className,
|
|
children,
|
|
style,
|
|
...restProps,
|
|
} = props;
|
|
const { radioGroup } = context;
|
|
let radioProps: RadioProps = { ...restProps };
|
|
if (radioGroup) {
|
|
radioProps.name = radioGroup.name;
|
|
radioProps.onChange = radioGroup.onChange;
|
|
radioProps.checked = props.value === radioGroup.value;
|
|
radioProps.disabled = props.disabled || radioGroup.disabled;
|
|
}
|
|
const wrapperClassString = classNames(className, {
|
|
[`${prefixCls}-wrapper`]: true,
|
|
[`${prefixCls}-wrapper-checked`]: radioProps.checked,
|
|
[`${prefixCls}-wrapper-disabled`]: radioProps.disabled,
|
|
});
|
|
|
|
return (
|
|
<label
|
|
className={wrapperClassString}
|
|
style={style}
|
|
onMouseEnter={props.onMouseEnter}
|
|
onMouseLeave={props.onMouseLeave}
|
|
>
|
|
<RcCheckbox
|
|
{...radioProps}
|
|
prefixCls={prefixCls}
|
|
/>
|
|
{children !== undefined ? <span>{children}</span> : null}
|
|
</label>
|
|
);
|
|
}
|
|
}
|