[improvement] extract relation mixin

This commit is contained in:
陈嘉涵 2019-05-06 20:30:54 +08:00
parent 7014d67704
commit 8de7a287f6
3 changed files with 47 additions and 23 deletions

View File

@ -2,34 +2,25 @@ import { use, isDef } from '../utils';
import Cell from '../cell'; import Cell from '../cell';
import Icon from '../icon'; import Icon from '../icon';
import Popup from '../popup'; import Popup from '../popup';
import { ChildrenMixin } from '../mixins/relation';
const [sfc, bem] = use('dropdown-item'); const [sfc, bem] = use('dropdown-item');
export default sfc({ export default sfc({
mixins: [ChildrenMixin('vanDropdownMenu')],
props: { props: {
value: null, value: null,
title: String, title: String,
options: Array options: Array
}, },
inject: ['vanDropdownMenu'],
data() { data() {
return { return {
show: false show: false
}; };
}, },
created() {
const { items } = this.vanDropdownMenu;
const index = this.vanDropdownMenu.slots().indexOf(this.$vnode);
items.splice(index === -1 ? items.length : index, 0, this);
},
beforeDestroy() {
this.vanDropdownMenu.items = this.vanDropdownMenu.items.filter(item => item !== this);
},
computed: { computed: {
displayTitle() { displayTitle() {
if (this.title) { if (this.title) {
@ -48,7 +39,7 @@ export default sfc({
}, },
render(h) { render(h) {
const { top, zIndex, activeColor } = this.vanDropdownMenu; const { top, zIndex, activeColor } = this.parent;
const Options = this.options.map(option => { const Options = this.options.map(option => {
const active = option.value === this.value; const active = option.value === this.value;

View File

@ -1,9 +1,12 @@
import { use } from '../utils'; import { use } from '../utils';
import { BLUE } from '../utils/color'; import { BLUE } from '../utils/color';
import { ParentMixin } from '../mixins/relation';
const [sfc, bem] = use('dropdown-menu'); const [sfc, bem] = use('dropdown-menu');
export default sfc({ export default sfc({
mixins: [ParentMixin('vanDropdownMenu')],
props: { props: {
zIndex: { zIndex: {
type: Number, type: Number,
@ -15,16 +18,9 @@ export default sfc({
} }
}, },
provide() {
return {
vanDropdownMenu: this
};
},
data() { data() {
return { return {
top: 0, top: 0
items: []
}; };
}, },
@ -34,7 +30,7 @@ export default sfc({
const rect = menu.getBoundingClientRect(); const rect = menu.getBoundingClientRect();
this.top = rect.y + rect.height; this.top = rect.y + rect.height;
this.items.forEach((item, index) => { this.children.forEach((item, index) => {
if (index === active) { if (index === active) {
item.toggle(); item.toggle();
} else { } else {
@ -45,7 +41,7 @@ export default sfc({
}, },
render(h) { render(h) {
const Titles = this.items.map((item, index) => ( const Titles = this.children.map((item, index) => (
<div <div
class={bem('item')} class={bem('item')}
onClick={() => { onClick={() => {

View File

@ -0,0 +1,37 @@
export function ChildrenMixin(parent) {
return {
inject: [parent],
computed: {
parent() {
return this[parent];
}
},
created() {
const { children } = this.parent;
const index = this.parent.slots().indexOf(this.$vnode);
children.splice(index === -1 ? children.length : index, 0, this);
},
beforeDestroy() {
this.parent.children = this.parent.children.filter(item => item !== this);
}
};
}
export function ParentMixin(parent) {
return {
provide() {
return {
[parent]: this
};
},
data() {
return {
children: []
};
}
};
}