mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
Merge pull request #148 from chenjiahan/dev
fix: tab's props not observable
This commit is contained in:
commit
ae2c2dbe32
@ -84,8 +84,7 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
isChecked() {
|
isChecked() {
|
||||||
const currentValue = this.currentValue;
|
const { currentValue } = this;
|
||||||
console.log('this.currentValue:', this.currentValue);
|
|
||||||
if ({}.toString.call(currentValue) === '[object Boolean]') {
|
if ({}.toString.call(currentValue) === '[object Boolean]') {
|
||||||
return currentValue;
|
return currentValue;
|
||||||
} else if (currentValue !== null && currentValue !== undefined) {
|
} else if (currentValue !== null && currentValue !== undefined) {
|
||||||
|
@ -1,35 +1,51 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="van-tab__pane" :class="classNames">
|
<div :class="['van-tab__pane', { 'van-tab__pane--select': key === $parent.curActive }]">
|
||||||
<slot></slot>
|
<slot></slot>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import findParent from '../mixins/findParent';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'van-tab',
|
name: 'van-tab',
|
||||||
|
|
||||||
|
mixins: [findParent],
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
// 选项卡头显示文字
|
|
||||||
title: {
|
title: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true
|
required: true
|
||||||
},
|
},
|
||||||
disabled: Boolean
|
disabled: Boolean
|
||||||
},
|
},
|
||||||
data() {
|
|
||||||
const nextIndex = this.$parent.tabs.length;
|
|
||||||
this.$parent.tabs.push({
|
|
||||||
title: this.title,
|
|
||||||
disabled: this.disabled,
|
|
||||||
index: nextIndex
|
|
||||||
});
|
|
||||||
|
|
||||||
|
data() {
|
||||||
|
this.findParentByComponentName('van-tabs');
|
||||||
|
const nextIndex = this.parentGroup.tabs.length;
|
||||||
|
this.updateParentData(nextIndex);
|
||||||
return {
|
return {
|
||||||
key: nextIndex
|
key: nextIndex
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
computed: {
|
|
||||||
classNames() {
|
watch: {
|
||||||
return { 'van-tab__pane--select': this.key === this.$parent.curActive };
|
title() {
|
||||||
|
this.updateParentData();
|
||||||
|
},
|
||||||
|
disabled() {
|
||||||
|
this.updateParentData();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
methods: {
|
||||||
|
updateParentData(nextIndex) {
|
||||||
|
const index = arguments.length ? nextIndex : this.key;
|
||||||
|
this.parentGroup.tabs.splice(index, 1, {
|
||||||
|
title: this.title,
|
||||||
|
disabled: this.disabled,
|
||||||
|
index
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -8,9 +8,9 @@
|
|||||||
v-for="(tab, index) in tabs"
|
v-for="(tab, index) in tabs"
|
||||||
:key="index"
|
:key="index"
|
||||||
class="van-tab van-hairline"
|
class="van-tab van-hairline"
|
||||||
:class="{'van-tab--active': index === curActive}"
|
:class="{ 'van-tab--active': index === curActive }"
|
||||||
ref="tabkey"
|
ref="tabkey"
|
||||||
@click="handleTabClick(index, tab)"
|
@click="handleTabClick(index)"
|
||||||
>
|
>
|
||||||
{{ tab.title }}
|
{{ tab.title }}
|
||||||
</div>
|
</div>
|
||||||
@ -27,9 +27,9 @@
|
|||||||
v-for="(tab, index) in tabs"
|
v-for="(tab, index) in tabs"
|
||||||
:key="index"
|
:key="index"
|
||||||
class="van-tab van-hairline"
|
class="van-tab van-hairline"
|
||||||
:class="{'van-tab--active': index === curActive}"
|
:class="{ 'van-tab--active': index === curActive }"
|
||||||
ref="tabkey"
|
ref="tabkey"
|
||||||
@click="handleTabClick(index, tab)"
|
@click="handleTabClick(index)"
|
||||||
>
|
>
|
||||||
{{ tab.title }}
|
{{ tab.title }}
|
||||||
</div>
|
</div>
|
||||||
@ -134,14 +134,8 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
/**
|
handleTabClick(index) {
|
||||||
* tab点击事件
|
if (this.tabs[index].disabled) {
|
||||||
*
|
|
||||||
* @param {number} index tab在tabs中的索引
|
|
||||||
* @param {Object} el tab的vue实例
|
|
||||||
*/
|
|
||||||
handleTabClick(index, el) {
|
|
||||||
if (el.disabled) {
|
|
||||||
this.$emit('disabled', index);
|
this.$emit('disabled', index);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<van-tabs :active="active" :duration="duration" @click="handleTabClick" @disabled="handleTabDisabledClick">
|
<van-tabs :active="active" :duration="duration" @click="handleTabClick" @disabled="handleTabDisabledClick">
|
||||||
<van-tab title="选项一">内容一</van-tab>
|
<van-tab :title="firstTabTitle" :disabled="firstTabDisabled">内容一</van-tab>
|
||||||
<van-tab title="选项二">内容二</van-tab>
|
<van-tab title="选项二">内容二</van-tab>
|
||||||
<van-tab title="选项三" disabled>内容三</van-tab>
|
<van-tab title="选项三" disabled>内容三</van-tab>
|
||||||
<van-tab title="选项四">内容四</van-tab>
|
<van-tab title="选项四">内容四</van-tab>
|
||||||
@ -17,6 +17,16 @@ export default {
|
|||||||
'van-tabs': Tabs
|
'van-tabs': Tabs
|
||||||
},
|
},
|
||||||
|
|
||||||
|
props: {
|
||||||
|
firstTabTitle: {
|
||||||
|
type: String,
|
||||||
|
default: '选项一'
|
||||||
|
},
|
||||||
|
firstTabDisabled: {
|
||||||
|
type: Boolean
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
active: 0,
|
active: 0,
|
||||||
|
@ -146,4 +146,15 @@ describe('Tabs', () => {
|
|||||||
done();
|
done();
|
||||||
}, 1200);
|
}, 1200);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('watch tab props changes', (done) => {
|
||||||
|
wrapper = mount(TabsTestComponent);
|
||||||
|
wrapper.vm.firstTabTitle = '测试标题';
|
||||||
|
wrapper.vm.firstTabDisabled = true;
|
||||||
|
|
||||||
|
wrapper.vm.$nextTick(() => {
|
||||||
|
expect(wrapper.find('.van-tab')[0].text().replace(/\n|\s/g, '')).to.equal('测试标题');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user