Browse Source

Merge pull request #56 from ant-design/radio

fix radiogroup api
pull/57/head
yiminghe 9 years ago
parent
commit
911d89b0d5
  1. 10
      components/radio/demo/radiogroup.md
  2. 48
      components/radio/group.jsx
  3. 3
      components/radio/index.md

10
components/radio/demo/radiogroup.md

@ -13,24 +13,24 @@ var RadioGroup = antd.RadioGroup;
var App = React.createClass({ var App = React.createClass({
getInitialState: function () { getInitialState: function () {
return { return {
selectedValue:"a" value:"a"
}; };
}, },
onChange(ev) { onChange(ev) {
console.log('radio checked:' + ev.target.value); console.log('radio checked:' + ev.target.value);
this.setState({ this.setState({
selectedValue:ev.target.value value:ev.target.value
}) })
}, },
render() { render() {
return<div> return<div>
<RadioGroup onChange={this.onChange}> <RadioGroup onChange={this.onChange} value={this.state.value}>
<Radio value="a" checked={true}>A</Radio> <Radio value="a">A</Radio>
<Radio value="b" >B</Radio> <Radio value="b" >B</Radio>
<Radio value="c" >C</Radio> <Radio value="c" >C</Radio>
<Radio value="d" disabled={true}>D</Radio> <Radio value="d" disabled={true}>D</Radio>
</RadioGroup> </RadioGroup>
你选中的:&nbsp;&nbsp;{this.state.selectedValue} 你选中的:&nbsp;&nbsp;{this.state.value}
</div> </div>
} }
}); });

48
components/radio/group.jsx

@ -1,32 +1,46 @@
var React = require('react'); var React = require('react');
var Radio = require('./index'); 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({ var AntRadioGroup = React.createClass({
getDefaultProps: function () { getDefaultProps: function () {
return { return {
prefixCls: 'ant-radio-group' prefixCls: 'ant-radio-group',
onChange: function () {
}
}; };
}, },
getInitialState: function () { getInitialState: function () {
var value = null; var props = this.props;
this.props.children.forEach(function (radio) {
if (radio.props && radio.props.checked) {
value = radio.props.value;
}
return false;
});
return { 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 () { render: function () {
var self = this; var props = this.props;
var props = self.props; var children = props.children.map((radio) => {
var children = props.children.map(function (radio) {
if (radio.props) { if (radio.props) {
return <Radio {...radio.props} return <Radio
onChange={self.onRadioChange} key={radio.props.value}
checked={self.state.selectedValue === radio.props.value} {...radio.props}
onChange={this.onRadioChange}
checked={this.state.value === radio.props.value}
/>; />;
} }
return radio; return radio;
@ -39,12 +53,10 @@ var AntRadioGroup = React.createClass({
}, },
onRadioChange: function (ev) { onRadioChange: function (ev) {
this.setState({ this.setState({
selectedValue: ev.target.value value: ev.target.value
}); });
if (typeof this.props.onChange === 'function') {
this.props.onChange(ev); this.props.onChange(ev);
} }
}
}); });
module.exports = AntRadioGroup; module.exports = AntRadioGroup;

3
components/radio/index.md

@ -30,3 +30,6 @@
| 参数 | 说明 | 类型 | 必选值 | 默认值 | | 参数 | 说明 | 类型 | 必选值 | 默认值 |
|----------------|------------------------------------------|------------|---------|--------| |----------------|------------------------------------------|------------|---------|--------|
| onChange | 变化时回调函数,组合时必须 | Function(e:Event) | false | null | | onChange | 变化时回调函数,组合时必须 | Function(e:Event) | false | null |
| value | 当前值 | | |
| defaultValue | 初始值 | | |

Loading…
Cancel
Save