ant-design/components/drawer/index.tsx
陈帅 785c132262
meger feature to master (#16421)
* use ul in list

* update snapshot

* update comment

* feat: TreeSelect support `showSearch` in multiple mode (#15933)

* update rc-tree-select

* typo

* update desc & snapshot

* update desc & snapshot

* check default showSearch

* feat: table customizing variable (#15971)

* feat: added table selected row color variable

* fix: @table-selected-row-color default is inherit

* feat: Upload support customize previewFile (#15984)

* support preview file

* use promise

* dealy load

* use canvas of render

* use domHook of test

* update demo

* add snapshot

* update types

* update testcase

* feat: form customizing variables (#15954)

* fix: added styling form input background-color

* feat: added '@form-warning-input-bg' variable

* feat: added '@form-error-input-bg' variable

* use li wrap with comment

* feat: Support append theme less file with less-variable (#16118)

* add override

* add override support

* update doc

* feat: dropdown support set right icon

* docs: update doc of dropdown component

* style: format dropdown-button.md

* test: update updateSnapshot

* style: format dropdown-button.md

* test: update updateSnapshot

* test: update updateSnapshot

* style: change style of dropdown-button demo

* fix: fix document table order

* feat: Support SkeletonAvatarProps.size accept number (#16078) (#16128)

* chore:update style of demo

* feat: Notification functions accept top, bottom and getContainer as arguments

* drawer: add afterVisibleChange

* rm onVisibleChange

* update

* feat: 🇭🇷 hr_HR locale (#16258)

* Added Croatian locale

* fixed lint error

*  Add test cases for hr_HR

* 📝 update i18n documentation

* feat:  add `htmlFor` in Form.Item (#16278)

* add htmlFor in Form.Item

* update doc

* feat: Button support `link` type (#16289)

close #15892

* feat: Add Timeline.Item.position (#16148) (#16193)

* fix: Timeline.pendingDot interface documentation there is a small problem (#16177)

* feat: Add Timeline.Item.position (#16148)

* doc: add version infomation for Timeline.Item.position

* refactor: Update Tree & TreeSelect deps (#16330)

* use CSSMotion

* update snapshot

* feat: Collapse support `expandIconPosition` (#16365)

* update doc

* support expandIconPosition

* update snapshot

* feat: Breadcrumb  support DropDown (#16315)

* breadcrumbs support drop down menu

* update doc

* add require less

* fix test

* fix md doc

* less code

* fix  style warning

* update snap

* add children render test

* feat: TreeNode support checkable

* feat: add optional to support top and left slick dots (#16186) (#16225)

* add optional to support top and left slick dots

* update carousel snapshot

* Update doc, add placement demo

* update carousel placement demo snapshots

* rename dots placement to position

* update vertical as deprecated

* rename dotsPosition to dotPosition

* refine code

* add warning testcase for vertical

* remove unused warning

* update expression

* Additional test case for dotPosition

* refactor: Upgrade `rc-tree-select` to support pure React motion (#16402)

* upgrade `rc-tree-select`

* update snapshot

* 3.17.0 changelog

* fix warning

* fix review warning
2019-05-06 12:04:39 +08:00

279 lines
7.1 KiB
TypeScript

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';
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);
type EventType = React.MouseEvent<HTMLDivElement> | React.MouseEvent<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;
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;
}
export interface IDrawerState {
push?: boolean;
}
class Drawer extends React.Component<DrawerProps & ConfigConsumerProps, IDrawerState> {
static propTypes = {
closable: PropTypes.bool,
destroyOnClose: PropTypes.bool,
getContainer: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object as PropTypes.Requireable<HTMLElement>,
PropTypes.func,
PropTypes.bool,
]),
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),
onClose: PropTypes.func,
afterVisibleChange: PropTypes.func,
className: PropTypes.string,
};
static defaultProps = {
width: 256,
height: 256,
closable: true,
placement: 'right' as placementType,
maskClosable: true,
mask: true,
level: null,
};
readonly state = {
push: false,
};
parentDrawer: Drawer;
destroyClose: boolean;
public componentDidUpdate(preProps: DrawerProps) {
if (preProps.visible !== this.props.visible && this.parentDrawer) {
if (this.props.visible) {
this.parentDrawer.push();
} else {
this.parentDrawer.pull();
}
}
}
close = (e: EventType) => {
if (this.props.visible !== undefined) {
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,
});
};
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 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)`;
}
};
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) {
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 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>;
}
}
export default withConfigConsumer<DrawerProps>({
prefixCls: 'drawer',
})(Drawer);