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.

278 lines
7.1 KiB

7 years ago
import * as React from 'react';
import * as PropTypes from 'prop-types';
import RcDrawer from 'rc-drawer';
import createReactContext, { Context } from '@ant-design/create-react-context';
6 years ago
import warning from '../_util/warning';
import classNames from 'classnames';
import Icon from '../icon';
import { withConfigConsumer, ConfigConsumerProps } from '../config-provider';
import { tuple } from '../_util/type';
const DrawerContext: Context<Drawer | null> = createReactContext(null);
7 years ago
type EventType = React.MouseEvent<HTMLDivElement> | React.MouseEvent<HTMLButtonElement>;
7 years ago
type getContainerFunc = () => HTMLElement;
7 years ago
const PlacementTypes = tuple('top', 'right', 'bottom', 'left');
type placementType = (typeof PlacementTypes)[number];
export interface DrawerProps {
7 years ago
closable?: boolean;
destroyOnClose?: boolean;
getContainer?: string | HTMLElement | getContainerFunc;
7 years ago
maskClosable?: boolean;
mask?: boolean;
maskStyle?: React.CSSProperties;
style?: React.CSSProperties;
bodyStyle?: React.CSSProperties;
7 years ago
title?: React.ReactNode;
visible?: boolean;
width?: number | string;
height?: number | string;
/* deprecated, use className instead */
7 years ago
wrapClassName?: string;
zIndex?: number;
prefixCls?: string;
push?: boolean;
placement?: placementType;
7 years ago
onClose?: (e: EventType) => void;
afterVisibleChange?: (visible: boolean) => void;
className?: string;
handler?: React.ReactNode;
7 years ago
}
export interface IDrawerState {
push?: boolean;
7 years ago
}
class Drawer extends React.Component<DrawerProps & ConfigConsumerProps, IDrawerState> {
7 years ago
static propTypes = {
closable: PropTypes.bool,
destroyOnClose: PropTypes.bool,
getContainer: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object as PropTypes.Requireable<HTMLElement>,
PropTypes.func,
PropTypes.bool,
]),
7 years ago
maskClosable: PropTypes.bool,
mask: PropTypes.bool,
maskStyle: PropTypes.object,
style: PropTypes.object,
title: PropTypes.node,
visible: PropTypes.bool,
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
zIndex: PropTypes.number,
prefixCls: PropTypes.string,
placement: PropTypes.oneOf(PlacementTypes),
7 years ago
onClose: PropTypes.func,
afterVisibleChange: PropTypes.func,
className: PropTypes.string,
7 years ago
};
7 years ago
7 years ago
static defaultProps = {
7 years ago
width: 256,
height: 256,
7 years ago
closable: true,
placement: 'right' as placementType,
7 years ago
maskClosable: true,
mask: true,
level: null,
7 years ago
};
7 years ago
readonly state = {
push: false,
};
parentDrawer: Drawer;
destroyClose: boolean;
6 years ago
public componentDidUpdate(preProps: DrawerProps) {
const { visible } = this.props;
if (preProps.visible !== visible && this.parentDrawer) {
if (visible) {
this.parentDrawer.push();
} else {
this.parentDrawer.pull();
}
}
}
6 years ago
7 years ago
close = (e: EventType) => {
const { visible, onClose } = this.props;
if (visible !== undefined && onClose) {
onClose(e);
7 years ago
}
};
6 years ago
onMaskClick = (e: any) => {
if (!this.props.maskClosable && !e.keyCode) {
7 years ago
return;
}
this.close(e);
};
6 years ago
push = () => {
this.setState({
push: true,
});
};
6 years ago
pull = () => {
this.setState({
push: false,
});
};
6 years ago
onDestroyTransitionEnd = () => {
const isDestroyOnClose = this.getDestroyOnClose();
if (!isDestroyOnClose) {
return;
}
6 years ago
if (!this.props.visible) {
this.destroyClose = true;
6 years ago
this.forceUpdate();
}
};
getDestroyOnClose = () => this.props.destroyOnClose && !this.props.visible;
// get drawar push width or height
getPushTransform = (placement?: placementType) => {
if (placement === 'left' || placement === 'right') {
return `translateX(${placement === 'left' ? 180 : -180}px)`;
}
if (placement === 'top' || placement === 'bottom') {
return `translateY(${placement === 'top' ? 180 : -180}px)`;
}
};
6 years ago
getRcDrawerStyle = () => {
const { zIndex, placement, style } = this.props;
const { push } = this.state;
return {
zIndex,
transform: push ? this.getPushTransform(placement) : undefined,
...style,
};
};
renderHeader() {
const { title, prefixCls, closable } = this.props;
if (!title && !closable) {
return null;
}
const headerClassName = title ? `${prefixCls}-header` : `${prefixCls}-header-no-title`;
return (
<div className={headerClassName}>
{title && <div className={`${prefixCls}-title`}>{title}</div>}
{closable && this.renderCloseIcon()}
</div>
);
}
renderCloseIcon() {
const { closable, prefixCls } = this.props;
return (
closable && (
<button onClick={this.close} aria-label="Close" className={`${prefixCls}-close`}>
<Icon type="close" />
</button>
)
);
}
// render drawer body dom
renderBody = () => {
const { bodyStyle, placement, prefixCls, visible } = this.props;
if (this.destroyClose && !visible) {
6 years ago
return null;
}
this.destroyClose = false;
const containerStyle: React.CSSProperties =
placement === 'left' || placement === 'right'
? {
overflow: 'auto',
height: '100%',
}
: {};
const isDestroyOnClose = this.getDestroyOnClose();
6 years ago
if (isDestroyOnClose) {
// Increase the opacity transition, delete children after closing.
6 years ago
containerStyle.opacity = 0;
containerStyle.transition = 'opacity .3s';
7 years ago
}
7 years ago
return (
6 years ago
<div
className={`${prefixCls}-wrapper-body`}
6 years ago
style={containerStyle}
onTransitionEnd={this.onDestroyTransitionEnd}
6 years ago
>
{this.renderHeader()}
<div className={`${prefixCls}-body`} style={bodyStyle}>
{this.props.children}
</div>
7 years ago
</div>
7 years ago
);
};
// render Provider for Multi-level drawe
renderProvider = (value: Drawer) => {
const {
prefixCls,
zIndex,
style,
placement,
className,
wrapClassName,
width,
height,
...rest
} = this.props;
warning(
wrapClassName === undefined,
'Drawer',
'wrapClassName is deprecated, please use className instead.',
);
const haveMask = rest.mask ? '' : 'no-mask';
this.parentDrawer = value;
const offsetStyle: any = {};
if (placement === 'left' || placement === 'right') {
offsetStyle.width = width;
} else {
offsetStyle.height = height;
}
return (
<DrawerContext.Provider value={this}>
<RcDrawer
handler={false}
{...rest}
{...offsetStyle}
prefixCls={prefixCls}
open={this.props.visible}
onMaskClick={this.onMaskClick}
showMask={this.props.mask}
placement={placement}
style={this.getRcDrawerStyle()}
className={classNames(wrapClassName, className, haveMask)}
>
{this.renderBody()}
</RcDrawer>
</DrawerContext.Provider>
);
};
render() {
return <DrawerContext.Consumer>{this.renderProvider}</DrawerContext.Consumer>;
7 years ago
}
}
export default withConfigConsumer<DrawerProps>({
prefixCls: 'drawer',
})(Drawer);