mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-29 13:47:02 +08:00
Merge branch 'master' of github.com:ant-design/ant-design
This commit is contained in:
commit
a9a9f88466
@ -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>
|
||||
你选中的: {this.state.selectedValue}
|
||||
你选中的: {this.state.value}
|
||||
</div>
|
||||
}
|
||||
});
|
||||
|
@ -1,32 +1,46 @@
|
||||
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 | 初始值 | | |
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user