2019-01-12 03:33:27 +00:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import VcDrawer from '../vc-drawer/src';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import Icon from '../icon';
|
|
|
|
import { getComponentFromProp, getOptionProps } from '../_util/props-util';
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2018-09-19 05:21:57 +00:00
|
|
|
const Drawer = {
|
2018-09-05 13:28:54 +00:00
|
|
|
name: 'ADrawer',
|
|
|
|
props: {
|
|
|
|
closable: PropTypes.bool.def(true),
|
|
|
|
destroyOnClose: PropTypes.bool,
|
|
|
|
getContainer: PropTypes.any,
|
|
|
|
maskClosable: PropTypes.bool.def(true),
|
2018-12-08 11:07:04 +00:00
|
|
|
mask: PropTypes.bool.def(true),
|
2018-09-05 13:28:54 +00:00
|
|
|
maskStyle: PropTypes.object,
|
2019-01-02 13:59:35 +00:00
|
|
|
wrapStyle: PropTypes.object,
|
2018-09-05 13:28:54 +00:00
|
|
|
title: PropTypes.any,
|
|
|
|
visible: PropTypes.bool,
|
|
|
|
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).def(256),
|
2018-12-08 11:07:04 +00:00
|
|
|
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).def(256),
|
2018-09-05 13:28:54 +00:00
|
|
|
zIndex: PropTypes.number,
|
|
|
|
prefixCls: PropTypes.string.def('ant-drawer'),
|
2018-12-08 11:07:04 +00:00
|
|
|
placement: PropTypes.oneOf(['top', 'right', 'bottom', 'left']).def('right'),
|
2018-09-05 13:28:54 +00:00
|
|
|
level: PropTypes.any.def(null),
|
2019-01-02 03:04:38 +00:00
|
|
|
wrapClassName: PropTypes.string, // not use class like react, vue will add class to root dom
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
|
|
|
mixins: [BaseMixin],
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
|
|
|
this.destoryClose = false;
|
|
|
|
this.preVisible = this.$props.visible;
|
2018-09-05 13:28:54 +00:00
|
|
|
return {
|
|
|
|
_push: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
|
|
|
inject: {
|
|
|
|
parentDrawer: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
provide() {
|
2018-09-05 13:28:54 +00:00
|
|
|
return {
|
|
|
|
parentDrawer: this,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
updated() {
|
2018-09-05 13:28:54 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.preVisible !== this.visible && this.parentDrawer) {
|
|
|
|
if (this.visible) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.parentDrawer.push();
|
2018-09-05 13:28:54 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.parentDrawer.pull();
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.preVisible = this.visible;
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
close(e) {
|
2018-09-05 13:28:54 +00:00
|
|
|
if (this.visible !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.$emit('close', e);
|
|
|
|
return;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
onMaskClick(e) {
|
2018-09-05 13:28:54 +00:00
|
|
|
if (!this.maskClosable) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.close(e);
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
push() {
|
2018-09-05 13:28:54 +00:00
|
|
|
this.setState({
|
|
|
|
_push: true,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
pull() {
|
2018-09-05 13:28:54 +00:00
|
|
|
this.setState({
|
|
|
|
_push: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
onDestoryTransitionEnd() {
|
|
|
|
const isDestroyOnClose = this.getDestoryOnClose();
|
2018-09-05 13:28:54 +00:00
|
|
|
if (!isDestroyOnClose) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
if (!this.visible) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.destoryClose = true;
|
|
|
|
this.$forceUpdate();
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
getDestoryOnClose() {
|
|
|
|
return this.destroyOnClose && !this.visible;
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2018-12-08 11:07:04 +00:00
|
|
|
// get drawar push width or height
|
2019-01-12 03:33:27 +00:00
|
|
|
getPushTransform(placement) {
|
2018-12-08 11:07:04 +00:00
|
|
|
if (placement === 'left' || placement === 'right') {
|
2019-01-12 03:33:27 +00:00
|
|
|
return `translateX(${placement === 'left' ? 180 : -180}px)`;
|
2018-12-08 11:07:04 +00:00
|
|
|
}
|
|
|
|
if (placement === 'top' || placement === 'bottom') {
|
2019-01-12 03:33:27 +00:00
|
|
|
return `translateY(${placement === 'top' ? 180 : -180}px)`;
|
2018-12-08 11:07:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
// render drawer body dom
|
2019-01-12 03:33:27 +00:00
|
|
|
renderBody() {
|
2018-09-05 13:28:54 +00:00
|
|
|
if (this.destoryClose && !this.visible) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return null;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.destoryClose = false;
|
|
|
|
const { placement } = this.$props;
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const containerStyle =
|
|
|
|
placement === 'left' || placement === 'right'
|
|
|
|
? {
|
|
|
|
overflow: 'auto',
|
|
|
|
height: '100%',
|
|
|
|
}
|
|
|
|
: {};
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const isDestroyOnClose = this.getDestoryOnClose();
|
2018-09-05 13:28:54 +00:00
|
|
|
if (isDestroyOnClose) {
|
|
|
|
// Increase the opacity transition, delete children after closing.
|
2019-01-12 03:33:27 +00:00
|
|
|
containerStyle.opacity = 0;
|
|
|
|
containerStyle.transition = 'opacity .3s';
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
const { prefixCls, closable } = this.$props;
|
|
|
|
const title = getComponentFromProp(this, 'title');
|
2018-12-08 11:07:04 +00:00
|
|
|
// is have header dom
|
2019-01-12 03:33:27 +00:00
|
|
|
let header;
|
2018-09-05 13:28:54 +00:00
|
|
|
if (title) {
|
|
|
|
header = (
|
2019-01-12 03:33:27 +00:00
|
|
|
<div key="header" class={`${prefixCls}-header`}>
|
2018-09-05 13:28:54 +00:00
|
|
|
<div class={`${prefixCls}-title`}>{title}</div>
|
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2018-12-08 11:07:04 +00:00
|
|
|
// is have closer button
|
2019-01-12 03:33:27 +00:00
|
|
|
let closer;
|
2018-09-05 13:28:54 +00:00
|
|
|
if (closable) {
|
|
|
|
closer = (
|
2019-01-12 03:33:27 +00:00
|
|
|
<button key="closer" onClick={this.close} aria-label="Close" class={`${prefixCls}-close`}>
|
2018-12-08 11:07:04 +00:00
|
|
|
<span class={`${prefixCls}-close-x`}>
|
2019-01-12 03:33:27 +00:00
|
|
|
<Icon type="close" />
|
2018-12-08 11:07:04 +00:00
|
|
|
</span>
|
2018-09-05 13:28:54 +00:00
|
|
|
</button>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
class={`${prefixCls}-wrapper-body`}
|
|
|
|
style={containerStyle}
|
|
|
|
onTransitionend={this.onDestoryTransitionEnd}
|
|
|
|
>
|
|
|
|
{header}
|
|
|
|
{closer}
|
2019-01-12 03:33:27 +00:00
|
|
|
<div key="body" class={`${prefixCls}-body`}>
|
2018-09-05 13:28:54 +00:00
|
|
|
{this.$slots.default}
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
getRcDrawerStyle() {
|
|
|
|
const { zIndex, placement, maskStyle, wrapStyle } = this.$props;
|
|
|
|
const { _push: push } = this.$data;
|
2019-01-02 13:59:35 +00:00
|
|
|
return {
|
|
|
|
...maskStyle,
|
|
|
|
zIndex,
|
|
|
|
transform: push ? this.getPushTransform(placement) : undefined,
|
|
|
|
...wrapStyle,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-12-08 11:07:04 +00:00
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
const props = getOptionProps(this);
|
|
|
|
const { width, height, visible, placement, wrapClassName, ...rest } = props;
|
|
|
|
const haveMask = rest.mask ? '' : 'no-mask';
|
|
|
|
const offsetStyle = {};
|
2018-12-08 11:07:04 +00:00
|
|
|
if (placement === 'left' || placement === 'right') {
|
2019-01-12 03:33:27 +00:00
|
|
|
offsetStyle.width = typeof width === 'number' ? `${width}px` : width;
|
2018-12-08 11:07:04 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
offsetStyle.height = typeof height === 'number' ? `${height}px` : height;
|
2018-12-08 11:07:04 +00:00
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
const vcDrawerProps = {
|
|
|
|
props: {
|
|
|
|
handler: false,
|
2018-12-08 11:07:04 +00:00
|
|
|
...rest,
|
|
|
|
...offsetStyle,
|
2018-09-05 13:28:54 +00:00
|
|
|
open: visible,
|
2018-12-08 11:07:04 +00:00
|
|
|
showMask: props.mask,
|
2018-09-05 13:28:54 +00:00
|
|
|
placement,
|
2019-01-02 03:04:38 +00:00
|
|
|
className: classnames({
|
|
|
|
[wrapClassName]: !!wrapClassName,
|
|
|
|
[haveMask]: !!haveMask,
|
|
|
|
}),
|
2019-01-02 13:59:35 +00:00
|
|
|
wrapStyle: this.getRcDrawerStyle(),
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
|
|
|
on: {
|
|
|
|
maskClick: this.onMaskClick,
|
|
|
|
...this.$listeners,
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
return <VcDrawer {...vcDrawerProps}>{this.renderBody()}</VcDrawer>;
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-19 05:21:57 +00:00
|
|
|
|
|
|
|
/* istanbul ignore next */
|
2019-01-12 03:33:27 +00:00
|
|
|
Drawer.install = function(Vue) {
|
|
|
|
Vue.component(Drawer.name, Drawer);
|
|
|
|
};
|
2018-09-19 05:21:57 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export default Drawer;
|