[new feature] Tabbar: use relation mixin

This commit is contained in:
陈嘉涵 2019-05-06 20:46:16 +08:00
parent 9c6c10fdcd
commit e6dbe06ee8
3 changed files with 15 additions and 18 deletions

View File

@ -15,7 +15,12 @@ export function ChildrenMixin(parent) {
created() { created() {
const { children } = this.parent; const { children } = this.parent;
const index = this.parent.slots().indexOf(this.$vnode); const index = this.parent.slots().indexOf(this.$vnode);
children.splice(index === -1 ? children.length : index, 0, this);
if (index === -1) {
children.push(this);
} else {
children.splice(index, 0, this);
}
}, },
beforeDestroy() { beforeDestroy() {

View File

@ -2,10 +2,13 @@ import { use } from '../utils';
import Icon from '../icon'; import Icon from '../icon';
import Info from '../info'; import Info from '../info';
import { route, routeProps } from '../utils/router'; import { route, routeProps } from '../utils/router';
import { ChildrenMixin } from '../mixins/relation';
const [sfc, bem] = use('tabbar-item'); const [sfc, bem] = use('tabbar-item');
export default sfc({ export default sfc({
mixins: [ChildrenMixin('vanTabbar')],
props: { props: {
...routeProps, ...routeProps,
icon: String, icon: String,
@ -19,17 +22,9 @@ export default sfc({
}; };
}, },
beforeCreate() {
this.$parent.items.push(this);
},
destroyed() {
this.$parent.items.splice(this.$parent.items.indexOf(this), 1);
},
methods: { methods: {
onClick(event) { onClick(event) {
this.$parent.onChange(this.$parent.items.indexOf(this)); this.parent.onChange(this.index);
this.$emit('click', event); this.$emit('click', event);
route(this.$router, this); route(this.$router, this);
} }
@ -37,7 +32,7 @@ export default sfc({
render(h) { render(h) {
const { icon, slots, active } = this; const { icon, slots, active } = this;
const color = this.$parent[active ? 'activeColor' : 'inactiveColor']; const color = this.parent[active ? 'activeColor' : 'inactiveColor'];
return ( return (
<div class={bem({ active })} style={{ color }} onClick={this.onClick}> <div class={bem({ active })} style={{ color }} onClick={this.onClick}>

View File

@ -1,13 +1,10 @@
import { use } from '../utils'; import { use } from '../utils';
import { ParentMixin } from '../mixins/relation';
const [sfc, bem] = use('tabbar'); const [sfc, bem] = use('tabbar');
export default sfc({ export default sfc({
data() { mixins: [ParentMixin('vanTabbar')],
return {
items: []
};
},
props: { props: {
value: Number, value: Number,
@ -25,7 +22,7 @@ export default sfc({
}, },
watch: { watch: {
items() { children() {
this.setActiveItem(); this.setActiveItem();
}, },
@ -36,7 +33,7 @@ export default sfc({
methods: { methods: {
setActiveItem() { setActiveItem() {
this.items.forEach((item, index) => { this.children.forEach((item, index) => {
item.active = index === this.value; item.active = index === this.value;
}); });
}, },