refactor: ts radio, ref: #2639

This commit is contained in:
Benjy Cui 2016-08-10 10:26:42 +08:00
parent 77e47c5807
commit d3d6faf2f3
4 changed files with 47 additions and 6 deletions

View File

@ -34,6 +34,9 @@ export { Cascader };
import LocaleProvider from './locale-provider';
export { LocaleProvider };
import message from './message';
export { message };
import Popconfirm from './popconfirm';
export { Popconfirm };
@ -43,6 +46,9 @@ export { Popover };
import Progress from './progress';
export { Progress };
import Radio from './radio';
export { Radio };
import Rate from './rate';
export { Rate };

View File

@ -7,7 +7,7 @@ import assign from 'object-assign';
function getCheckedValue(children) {
let value = null;
let matched = false;
React.Children.forEach(children, (radio) => {
React.Children.forEach(children, (radio: any) => {
if (radio && radio.props && radio.props.checked) {
value = radio.props.value;
matched = true;
@ -16,7 +16,21 @@ function getCheckedValue(children) {
return matched ? { value } : undefined;
}
export default class RadioGroup extends React.Component {
export interface RadioGroupProps {
/** 选项变化时的回调函数*/
onChange?: React.FormEventHandler;
/** 用于设置当前选中的值*/
value?: string | number;
/** 默认选中的值*/
defaultValue?: string | number;
/** 大小,只对按钮样式生效*/
size?: 'large' | 'default' | 'small';
style?: React.CSSProperties;
prefixCls?: string;
disabled?: boolean;
}
export default class RadioGroup extends React.Component<RadioGroupProps, any> {
static defaultProps = {
prefixCls: 'ant-radio-group',
disabled: false,
@ -65,11 +79,11 @@ export default class RadioGroup extends React.Component {
}
render() {
const props = this.props;
const children = React.Children.map(props.children, (radio) => {
const children = React.Children.map(props.children, (radio: any) => {
if (radio && (radio.type === Radio || radio.type === RadioButton) && radio.props) {
const keyProps = {};
if (!('key' in radio) && typeof radio.props.value === 'string') {
keyProps.key = radio.props.value;
(keyProps as any).key = radio.props.value;
}
return React.cloneElement(radio, assign({}, keyProps, radio.props, {
onChange: this.onRadioChange,

View File

@ -3,7 +3,23 @@ import * as React from 'react';
import classNames from 'classnames';
import PureRenderMixin from 'react-addons-pure-render-mixin';
export default class Radio extends React.Component {
export interface RadioProps {
/** 指定当前是否选中*/
checked?: boolean;
/** 初始是否选中*/
defaultChecked?: boolean;
/** 根据 value 进行比较,判断是否选中 */
value?: string | number;
style?: React.CSSProperties;
prefixCls?: string;
disabled?: boolean;
className?: string;
}
export default class Radio extends React.Component<RadioProps, any> {
static Group: any;
static Button: any;
static defaultProps = {
prefixCls: 'ant-radio',
};

View File

@ -1,7 +1,12 @@
import * as React from 'react';
import Radio from './radio';
export default class RadioButton extends React.Component {
export interface RadioButtonProps {
value: string | number;
style?: React.CSSProperties;
}
export default class RadioButton extends React.Component<RadioButtonProps, any> {
static defaultProps = {
prefixCls: 'ant-radio-button',
};