mirror of
https://github.com/ant-design/ant-design.git
synced 2025-08-06 16:06:28 +08:00
refactor: RadioGroup's cloneElement to context (#5453)
This commit is contained in:
parent
50e5cd3459
commit
ab6e521ea2
@ -1,7 +1,6 @@
|
|||||||
import React from 'react';
|
import React, { PropTypes } from 'react';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import PureRenderMixin from 'rc-util/lib/PureRenderMixin';
|
import shallowEqual from 'shallowequal';
|
||||||
import assign from 'object-assign';
|
|
||||||
|
|
||||||
function getCheckedValue(children) {
|
function getCheckedValue(children) {
|
||||||
let value = null;
|
let value = null;
|
||||||
@ -36,6 +35,11 @@ export default class RadioGroup extends React.Component<RadioGroupProps, any> {
|
|||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
disabled: false,
|
disabled: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static childContextTypes = {
|
||||||
|
radioGroup: PropTypes.any,
|
||||||
|
};
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
let value;
|
let value;
|
||||||
@ -51,6 +55,17 @@ export default class RadioGroup extends React.Component<RadioGroupProps, any> {
|
|||||||
value,
|
value,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getChildContext() {
|
||||||
|
return {
|
||||||
|
radioGroup: {
|
||||||
|
onChange: this.onRadioChange,
|
||||||
|
value: this.state.value,
|
||||||
|
disabled: this.props.disabled,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
componentWillReceiveProps(nextProps) {
|
componentWillReceiveProps(nextProps) {
|
||||||
if ('value' in nextProps) {
|
if ('value' in nextProps) {
|
||||||
this.setState({
|
this.setState({
|
||||||
@ -65,9 +80,13 @@ export default class RadioGroup extends React.Component<RadioGroupProps, any> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
shouldComponentUpdate(...args) {
|
|
||||||
return PureRenderMixin.shouldComponentUpdate.apply(this, args);
|
shouldComponentUpdate(nextProps, nextState, nextContext) {
|
||||||
|
return !shallowEqual(this.props, nextProps) ||
|
||||||
|
!shallowEqual(this.state, nextState) ||
|
||||||
|
!shallowEqual(this.context.group, nextContext.group);
|
||||||
}
|
}
|
||||||
|
|
||||||
onRadioChange = (ev) => {
|
onRadioChange = (ev) => {
|
||||||
const lastValue = this.state.value;
|
const lastValue = this.state.value;
|
||||||
const { value } = ev.target;
|
const { value } = ev.target;
|
||||||
@ -84,18 +103,7 @@ export default class RadioGroup extends React.Component<RadioGroupProps, any> {
|
|||||||
}
|
}
|
||||||
render() {
|
render() {
|
||||||
const props = this.props;
|
const props = this.props;
|
||||||
const children = !props.children ? [] : React.Children.map(props.children, (radio: any) => {
|
const { prefixCls = 'ant-radio-group', className = '', children } = props;
|
||||||
if (radio && radio.type && (radio.type.__ANT_RADIO || radio.type.__ANT_RADIO_BUTTON) && radio.props) {
|
|
||||||
return React.cloneElement(radio, assign({}, radio.props, {
|
|
||||||
onChange: this.onRadioChange,
|
|
||||||
checked: this.state.value === radio.props.value,
|
|
||||||
disabled: radio.props.disabled || this.props.disabled,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
return radio;
|
|
||||||
});
|
|
||||||
|
|
||||||
const { prefixCls = 'ant-radio-group', className = '' } = props;
|
|
||||||
const classString = classNames(prefixCls, {
|
const classString = classNames(prefixCls, {
|
||||||
[`${prefixCls}-${props.size}`]: props.size,
|
[`${prefixCls}-${props.size}`]: props.size,
|
||||||
}, className);
|
}, className);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import RcRadio from 'rc-radio';
|
import RcRadio from 'rc-radio';
|
||||||
import React from 'react';
|
import React, { PropTypes } from 'react';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import PureRenderMixin from 'rc-util/lib/PureRenderMixin';
|
import shallowEqual from 'shallowequal';
|
||||||
|
|
||||||
export interface RadioProps {
|
export interface RadioProps {
|
||||||
/** 指定当前是否选中*/
|
/** 指定当前是否选中*/
|
||||||
@ -20,23 +20,35 @@ export interface RadioProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default class Radio extends React.Component<RadioProps, any> {
|
export default class Radio extends React.Component<RadioProps, any> {
|
||||||
static __ANT_RADIO = true;
|
|
||||||
|
|
||||||
static Group: any;
|
static Group: any;
|
||||||
static Button: any;
|
static Button: any;
|
||||||
|
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
prefixCls: 'ant-radio',
|
prefixCls: 'ant-radio',
|
||||||
};
|
};
|
||||||
shouldComponentUpdate(...args) {
|
|
||||||
return PureRenderMixin.shouldComponentUpdate.apply(this, args);
|
static contextTypes = {
|
||||||
|
radioGroup: PropTypes.any,
|
||||||
|
};
|
||||||
|
|
||||||
|
shouldComponentUpdate(nextProps, nextState, nextContext) {
|
||||||
|
return !shallowEqual(this.props, nextProps) ||
|
||||||
|
!shallowEqual(this.state, nextState) ||
|
||||||
|
!shallowEqual(this.context.radioGroup, nextContext.radioGroup);
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { prefixCls, className, children, style, ...restProps } = this.props;
|
const { prefixCls, className, children, style, ...restProps } = this.props;
|
||||||
|
let radioProps: RadioProps = { ...restProps };
|
||||||
|
if (this.context.radioGroup) {
|
||||||
|
radioProps.onChange = this.context.radioGroup.onChange;
|
||||||
|
radioProps.checked = this.props.value === this.context.radioGroup.value;
|
||||||
|
radioProps.disabled = this.props.disabled || this.context.radioGroup.disabled;
|
||||||
|
}
|
||||||
const wrapperClassString = classNames({
|
const wrapperClassString = classNames({
|
||||||
[`${prefixCls}-wrapper`]: true,
|
[`${prefixCls}-wrapper`]: true,
|
||||||
[`${prefixCls}-wrapper-checked`]: restProps.checked,
|
[`${prefixCls}-wrapper-checked`]: radioProps.checked,
|
||||||
[`${prefixCls}-wrapper-disabled`]: restProps.disabled,
|
[`${prefixCls}-wrapper-disabled`]: radioProps.disabled,
|
||||||
}, className);
|
}, className);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -46,7 +58,10 @@ export default class Radio extends React.Component<RadioProps, any> {
|
|||||||
onMouseEnter={this.props.onMouseEnter}
|
onMouseEnter={this.props.onMouseEnter}
|
||||||
onMouseLeave={this.props.onMouseLeave}
|
onMouseLeave={this.props.onMouseLeave}
|
||||||
>
|
>
|
||||||
<RcRadio {...restProps} prefixCls={prefixCls} />
|
<RcRadio
|
||||||
|
{...radioProps}
|
||||||
|
prefixCls={prefixCls}
|
||||||
|
/>
|
||||||
{children !== undefined ? <span>{children}</span> : null}
|
{children !== undefined ? <span>{children}</span> : null}
|
||||||
</label>
|
</label>
|
||||||
);
|
);
|
||||||
|
@ -1,20 +1,33 @@
|
|||||||
import React from 'react';
|
import React, { PropTypes } from 'react';
|
||||||
import Radio from './radio';
|
import Radio from './radio';
|
||||||
|
|
||||||
export interface RadioButtonProps {
|
export interface RadioButtonProps {
|
||||||
value: string | number;
|
value: string | number;
|
||||||
style?: React.CSSProperties;
|
style?: React.CSSProperties;
|
||||||
|
disabled?: boolean;
|
||||||
|
checked?: boolean;
|
||||||
|
onChange?: (e: any) => any;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class RadioButton extends React.Component<RadioButtonProps, any> {
|
export default class RadioButton extends React.Component<RadioButtonProps, any> {
|
||||||
static __ANT_RADIO_BUTTON = true;
|
|
||||||
|
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
prefixCls: 'ant-radio-button',
|
prefixCls: 'ant-radio-button',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static contextTypes = {
|
||||||
|
radioGroup: PropTypes.any,
|
||||||
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
let radioProps: RadioButtonProps = { ...this.props };
|
||||||
|
if (this.context.radioGroup) {
|
||||||
|
radioProps.onChange = this.context.radioGroup.onChange;
|
||||||
|
radioProps.checked = this.props.value === this.context.radioGroup.value;
|
||||||
|
radioProps.disabled = this.props.disabled || this.context.radioGroup.disabled;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Radio {...this.props} />
|
<Radio {...radioProps} />
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user