提交 015738ff 编写于 作者: Y yiminghe

fix radiogroup api

上级 53763092
......@@ -13,24 +13,24 @@ var RadioGroup = antd.RadioGroup;
var App = React.createClass({
getInitialState: function () {
return {
selectedValue:"a"
value:"a"
};
},
onChange(ev) {
console.log('radio checked:' + ev.target.value);
this.setState({
selectedValue:ev.target.value
value:ev.target.value
})
},
render() {
return<div>
<RadioGroup onChange={this.onChange}>
<Radio value="a" checked={true}>A</Radio>
<RadioGroup onChange={this.onChange} value={this.state.value}>
<Radio value="a">A</Radio>
<Radio value="b" >B</Radio>
<Radio value="c" >C</Radio>
<Radio value="d" disabled={true}>D</Radio>
</RadioGroup>
你选中的:&nbsp;&nbsp;{this.state.selectedValue}
你选中的:&nbsp;&nbsp;{this.state.value}
</div>
}
});
......
var React = require('react');
var Radio = require('./index');
function getCheckedValue(children) {
var checkedValue = null;
children.forEach(function (radio) {
if (radio.props && radio.props.checked) {
checkedValue = radio.props.value;
}
});
return checkedValue;
}
var AntRadioGroup = React.createClass({
getDefaultProps: function () {
return {
prefixCls: 'ant-radio-group'
prefixCls: 'ant-radio-group',
onChange: function () {
}
};
},
getInitialState: function () {
var value = null;
this.props.children.forEach(function (radio) {
if (radio.props && radio.props.checked) {
value = radio.props.value;
}
return false;
});
var props = this.props;
return {
selectedValue: value
value: props.value || props.defaultValue || getCheckedValue(props.children)
};
},
componentWillReceiveProps(nextProps) {
if ('value' in nextProps || getCheckedValue(nextProps.children)) {
this.setState({
value: nextProps.value || getCheckedValue(nextProps.children)
});
}
},
render: function () {
var self = this;
var props = self.props;
var children = props.children.map(function (radio) {
var props = this.props;
var children = props.children.map((radio) => {
if (radio.props) {
return <Radio {...radio.props}
onChange={self.onRadioChange}
checked={self.state.selectedValue === radio.props.value}
return <Radio
key={radio.props.value}
{...radio.props}
onChange={this.onRadioChange}
checked={this.state.value === radio.props.value}
/>;
}
return radio;
......@@ -39,11 +53,9 @@ var AntRadioGroup = React.createClass({
},
onRadioChange: function (ev) {
this.setState({
selectedValue: ev.target.value
value: ev.target.value
});
if (typeof this.props.onChange === 'function') {
this.props.onChange(ev);
}
this.props.onChange(ev);
}
});
......
......@@ -30,3 +30,6 @@
| 参数 | 说明 | 类型 | 必选值 | 默认值 |
|----------------|------------------------------------------|------------|---------|--------|
| onChange | 变化时回调函数,组合时必须 | Function(e:Event) | false | null |
| value | 当前值 | | |
| defaultValue | 初始值 | | |
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册