2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2018-08-07 21:07:52 +08:00
|
|
|
import * as PropTypes from 'prop-types';
|
2016-02-28 00:16:32 +08:00
|
|
|
import classNames from 'classnames';
|
2017-03-28 11:56:38 +08:00
|
|
|
import Radio from './radio';
|
2018-12-07 20:02:01 +08:00
|
|
|
import {
|
|
|
|
RadioGroupProps,
|
|
|
|
RadioGroupState,
|
|
|
|
RadioChangeEvent,
|
|
|
|
RadioGroupButtonStyle,
|
|
|
|
} from './interface';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2020-01-03 13:38:16 +08:00
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
2015-07-17 16:06:46 +08:00
|
|
|
|
2017-11-21 15:12:03 +08:00
|
|
|
function getCheckedValue(children: React.ReactNode) {
|
2016-02-20 16:39:49 +08:00
|
|
|
let value = null;
|
2016-02-21 14:04:08 +08:00
|
|
|
let matched = false;
|
2016-08-10 10:26:42 +08:00
|
|
|
React.Children.forEach(children, (radio: any) => {
|
2016-03-04 12:27:58 +08:00
|
|
|
if (radio && radio.props && radio.props.checked) {
|
2016-02-20 16:39:49 +08:00
|
|
|
value = radio.props.value;
|
2016-02-21 14:04:08 +08:00
|
|
|
matched = true;
|
2015-07-20 22:36:49 +08:00
|
|
|
}
|
|
|
|
});
|
2016-02-21 14:04:08 +08:00
|
|
|
return matched ? { value } : undefined;
|
2015-07-20 22:36:49 +08:00
|
|
|
}
|
|
|
|
|
2020-02-25 16:23:34 +08:00
|
|
|
class RadioGroup extends React.PureComponent<RadioGroupProps, RadioGroupState> {
|
2016-03-28 23:21:47 +08:00
|
|
|
static defaultProps = {
|
2018-07-23 10:01:42 +08:00
|
|
|
buttonStyle: 'outline' as RadioGroupButtonStyle,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2017-03-23 17:19:50 +08:00
|
|
|
|
|
|
|
static childContextTypes = {
|
|
|
|
radioGroup: PropTypes.any,
|
|
|
|
};
|
|
|
|
|
2018-12-17 15:01:55 +08:00
|
|
|
static getDerivedStateFromProps(nextProps: RadioGroupProps) {
|
|
|
|
if ('value' in nextProps) {
|
|
|
|
return {
|
|
|
|
value: nextProps.value,
|
|
|
|
};
|
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
const checkedValue = getCheckedValue(nextProps.children);
|
|
|
|
if (checkedValue) {
|
|
|
|
return {
|
|
|
|
value: checkedValue.value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-17 15:01:55 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-11-21 15:12:03 +08:00
|
|
|
constructor(props: RadioGroupProps) {
|
2016-03-28 23:21:47 +08:00
|
|
|
super(props);
|
2016-02-20 16:39:49 +08:00
|
|
|
let value;
|
|
|
|
if ('value' in props) {
|
|
|
|
value = props.value;
|
|
|
|
} else if ('defaultValue' in props) {
|
|
|
|
value = props.defaultValue;
|
|
|
|
} else {
|
|
|
|
const checkedValue = getCheckedValue(props.children);
|
|
|
|
value = checkedValue && checkedValue.value;
|
|
|
|
}
|
2016-03-28 23:21:47 +08:00
|
|
|
this.state = {
|
2016-02-20 16:39:49 +08:00
|
|
|
value,
|
2015-07-17 16:06:46 +08:00
|
|
|
};
|
2016-03-28 23:21:47 +08:00
|
|
|
}
|
2017-03-23 17:19:50 +08:00
|
|
|
|
|
|
|
getChildContext() {
|
|
|
|
return {
|
|
|
|
radioGroup: {
|
|
|
|
onChange: this.onRadioChange,
|
|
|
|
value: this.state.value,
|
|
|
|
disabled: this.props.disabled,
|
2017-07-31 11:33:47 +08:00
|
|
|
name: this.props.name,
|
2017-03-23 17:19:50 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-08 20:34:40 +08:00
|
|
|
onRadioChange = (ev: RadioChangeEvent) => {
|
2016-12-19 10:52:47 +08:00
|
|
|
const lastValue = this.state.value;
|
|
|
|
const { value } = ev.target;
|
2016-02-20 16:39:49 +08:00
|
|
|
if (!('value' in this.props)) {
|
|
|
|
this.setState({
|
2016-12-19 10:52:47 +08:00
|
|
|
value,
|
2016-02-20 16:39:49 +08:00
|
|
|
});
|
|
|
|
}
|
2016-10-24 16:30:38 +08:00
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
const { onChange } = this.props;
|
2016-12-19 10:52:47 +08:00
|
|
|
if (onChange && value !== lastValue) {
|
2016-10-24 16:30:38 +08:00
|
|
|
onChange(ev);
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2019-01-18 18:12:56 +08:00
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
renderGroup = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
const { props } = this;
|
2020-01-03 13:38:16 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className = '',
|
|
|
|
options,
|
|
|
|
buttonStyle,
|
|
|
|
size: customizeSize,
|
|
|
|
} = props;
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('radio', customizePrefixCls);
|
2018-04-22 16:35:34 +08:00
|
|
|
const groupPrefixCls = `${prefixCls}-group`;
|
2019-08-05 18:38:10 +08:00
|
|
|
let { children } = props;
|
2017-03-28 11:56:38 +08:00
|
|
|
|
|
|
|
// 如果存在 options, 优先使用
|
2017-11-27 10:49:17 +08:00
|
|
|
if (options && options.length > 0) {
|
2019-08-05 18:38:10 +08:00
|
|
|
children = options.map(option => {
|
2018-12-07 20:02:01 +08:00
|
|
|
if (typeof option === 'string') {
|
|
|
|
// 此处类型自动推导为 string
|
2017-03-28 11:56:38 +08:00
|
|
|
return (
|
|
|
|
<Radio
|
2019-08-06 15:04:10 +08:00
|
|
|
key={option}
|
2018-04-22 16:35:34 +08:00
|
|
|
prefixCls={prefixCls}
|
2017-03-28 11:56:38 +08:00
|
|
|
disabled={this.props.disabled}
|
|
|
|
value={option}
|
|
|
|
checked={this.state.value === option}
|
|
|
|
>
|
|
|
|
{option}
|
|
|
|
</Radio>
|
|
|
|
);
|
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
// 此处类型自动推导为 { label: string value: string }
|
|
|
|
return (
|
|
|
|
<Radio
|
2019-08-06 15:04:10 +08:00
|
|
|
key={`radio-group-value-options-${option.value}`}
|
2019-08-05 18:38:10 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
disabled={option.disabled || this.props.disabled}
|
|
|
|
value={option.value}
|
|
|
|
checked={this.state.value === option.value}
|
2020-02-04 17:34:02 +08:00
|
|
|
style={option.style}
|
2019-08-05 18:38:10 +08:00
|
|
|
>
|
|
|
|
{option.label}
|
|
|
|
</Radio>
|
|
|
|
);
|
2017-03-28 11:56:38 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-14 11:52:18 +08:00
|
|
|
return (
|
2020-01-03 13:38:16 +08:00
|
|
|
<SizeContext.Consumer>
|
|
|
|
{size => {
|
|
|
|
const mergedSize = customizeSize || size;
|
|
|
|
const classString = classNames(
|
|
|
|
groupPrefixCls,
|
|
|
|
`${groupPrefixCls}-${buttonStyle}`,
|
|
|
|
{
|
|
|
|
[`${groupPrefixCls}-${mergedSize}`]: mergedSize,
|
|
|
|
[`${groupPrefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classString}
|
|
|
|
style={props.style}
|
|
|
|
onMouseEnter={props.onMouseEnter}
|
|
|
|
onMouseLeave={props.onMouseLeave}
|
|
|
|
id={props.id}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</SizeContext.Consumer>
|
2016-11-14 11:52:18 +08:00
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderGroup}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-03-28 23:21:47 +08:00
|
|
|
}
|
2018-12-17 15:01:55 +08:00
|
|
|
|
|
|
|
export default RadioGroup;
|