2021-06-26 01:35:40 +00:00
|
|
|
import type { CSSProperties } from 'vue';
|
|
|
|
import { inject, provide, nextTick, defineComponent } from 'vue';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classnames from '../_util/classNames';
|
2020-03-07 11:45:13 +00:00
|
|
|
import omit from 'omit.js';
|
2019-01-12 03:33:27 +00:00
|
|
|
import VcDrawer from '../vc-drawer/src';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
2020-03-26 04:39:39 +00:00
|
|
|
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
|
2020-06-02 15:21:24 +00:00
|
|
|
import { getComponent, getOptionProps } from '../_util/props-util';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-11-01 07:03:33 +00:00
|
|
|
import { tuple, withInstall } from '../_util/type';
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2020-10-15 09:58:05 +00:00
|
|
|
const PlacementTypes = tuple('top', 'right', 'bottom', 'left');
|
|
|
|
type placementType = typeof PlacementTypes[number];
|
|
|
|
const Drawer = defineComponent({
|
2018-09-05 13:28:54 +00:00
|
|
|
name: 'ADrawer',
|
2020-11-07 13:19:50 +00:00
|
|
|
mixins: [BaseMixin],
|
2020-06-02 15:21:24 +00:00
|
|
|
inheritAttrs: false,
|
2018-09-05 13:28:54 +00:00
|
|
|
props: {
|
2020-10-10 10:16:28 +00:00
|
|
|
closable: PropTypes.looseBool.def(true),
|
|
|
|
destroyOnClose: PropTypes.looseBool,
|
2018-09-05 13:28:54 +00:00
|
|
|
getContainer: PropTypes.any,
|
2020-10-10 10:16:28 +00:00
|
|
|
maskClosable: PropTypes.looseBool.def(true),
|
|
|
|
mask: PropTypes.looseBool.def(true),
|
2018-09-05 13:28:54 +00:00
|
|
|
maskStyle: PropTypes.object,
|
2019-01-02 13:59:35 +00:00
|
|
|
wrapStyle: PropTypes.object,
|
2019-09-10 10:57:08 +00:00
|
|
|
bodyStyle: PropTypes.object,
|
2020-03-07 11:45:13 +00:00
|
|
|
headerStyle: PropTypes.object,
|
2020-01-15 06:32:24 +00:00
|
|
|
drawerStyle: PropTypes.object,
|
2020-10-15 09:58:05 +00:00
|
|
|
title: PropTypes.VNodeChild,
|
2020-10-10 10:16:28 +00:00
|
|
|
visible: PropTypes.looseBool,
|
2018-09-05 13:28:54 +00:00
|
|
|
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,
|
2019-04-07 03:46:22 +00:00
|
|
|
prefixCls: PropTypes.string,
|
2020-10-15 09:58:05 +00:00
|
|
|
placement: PropTypes.oneOf(PlacementTypes).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
|
2020-10-15 09:58:05 +00:00
|
|
|
handle: PropTypes.VNodeChild,
|
2020-03-07 11:45:13 +00:00
|
|
|
afterVisibleChange: PropTypes.func,
|
2020-10-10 10:16:28 +00:00
|
|
|
keyboard: PropTypes.looseBool.def(true),
|
2020-07-29 12:57:32 +00:00
|
|
|
onClose: PropTypes.func,
|
|
|
|
'onUpdate:visible': PropTypes.func,
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2020-10-15 09:58:05 +00:00
|
|
|
setup(props) {
|
2020-09-30 02:47:18 +00:00
|
|
|
const configProvider = inject('configProvider', defaultConfigProvider);
|
2018-09-05 13:28:54 +00:00
|
|
|
return {
|
2020-06-02 14:09:34 +00:00
|
|
|
configProvider,
|
2020-10-15 09:58:05 +00:00
|
|
|
destroyClose: false,
|
|
|
|
preVisible: props.visible,
|
2020-10-19 08:43:10 +00:00
|
|
|
parentDrawer: inject('parentDrawer', null),
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2020-11-07 07:37:17 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
sPush: false,
|
|
|
|
};
|
|
|
|
},
|
2020-06-02 14:09:34 +00:00
|
|
|
beforeCreate() {
|
|
|
|
provide('parentDrawer', this);
|
|
|
|
},
|
2020-03-07 11:45:13 +00: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 03:33:27 +00:00
|
|
|
updated() {
|
2020-06-02 14:09:34 +00:00
|
|
|
nextTick(() => {
|
2018-09-05 13:28:54 +00:00
|
|
|
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
|
|
|
},
|
2020-06-11 08:13:09 +00:00
|
|
|
beforeUnmount() {
|
2020-03-07 11:45:13 +00:00
|
|
|
// unmount drawer in child, clear push.
|
|
|
|
if (this.parentDrawer) {
|
|
|
|
this.parentDrawer.pull();
|
|
|
|
}
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
methods: {
|
2020-11-10 07:13:31 +00:00
|
|
|
domFocus() {
|
|
|
|
if (this.$refs.vcDrawer) {
|
|
|
|
(this.$refs.vcDrawer as any).domFocus();
|
|
|
|
}
|
|
|
|
},
|
2020-10-15 09:58:05 +00:00
|
|
|
close(e: Event) {
|
2020-07-29 12:57:32 +00:00
|
|
|
this.$emit('update:visible', false);
|
2020-03-07 11:45:13 +00:00
|
|
|
this.$emit('close', e);
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
// onMaskClick(e) {
|
|
|
|
// if (!this.maskClosable) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// this.close(e);
|
|
|
|
// },
|
2019-01-12 03:33:27 +00:00
|
|
|
push() {
|
2018-09-05 13:28:54 +00:00
|
|
|
this.setState({
|
2020-10-15 09:58:05 +00:00
|
|
|
sPush: 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() {
|
2020-11-10 07:13:31 +00:00
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
sPush: false,
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
this.domFocus();
|
|
|
|
},
|
|
|
|
);
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-04-07 03:46:22 +00:00
|
|
|
onDestroyTransitionEnd() {
|
|
|
|
const isDestroyOnClose = this.getDestroyOnClose();
|
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-04-07 03:46:22 +00:00
|
|
|
this.destroyClose = true;
|
2021-08-10 06:36:28 +00:00
|
|
|
(this as any).$forceUpdate();
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-04-07 03:46:22 +00:00
|
|
|
getDestroyOnClose() {
|
2019-01-12 03:33:27 +00:00
|
|
|
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
|
2020-10-15 09:58:05 +00:00
|
|
|
getPushTransform(placement?: placementType) {
|
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
|
|
|
}
|
|
|
|
},
|
2019-04-07 03:46:22 +00:00
|
|
|
getRcDrawerStyle() {
|
2020-01-15 06:32:24 +00:00
|
|
|
const { zIndex, placement, wrapStyle } = this.$props;
|
2020-10-15 09:58:05 +00:00
|
|
|
const { sPush: push } = this.$data;
|
2019-04-07 03:46:22 +00:00
|
|
|
return {
|
|
|
|
zIndex,
|
|
|
|
transform: push ? this.getPushTransform(placement) : undefined,
|
2020-01-15 06:32:24 +00:00
|
|
|
...wrapStyle,
|
2019-04-07 03:46:22 +00:00
|
|
|
};
|
|
|
|
},
|
2020-10-15 09:58:05 +00:00
|
|
|
renderHeader(prefixCls: string) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { closable, headerStyle } = this.$props;
|
2020-06-02 15:21:24 +00:00
|
|
|
const title = getComponent(this, 'title');
|
2019-04-07 03:46:22 +00:00
|
|
|
if (!title && !closable) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const headerClassName = title ? `${prefixCls}-header` : `${prefixCls}-header-no-title`;
|
|
|
|
return (
|
2020-03-07 11:45:13 +00:00
|
|
|
<div class={headerClassName} style={headerStyle}>
|
2019-04-07 03:46:22 +00:00
|
|
|
{title && <div class={`${prefixCls}-title`}>{title}</div>}
|
|
|
|
{closable ? this.renderCloseIcon(prefixCls) : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
2020-10-15 09:58:05 +00:00
|
|
|
renderCloseIcon(prefixCls: string) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { closable } = this;
|
2019-04-07 03:46:22 +00:00
|
|
|
return (
|
2020-03-07 11:45:13 +00:00
|
|
|
closable && (
|
|
|
|
<button key="closer" onClick={this.close} aria-label="Close" class={`${prefixCls}-close`}>
|
2020-03-26 04:39:39 +00:00
|
|
|
<CloseOutlined />
|
2020-03-07 11:45:13 +00:00
|
|
|
</button>
|
|
|
|
)
|
2019-04-07 03:46:22 +00:00
|
|
|
);
|
|
|
|
},
|
2018-12-08 11:07:04 +00:00
|
|
|
// render drawer body dom
|
2020-10-15 09:58:05 +00:00
|
|
|
renderBody(prefixCls: string) {
|
2019-04-07 03:46:22 +00:00
|
|
|
if (this.destroyClose && !this.visible) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return null;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2019-04-07 03:46:22 +00:00
|
|
|
this.destroyClose = false;
|
2020-03-07 11:45:13 +00:00
|
|
|
const { bodyStyle, drawerStyle } = this.$props;
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2020-10-15 09:58:05 +00:00
|
|
|
const containerStyle: CSSProperties = {};
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-04-07 03:46:22 +00:00
|
|
|
const isDestroyOnClose = this.getDestroyOnClose();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
class={`${prefixCls}-wrapper-body`}
|
2020-01-15 06:32:24 +00:00
|
|
|
style={{ ...containerStyle, ...drawerStyle }}
|
2019-04-07 03:46:22 +00:00
|
|
|
onTransitionend={this.onDestroyTransitionEnd}
|
2018-09-05 13:28:54 +00:00
|
|
|
>
|
2019-04-07 03:46:22 +00:00
|
|
|
{this.renderHeader(prefixCls)}
|
2019-09-10 10:57:08 +00:00
|
|
|
<div key="body" class={`${prefixCls}-body`} style={bodyStyle}>
|
2020-10-24 12:36:57 +00:00
|
|
|
{this.$slots.default?.()}
|
2018-09-05 13:28:54 +00:00
|
|
|
</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
|
|
|
render() {
|
2020-10-15 09:58:05 +00:00
|
|
|
const props: any = getOptionProps(this);
|
2019-04-07 03:46:22 +00:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2019-05-28 03:37:38 +00:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
visible,
|
|
|
|
placement,
|
|
|
|
wrapClassName,
|
2020-03-07 11:45:13 +00:00
|
|
|
mask,
|
2019-05-28 03:37:38 +00:00
|
|
|
...rest
|
2019-04-07 03:46:22 +00:00
|
|
|
} = props;
|
2020-03-07 11:45:13 +00:00
|
|
|
const haveMask = mask ? '' : 'no-mask';
|
2020-10-15 09:58:05 +00:00
|
|
|
const offsetStyle: CSSProperties = {};
|
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
|
|
|
}
|
2020-06-02 15:21:24 +00:00
|
|
|
const handler = getComponent(this, 'handle') || false;
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-04-07 03:46:22 +00:00
|
|
|
const prefixCls = getPrefixCls('drawer', customizePrefixCls);
|
2020-11-14 05:56:39 +00:00
|
|
|
const { class: className } = this.$attrs;
|
2020-10-15 09:58:05 +00:00
|
|
|
const vcDrawerProps: any = {
|
2020-06-02 15:21:24 +00:00
|
|
|
...this.$attrs,
|
2020-06-02 14:09:34 +00:00
|
|
|
...omit(rest, [
|
|
|
|
'closable',
|
|
|
|
'destroyOnClose',
|
|
|
|
'drawerStyle',
|
|
|
|
'headerStyle',
|
|
|
|
'bodyStyle',
|
|
|
|
'title',
|
|
|
|
'push',
|
|
|
|
'visible',
|
|
|
|
'getPopupContainer',
|
|
|
|
'rootPrefixCls',
|
|
|
|
'getPrefixCls',
|
|
|
|
'renderEmpty',
|
|
|
|
'csp',
|
|
|
|
'pageHeader',
|
|
|
|
'autoInsertSpaceInButton',
|
|
|
|
]),
|
2020-07-29 12:57:32 +00:00
|
|
|
onClose: this.close,
|
2020-06-02 14:09:34 +00:00
|
|
|
handler,
|
|
|
|
...offsetStyle,
|
|
|
|
prefixCls,
|
|
|
|
open: visible,
|
|
|
|
showMask: mask,
|
|
|
|
placement,
|
2020-07-29 12:57:32 +00:00
|
|
|
class: classnames({
|
2020-11-14 05:56:39 +00:00
|
|
|
[className as string]: !!className,
|
2020-06-02 14:09:34 +00:00
|
|
|
[wrapClassName]: !!wrapClassName,
|
|
|
|
[haveMask]: !!haveMask,
|
|
|
|
}),
|
|
|
|
wrapStyle: this.getRcDrawerStyle(),
|
2020-11-10 07:13:31 +00:00
|
|
|
ref: 'vcDrawer',
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-06-02 15:21:24 +00:00
|
|
|
return <VcDrawer {...vcDrawerProps}>{this.renderBody(prefixCls)}</VcDrawer>;
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2020-10-15 09:58:05 +00:00
|
|
|
});
|
2018-09-19 05:21:57 +00:00
|
|
|
|
2020-11-01 07:03:33 +00:00
|
|
|
export default withInstall(Drawer);
|