[完善]tab关闭和切换tab操作

This commit is contained in:
xumi 2021-12-10 08:01:48 +08:00
parent aa04ac9ad1
commit 06d38985c2
3 changed files with 92 additions and 15 deletions

View File

@ -94,6 +94,7 @@ export default {
<lay-tab type="card" v-model="current4" :allow-close="allowClose" @change="change" @close="close"> <lay-tab type="card" v-model="current4" :allow-close="allowClose" @change="change" @close="close">
<lay-tab-item title="选项一" id="1"><div style="padding:20px">选项一</div></lay-tab-item> <lay-tab-item title="选项一" id="1"><div style="padding:20px">选项一</div></lay-tab-item>
<lay-tab-item title="选项二" id="2"><div style="padding:20px">选项二</div></lay-tab-item> <lay-tab-item title="选项二" id="2"><div style="padding:20px">选项二</div></lay-tab-item>
<lay-tab-item title="选项三" id="3"><div style="padding:20px">选项三</div></lay-tab-item>
</lay-tab> </lay-tab>
</template> </template>
@ -111,7 +112,7 @@ export default {
} }
const close = function(id){ const close = function(id){
console.log("需要关闭:" + id) console.log("当前关闭:" + id)
} }
return { return {
@ -126,14 +127,64 @@ export default {
::: :::
::: title 控制是否关闭
:::
::: demo
<template>
<lay-tab type="card" v-model="current5" allow-close @change="change5" @close="close5" :beforeClose="beforeClose">
<lay-tab-item title="选项一" id="1"><div style="padding:20px">选项一</div></lay-tab-item>
<lay-tab-item title="选项二" id="2"><div style="padding:20px">选项二</div></lay-tab-item>
<lay-tab-item title="选项三" id="3"><div style="padding:20px">选项三</div></lay-tab-item>
<lay-tab-item title="选项四" id="4"><div style="padding:20px">选项四</div></lay-tab-item>
<lay-tab-item title="选项五" id="5"><div style="padding:20px">选项五</div></lay-tab-item>
</lay-tab>
<div style="color:#ff5722;">id为单数的可以关闭</div>
</template>
<script>
import { ref } from 'vue'
export default {
setup() {
const current5 = ref("1")
const change5 = function(id){
console.log("当前值:" +id)
}
const beforeClose = function(id){
return parseInt(id) % 2 === 1;
}
const close5 = function(id){
console.log("当前关闭:" + id)
}
return {
current5,
change5,
beforeClose
close5
}
}
}
</script>
:::
::: title 嵌套循环 ::: title 嵌套循环
::: :::
::: demo ::: demo
<template> <template>
<lay-tab type="card" v-model="current5" @change="change5"> <lay-tab type="card" v-model="current6" @change="change6">
<lay-tab-item v-for="a in arr" :key="a" :title="a.title" :id="a.id"> <lay-tab-item v-for="a in arr" :key="a" :title="a.title" :id="a.id">
内容{{a.id}}
</lay-tab-item> </lay-tab-item>
</lay-tab> </lay-tab>
</template> </template>
@ -144,8 +195,8 @@ import { ref } from 'vue'
export default { export default {
setup() { setup() {
const current5 = ref('1') const current6 = ref('1')
const change5 = function(id){ const change6 = function(id){
alert(id) alert(id)
} }
@ -157,7 +208,7 @@ export default {
arr.value.push({id:'3', title:'选项三'}) arr.value.push({id:'3', title:'选项三'})
return { return {
current5, current6,
arr arr
} }
} }
@ -176,6 +227,8 @@ export default {
| v-model | 当前激活 | -- | | v-model | 当前激活 | -- |
| type | 主题样式 | -- | | type | 主题样式 | -- |
| allow-close | 允许关闭 | `true` `false` | | allow-close | 允许关闭 | `true` `false` |
| before-close | `Function`关闭之前的回调钩子函数 | 参数(`id`), `return false` 表示不进行关闭 |
| before-leave | `Function`切换标签之前的回调钩子函数 | 参数(`id`), `return false` 表示不进行切换 |
::: :::
@ -186,7 +239,7 @@ export default {
| 事件 | 描述 | 参数 | | 事件 | 描述 | 参数 |
| ------ | -------- | ---- | | ------ | -------- | ---- |
| change | 选中切换 | -- | | change | 选中切换 | id |
| close | 关闭事件 | -- | | close | 关闭事件 | id |
::: :::

View File

@ -1,8 +1,8 @@
<template> <template>
<div class="layui-tab" :class="[type ? 'layui-tab-' + type : '']"> <div class="layui-tab" :class="[type ? 'layui-tab-' + type : '']" v-if="active">
<ul class="layui-tab-title"> <ul class="layui-tab-title">
<li <li
v-for="children in childrens" v-for="(children, index) in childrens"
:key="children" :key="children"
:class="[children.props.id === active ? 'layui-this' : '']" :class="[children.props.id === active ? 'layui-this' : '']"
@click.stop="change(children.props.id)" @click.stop="change(children.props.id)"
@ -11,7 +11,7 @@
<i <i
v-if="allowClose" v-if="allowClose"
class="layui-icon layui-icon-close layui-unselect layui-tab-close" class="layui-icon layui-icon-close layui-unselect layui-tab-close"
@click.stop="close(children.props.id)" @click.stop="close(index, children.props.id)"
></i> ></i>
</li> </li>
</ul> </ul>
@ -60,6 +60,8 @@ const props = defineProps<{
type?: string type?: string
modelValue: string modelValue: string
allowClose?: boolean allowClose?: boolean
beforeClose?: Function
beforeLeave?: Function
}>() }>()
const emit = defineEmits(['update:modelValue', 'change', 'close']) const emit = defineEmits(['update:modelValue', 'change', 'close'])
@ -75,11 +77,27 @@ const active = computed({
const slotsChange = ref(true) const slotsChange = ref(true)
const change = function (id: any) { const change = function (id: any) {
// return false, tab
if (props.beforeLeave && props.beforeLeave(id) === false) {
return ;
}
emit('update:modelValue', id) emit('update:modelValue', id)
emit('change', id) emit('change', id)
} }
const close = function (id: any) { const close = function (index : number, id: any) {
// return false, tab
if (props.beforeClose && props.beforeClose(id) === false) {
return ;
}
// tab
childrens.value.splice(index, 1);
// tabtab
if (active.value === id) {
const nextChildren = childrens.value[(index === childrens.value.length ? 0 : index)];
change(nextChildren && nextChildren.props ? nextChildren.props.id : "")
}
emit('close', id) emit('close', id)
} }

View File

@ -11,11 +11,17 @@ export default {
</script> </script>
<script setup name="LayTabItem" lang="ts"> <script setup name="LayTabItem" lang="ts">
import { defineProps, inject, defineEmits, Ref } from 'vue' import { withDefaults, defineProps, inject, Ref } from 'vue'
const props = defineProps<{ const props = withDefaults(
id?: string defineProps<{
}>() id: string
title: string
closable?: boolean
}>(),{
closable: true
}
);
const active = inject('active') const active = inject('active')
const slotsChange: Ref<boolean> = inject('slotsChange') as Ref<boolean> const slotsChange: Ref<boolean> = inject('slotsChange') as Ref<boolean>