未验证 提交 f5ca2ebe 编写于 作者: xiaozhumaopao's avatar xiaozhumaopao 提交者: GitHub

fix(tab): 修复 switch-tab 事件失效 #635 (#637)

* fix: tab组件兼容taro

* fix: 完善tab文档和样式

* fix: 兼容小程序

* fix: 修改tab监听事件
Co-authored-by: richard_1015's avatarrichard1015 <51844712@qq.com>
上级 398ae748
...@@ -18,7 +18,11 @@ ...@@ -18,7 +18,11 @@
<h2>defaultIndex设置默认显示tab,iconType为half时切换选中icon样式</h2> <h2>defaultIndex设置默认显示tab,iconType为half时切换选中icon样式</h2>
<h2>switchTab监听切换tab返回事件</h2> <h2>switchTab监听切换tab返回事件</h2>
<nut-tab :default-index="1" @switch-tab="switchTab" icon-type="half"> <nut-tab
:default-index="defaultIndexs"
@switch-tab="switchTab"
icon-type="half"
>
<nut-tab-panel tab-title="全部" <nut-tab-panel tab-title="全部"
><p class="content">这里是页签全部内容</p></nut-tab-panel ><p class="content">这里是页签全部内容</p></nut-tab-panel
> >
...@@ -160,6 +164,7 @@ ...@@ -160,6 +164,7 @@
<script lang="ts"> <script lang="ts">
import { ref, reactive, toRefs } from 'vue'; import { ref, reactive, toRefs } from 'vue';
import { createComponent } from '../../utils/create'; import { createComponent } from '../../utils/create';
import func from './vue-temp/vue-editor-bridge';
const { createDemo } = createComponent('tab'); const { createDemo } = createComponent('tab');
export default createDemo({ export default createDemo({
props: {}, props: {},
...@@ -174,16 +179,18 @@ export default createDemo({ ...@@ -174,16 +179,18 @@ export default createDemo({
} }
] ]
}); });
const defaultIndexs = ref(1);
function changeList() { function changeList() {
resData.editList.push({ resData.editList.push({
title: '标签' + resData.editList.length title: '标签' + resData.editList.length
}); });
} }
function switchTab(activeInddex: number, event: MouseEvent) { function switchTab(activeInddex: number) {
console.log(activeInddex, event); console.log(activeInddex);
} }
return { return {
...toRefs(resData), ...toRefs(resData),
defaultIndexs,
changeList, changeList,
switchTab switchTab
}; };
......
...@@ -19,6 +19,7 @@ app.use(TabPanel); ...@@ -19,6 +19,7 @@ app.use(TabPanel);
app.use(Swiper); app.use(Swiper);
app.use(SwiperItem); app.use(SwiperItem);
``` ```
## 基础样式,默认 tab-title 宽度均分相等 ## 基础样式,默认 tab-title 宽度均分相等
...@@ -61,18 +62,16 @@ switchTab 监听切换 tab 返回事件 ...@@ -61,18 +62,16 @@ switchTab 监听切换 tab 返回事件
> >
</nut-tab> </nut-tab>
<script lang="ts"> <script lang="ts">
import { createComponent } from '@/packages/utils/create'; export default {
const { createDemo } = createComponent('tab');
export default createDemo({
setup() { setup() {
function switchTab(activeInddex: number, event: MouseEvent) { function switchTab(activeInddex: number) {
console.log(activeInddex, event); console.log(activeInddex);
} }
return { return {
switchTab switchTab
}; };
} }
}); }
</script> </script>
``` ```
...@@ -207,9 +206,7 @@ switchTab 监听切换 tab 返回事件 ...@@ -207,9 +206,7 @@ switchTab 监听切换 tab 返回事件
</div> </div>
<script lang="ts"> <script lang="ts">
import { ref , reactive,toRefs } from "vue"; import { ref , reactive,toRefs } from "vue";
import { createComponent } from '../../utils/create'; export default {
const { createDemo } = createComponent('tab');
export default createDemo({
setup() { setup() {
const resData = reactive({ const resData = reactive({
editList:[ editList:[
...@@ -231,7 +228,7 @@ export default createDemo({ ...@@ -231,7 +228,7 @@ export default createDemo({
changeList changeList
}; };
} }
}); }
</script> </script>
``` ```
...@@ -258,4 +255,4 @@ export default createDemo({ ...@@ -258,4 +255,4 @@ export default createDemo({
| 事件名称 | 说明 | 回调参数 | | 事件名称 | 说明 | 回调参数 |
| ---------- | ------------------ | ---------------------- | | ---------- | ------------------ | ---------------------- |
| switch-tab | 切换页签时触发事件 | 点击的索引值和触发元素 | | switch-tab | 切换页签时触发事件 | 当前活动状态的索引值 |
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
class="tab-title-scroll" class="tab-title-scroll"
:scroll-with-animation="true" :scroll-with-animation="true"
> >
<view :class="['tab-title', randomTitleClass, iconType]"> <view :class="['tab-title', randomTitleClass, iconType]" ref="navlist">
<view <view
:class="[ :class="[
'tab-title-box', 'tab-title-box',
...@@ -95,7 +95,7 @@ export default create({ ...@@ -95,7 +95,7 @@ export default create({
components: { components: {
TabTitle TabTitle
}, },
emits: ['switchTab'], emits: ['switch-tab'],
setup(props, ctx) { setup(props, ctx) {
const titles: Array<DataTitle> = reactive([]); const titles: Array<DataTitle> = reactive([]);
const isLock = ref(false); const isLock = ref(false);
...@@ -162,7 +162,6 @@ export default create({ ...@@ -162,7 +162,6 @@ export default create({
function switchTitle(index: number, event) { function switchTitle(index: number, event) {
activeIndex.value = index; activeIndex.value = index;
centerTitle(index); centerTitle(index);
ctx.emit('switchTab', index, event);
} }
function initTitle() { function initTitle() {
titles.length = 0; titles.length = 0;
...@@ -203,6 +202,15 @@ export default create({ ...@@ -203,6 +202,15 @@ export default create({
initTitle(); initTitle();
} }
); );
watchEffect(() => {
activeIndex.value = props.defaultIndex;
});
watch(
() => activeIndex.value,
(val, oldVal) => {
ctx.emit('switch-tab', activeIndex.value);
}
);
return { return {
swiperClassName, swiperClassName,
titles, titles,
......
...@@ -89,6 +89,7 @@ export default create({ ...@@ -89,6 +89,7 @@ export default create({
components: { components: {
TabTitle TabTitle
}, },
emits: ['switch-tab'],
setup(props, ctx) { setup(props, ctx) {
const titles: Array<DataTitle> = reactive([]); const titles: Array<DataTitle> = reactive([]);
const isLock = ref(false); const isLock = ref(false);
...@@ -127,7 +128,6 @@ export default create({ ...@@ -127,7 +128,6 @@ export default create({
} }
} }
const changeTab = (index: number) => { const changeTab = (index: number) => {
console.log(index);
activeIndex.value = index; activeIndex.value = index;
centerTitle(index); centerTitle(index);
...@@ -140,6 +140,7 @@ export default create({ ...@@ -140,6 +140,7 @@ export default create({
nutuiSwiper.value.to(index); nutuiSwiper.value.to(index);
} }
function initTitle() { function initTitle() {
console.log(11);
titles.length = 0; titles.length = 0;
if (ctx.slots.default) { if (ctx.slots.default) {
const slots: VNode[] = const slots: VNode[] =
...@@ -171,6 +172,15 @@ export default create({ ...@@ -171,6 +172,15 @@ export default create({
initTitle(); initTitle();
} }
); );
watchEffect(() => {
activeIndex.value = props.defaultIndex;
});
watch(
() => activeIndex.value,
(val, oldVal) => {
ictx.emit('switch-tab', activeIndex.value);
}
);
return { return {
swiperClassName, swiperClassName,
titles, titles,
......
...@@ -18,7 +18,11 @@ ...@@ -18,7 +18,11 @@
<h2>defaultIndex设置默认显示tab,iconType为half时切换选中icon样式</h2> <h2>defaultIndex设置默认显示tab,iconType为half时切换选中icon样式</h2>
<h2>switchTab监听切换tab返回事件</h2> <h2>switchTab监听切换tab返回事件</h2>
<nut-tab :default-index="1" @switch-tab="switchTab" icon-type="half"> <nut-tab
:default-index="currIndex"
@switch-tab="switchTab"
icon-type="half"
>
<nut-tab-panel tab-title="全部" <nut-tab-panel tab-title="全部"
><p class="content">这里是页签全部内容</p></nut-tab-panel ><p class="content">这里是页签全部内容</p></nut-tab-panel
> >
...@@ -155,17 +159,23 @@ export default { ...@@ -155,17 +159,23 @@ export default {
} }
] ]
}); });
const currIndex = ref(0);
function changeList() { function changeList() {
resData.editList.push({ resData.editList.push({
title: '标签' + resData.editList.length title: '标签' + resData.editList.length
}); });
} }
function switchTab(activeInddex: number, event: MouseEvent) { function switchTab(activeInddex: number) {
console.log(activeInddex, event); console.log(activeInddex);
}
function changeIndex() {
let aa = currIndex.value;
currIndex.value = aa + 1;
} }
return { return {
...toRefs(resData), ...toRefs(resData),
changeList, changeList,
currIndex,
switchTab switchTab
}; };
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册