2019-01-12 11:33:27 +08: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';
|
2019-04-07 11:46:22 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2019-08-28 10:50:19 +08:00
|
|
|
import Base from '../base';
|
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',
|
|
|
|
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,
|
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,
|
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
|
|
|
},
|
|
|
|
inject: {
|
|
|
|
parentDrawer: {
|
2019-01-28 21:09:13 +08:00
|
|
|
default: () => null,
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2019-09-11 22:35:25 +08:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
provide() {
|
2018-09-05 21:28:54 +08:00
|
|
|
return {
|
|
|
|
parentDrawer: this,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
updated() {
|
2018-09-05 21:28:54 +08:00
|
|
|
this.$nextTick(() => {
|
|
|
|
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
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
close(e) {
|
2018-09-05 21:28:54 +08:00
|
|
|
if (this.visible !== undefined) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.$emit('close', e);
|
|
|
|
return;
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
onMaskClick(e) {
|
2018-09-05 21:28:54 +08:00
|
|
|
if (!this.maskClosable) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
this.close(e);
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
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() {
|
2019-09-10 18:57:08 +08:00
|
|
|
const { zIndex, placement } = this.$props;
|
2019-04-07 11:46:22 +08:00
|
|
|
const { _push: push } = this.$data;
|
|
|
|
return {
|
|
|
|
zIndex,
|
|
|
|
transform: push ? this.getPushTransform(placement) : undefined,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
renderHeader(prefixCls) {
|
|
|
|
const { closable } = this.$props;
|
|
|
|
const title = getComponentFromProp(this, 'title');
|
|
|
|
if (!title && !closable) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const headerClassName = title ? `${prefixCls}-header` : `${prefixCls}-header-no-title`;
|
|
|
|
return (
|
|
|
|
<div class={headerClassName}>
|
|
|
|
{title && <div class={`${prefixCls}-title`}>{title}</div>}
|
|
|
|
{closable ? this.renderCloseIcon(prefixCls) : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
renderCloseIcon(prefixCls) {
|
|
|
|
return (
|
|
|
|
<button key="closer" onClick={this.close} aria-label="Close" class={`${prefixCls}-close`}>
|
|
|
|
<Icon type="close" />
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
},
|
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;
|
2019-09-10 18:57:08 +08:00
|
|
|
const { placement, bodyStyle } = this.$props;
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const containerStyle =
|
|
|
|
placement === 'left' || placement === 'right'
|
|
|
|
? {
|
|
|
|
overflow: 'auto',
|
|
|
|
height: '100%',
|
|
|
|
}
|
|
|
|
: {};
|
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`}
|
|
|
|
style={containerStyle}
|
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}>
|
2018-09-05 21:28:54 +08:00
|
|
|
{this.$slots.default}
|
|
|
|
</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,
|
|
|
|
...rest
|
2019-04-07 11:46:22 +08:00
|
|
|
} = props;
|
2019-01-12 11:33:27 +08:00
|
|
|
const haveMask = rest.mask ? '' : 'no-mask';
|
|
|
|
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
|
|
|
}
|
2019-04-04 23:28:50 +08:00
|
|
|
const handler = getComponentFromProp(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 = {
|
|
|
|
props: {
|
2018-12-08 19:07:04 +08:00
|
|
|
...rest,
|
2019-04-04 23:28:50 +08:00
|
|
|
handler,
|
2018-12-08 19:07:04 +08:00
|
|
|
...offsetStyle,
|
2019-04-07 11:46:22 +08:00
|
|
|
prefixCls,
|
2018-09-05 21:28:54 +08:00
|
|
|
open: visible,
|
2018-12-08 19:07:04 +08:00
|
|
|
showMask: props.mask,
|
2018-09-05 21:28:54 +08:00
|
|
|
placement,
|
2019-01-02 11:04:38 +08:00
|
|
|
className: classnames({
|
|
|
|
[wrapClassName]: !!wrapClassName,
|
|
|
|
[haveMask]: !!haveMask,
|
|
|
|
}),
|
2019-01-02 21:59:35 +08:00
|
|
|
wrapStyle: this.getRcDrawerStyle(),
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
|
|
|
on: {
|
|
|
|
maskClick: this.onMaskClick,
|
|
|
|
...this.$listeners,
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2019-04-07 11:46:22 +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 */
|
2019-01-12 11:33:27 +08:00
|
|
|
Drawer.install = function(Vue) {
|
2019-08-28 10:50:19 +08:00
|
|
|
Vue.use(Base);
|
2019-01-12 11:33:27 +08:00
|
|
|
Vue.component(Drawer.name, Drawer);
|
|
|
|
};
|
2018-09-19 13:21:57 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
export default Drawer;
|