2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2017-05-09 13:40:33 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2016-02-28 00:16:32 +08:00
|
|
|
import classNames from 'classnames';
|
2017-03-23 17:19:50 +08:00
|
|
|
import shallowEqual from 'shallowequal';
|
2017-03-28 11:56:38 +08:00
|
|
|
import Radio from './radio';
|
2018-03-08 20:34:40 +08:00
|
|
|
import { RadioGroupProps, RadioGroupState, RadioChangeEvent } from './interface';
|
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
|
|
|
}
|
|
|
|
|
2017-11-21 15:12:03 +08:00
|
|
|
export default class RadioGroup extends React.Component<RadioGroupProps, RadioGroupState> {
|
2016-03-28 23:21:47 +08:00
|
|
|
static defaultProps = {
|
|
|
|
disabled: false,
|
2018-04-22 16:35:34 +08:00
|
|
|
prefixCls: 'ant-radio',
|
2018-06-28 14:59:19 +08:00
|
|
|
buttonStyle: 'outline',
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2017-03-23 17:19:50 +08:00
|
|
|
|
|
|
|
static childContextTypes = {
|
|
|
|
radioGroup: PropTypes.any,
|
|
|
|
};
|
|
|
|
|
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
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-21 15:12:03 +08:00
|
|
|
componentWillReceiveProps(nextProps: RadioGroupProps) {
|
2016-02-20 16:39:49 +08:00
|
|
|
if ('value' in nextProps) {
|
2015-07-20 22:36:49 +08:00
|
|
|
this.setState({
|
2016-02-21 14:04:08 +08:00
|
|
|
value: nextProps.value,
|
2015-07-20 22:36:49 +08:00
|
|
|
});
|
2016-02-20 16:39:49 +08:00
|
|
|
} else {
|
|
|
|
const checkedValue = getCheckedValue(nextProps.children);
|
|
|
|
if (checkedValue) {
|
|
|
|
this.setState({
|
2016-02-21 14:04:08 +08:00
|
|
|
value: checkedValue.value,
|
2016-02-20 16:39:49 +08:00
|
|
|
});
|
|
|
|
}
|
2015-07-20 22:36:49 +08:00
|
|
|
}
|
2016-03-28 23:21:47 +08:00
|
|
|
}
|
2017-03-23 17:19:50 +08:00
|
|
|
|
2017-11-21 15:12:03 +08:00
|
|
|
shouldComponentUpdate(nextProps: RadioGroupProps, nextState: RadioGroupState) {
|
2017-03-23 17:19:50 +08:00
|
|
|
return !shallowEqual(this.props, nextProps) ||
|
2017-04-14 16:20:09 +08:00
|
|
|
!shallowEqual(this.state, nextState);
|
2016-06-12 16:15:11 +08:00
|
|
|
}
|
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
|
|
|
|
|
|
|
const onChange = this.props.onChange;
|
2016-12-19 10:52:47 +08:00
|
|
|
if (onChange && value !== lastValue) {
|
2016-10-24 16:30:38 +08:00
|
|
|
onChange(ev);
|
|
|
|
}
|
2016-03-28 23:21:47 +08:00
|
|
|
}
|
2016-01-07 17:46:46 +08:00
|
|
|
render() {
|
2016-01-21 22:45:21 +08:00
|
|
|
const props = this.props;
|
2018-06-28 14:59:19 +08:00
|
|
|
const { prefixCls, className = '', options, buttonStyle } = props;
|
2018-04-22 16:35:34 +08:00
|
|
|
const groupPrefixCls = `${prefixCls}-group`;
|
2018-06-28 14:59:19 +08:00
|
|
|
const classString = classNames(groupPrefixCls, `${groupPrefixCls}-${buttonStyle}`, {
|
2018-04-22 16:35:34 +08:00
|
|
|
[`${groupPrefixCls}-${props.size}`]: props.size,
|
2016-11-30 10:20:23 +08:00
|
|
|
}, className);
|
2017-03-28 11:56:38 +08:00
|
|
|
|
|
|
|
let children: React.ReactChildren[] | React.ReactElement<any>[] | React.ReactNode = props.children;
|
|
|
|
|
|
|
|
// 如果存在 options, 优先使用
|
2017-11-27 10:49:17 +08:00
|
|
|
if (options && options.length > 0) {
|
|
|
|
children = options.map((option, index) => {
|
2017-03-28 11:56:38 +08:00
|
|
|
if (typeof option === 'string') { // 此处类型自动推导为 string
|
|
|
|
return (
|
|
|
|
<Radio
|
|
|
|
key={index}
|
2018-04-22 16:35:34 +08:00
|
|
|
prefixCls={prefixCls}
|
2017-03-28 11:56:38 +08:00
|
|
|
disabled={this.props.disabled}
|
|
|
|
value={option}
|
|
|
|
onChange={this.onRadioChange}
|
|
|
|
checked={this.state.value === option}
|
|
|
|
>
|
|
|
|
{option}
|
|
|
|
</Radio>
|
|
|
|
);
|
|
|
|
} else { // 此处类型自动推导为 { label: string value: string }
|
|
|
|
return (
|
|
|
|
<Radio
|
|
|
|
key={index}
|
2018-04-22 16:35:34 +08:00
|
|
|
prefixCls={prefixCls}
|
2017-03-28 11:56:38 +08:00
|
|
|
disabled={option.disabled || this.props.disabled}
|
|
|
|
value={option.value}
|
|
|
|
onChange={this.onRadioChange}
|
|
|
|
checked={this.state.value === option.value}
|
|
|
|
>
|
|
|
|
{option.label}
|
|
|
|
</Radio>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-14 11:52:18 +08:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classString}
|
|
|
|
style={props.style}
|
|
|
|
onMouseEnter={props.onMouseEnter}
|
|
|
|
onMouseLeave={props.onMouseLeave}
|
2017-11-27 10:49:17 +08:00
|
|
|
id={props.id}
|
2016-11-14 11:52:18 +08:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
2016-03-28 23:21:47 +08:00
|
|
|
}
|
|
|
|
}
|