[improvement] create emitter (#3656)

This commit is contained in:
neverland 2019-06-27 10:28:37 +08:00 committed by GitHub
parent c0e82773ea
commit 1d9301b158
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 14 deletions

View File

@ -54,6 +54,14 @@ export default sfc({
} }
}, },
beforeCreate() {
const createEmitter = eventName => () => this.$emit(eventName);
this.onOpen = createEmitter('open');
this.onClose = createEmitter('close');
this.onOpened = createEmitter('opened');
},
render(h) { render(h) {
const { const {
zIndex, zIndex,
@ -87,8 +95,6 @@ export default sfc({
); );
}); });
const emit = eventName => () => this.$emit(eventName);
const style = { zIndex }; const style = { zIndex };
if (direction === 'down') { if (direction === 'down') {
style.top = `${offset}px`; style.top = `${offset}px`;
@ -100,15 +106,15 @@ export default sfc({
<div vShow={this.showWrapper} style={style} class={bem([direction])}> <div vShow={this.showWrapper} style={style} class={bem([direction])}>
<Popup <Popup
vModel={this.showPopup} vModel={this.showPopup}
overlay={overlay}
class={bem('content')}
position={direction === 'down' ? 'top' : 'bottom'} position={direction === 'down' ? 'top' : 'bottom'}
duration={this.transition ? duration : 0} duration={this.transition ? duration : 0}
class={bem('content')}
overlay={overlay}
closeOnClickOverlay={closeOnClickOverlay} closeOnClickOverlay={closeOnClickOverlay}
overlayStyle={{ position: 'absolute' }} overlayStyle={{ position: 'absolute' }}
onOpen={emit('open')} onOpen={this.onOpen}
onOpened={emit('opened')} onClose={this.onClose}
onClose={emit('close')} onOpened={this.onOpened}
onClosed={() => { onClosed={() => {
this.transition = true; this.transition = true;
this.showWrapper = false; this.showWrapper = false;

View File

@ -26,13 +26,20 @@ export default sfc({
} }
}, },
beforeCreate() {
const createEmitter = eventName => event => this.$emit(eventName, event);
this.onClick = createEmitter('click');
this.onOpened = createEmitter('opened');
this.onClosed = createEmitter('closed');
},
render(h) { render(h) {
if (!this.shouldRender) { if (!this.shouldRender) {
return; return;
} }
const { position, duration } = this; const { position, duration } = this;
const emit = eventName => event => this.$emit(eventName, event);
const transitionName = const transitionName =
this.transition || this.transition ||
@ -46,14 +53,14 @@ export default sfc({
return ( return (
<transition <transition
name={transitionName} name={transitionName}
onAfterEnter={emit('opened')} onAfterEnter={this.onOpened}
onAfterLeave={emit('closed')} onAfterLeave={this.onClosed}
> >
<div <div
vShow={this.value} vShow={this.value}
style={style} style={style}
class={bem({ [position]: position })} class={bem({ [position]: position })}
onClick={emit('click')} onClick={this.onClick}
> >
{this.slots()} {this.slots()}
</div> </div>

View File

@ -20,7 +20,7 @@ function SkuActions(
slots: DefaultSlots, slots: DefaultSlots,
ctx: RenderContext<SkuActionsProps> ctx: RenderContext<SkuActionsProps>
) { ) {
const emit = (name: string) => () => { const createEmitter = (name: string) => () => {
props.skuEventBus.$emit(name); props.skuEventBus.$emit(name);
}; };
@ -32,7 +32,7 @@ function SkuActions(
size="large" size="large"
type="warning" type="warning"
text="加入购物车" text="加入购物车"
onClick={emit('sku:addCart')} onClick={createEmitter('sku:addCart')}
/> />
)} )}
<Button <Button
@ -40,7 +40,7 @@ function SkuActions(
size="large" size="large"
type="danger" type="danger"
text={props.buyText || '立即购买'} text={props.buyText || '立即购买'}
onClick={emit('sku:buy')} onClick={createEmitter('sku:buy')}
/> />
</div> </div>
); );