You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.7 KiB

import * as React from 'react';
import * as PropTypes from 'prop-types';
import RcSwitch from 'rc-switch';
import classNames from 'classnames';
import omit from 'omit.js';
import Wave from '../_util/wave';
import Icon from '../icon';
10 years ago
export interface SwitchProps {
prefixCls?: string;
size?: 'small' | 'default';
className?: string;
checked?: boolean;
defaultChecked?: boolean;
onChange?: (checked: boolean) => any;
checkedChildren?: React.ReactNode;
unCheckedChildren?: React.ReactNode;
disabled?: boolean;
loading?: boolean;
}
export default class Switch extends React.Component<SwitchProps, {}> {
static defaultProps = {
prefixCls: 'ant-switch',
};
static propTypes = {
prefixCls: PropTypes.string,
// HACK: https://github.com/ant-design/ant-design/issues/5368
// size=default and size=large are the same
size: PropTypes.oneOf(['small', 'default', 'large']),
className: PropTypes.string,
};
private rcSwitch: typeof RcSwitch;
focus() {
this.rcSwitch.focus();
}
blur() {
this.rcSwitch.blur();
}
saveSwitch = (node: typeof RcSwitch) => {
this.rcSwitch = node;
}
10 years ago
render() {
const { prefixCls, size, loading, className = '' } = this.props;
const classes = classNames(className, {
[`${prefixCls}-small`]: size === 'small',
[`${prefixCls}-loading`]: loading,
});
const loadingIcon = loading ? (
<Icon
6 years ago
type="loading"
className={`${prefixCls}-loading-icon`}
/>
) : null;
return (
<Wave insertExtraNode>
<RcSwitch
{...omit(this.props, ['loading'])}
className={classes}
ref={this.saveSwitch}
6 years ago
loadingIcon={loadingIcon}
/>
</Wave>
);
10 years ago
}
}