mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-25 11:40:04 +08:00
4af2b62cb5
* Add some missing TypeScript prop definitition, clean up date-picker definitions * use DatePickerMode instead of redundant enumeration of modes * decade was missing from the list of modes See https://codesandbox.io/s/388xplj646 - activate DatePicker - click on year in header - click on years in header - "decade" is printed on footer as mode for renderExtraFooter() * add decade as possible value for mode prop
80 lines
2.2 KiB
TypeScript
Executable File
80 lines
2.2 KiB
TypeScript
Executable File
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';
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
|
|
|
export interface SwitchProps {
|
|
prefixCls?: string;
|
|
size?: 'small' | 'default';
|
|
className?: string;
|
|
checked?: boolean;
|
|
defaultChecked?: boolean;
|
|
onChange?: (checked: boolean, event: MouseEvent) => any;
|
|
onClick?: (checked: boolean, event: MouseEvent) => any;
|
|
checkedChildren?: React.ReactNode;
|
|
unCheckedChildren?: React.ReactNode;
|
|
disabled?: boolean;
|
|
loading?: boolean;
|
|
autoFocus?: boolean;
|
|
style?: React.CSSProperties;
|
|
title?: string;
|
|
}
|
|
|
|
export default class Switch extends React.Component<SwitchProps, {}> {
|
|
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']) as PropTypes.Requireable<
|
|
SwitchProps['size']
|
|
>,
|
|
className: PropTypes.string,
|
|
};
|
|
|
|
private rcSwitch: typeof RcSwitch;
|
|
|
|
focus() {
|
|
this.rcSwitch.focus();
|
|
}
|
|
|
|
blur() {
|
|
this.rcSwitch.blur();
|
|
}
|
|
|
|
saveSwitch = (node: typeof RcSwitch) => {
|
|
this.rcSwitch = node;
|
|
};
|
|
|
|
renderSwitch = ({ getPrefixCls }: ConfigConsumerProps) => {
|
|
const { prefixCls: customizePrefixCls, size, loading, className = '', disabled } = this.props;
|
|
const prefixCls = getPrefixCls('switch', customizePrefixCls);
|
|
const classes = classNames(className, {
|
|
[`${prefixCls}-small`]: size === 'small',
|
|
[`${prefixCls}-loading`]: loading,
|
|
});
|
|
const loadingIcon = loading ? (
|
|
<Icon type="loading" className={`${prefixCls}-loading-icon`} />
|
|
) : null;
|
|
return (
|
|
<Wave insertExtraNode>
|
|
<RcSwitch
|
|
{...omit(this.props, ['loading'])}
|
|
prefixCls={prefixCls}
|
|
className={classes}
|
|
disabled={disabled || loading}
|
|
ref={this.saveSwitch}
|
|
loadingIcon={loadingIcon}
|
|
/>
|
|
</Wave>
|
|
);
|
|
};
|
|
|
|
render() {
|
|
return <ConfigConsumer>{this.renderSwitch}</ConfigConsumer>;
|
|
}
|
|
}
|