From 015738ffebf10697ac6593c3cd13db67244d634a Mon Sep 17 00:00:00 2001 From: yiminghe Date: Mon, 20 Jul 2015 22:36:49 +0800 Subject: [PATCH] fix radiogroup api --- components/radio/demo/radiogroup.md | 10 +++--- components/radio/group.jsx | 50 ++++++++++++++++++----------- components/radio/index.md | 3 ++ 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/components/radio/demo/radiogroup.md b/components/radio/demo/radiogroup.md index 659386814e..95b28087c3 100644 --- a/components/radio/demo/radiogroup.md +++ b/components/radio/demo/radiogroup.md @@ -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
- - A + + A B C D - 你选中的:  {this.state.selectedValue} + 你选中的:  {this.state.value}
} }); diff --git a/components/radio/group.jsx b/components/radio/group.jsx index aa143170be..5f43e478a1 100644 --- a/components/radio/group.jsx +++ b/components/radio/group.jsx @@ -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 ; } 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); } }); diff --git a/components/radio/index.md b/components/radio/index.md index 1f409e095b..09b40542cb 100644 --- a/components/radio/index.md +++ b/components/radio/index.md @@ -30,3 +30,6 @@ | 参数 | 说明 | 类型 | 必选值 | 默认值 | |----------------|------------------------------------------|------------|---------|--------| | onChange | 变化时回调函数,组合时必须 | Function(e:Event) | false | null | +| value | 当前值 | | | +| defaultValue | 初始值 | | | +