ant-design/components/radio/group.tsx

163 lines
4.2 KiB
TypeScript
Raw Normal View History

import React, { PropTypes } from 'react';
2016-02-28 00:16:32 +08:00
import classNames from 'classnames';
import shallowEqual from 'shallowequal';
2017-03-28 11:56:38 +08:00
import Radio from './radio';
2015-07-17 16:06:46 +08:00
2015-07-20 22:36:49 +08:00
function getCheckedValue(children) {
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
}
2016-08-10 10:26:42 +08:00
export interface RadioGroupProps {
2016-11-03 11:33:26 +08:00
prefixCls?: string;
className?: string;
2016-08-10 10:26:42 +08:00
/** 选项变化时的回调函数*/
2016-10-19 17:51:33 +08:00
onChange?: React.FormEventHandler<any>;
2016-08-10 10:26:42 +08:00
/** 用于设置当前选中的值*/
value?: string | number;
/** 默认选中的值*/
defaultValue?: string | number;
/** 大小,只对按钮样式生效*/
size?: 'large' | 'default' | 'small';
style?: React.CSSProperties;
disabled?: boolean;
onMouseEnter?: React.FormEventHandler<any>;
onMouseLeave?: React.FormEventHandler<any>;
2017-03-28 11:56:38 +08:00
/** 以配置的方式设置 Radio 子元素,设置了此参数,会忽略 children */
options?: Array<string | {
label: string;
value: string;
disabled?: boolean;
}>;
2016-08-10 10:26:42 +08:00
}
export default class RadioGroup extends React.Component<RadioGroupProps, any> {
static defaultProps = {
disabled: false,
2016-07-13 11:14:24 +08:00
};
static childContextTypes = {
radioGroup: PropTypes.any,
};
constructor(props) {
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,
},
};
}
2015-07-20 22:36:49 +08:00
componentWillReceiveProps(nextProps) {
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
}
}
shouldComponentUpdate(nextProps, nextState, nextContext) {
return !shallowEqual(this.props, nextProps) ||
2017-03-28 11:56:38 +08:00
!shallowEqual(this.state, nextState) ||
!shallowEqual(this.context.group, nextContext.group);
2016-06-12 16:15:11 +08:00
}
onRadioChange = (ev) => {
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);
}
}
render() {
2016-01-21 22:45:21 +08:00
const props = this.props;
2017-03-28 11:56:38 +08:00
const { prefixCls = 'ant-radio-group', className = '' } = props;
const classString = classNames(prefixCls, {
2016-10-24 16:30:38 +08:00
[`${prefixCls}-${props.size}`]: props.size,
}, className);
2017-03-28 11:56:38 +08:00
let children: React.ReactChildren[] | React.ReactElement<any>[] | React.ReactNode = props.children;
// 如果存在 options, 优先使用
if (props.options && props.options.length > 0) {
children = props.options.map((option, index) => {
if (typeof option === 'string') { // 此处类型自动推导为 string
return (
<Radio
key={index}
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}
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}
>
{children}
</div>
);
}
}