ant-design/components/drawer/index.tsx
vagusX 149729e524
use ant design icons 4.0 (#18217)
* feat: use @ant-design/icons@4.0

* feat: use createFromIconfontCN to make site works

* feat: update doc for Icon

* feat: use icon in component Alert

* feat: use icon in component Avatar

* feat: use icon in component Breadcrumb

* feat: use icon in component Button

* feat: use icon in component Cascader

* feat: use icon in component Collapse

* feat: use icon in component Datepicker

* feat: use icon in component Dropdown

* feat: use icon in component Form

* feat: use icon in component Input

* feat: use icon in component InputNumber

* feat: use icon in component Layout

* feat: use icon in component Mention

* feat: use icon in component Message

* feat: use icon in component Modal

* feat: use icon in component Notification

* feat: use icon in component PageHeader

* feat: use icon in component Pagination

* feat: use icon in component Popconfirm

* feat: use icon in component Progress

* feat: use icon in component Rate

* feat: use icon in component Result

* feat: use icon in component Select

* feat: use icon in component Step

* feat: use icon in component Switch

* feat: use icon in component Table

* feat: use icon in component Tab

* feat: use icon in component Tag

* feat: handle rest component which using Icon

* fix: remove unused vars

* feat: use latest alpha ant design icons

* fix: failed test in uploadlist.test.js

* test: update snapshot for icons

* doc: add Icon for site

* doc: use @ant-design/icons in site

* chore: use latest icons

* fix: tslint issue

* fix: test cases

* fix: types for react

* fix: lint rules for import orders

* fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render

* fix: eslint error in demo/**.md

* inject antd icons

* update snapshot

* fix site

* doc: update docs

* fix: code snippets icon in site

* feat: use latest @ant-design/icons

* fix: icon props in message
2019-08-13 14:07:17 +08:00

277 lines
6.9 KiB
TypeScript

import * as React from 'react';
import RcDrawer from 'rc-drawer';
import createReactContext from '@ant-design/create-react-context';
import { Close } from '@ant-design/icons';
import classNames from 'classnames';
import omit from 'omit.js';
import warning from '../_util/warning';
import { withConfigConsumer, ConfigConsumerProps } from '../config-provider';
import { tuple } from '../_util/type';
const DrawerContext = createReactContext<Drawer | null>(null);
type EventType =
| React.KeyboardEvent<HTMLDivElement>
| React.MouseEvent<HTMLDivElement | HTMLButtonElement>;
type getContainerFunc = () => HTMLElement;
const PlacementTypes = tuple('top', 'right', 'bottom', 'left');
type placementType = (typeof PlacementTypes)[number];
export interface DrawerProps {
closable?: boolean;
destroyOnClose?: boolean;
getContainer?: string | HTMLElement | getContainerFunc | false;
maskClosable?: boolean;
mask?: boolean;
maskStyle?: React.CSSProperties;
style?: React.CSSProperties;
bodyStyle?: React.CSSProperties;
title?: React.ReactNode;
visible?: boolean;
width?: number | string;
height?: number | string;
/* deprecated, use className instead */
wrapClassName?: string;
zIndex?: number;
prefixCls?: string;
push?: boolean;
placement?: placementType;
onClose?: (e: EventType) => void;
afterVisibleChange?: (visible: boolean) => void;
className?: string;
handler?: React.ReactNode;
keyboard?: boolean;
}
export interface IDrawerState {
push?: boolean;
}
class Drawer extends React.Component<DrawerProps & ConfigConsumerProps, IDrawerState> {
static defaultProps = {
width: 256,
height: 256,
closable: true,
placement: 'right' as placementType,
maskClosable: true,
mask: true,
level: null,
keyboard: true,
};
readonly state = {
push: false,
};
parentDrawer: Drawer | null;
destroyClose: boolean;
public componentDidMount() {
// fix: delete drawer in child and re-render, no push started.
// <Drawer>{show && <Drawer />}</Drawer>
const { visible } = this.props;
if (visible && this.parentDrawer) {
this.parentDrawer.push();
}
}
public componentDidUpdate(preProps: DrawerProps) {
const { visible } = this.props;
if (preProps.visible !== visible && this.parentDrawer) {
if (visible) {
this.parentDrawer.push();
} else {
this.parentDrawer.pull();
}
}
}
public componentWillUnmount() {
// unmount drawer in child, clear push.
if (this.parentDrawer) {
this.parentDrawer.pull();
this.parentDrawer = null;
}
}
push = () => {
this.setState({
push: true,
});
};
pull = () => {
this.setState({
push: false,
});
};
onDestroyTransitionEnd = () => {
const isDestroyOnClose = this.getDestroyOnClose();
if (!isDestroyOnClose) {
return;
}
if (!this.props.visible) {
this.destroyClose = true;
this.forceUpdate();
}
};
getDestroyOnClose = () => this.props.destroyOnClose && !this.props.visible;
// get drawer 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)`;
}
};
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, onClose } = this.props;
return (
closable && (
// eslint-disable-next-line react/button-has-type
<button onClick={onClose} aria-label="Close" className={`${prefixCls}-close`}>
<Close />
</button>
)
);
}
// render drawer body dom
renderBody = () => {
const { bodyStyle, placement, prefixCls, visible } = this.props;
if (this.destroyClose && !visible) {
return null;
}
this.destroyClose = false;
const containerStyle: React.CSSProperties =
placement === 'left' || placement === 'right'
? {
overflow: 'auto',
height: '100%',
}
: {};
const isDestroyOnClose = this.getDestroyOnClose();
if (isDestroyOnClose) {
// Increase the opacity transition, delete children after closing.
containerStyle.opacity = 0;
containerStyle.transition = 'opacity .3s';
}
return (
<div
className={`${prefixCls}-wrapper-body`}
style={containerStyle}
onTransitionEnd={this.onDestroyTransitionEnd}
>
{this.renderHeader()}
<div className={`${prefixCls}-body`} style={bodyStyle}>
{this.props.children}
</div>
</div>
);
};
// render Provider for Multi-level drawer
renderProvider = (value: Drawer) => {
const {
prefixCls,
placement,
className,
wrapClassName,
width,
height,
mask,
...rest
} = this.props;
warning(
wrapClassName === undefined,
'Drawer',
'wrapClassName is deprecated, please use className instead.',
);
const haveMask = 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}
{...omit(rest, [
'zIndex',
'style',
'closable',
'destroyOnClose',
'bodyStyle',
'title',
'push',
'visible',
'getPopupContainer',
'rootPrefixCls',
'getPrefixCls',
'renderEmpty',
'csp',
'autoInsertSpaceInButton',
])}
{...offsetStyle}
prefixCls={prefixCls}
open={this.props.visible}
showMask={mask}
placement={placement}
style={this.getRcDrawerStyle()}
className={classNames(wrapClassName, className, haveMask)}
>
{this.renderBody()}
</RcDrawer>
</DrawerContext.Provider>
);
};
render() {
return <DrawerContext.Consumer>{this.renderProvider}</DrawerContext.Consumer>;
}
}
export default withConfigConsumer<DrawerProps>({
prefixCls: 'drawer',
})(Drawer);