ant-design/components/radio/group.tsx

170 lines
4.4 KiB
TypeScript
Raw Normal View History

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';
import shallowEqual from 'shallowequal';
import { polyfill } from 'react-lifecycles-compat';
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';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
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
}
class RadioGroup extends React.Component<RadioGroupProps, RadioGroupState> {
static defaultProps = {
disabled: false,
2018-07-23 10:01:42 +08:00
buttonStyle: 'outline' as RadioGroupButtonStyle,
2016-07-13 11:14:24 +08:00
};
static childContextTypes = {
radioGroup: PropTypes.any,
};
static getDerivedStateFromProps(nextProps: RadioGroupProps) {
if ('value' in nextProps) {
return {
value: nextProps.value,
};
} else {
const checkedValue = getCheckedValue(nextProps.children);
if (checkedValue) {
return {
value: checkedValue.value,
};
}
}
return null;
}
2017-11-21 15:12:03 +08:00
constructor(props: RadioGroupProps) {
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;
}
this.state = {
2016-02-20 16:39:49 +08:00
value,
2015-07-17 16:06:46 +08:00
};
}
getChildContext() {
return {
radioGroup: {
onChange: this.onRadioChange,
value: this.state.value,
disabled: this.props.disabled,
name: this.props.name,
},
};
}
2017-11-21 15:12:03 +08:00
shouldComponentUpdate(nextProps: RadioGroupProps, nextState: RadioGroupState) {
2018-12-07 20:02:01 +08:00
return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState);
2016-06-12 16:15:11 +08:00
}
onRadioChange = (ev: RadioChangeEvent) => {
const lastValue = this.state.value;
const { value } = ev.target;
2016-02-20 16:39:49 +08:00
if (!('value' in this.props)) {
this.setState({
value,
2016-02-20 16:39:49 +08:00
});
}
2016-10-24 16:30:38 +08:00
const onChange = this.props.onChange;
if (onChange && value !== lastValue) {
2016-10-24 16:30:38 +08:00
onChange(ev);
}
2018-12-07 20:02:01 +08:00
};
renderGroup = ({ getPrefixCls }: ConfigConsumerProps) => {
2016-01-21 22:45:21 +08:00
const props = this.props;
const { prefixCls: customizePrefixCls, className = '', options, buttonStyle } = props;
const prefixCls = getPrefixCls('radio', customizePrefixCls);
const groupPrefixCls = `${prefixCls}-group`;
2018-12-07 20:02:01 +08:00
const classString = classNames(
groupPrefixCls,
`${groupPrefixCls}-${buttonStyle}`,
{
[`${groupPrefixCls}-${props.size}`]: props.size,
},
className,
);
2017-03-28 11:56:38 +08:00
2018-12-07 20:02:01 +08:00
let children: React.ReactChildren[] | React.ReactElement<any>[] | React.ReactNode =
props.children;
2017-03-28 11:56:38 +08:00
// 如果存在 options, 优先使用
2017-11-27 10:49:17 +08:00
if (options && options.length > 0) {
children = options.map((option, index) => {
2018-12-07 20:02:01 +08:00
if (typeof option === 'string') {
// 此处类型自动推导为 string
2017-03-28 11:56:38 +08:00
return (
<Radio
key={index}
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>
);
2018-12-07 20:02:01 +08:00
} else {
// 此处类型自动推导为 { label: string value: string }
2017-03-28 11:56:38 +08:00
return (
<Radio
key={index}
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>
);
}
});
}
return (
<div
className={classString}
style={props.style}
onMouseEnter={props.onMouseEnter}
onMouseLeave={props.onMouseLeave}
2017-11-27 10:49:17 +08:00
id={props.id}
>
{children}
</div>
);
2018-12-07 20:02:01 +08:00
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderGroup}</ConfigConsumer>;
}
}
polyfill(RadioGroup);
export default RadioGroup;