2020-06-02 22:09:34 +08:00
|
|
|
import { inject, provide, nextTick } from 'vue';
|
2019-01-12 11:33:27 +08:00
|
|
|
import classnames from 'classnames';
|
2020-03-07 19:45:13 +08:00
|
|
|
import omit from 'omit.js';
|
2019-01-12 11:33:27 +08:00
|
|
|
import VcDrawer from '../vc-drawer/src';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
2020-03-26 12:39:39 +08:00
|
|
|
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
|
2020-06-02 23:21:24 +08:00
|
|
|
import { getComponent, getOptionProps } from '../_util/props-util';
|
2019-04-07 11:46:22 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2018-09-19 13:21:57 +08:00
|
|
|
const Drawer = {
|
2018-09-05 21:28:54 +08:00
|
|
|
name: 'ADrawer',
|
2020-06-02 23:21:24 +08:00
|
|
|
inheritAttrs: false,
|
2018-09-05 21:28:54 +08:00
|
|
|
props: {
|
|
|
|
closable: PropTypes.bool.def(true),
|
|
|
|
destroyOnClose: PropTypes.bool,
|
|
|
|
getContainer: PropTypes.any,
|
|
|
|
maskClosable: PropTypes.bool.def(true),
|
2018-12-08 19:07:04 +08:00
|
|
|
mask: PropTypes.bool.def(true),
|
2018-09-05 21:28:54 +08:00
|
|
|
maskStyle: PropTypes.object,
|
2019-01-02 21:59:35 +08:00
|
|
|
wrapStyle: PropTypes.object,
|
2019-09-10 18:57:08 +08:00
|
|
|
bodyStyle: PropTypes.object,
|
2020-03-07 19:45:13 +08:00
|
|
|
headerStyle: PropTypes.object,
|
2020-01-15 14:32:24 +08:00
|
|
|
drawerStyle: PropTypes.object,
|
2018-09-05 21:28:54 +08:00
|
|
|
title: PropTypes.any,
|
|
|
|
visible: PropTypes.bool,
|
|
|
|
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).def(256),
|
2018-12-08 19:07:04 +08:00
|
|
|
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).def(256),
|
2018-09-05 21:28:54 +08:00
|
|
|
zIndex: PropTypes.number,
|
2019-04-07 11:46:22 +08:00
|
|
|
prefixCls: PropTypes.string,
|
2018-12-08 19:07:04 +08:00
|
|
|
placement: PropTypes.oneOf(['top', 'right', 'bottom', 'left']).def('right'),
|
2018-09-05 21:28:54 +08:00
|
|
|
level: PropTypes.any.def(null),
|
2019-01-02 11:04:38 +08:00
|
|
|
wrapClassName: PropTypes.string, // not use class like react, vue will add class to root dom
|
2019-04-04 23:28:50 +08:00
|
|
|
handle: PropTypes.any,
|
2020-03-07 19:45:13 +08:00
|
|
|
afterVisibleChange: PropTypes.func,
|
|
|
|
keyboard: PropTypes.bool.def(true),
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
|
|
|
mixins: [BaseMixin],
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
2019-04-07 11:46:22 +08:00
|
|
|
this.destroyClose = false;
|
2019-01-12 11:33:27 +08:00
|
|
|
this.preVisible = this.$props.visible;
|
2018-09-05 21:28:54 +08:00
|
|
|
return {
|
|
|
|
_push: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2020-06-02 22:09:34 +08:00
|
|
|
setup() {
|
|
|
|
const configProvider = inject('configProvider', ConfigConsumerProps);
|
2018-09-05 21:28:54 +08:00
|
|
|
return {
|
2020-06-02 22:09:34 +08:00
|
|
|
configProvider,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2020-06-02 22:09:34 +08:00
|
|
|
// inject: {
|
|
|
|
// parentDrawer: {
|
|
|
|
// default: () => null,
|
|
|
|
// },
|
|
|
|
// configProvider: { default: () => ConfigConsumerProps },
|
|
|
|
// },
|
|
|
|
// provide() {
|
|
|
|
// return {
|
|
|
|
// parentDrawer: this,
|
|
|
|
// };
|
|
|
|
// },
|
|
|
|
beforeCreate() {
|
|
|
|
const parentDrawer = inject('parentDrawer', null);
|
|
|
|
provide('parentDrawer', this);
|
|
|
|
this.parentDrawer = parentDrawer;
|
|
|
|
},
|
2020-03-07 19:45:13 +08:00
|
|
|
mounted() {
|
|
|
|
// fix: delete drawer in child and re-render, no push started.
|
|
|
|
// <Drawer>{show && <Drawer />}</Drawer>
|
|
|
|
const { visible } = this;
|
|
|
|
if (visible && this.parentDrawer) {
|
|
|
|
this.parentDrawer.push();
|
|
|
|
}
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
updated() {
|
2020-06-02 22:09:34 +08:00
|
|
|
nextTick(() => {
|
2018-09-05 21:28:54 +08:00
|
|
|
if (this.preVisible !== this.visible && this.parentDrawer) {
|
|
|
|
if (this.visible) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.parentDrawer.push();
|
2018-09-05 21:28:54 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.parentDrawer.pull();
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
this.preVisible = this.visible;
|
|
|
|
});
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2020-03-07 19:45:13 +08:00
|
|
|
beforeDestroy() {
|
|
|
|
// unmount drawer in child, clear push.
|
|
|
|
if (this.parentDrawer) {
|
|
|
|
this.parentDrawer.pull();
|
|
|
|
}
|
|
|
|
},
|
2018-09-05 21:28:54 +08:00
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
close(e) {
|
2020-03-07 19:45:13 +08:00
|
|
|
this.$emit('close', e);
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2020-03-07 19:45:13 +08:00
|
|
|
// onMaskClick(e) {
|
|
|
|
// if (!this.maskClosable) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// this.close(e);
|
|
|
|
// },
|
2019-01-12 11:33:27 +08:00
|
|
|
push() {
|
2018-09-05 21:28:54 +08:00
|
|
|
this.setState({
|
|
|
|
_push: true,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
pull() {
|
2018-09-05 21:28:54 +08:00
|
|
|
this.setState({
|
|
|
|
_push: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2019-04-07 11:46:22 +08:00
|
|
|
onDestroyTransitionEnd() {
|
|
|
|
const isDestroyOnClose = this.getDestroyOnClose();
|
2018-09-05 21:28:54 +08:00
|
|
|
if (!isDestroyOnClose) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
|
|
|
if (!this.visible) {
|
2019-04-07 11:46:22 +08:00
|
|
|
this.destroyClose = true;
|
2019-01-12 11:33:27 +08:00
|
|
|
this.$forceUpdate();
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-04-07 11:46:22 +08:00
|
|
|
getDestroyOnClose() {
|
2019-01-12 11:33:27 +08:00
|
|
|
return this.destroyOnClose && !this.visible;
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2018-12-08 19:07:04 +08:00
|
|
|
// get drawar push width or height
|
2019-01-12 11:33:27 +08:00
|
|
|
getPushTransform(placement) {
|
2018-12-08 19:07:04 +08:00
|
|
|
if (placement === 'left' || placement === 'right') {
|
2019-01-12 11:33:27 +08:00
|
|
|
return `translateX(${placement === 'left' ? 180 : -180}px)`;
|
2018-12-08 19:07:04 +08:00
|
|
|
}
|
|
|
|
if (placement === 'top' || placement === 'bottom') {
|
2019-01-12 11:33:27 +08:00
|
|
|
return `translateY(${placement === 'top' ? 180 : -180}px)`;
|
2018-12-08 19:07:04 +08:00
|
|
|
}
|
|
|
|
},
|
2019-04-07 11:46:22 +08:00
|
|
|
getRcDrawerStyle() {
|
2020-01-15 14:32:24 +08:00
|
|
|
const { zIndex, placement, wrapStyle } = this.$props;
|
2019-04-07 11:46:22 +08:00
|
|
|
const { _push: push } = this.$data;
|
|
|
|
return {
|
|
|
|
zIndex,
|
|
|
|
transform: push ? this.getPushTransform(placement) : undefined,
|
2020-01-15 14:32:24 +08:00
|
|
|
...wrapStyle,
|
2019-04-07 11:46:22 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
renderHeader(prefixCls) {
|
2020-03-07 19:45:13 +08:00
|
|
|
const { closable, headerStyle } = this.$props;
|
2020-06-02 23:21:24 +08:00
|
|
|
const title = getComponent(this, 'title');
|
2019-04-07 11:46:22 +08:00
|
|
|
if (!title && !closable) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const headerClassName = title ? `${prefixCls}-header` : `${prefixCls}-header-no-title`;
|
|
|
|
return (
|
2020-03-07 19:45:13 +08:00
|
|
|
<div class={headerClassName} style={headerStyle}>
|
2019-04-07 11:46:22 +08:00
|
|
|
{title && <div class={`${prefixCls}-title`}>{title}</div>}
|
|
|
|
{closable ? this.renderCloseIcon(prefixCls) : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
renderCloseIcon(prefixCls) {
|
2020-03-07 19:45:13 +08:00
|
|
|
const { closable } = this;
|
2019-04-07 11:46:22 +08:00
|
|
|
return (
|
2020-03-07 19:45:13 +08:00
|
|
|
closable && (
|
|
|
|
<button key="closer" onClick={this.close} aria-label="Close" class={`${prefixCls}-close`}>
|
2020-03-26 12:39:39 +08:00
|
|
|
<CloseOutlined />
|
2020-03-07 19:45:13 +08:00
|
|
|
</button>
|
|
|
|
)
|
2019-04-07 11:46:22 +08:00
|
|
|
);
|
|
|
|
},
|
2018-12-08 19:07:04 +08:00
|
|
|
// render drawer body dom
|
2019-04-07 11:46:22 +08:00
|
|
|
renderBody(prefixCls) {
|
|
|
|
if (this.destroyClose && !this.visible) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return null;
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
2019-04-07 11:46:22 +08:00
|
|
|
this.destroyClose = false;
|
2020-03-07 19:45:13 +08:00
|
|
|
const { bodyStyle, drawerStyle } = this.$props;
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
const containerStyle = {};
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2019-04-07 11:46:22 +08:00
|
|
|
const isDestroyOnClose = this.getDestroyOnClose();
|
2018-09-05 21:28:54 +08:00
|
|
|
if (isDestroyOnClose) {
|
|
|
|
// Increase the opacity transition, delete children after closing.
|
2019-01-12 11:33:27 +08:00
|
|
|
containerStyle.opacity = 0;
|
|
|
|
containerStyle.transition = 'opacity .3s';
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
class={`${prefixCls}-wrapper-body`}
|
2020-01-15 14:32:24 +08:00
|
|
|
style={{ ...containerStyle, ...drawerStyle }}
|
2019-04-07 11:46:22 +08:00
|
|
|
onTransitionend={this.onDestroyTransitionEnd}
|
2018-09-05 21:28:54 +08:00
|
|
|
>
|
2019-04-07 11:46:22 +08:00
|
|
|
{this.renderHeader(prefixCls)}
|
2019-09-10 18:57:08 +08:00
|
|
|
<div key="body" class={`${prefixCls}-body`} style={bodyStyle}>
|
2020-06-02 22:09:34 +08:00
|
|
|
{this.$slots.default && this.$slots.default()}
|
2018-09-05 21:28:54 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
|
|
|
const props = getOptionProps(this);
|
2019-04-07 11:46:22 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2019-05-28 11:37:38 +08:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
visible,
|
|
|
|
placement,
|
|
|
|
wrapClassName,
|
2020-03-07 19:45:13 +08:00
|
|
|
mask,
|
2019-05-28 11:37:38 +08:00
|
|
|
...rest
|
2019-04-07 11:46:22 +08:00
|
|
|
} = props;
|
2020-03-07 19:45:13 +08:00
|
|
|
const haveMask = mask ? '' : 'no-mask';
|
2019-01-12 11:33:27 +08:00
|
|
|
const offsetStyle = {};
|
2018-12-08 19:07:04 +08:00
|
|
|
if (placement === 'left' || placement === 'right') {
|
2019-01-12 11:33:27 +08:00
|
|
|
offsetStyle.width = typeof width === 'number' ? `${width}px` : width;
|
2018-12-08 19:07:04 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
offsetStyle.height = typeof height === 'number' ? `${height}px` : height;
|
2018-12-08 19:07:04 +08:00
|
|
|
}
|
2020-06-02 23:21:24 +08:00
|
|
|
const handler = getComponent(this, 'handle') || false;
|
2019-09-11 22:35:25 +08:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-04-07 11:46:22 +08:00
|
|
|
const prefixCls = getPrefixCls('drawer', customizePrefixCls);
|
|
|
|
|
2018-09-05 21:28:54 +08:00
|
|
|
const vcDrawerProps = {
|
2020-06-02 23:21:24 +08:00
|
|
|
...this.$attrs,
|
2020-06-02 22:09:34 +08:00
|
|
|
...omit(rest, [
|
|
|
|
'closable',
|
|
|
|
'destroyOnClose',
|
|
|
|
'drawerStyle',
|
|
|
|
'headerStyle',
|
|
|
|
'bodyStyle',
|
|
|
|
'title',
|
|
|
|
'push',
|
|
|
|
'visible',
|
|
|
|
'getPopupContainer',
|
|
|
|
'rootPrefixCls',
|
|
|
|
'getPrefixCls',
|
|
|
|
'renderEmpty',
|
|
|
|
'csp',
|
|
|
|
'pageHeader',
|
|
|
|
'autoInsertSpaceInButton',
|
|
|
|
]),
|
|
|
|
handler,
|
|
|
|
...offsetStyle,
|
|
|
|
prefixCls,
|
|
|
|
open: visible,
|
|
|
|
showMask: mask,
|
|
|
|
placement,
|
|
|
|
className: classnames({
|
|
|
|
[wrapClassName]: !!wrapClassName,
|
|
|
|
[haveMask]: !!haveMask,
|
|
|
|
}),
|
|
|
|
wrapStyle: this.getRcDrawerStyle(),
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2020-06-02 23:21:24 +08:00
|
|
|
return <VcDrawer {...vcDrawerProps}>{this.renderBody(prefixCls)}</VcDrawer>;
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-09-19 13:21:57 +08:00
|
|
|
|
|
|
|
/* istanbul ignore next */
|
2020-06-02 22:09:34 +08:00
|
|
|
Drawer.install = function(app) {
|
|
|
|
app.component(Drawer.name, Drawer);
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-09-19 13:21:57 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
export default Drawer;
|