[bugfix] Tab: props observer (#553)

This commit is contained in:
neverland 2018-09-11 10:08:41 +08:00 committed by GitHub
parent 0fe8d5f915
commit c166c8e36c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 76 additions and 34 deletions

View File

@ -9,7 +9,13 @@ export const basic = Behavior({
wx.createSelectorQuery() wx.createSelectorQuery()
.in(this)[all ? 'selectAll' : 'select'](selector) .in(this)[all ? 'selectAll' : 'select'](selector)
.boundingClientRect(rect => { .boundingClientRect(rect => {
rect && resolve(rect); if (all && Array.isArray(rect) && rect.length) {
resolve(rect);
}
if (!all && rect) {
resolve(rect);
}
}) })
.exec(); .exec();
}); });

11
dist/tab/index.js vendored
View File

@ -2,13 +2,22 @@ import { create } from '../common/create';
create({ create({
props: { props: {
disabled: Boolean, disabled: {
type: Boolean,
observer() {
const parent = this.getRelationNodes('../tabs/index')[0];
if (parent) {
parent.updateTabs();
}
}
},
title: { title: {
type: String, type: String,
observer() { observer() {
const parent = this.getRelationNodes('../tabs/index')[0]; const parent = this.getRelationNodes('../tabs/index')[0];
if (parent) { if (parent) {
parent.setLine(); parent.setLine();
parent.updateTabs();
} }
} }
} }

36
dist/tabs/index.js vendored
View File

@ -5,21 +5,16 @@ create({
'../tab/index': { '../tab/index': {
type: 'descendant', type: 'descendant',
linked(target) { linked(child) {
const { tabs } = this.data; this.data.tabs.push({
tabs.push({ instance: child,
instance: target, data: child.data
data: target.data
}); });
this.setData({ this.updateTabs();
tabs,
scrollable: tabs.length > this.data.swipeThreshold
});
this.setActiveTab();
}, },
unlinked(target) { unlinked(child) {
const tabs = this.data.tabs.filter(item => item.instance !== target); const tabs = this.data.tabs.filter(item => item.instance !== child);
this.setData({ this.setData({
tabs, tabs,
scrollable: tabs.length > this.data.swipeThreshold scrollable: tabs.length > this.data.swipeThreshold
@ -40,7 +35,8 @@ create({
}, },
active: { active: {
type: null, type: null,
value: 0 value: 0,
observer: 'setActiveTab'
}, },
type: { type: {
type: String, type: String,
@ -73,6 +69,15 @@ create({
}, },
methods: { methods: {
updateTabs() {
const { tabs } = this.data;
this.setData({
tabs,
scrollable: tabs.length > this.data.swipeThreshold
});
this.setActiveTab();
},
trigger(eventName, index) { trigger(eventName, index) {
this.$emit(eventName, { this.$emit(eventName, {
index, index,
@ -95,8 +100,6 @@ create({
this.trigger('change', active); this.trigger('change', active);
this.setData({ active }); this.setData({ active });
this.setActiveTab(); this.setActiveTab();
this.setLine();
this.scrollIntoView();
} }
}, },
@ -138,6 +141,9 @@ create({
item.instance.setData(data); item.instance.setData(data);
} }
}); });
this.setLine();
this.scrollIntoView();
}, },
// scroll active tab into view // scroll active tab into view

View File

@ -9,7 +9,13 @@ export const basic = Behavior({
wx.createSelectorQuery() wx.createSelectorQuery()
.in(this)[all ? 'selectAll' : 'select'](selector) .in(this)[all ? 'selectAll' : 'select'](selector)
.boundingClientRect(rect => { .boundingClientRect(rect => {
rect && resolve(rect); if (all && Array.isArray(rect) && rect.length) {
resolve(rect);
}
if (!all && rect) {
resolve(rect);
}
}) })
.exec(); .exec();
}); });

View File

@ -2,13 +2,22 @@ import { create } from '../common/create';
create({ create({
props: { props: {
disabled: Boolean, disabled: {
type: Boolean,
observer() {
const parent = this.getRelationNodes('../tabs/index')[0];
if (parent) {
parent.updateTabs();
}
}
},
title: { title: {
type: String, type: String,
observer() { observer() {
const parent = this.getRelationNodes('../tabs/index')[0]; const parent = this.getRelationNodes('../tabs/index')[0];
if (parent) { if (parent) {
parent.setLine(); parent.setLine();
parent.updateTabs();
} }
} }
} }

View File

@ -5,21 +5,16 @@ create({
'../tab/index': { '../tab/index': {
type: 'descendant', type: 'descendant',
linked(target) { linked(child) {
const { tabs } = this.data; this.data.tabs.push({
tabs.push({ instance: child,
instance: target, data: child.data
data: target.data
}); });
this.setData({ this.updateTabs();
tabs,
scrollable: tabs.length > this.data.swipeThreshold
});
this.setActiveTab();
}, },
unlinked(target) { unlinked(child) {
const tabs = this.data.tabs.filter(item => item.instance !== target); const tabs = this.data.tabs.filter(item => item.instance !== child);
this.setData({ this.setData({
tabs, tabs,
scrollable: tabs.length > this.data.swipeThreshold scrollable: tabs.length > this.data.swipeThreshold
@ -40,7 +35,8 @@ create({
}, },
active: { active: {
type: null, type: null,
value: 0 value: 0,
observer: 'setActiveTab'
}, },
type: { type: {
type: String, type: String,
@ -73,6 +69,15 @@ create({
}, },
methods: { methods: {
updateTabs() {
const { tabs } = this.data;
this.setData({
tabs,
scrollable: tabs.length > this.data.swipeThreshold
});
this.setActiveTab();
},
trigger(eventName, index) { trigger(eventName, index) {
this.$emit(eventName, { this.$emit(eventName, {
index, index,
@ -95,8 +100,6 @@ create({
this.trigger('change', active); this.trigger('change', active);
this.setData({ active }); this.setData({ active });
this.setActiveTab(); this.setActiveTab();
this.setLine();
this.scrollIntoView();
} }
}, },
@ -138,6 +141,9 @@ create({
item.instance.setData(data); item.instance.setData(data);
} }
}); });
this.setLine();
this.scrollIntoView();
}, },
// scroll active tab into view // scroll active tab into view