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.

189 lines
4.6 KiB

7 years ago
import * as React from 'react';
import RcDrawer from 'rc-drawer';
7 years ago
import PropTypes from 'prop-types';
import createReactContext, { Context } from 'create-react-context';
import isNumeric from '../_util/isNumeric';
const DrawerContext: Context<Drawer | null> = createReactContext(null);
7 years ago
type EventType =
| React.MouseEvent<HTMLDivElement>
| React.MouseEvent<HTMLButtonElement>;
7 years ago
type getContainerfunc = () => HTMLElement;
export interface DrawerProps {
7 years ago
closable?: boolean;
destroyOnClose?: boolean;
7 years ago
getContainer?: string | HTMLElement | getContainerfunc;
7 years ago
maskClosable?: boolean;
mask?: boolean;
maskStyle?: React.CSSProperties;
style?: React.CSSProperties;
title?: React.ReactNode;
visible?: boolean;
width?: number | string;
wrapClassName?: string;
zIndex?: number;
prefixCls?: string;
push?: boolean;
7 years ago
placement?: 'left' | 'right';
7 years ago
onClose?: (e: EventType) => void;
}
export interface IDrawerState {
push?: boolean;
7 years ago
}
export default class Drawer extends React.Component<DrawerProps, IDrawerState> {
7 years ago
static propTypes = {
closable: PropTypes.bool,
destroyOnClose: PropTypes.bool,
getContainer: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
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]),
wrapClassName: PropTypes.string,
zIndex: PropTypes.number,
prefixCls: PropTypes.string,
placement: PropTypes.string,
onClose: PropTypes.func,
};
7 years ago
7 years ago
static defaultProps = {
prefixCls: 'ant-drawer',
7 years ago
width: 256,
7 years ago
closable: true,
placement: 'right',
7 years ago
maskClosable: true,
level: null,
7 years ago
};
7 years ago
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();
}
}
}
7 years ago
close = (e: EventType) => {
if (this.props.visible !== undefined) {
7 years ago
if (this.props.onClose) {
this.props.onClose(e);
}
return;
}
}
onMaskClick = (e: EventType) => {
if (!this.props.maskClosable) {
return;
}
this.close(e);
}
push = () => {
this.setState({
push: true,
});
}
pull = () => {
this.setState({
push: false,
});
}
7 years ago
renderBody = () => {
const { destroyOnClose, visible, width, placement } = this.props;
let containerStyle: React.CSSProperties = { width };
if (placement === 'left' || placement === 'right') {
containerStyle = {
overflow: 'auto',
height: '100%',
width,
};
}
if (destroyOnClose && !visible) {
return <div style={containerStyle}/>;
7 years ago
}
7 years ago
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>
);
}
7 years ago
return (
7 years ago
<div style={containerStyle}>
7 years ago
{header}
{closer}
7 years ago
<div className={`${prefixCls}-body`} style={this.props.style}>
{this.props.children}
</div>
</div>
7 years ago
);
}
renderProvider = (value: Drawer) => {
let { width, zIndex, style, placement, ...rest } = this.props;
if (isNumeric(width)) {
7 years ago
width = `${width}px`;
}
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}
class={this.props.wrapClassName}
>
{this.renderBody()}
</RcDrawer>
</DrawerContext.Provider>
);
}
render() {
7 years ago
return (
<DrawerContext.Consumer>{this.renderProvider}</DrawerContext.Consumer>
7 years ago
);
}
}