ant-design/components/radio/group.tsx

89 lines
2.5 KiB
TypeScript
Raw Normal View History

2016-07-07 20:25:03 +08:00
import * as React from 'react';
2016-02-28 00:16:32 +08:00
import classNames from 'classnames';
2016-03-04 12:27:58 +08:00
import Radio from './radio';
import RadioButton from './radioButton';
import PureRenderMixin from 'react-addons-pure-render-mixin';
2016-06-22 13:18:43 +08:00
import assign from 'object-assign';
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;
React.Children.forEach(children, (radio) => {
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
}
export default class RadioGroup extends React.Component {
static defaultProps = {
prefixCls: 'ant-radio-group',
disabled: false,
onChange() {
},
2016-07-13 11:14:24 +08:00
};
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
};
}
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
}
}
2016-06-12 16:15:11 +08:00
shouldComponentUpdate(...args) {
return PureRenderMixin.shouldComponentUpdate.apply(this, args);
}
onRadioChange = (ev) => {
2016-02-20 16:39:49 +08:00
if (!('value' in this.props)) {
this.setState({
2016-02-21 14:04:08 +08:00
value: ev.target.value,
2016-02-20 16:39:49 +08:00
});
}
2016-01-21 22:45:21 +08:00
this.props.onChange(ev);
}
render() {
2016-01-21 22:45:21 +08:00
const props = this.props;
const children = React.Children.map(props.children, (radio) => {
2016-03-04 12:27:58 +08:00
if (radio && (radio.type === Radio || radio.type === RadioButton) && radio.props) {
2016-02-20 16:39:49 +08:00
const keyProps = {};
if (!('key' in radio) && typeof radio.props.value === 'string') {
keyProps.key = radio.props.value;
}
2016-06-22 13:18:43 +08:00
return React.cloneElement(radio, assign({}, keyProps, radio.props, {
2016-01-21 22:45:21 +08:00
onChange: this.onRadioChange,
checked: this.state.value === radio.props.value,
disabled: radio.props.disabled || this.props.disabled,
2016-06-22 13:18:43 +08:00
}));
2015-07-17 16:06:46 +08:00
}
return radio;
});
2016-02-28 00:16:32 +08:00
const classString = classNames({
[props.prefixCls]: true,
[`${props.prefixCls}-${props.size}`]: props.size,
});
return <div className={classString} style={props.style}>{children}</div>;
}
}