2018-05-23 10:56:37 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import RcDrawer from 'rc-drawer-menu';
|
2018-05-25 15:10:45 +08:00
|
|
|
import { isNull, isNumber } from 'util';
|
2018-05-23 10:56:37 +08:00
|
|
|
|
|
|
|
type EventType =
|
|
|
|
| React.MouseEvent<HTMLDivElement>
|
|
|
|
| React.MouseEvent<HTMLButtonElement>;
|
|
|
|
|
|
|
|
export interface IDrawerProps {
|
|
|
|
closable?: boolean;
|
|
|
|
// @todo 下一步增加
|
|
|
|
destroyOnClose?: boolean;
|
|
|
|
getContainer?: HTMLElement;
|
|
|
|
maskClosable?: boolean;
|
|
|
|
mask?: boolean;
|
|
|
|
maskStyle?: React.CSSProperties;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
title?: React.ReactNode;
|
|
|
|
visible?: boolean;
|
|
|
|
width?: number | string;
|
|
|
|
wrapClassName?: string;
|
|
|
|
// @todo 下一步增加
|
|
|
|
zIndex?: number;
|
|
|
|
prefixCls?: string;
|
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 {
|
|
|
|
visible?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Drawer extends React.Component<
|
|
|
|
IDrawerProps,
|
|
|
|
IDrawerState
|
|
|
|
> {
|
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-drawer',
|
|
|
|
width: 325,
|
|
|
|
closable: true,
|
|
|
|
};
|
|
|
|
static getDerivedStateFromProps(
|
|
|
|
nextProps: IDrawerProps,
|
|
|
|
prevState: IDrawerState,
|
|
|
|
) {
|
|
|
|
const nextState: IDrawerState = {};
|
|
|
|
if (!isNull(nextProps.visible) && nextProps.visible !== prevState.visible) {
|
|
|
|
nextState.visible = nextProps.visible;
|
|
|
|
}
|
|
|
|
return nextState;
|
|
|
|
}
|
|
|
|
constructor(props: IDrawerProps) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
visible: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
close = (e: EventType) => {
|
|
|
|
if (!isNull(this.props.visible)) {
|
|
|
|
if (this.props.onClose) {
|
|
|
|
this.props.onClose(e);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
visible: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
onMaskClick = (e: EventType) => {
|
|
|
|
if (!this.props.maskClosable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.close(e);
|
|
|
|
}
|
|
|
|
renderBody = () => {
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const containerStyle = { width: this.props.width };
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
render() {
|
2018-05-25 15:10:45 +08:00
|
|
|
let { width } = this.props;
|
|
|
|
if (isNumber(width)) {
|
|
|
|
width = `${width}px`;
|
|
|
|
}
|
2018-05-23 10:56:37 +08:00
|
|
|
return (
|
|
|
|
<RcDrawer
|
|
|
|
{...this.props}
|
|
|
|
handleChild={false}
|
|
|
|
open={this.state.visible}
|
|
|
|
onMaskClick={this.close}
|
|
|
|
showMask={this.props.mask}
|
|
|
|
placement={this.props.placement}
|
|
|
|
>
|
2018-05-23 15:52:49 +08:00
|
|
|
{this.renderBody()}
|
2018-05-23 10:56:37 +08:00
|
|
|
</RcDrawer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|