(component): [tabitem] title 支持渲染函数

This commit is contained in:
sight 2022-08-27 20:41:06 +08:00
parent 8820748d8c
commit f30012c852
4 changed files with 38 additions and 13 deletions

View File

@ -8,7 +8,6 @@ export default {
import "./index.less"; import "./index.less";
import { LayIcon } from "@layui/icons-vue"; import { LayIcon } from "@layui/icons-vue";
import tabItem from "../tabItem/index.vue"; import tabItem from "../tabItem/index.vue";
import RenderTitle from "./renderTitle.vue";
import RenderFunction from "../_components/renderFunction"; import RenderFunction from "../_components/renderFunction";
import { import {
Component, Component,
@ -26,6 +25,7 @@ import {
reactive, reactive,
h, h,
createTextVNode, createTextVNode,
isVNode,
} from "vue"; } from "vue";
import { useResizeObserver } from "@vueuse/core"; import { useResizeObserver } from "@vueuse/core";
import { TabData, TabInjectKey } from "./interface"; import { TabData, TabInjectKey } from "./interface";
@ -268,9 +268,18 @@ const update = () => {
}; };
const renderTabChild = (child: TabData) => { const renderTabChild = (child: TabData) => {
return child.slots?.title
? () => h("span", child.slots?.title && child.slots.title()) if (child.slots?.title){
: () => createTextVNode(child.title); return () => h("span", child.slots?.title && child.slots.title())
}
if (typeof child.title === "function"){
// @ts-ignore
return () => child.title();
} else if (typeof child.title === "string"){
return () => createTextVNode(child.title as string);
}
}; };
useResizeObserver(navRef, update); useResizeObserver(navRef, update);

View File

@ -4,7 +4,7 @@ export const TabInjectKey = Symbol("layuiTab");
export interface TabData { export interface TabData {
id: string; id: string;
title?: string; title?: string | Function;
closable?: string | boolean; closable?: string | boolean;
slots: Slots; slots: Slots;
} }

View File

@ -20,7 +20,7 @@ import { TabInjectKey, TabsContext } from "../tab/interface";
export interface LayTabItemProps { export interface LayTabItemProps {
id: string; id: string;
title: string; title?: string | Function;
closable?: boolean | string; closable?: boolean | string;
} }

View File

@ -43,40 +43,56 @@ export default {
<template> <template>
<lay-tab v-model="current11" :allow-close="true"> <lay-tab v-model="current11" :allow-close="true">
<lay-tab-item title="选项一" id="1"> <lay-tab-item id="1">
<template #title> <template #title>
<lay-icon type="layui-icon-console"></lay-icon> <lay-icon type="layui-icon-console"></lay-icon>
<span style="margin-left:10px">选项一</span> <span style="margin-left:10px">选项一</span>
</template> </template>
<div style="padding:20px">选项一</div> <div style="padding:20px">选项一</div>
</lay-tab-item> </lay-tab-item>
<lay-tab-item title="选项一" id="2"> <lay-tab-item id="2">
<template #title> <template #title>
<lay-icon type="layui-icon-user"></lay-icon> <lay-icon type="layui-icon-user"></lay-icon>
<span style="margin-left:10px">选项</span> <span style="margin-left:10px">选项</span>
</template> </template>
<div style="padding:20px">选项二</div> <div style="padding:20px">选项二</div>
</lay-tab-item> </lay-tab-item>
<lay-tab-item title="选项一" id="3"> <lay-tab-item id="3">
<template #title> <template #title>
<lay-icon type="layui-icon-set"></lay-icon> <lay-icon type="layui-icon-set"></lay-icon>
<span style="margin-left:10px">选项</span> <span style="margin-left:10px">选项</span>
</template> </template>
<div style="padding:20px">选项三</div> <div style="padding:20px">选项三</div>
</lay-tab-item> </lay-tab-item>
<lay-tab-item id="4" :title="renderTitleFunc">
<div style="padding:20px">选项四</div>
</lay-tab-item>
</lay-tab> </lay-tab>
</template> </template>
<script> <script>
import { ref } from 'vue' import { ref,h , resolveComponent} from 'vue'
export default { export default {
setup() { setup() {
const LayIcon = resolveComponent("LayIcon");
const current11 = ref("1") const current11 = ref("1")
const renderTitleFunc = () => [
h(LayIcon,
{
type: "layui-icon-component",
}),
h("span",
{
style: "margin-left: 10px; color: red",
},
"选项四")
]
return { return {
current11 current11,
renderTitleFunc
} }
} }
} }