[完善]tab关闭和切换tab操作
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
<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">
|
||||
<li
|
||||
v-for="children in childrens"
|
||||
v-for="(children, index) in childrens"
|
||||
:key="children"
|
||||
:class="[children.props.id === active ? 'layui-this' : '']"
|
||||
@click.stop="change(children.props.id)"
|
||||
@@ -11,7 +11,7 @@
|
||||
<i
|
||||
v-if="allowClose"
|
||||
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>
|
||||
</li>
|
||||
</ul>
|
||||
@@ -60,6 +60,8 @@ const props = defineProps<{
|
||||
type?: string
|
||||
modelValue: string
|
||||
allowClose?: boolean
|
||||
beforeClose?: Function
|
||||
beforeLeave?: Function
|
||||
}>()
|
||||
|
||||
const emit = defineEmits(['update:modelValue', 'change', 'close'])
|
||||
@@ -75,11 +77,27 @@ const active = computed({
|
||||
const slotsChange = ref(true)
|
||||
|
||||
const change = function (id: any) {
|
||||
// 回调切换标签之前的回调钩子函数,只要不是return false, 则进行切换该tab
|
||||
if (props.beforeLeave && props.beforeLeave(id) === false) {
|
||||
return ;
|
||||
}
|
||||
emit('update:modelValue', 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);
|
||||
// 点击的是当前激活的tab,则需要切换到下一个tab
|
||||
if (active.value === id) {
|
||||
const nextChildren = childrens.value[(index === childrens.value.length ? 0 : index)];
|
||||
change(nextChildren && nextChildren.props ? nextChildren.props.id : "")
|
||||
}
|
||||
emit('close', id)
|
||||
}
|
||||
|
||||
|
||||
@@ -11,11 +11,17 @@ export default {
|
||||
</script>
|
||||
|
||||
<script setup name="LayTabItem" lang="ts">
|
||||
import { defineProps, inject, defineEmits, Ref } from 'vue'
|
||||
import { withDefaults, defineProps, inject, Ref } from 'vue'
|
||||
|
||||
const props = defineProps<{
|
||||
id?: string
|
||||
}>()
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
id: string
|
||||
title: string
|
||||
closable?: boolean
|
||||
}>(),{
|
||||
closable: true
|
||||
}
|
||||
);
|
||||
|
||||
const active = inject('active')
|
||||
const slotsChange: Ref<boolean> = inject('slotsChange') as Ref<boolean>
|
||||
|
||||
Reference in New Issue
Block a user