feat: update drawer

pull/802/head
wangxueliang 2019-04-07 11:46:22 +08:00
parent 7d6678265e
commit ca7a4becbc
2 changed files with 78 additions and 70 deletions

View File

@ -4,6 +4,7 @@ import PropTypes from '../_util/vue-types';
import BaseMixin from '../_util/BaseMixin'; import BaseMixin from '../_util/BaseMixin';
import Icon from '../icon'; import Icon from '../icon';
import { getComponentFromProp, getOptionProps } from '../_util/props-util'; import { getComponentFromProp, getOptionProps } from '../_util/props-util';
import { ConfigConsumerProps } from '../config-provider';
const Drawer = { const Drawer = {
name: 'ADrawer', name: 'ADrawer',
@ -20,7 +21,7 @@ const Drawer = {
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).def(256), width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).def(256),
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).def(256), height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).def(256),
zIndex: PropTypes.number, zIndex: PropTypes.number,
prefixCls: PropTypes.string.def('ant-drawer'), prefixCls: PropTypes.string,
placement: PropTypes.oneOf(['top', 'right', 'bottom', 'left']).def('right'), placement: PropTypes.oneOf(['top', 'right', 'bottom', 'left']).def('right'),
level: PropTypes.any.def(null), level: PropTypes.any.def(null),
wrapClassName: PropTypes.string, // not use class like react, vue will add class to root dom wrapClassName: PropTypes.string, // not use class like react, vue will add class to root dom
@ -28,7 +29,7 @@ const Drawer = {
}, },
mixins: [BaseMixin], mixins: [BaseMixin],
data() { data() {
this.destoryClose = false; this.destroyClose = false;
this.preVisible = this.$props.visible; this.preVisible = this.$props.visible;
return { return {
_push: false, _push: false,
@ -38,6 +39,7 @@ const Drawer = {
parentDrawer: { parentDrawer: {
default: () => null, default: () => null,
}, },
configProvider: { default: () => ({}) },
}, },
provide() { provide() {
return { return {
@ -79,18 +81,18 @@ const Drawer = {
_push: false, _push: false,
}); });
}, },
onDestoryTransitionEnd() { onDestroyTransitionEnd() {
const isDestroyOnClose = this.getDestoryOnClose(); const isDestroyOnClose = this.getDestroyOnClose();
if (!isDestroyOnClose) { if (!isDestroyOnClose) {
return; return;
} }
if (!this.visible) { if (!this.visible) {
this.destoryClose = true; this.destroyClose = true;
this.$forceUpdate(); this.$forceUpdate();
} }
}, },
getDestoryOnClose() { getDestroyOnClose() {
return this.destroyOnClose && !this.visible; return this.destroyOnClose && !this.visible;
}, },
// get drawar push width or height // get drawar push width or height
@ -102,65 +104,6 @@ const Drawer = {
return `translateY(${placement === 'top' ? 180 : -180}px)`; return `translateY(${placement === 'top' ? 180 : -180}px)`;
} }
}, },
// render drawer body dom
renderBody() {
if (this.destoryClose && !this.visible) {
return null;
}
this.destoryClose = false;
const { placement } = this.$props;
const containerStyle =
placement === 'left' || placement === 'right'
? {
overflow: 'auto',
height: '100%',
}
: {};
const isDestroyOnClose = this.getDestoryOnClose();
if (isDestroyOnClose) {
// Increase the opacity transition, delete children after closing.
containerStyle.opacity = 0;
containerStyle.transition = 'opacity .3s';
}
const { prefixCls, closable } = this.$props;
const title = getComponentFromProp(this, 'title');
// is have header dom
let header;
if (title) {
header = (
<div key="header" class={`${prefixCls}-header`}>
<div class={`${prefixCls}-title`}>{title}</div>
</div>
);
}
// is have closer button
let closer;
if (closable) {
closer = (
<button key="closer" onClick={this.close} aria-label="Close" class={`${prefixCls}-close`}>
<span class={`${prefixCls}-close-x`}>
<Icon type="close" />
</span>
</button>
);
}
return (
<div
class={`${prefixCls}-wrapper-body`}
style={containerStyle}
onTransitionend={this.onDestoryTransitionEnd}
>
{header}
{closer}
<div key="body" class={`${prefixCls}-body`}>
{this.$slots.default}
</div>
</div>
);
},
getRcDrawerStyle() { getRcDrawerStyle() {
const { zIndex, placement, maskStyle, wrapStyle } = this.$props; const { zIndex, placement, maskStyle, wrapStyle } = this.$props;
const { _push: push } = this.$data; const { _push: push } = this.$data;
@ -171,10 +114,74 @@ const Drawer = {
...wrapStyle, ...wrapStyle,
}; };
}, },
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>
);
},
// render drawer body dom
renderBody(prefixCls) {
if (this.destroyClose && !this.visible) {
return null;
}
this.destroyClose = false;
const {
placement,
} = this.$props;
const containerStyle =
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
class={`${prefixCls}-wrapper-body`}
style={containerStyle}
onTransitionend={this.onDestroyTransitionEnd}
>
{this.renderHeader(prefixCls)}
<div key="body" class={`${prefixCls}-body`}>
{this.$slots.default}
</div>
</div>
);
},
}, },
render() { render() {
const props = getOptionProps(this); const props = getOptionProps(this);
const { width, height, visible, placement, wrapClassName, ...rest } = props; const {
prefixCls: customizePrefixCls,
width, height, visible,
placement, wrapClassName, ...rest
} = props;
const haveMask = rest.mask ? '' : 'no-mask'; const haveMask = rest.mask ? '' : 'no-mask';
const offsetStyle = {}; const offsetStyle = {};
if (placement === 'left' || placement === 'right') { if (placement === 'left' || placement === 'right') {
@ -183,11 +190,15 @@ const Drawer = {
offsetStyle.height = typeof height === 'number' ? `${height}px` : height; offsetStyle.height = typeof height === 'number' ? `${height}px` : height;
} }
const handler = getComponentFromProp(this, 'handle') || false; const handler = getComponentFromProp(this, 'handle') || false;
const getPrefixCls = this.configProvider.getPrefixCls || ConfigConsumerProps.getPrefixCls;
const prefixCls = getPrefixCls('drawer', customizePrefixCls);
const vcDrawerProps = { const vcDrawerProps = {
props: { props: {
...rest, ...rest,
handler, handler,
...offsetStyle, ...offsetStyle,
prefixCls,
open: visible, open: visible,
showMask: props.mask, showMask: props.mask,
placement, placement,
@ -202,7 +213,7 @@ const Drawer = {
...this.$listeners, ...this.$listeners,
}, },
}; };
return <VcDrawer {...vcDrawerProps}>{this.renderBody()}</VcDrawer>; return <VcDrawer {...vcDrawerProps}>{this.renderBody(prefixCls)}</VcDrawer>;
}, },
}; };

View File

@ -1,5 +1,2 @@
import '../../style/index.less'; import '../../style/index.less';
import './index.less'; import './index.less';
// style dependencies
import '../../button/style';