2018-05-23 10:56:37 +08:00
|
|
|
import * as React from 'react';
|
2018-06-06 22:55:29 +08:00
|
|
|
import RcDrawer from 'rc-drawer';
|
2018-05-27 00:00:07 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2018-07-02 21:49:06 +08:00
|
|
|
import createReactContext, { Context } from 'create-react-context';
|
2018-07-10 21:17:19 +08:00
|
|
|
import isNumeric from '../_util/isNumeric';
|
2018-07-02 21:49:06 +08:00
|
|
|
|
|
|
|
const DrawerContext: Context<Drawer | null> = createReactContext(null);
|
2018-05-23 10:56:37 +08:00
|
|
|
|
|
|
|
type EventType =
|
|
|
|
| React.MouseEvent<HTMLDivElement>
|
|
|
|
| React.MouseEvent<HTMLButtonElement>;
|
|
|
|
|
2018-06-19 17:42:25 +08:00
|
|
|
type getContainerfunc = () => HTMLElement;
|
|
|
|
|
2018-06-06 21:12:26 +08:00
|
|
|
export interface DrawerProps {
|
2018-05-23 10:56:37 +08:00
|
|
|
closable?: boolean;
|
|
|
|
destroyOnClose?: boolean;
|
2018-06-19 17:42:25 +08:00
|
|
|
getContainer?: string | HTMLElement | getContainerfunc;
|
2018-05-23 10:56:37 +08:00
|
|
|
maskClosable?: boolean;
|
|
|
|
mask?: boolean;
|
|
|
|
maskStyle?: React.CSSProperties;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
title?: React.ReactNode;
|
|
|
|
visible?: boolean;
|
|
|
|
width?: number | string;
|
|
|
|
wrapClassName?: string;
|
|
|
|
zIndex?: number;
|
|
|
|
prefixCls?: string;
|
2018-07-02 21:49:06 +08:00
|
|
|
push?: boolean;
|
2018-05-25 15:10:45 +08:00
|
|
|
placement?: 'left' | 'right';
|
2018-05-23 10:56:37 +08:00
|
|
|
onClose?: (e: EventType) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IDrawerState {
|
2018-07-02 21:49:06 +08:00
|
|
|
push?: boolean;
|
2018-05-23 10:56:37 +08:00
|
|
|
}
|
|
|
|
|
2018-07-02 21:49:06 +08:00
|
|
|
export default class Drawer extends React.Component<DrawerProps, IDrawerState> {
|
2018-05-27 00:00:07 +08:00
|
|
|
static propTypes = {
|
|
|
|
closable: PropTypes.bool,
|
|
|
|
destroyOnClose: PropTypes.bool,
|
2018-07-02 21:49:06 +08:00
|
|
|
getContainer: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.object,
|
|
|
|
PropTypes.func,
|
|
|
|
PropTypes.bool,
|
|
|
|
]),
|
2018-05-27 00:00:07 +08:00
|
|
|
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]),
|
|
|
|
wrapClassName: PropTypes.string,
|
|
|
|
zIndex: PropTypes.number,
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
placement: PropTypes.string,
|
|
|
|
onClose: PropTypes.func,
|
|
|
|
};
|
2018-06-26 00:22:50 +08:00
|
|
|
|
2018-05-23 10:56:37 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-drawer',
|
2018-05-27 00:00:07 +08:00
|
|
|
width: 256,
|
2018-05-23 10:56:37 +08:00
|
|
|
closable: true,
|
2018-06-30 12:32:55 +08:00
|
|
|
placement: 'right',
|
2018-06-21 12:32:13 +08:00
|
|
|
maskClosable: true,
|
2018-07-02 12:27:06 +08:00
|
|
|
level: null,
|
2018-05-23 10:56:37 +08:00
|
|
|
};
|
2018-06-26 00:22:50 +08:00
|
|
|
|
2018-07-02 21:49:06 +08:00
|
|
|
readonly state = {
|
|
|
|
push: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
praentDrawer: Drawer;
|
|
|
|
public componentDidUpdate(preProps: DrawerProps) {
|
|
|
|
if (preProps.visible !== this.props.visible && this.praentDrawer) {
|
|
|
|
if (this.props.visible) {
|
|
|
|
this.praentDrawer.push();
|
|
|
|
} else {
|
|
|
|
this.praentDrawer.pull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 10:56:37 +08:00
|
|
|
close = (e: EventType) => {
|
2018-06-07 12:15:47 +08:00
|
|
|
if (this.props.visible !== undefined) {
|
2018-05-23 10:56:37 +08:00
|
|
|
if (this.props.onClose) {
|
|
|
|
this.props.onClose(e);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onMaskClick = (e: EventType) => {
|
|
|
|
if (!this.props.maskClosable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.close(e);
|
|
|
|
}
|
2018-07-02 21:49:06 +08:00
|
|
|
push = () => {
|
|
|
|
this.setState({
|
|
|
|
push: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
pull = () => {
|
|
|
|
this.setState({
|
|
|
|
push: false,
|
|
|
|
});
|
|
|
|
}
|
2018-05-23 10:56:37 +08:00
|
|
|
renderBody = () => {
|
2018-07-02 21:49:06 +08:00
|
|
|
const { destroyOnClose, visible, width, placement } = this.props;
|
2018-07-18 15:52:51 +08:00
|
|
|
let containerStyle: React.CSSProperties = { width };
|
|
|
|
if (placement === 'left' || placement === 'right') {
|
|
|
|
containerStyle = {
|
|
|
|
overflow: 'auto',
|
|
|
|
height: '100%',
|
|
|
|
width,
|
|
|
|
};
|
|
|
|
}
|
2018-06-28 17:54:03 +08:00
|
|
|
if (destroyOnClose && !visible) {
|
2018-07-18 15:52:51 +08:00
|
|
|
return <div style={containerStyle}/>;
|
2018-05-27 00:00:07 +08:00
|
|
|
}
|
2018-05-23 10:56:37 +08:00
|
|
|
const { prefixCls, title, closable } = this.props;
|
|
|
|
let header;
|
|
|
|
if (title) {
|
|
|
|
header = (
|
|
|
|
<div className={`${prefixCls}-header`}>
|
|
|
|
<div className={`${prefixCls}-title`}>{title}</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
let closer;
|
|
|
|
if (closable) {
|
|
|
|
closer = (
|
|
|
|
<button
|
|
|
|
onClick={this.close}
|
|
|
|
aria-label="Close"
|
|
|
|
className={`${prefixCls}-close`}
|
|
|
|
>
|
|
|
|
<span className={`${prefixCls}-close-x`} />
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
2018-07-18 15:52:51 +08:00
|
|
|
|
2018-05-23 10:56:37 +08:00
|
|
|
return (
|
2018-05-25 15:10:45 +08:00
|
|
|
<div style={containerStyle}>
|
2018-05-23 10:56:37 +08:00
|
|
|
{header}
|
|
|
|
{closer}
|
2018-05-25 15:10:45 +08:00
|
|
|
<div className={`${prefixCls}-body`} style={this.props.style}>
|
|
|
|
{this.props.children}
|
|
|
|
</div>
|
|
|
|
</div>
|
2018-05-23 10:56:37 +08:00
|
|
|
);
|
|
|
|
}
|
2018-07-02 21:49:06 +08:00
|
|
|
renderProvider = (value: Drawer) => {
|
|
|
|
let { width, zIndex, style, placement, ...rest } = this.props;
|
2018-07-10 21:17:19 +08:00
|
|
|
if (isNumeric(width)) {
|
2018-05-25 15:10:45 +08:00
|
|
|
width = `${width}px`;
|
|
|
|
}
|
2018-07-02 21:49:06 +08:00
|
|
|
const RcDrawerStyle = this.state.push
|
|
|
|
? {
|
|
|
|
zIndex,
|
|
|
|
transform: `translateX(${placement === 'left' ? 180 : -180}px)`,
|
|
|
|
}
|
|
|
|
: { zIndex };
|
|
|
|
this.praentDrawer = value;
|
|
|
|
return (
|
|
|
|
<DrawerContext.Provider value={this}>
|
|
|
|
<RcDrawer
|
|
|
|
{...rest}
|
|
|
|
handler={false}
|
|
|
|
open={this.props.visible}
|
|
|
|
onMaskClick={this.onMaskClick}
|
|
|
|
showMask={this.props.mask}
|
|
|
|
placement={placement}
|
|
|
|
style={RcDrawerStyle}
|
2018-07-18 15:52:51 +08:00
|
|
|
class={this.props.wrapClassName}
|
2018-07-02 21:49:06 +08:00
|
|
|
>
|
|
|
|
{this.renderBody()}
|
|
|
|
</RcDrawer>
|
|
|
|
</DrawerContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
render() {
|
2018-05-23 10:56:37 +08:00
|
|
|
return (
|
2018-07-02 21:49:06 +08:00
|
|
|
<DrawerContext.Consumer>{this.renderProvider}</DrawerContext.Consumer>
|
2018-05-23 10:56:37 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|