mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-24 18:36:51 +08:00
refactor(DropdownItem): remove portal mixin
This commit is contained in:
parent
5e5025d1cc
commit
0b208dc04b
@ -1,9 +1,10 @@
|
|||||||
|
import { Teleport } from 'vue';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { on, off } from '../utils/dom/event';
|
import { on, off } from '../utils/dom/event';
|
||||||
|
|
||||||
// Mixins
|
// Mixins
|
||||||
import { PortalMixin } from '../mixins/portal';
|
|
||||||
import { ChildrenMixin } from '../mixins/relation';
|
import { ChildrenMixin } from '../mixins/relation';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
@ -14,11 +15,12 @@ import Popup from '../popup';
|
|||||||
const [createComponent, bem] = createNamespace('dropdown-item');
|
const [createComponent, bem] = createNamespace('dropdown-item');
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
mixins: [PortalMixin({ ref: 'wrapper' }), ChildrenMixin('vanDropdownMenu')],
|
mixins: [ChildrenMixin('vanDropdownMenu')],
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
title: String,
|
title: String,
|
||||||
disabled: Boolean,
|
disabled: Boolean,
|
||||||
|
teleport: [String, Object],
|
||||||
modelValue: null,
|
modelValue: null,
|
||||||
titleClass: String,
|
titleClass: String,
|
||||||
options: {
|
options: {
|
||||||
@ -150,38 +152,42 @@ export default createComponent({
|
|||||||
style.bottom = `${offset}px`;
|
style.bottom = `${offset}px`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
const Content = (
|
||||||
<div>
|
<div
|
||||||
<div
|
vShow={this.showWrapper}
|
||||||
vShow={this.showWrapper}
|
ref="wrapper"
|
||||||
ref="wrapper"
|
style={style}
|
||||||
style={style}
|
class={bem([direction])}
|
||||||
class={bem([direction])}
|
onClick={this.onClickWrapper}
|
||||||
onClick={this.onClickWrapper}
|
>
|
||||||
|
<Popup
|
||||||
|
show={this.showPopup}
|
||||||
|
overlay={overlay}
|
||||||
|
class={bem('content')}
|
||||||
|
position={direction === 'down' ? 'top' : 'bottom'}
|
||||||
|
duration={this.transition ? duration : 0}
|
||||||
|
lazyRender={this.lazyRender}
|
||||||
|
overlayStyle={{ position: 'absolute' }}
|
||||||
|
closeOnClickOverlay={closeOnClickOverlay}
|
||||||
|
onOpen={this.onOpen}
|
||||||
|
onClose={this.onClose}
|
||||||
|
onOpened={this.onOpened}
|
||||||
|
onClosed={() => {
|
||||||
|
this.showWrapper = false;
|
||||||
|
this.$emit('closed');
|
||||||
|
}}
|
||||||
|
{...{ 'onUpdate:modelValue': this.onTogglePopup }}
|
||||||
>
|
>
|
||||||
<Popup
|
{Options}
|
||||||
show={this.showPopup}
|
{this.$slots.default?.()}
|
||||||
overlay={overlay}
|
</Popup>
|
||||||
class={bem('content')}
|
|
||||||
position={direction === 'down' ? 'top' : 'bottom'}
|
|
||||||
duration={this.transition ? duration : 0}
|
|
||||||
lazyRender={this.lazyRender}
|
|
||||||
overlayStyle={{ position: 'absolute' }}
|
|
||||||
closeOnClickOverlay={closeOnClickOverlay}
|
|
||||||
onOpen={this.onOpen}
|
|
||||||
onClose={this.onClose}
|
|
||||||
onOpened={this.onOpened}
|
|
||||||
onClosed={() => {
|
|
||||||
this.showWrapper = false;
|
|
||||||
this.$emit('closed');
|
|
||||||
}}
|
|
||||||
{...{ 'onUpdate:modelValue': this.onTogglePopup }}
|
|
||||||
>
|
|
||||||
{Options}
|
|
||||||
{this.$slots.default?.()}
|
|
||||||
</Popup>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (this.teleport) {
|
||||||
|
return <Teleport to={this.teleport}>{Content}</Teleport>;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Content;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -1,47 +0,0 @@
|
|||||||
function getElement(teleport) {
|
|
||||||
if (typeof teleport === 'string') {
|
|
||||||
return document.querySelector(teleport);
|
|
||||||
}
|
|
||||||
|
|
||||||
return teleport;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function PortalMixin({ ref, afterPortal } = {}) {
|
|
||||||
return {
|
|
||||||
props: {
|
|
||||||
teleport: [String, Object],
|
|
||||||
},
|
|
||||||
|
|
||||||
watch: {
|
|
||||||
teleport: 'portal',
|
|
||||||
},
|
|
||||||
|
|
||||||
mounted() {
|
|
||||||
if (this.teleport) {
|
|
||||||
this.portal();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
|
||||||
portal() {
|
|
||||||
const { teleport } = this;
|
|
||||||
const el = ref ? this.$refs[ref] : this.$el;
|
|
||||||
|
|
||||||
let container;
|
|
||||||
if (teleport) {
|
|
||||||
container = getElement(teleport);
|
|
||||||
} else if (this.$parent) {
|
|
||||||
container = this.$parent.$el;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (container && container !== el.parentNode) {
|
|
||||||
container.appendChild(el);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (afterPortal) {
|
|
||||||
afterPortal.call(this);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user