|
|
@ -5,9 +5,9 @@ import omit from 'omit.js';
|
|
|
|
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
|
|
|
|
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
|
|
|
|
|
|
|
|
|
|
|
|
import Wave from '../_util/wave';
|
|
|
|
import Wave from '../_util/wave';
|
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import warning from '../_util/warning';
|
|
|
|
|
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
|
|
|
|
|
|
|
import warning from '../_util/warning';
|
|
|
|
|
|
|
|
|
|
|
|
export type SwitchSize = 'small' | 'default';
|
|
|
|
export type SwitchSize = 'small' | 'default';
|
|
|
|
export type SwitchChangeEventHandler = (checked: boolean, event: MouseEvent) => void;
|
|
|
|
export type SwitchChangeEventHandler = (checked: boolean, event: MouseEvent) => void;
|
|
|
@ -30,72 +30,51 @@ export interface SwitchProps {
|
|
|
|
title?: string;
|
|
|
|
title?: string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default class Switch extends React.Component<SwitchProps, {}> {
|
|
|
|
interface CompoundedComponent
|
|
|
|
static __ANT_SWITCH = true;
|
|
|
|
extends React.ForwardRefExoticComponent<SwitchProps & React.RefAttributes<HTMLElement>> {
|
|
|
|
|
|
|
|
__ANT_SWITCH: boolean;
|
|
|
|
private rcSwitch: typeof RcSwitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
constructor(props: SwitchProps) {
|
|
|
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
warning(
|
|
|
|
const Switch = React.forwardRef<unknown, SwitchProps>((props, ref) => {
|
|
|
|
'checked' in props || !('value' in props),
|
|
|
|
warning(
|
|
|
|
'Switch',
|
|
|
|
'checked' in props || !('value' in props),
|
|
|
|
'`value` is not a valid prop, do you mean `checked`?',
|
|
|
|
'Switch',
|
|
|
|
);
|
|
|
|
'`value` is not a valid prop, do you mean `checked`?',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
saveSwitch = (node: typeof RcSwitch) => {
|
|
|
|
const {
|
|
|
|
this.rcSwitch = node;
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
};
|
|
|
|
size: customizeSize,
|
|
|
|
|
|
|
|
loading,
|
|
|
|
|
|
|
|
className = '',
|
|
|
|
|
|
|
|
disabled,
|
|
|
|
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
|
|
|
|
focus() {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
this.rcSwitch.focus();
|
|
|
|
const size = React.useContext(SizeContext);
|
|
|
|
}
|
|
|
|
const prefixCls = getPrefixCls('switch', customizePrefixCls);
|
|
|
|
|
|
|
|
const loadingIcon = loading ? <LoadingOutlined className={`${prefixCls}-loading-icon`} /> : null;
|
|
|
|
|
|
|
|
|
|
|
|
blur() {
|
|
|
|
const classes = classNames(className, {
|
|
|
|
this.rcSwitch.blur();
|
|
|
|
[`${prefixCls}-small`]: (customizeSize || size) === 'small',
|
|
|
|
}
|
|
|
|
[`${prefixCls}-loading`]: loading,
|
|
|
|
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
renderSwitch = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
|
|
|
return (
|
|
|
|
const {
|
|
|
|
<Wave insertExtraNode>
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
<RcSwitch
|
|
|
|
size: customizeSize,
|
|
|
|
{...omit(props, ['loading'])}
|
|
|
|
loading,
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
className = '',
|
|
|
|
className={classes}
|
|
|
|
disabled,
|
|
|
|
disabled={disabled || loading}
|
|
|
|
} = this.props;
|
|
|
|
ref={ref}
|
|
|
|
const prefixCls = getPrefixCls('switch', customizePrefixCls);
|
|
|
|
loadingIcon={loadingIcon}
|
|
|
|
const loadingIcon = loading ? (
|
|
|
|
/>
|
|
|
|
<LoadingOutlined className={`${prefixCls}-loading-icon`} />
|
|
|
|
</Wave>
|
|
|
|
) : null;
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
}) as CompoundedComponent;
|
|
|
|
<SizeContext.Consumer>
|
|
|
|
|
|
|
|
{size => {
|
|
|
|
|
|
|
|
const classes = classNames(className, {
|
|
|
|
|
|
|
|
[`${prefixCls}-small`]: (customizeSize || size) === 'small',
|
|
|
|
|
|
|
|
[`${prefixCls}-loading`]: loading,
|
|
|
|
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
Switch.__ANT_SWITCH = true;
|
|
|
|
<Wave insertExtraNode>
|
|
|
|
|
|
|
|
<RcSwitch
|
|
|
|
|
|
|
|
{...omit(this.props, ['loading'])}
|
|
|
|
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
|
|
|
|
className={classes}
|
|
|
|
|
|
|
|
disabled={disabled || loading}
|
|
|
|
|
|
|
|
ref={this.saveSwitch}
|
|
|
|
|
|
|
|
loadingIcon={loadingIcon}
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
</Wave>
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
</SizeContext.Consumer>
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
export default Switch;
|
|
|
|
return <ConfigConsumer>{this.renderSwitch}</ConfigConsumer>;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|