(tree): 新增 title 插槽, 允许自定义节点

This commit is contained in:
就眠儀式 2022-05-29 18:04:55 +08:00
parent 4385d0cffe
commit a8237710e2
6 changed files with 72 additions and 25 deletions

View File

@ -105,18 +105,24 @@ const childrenIndentSize = props.currentIndentSize + props.indentSize;
whiteSpace: column.ellipsisTooltip ? 'nowrap' : 'normal', whiteSpace: column.ellipsisTooltip ? 'nowrap' : 'normal',
}" }"
> >
<!-- 树表占位与缩进 --> <!-- 树表占位与缩进 -->
<span v-if="expandSpace && index === 0" :style="{'margin-right': currentIndentSize + 'px'}"></span> <span
v-if="expandSpace && index === 0"
:style="{ 'margin-right': currentIndentSize + 'px' }"
></span>
<span v-if="expandSpace && (!data.children && !slot.expand) && index === 0" class="layui-table-cell-expand-icon-spaced"></span> <span
v-if="
expandSpace && !data.children && !slot.expand && index === 0
"
class="layui-table-cell-expand-icon-spaced"
></span>
<lay-icon <lay-icon
v-if="(slot.expand || data.children) && index === 0" v-if="(slot.expand || data.children) && index === 0"
class="layui-table-cell-expand-icon" class="layui-table-cell-expand-icon"
:type="expandIconType" :type="expandIconType"
@click="handleExpand" @click="handleExpand"
></lay-icon> ></lay-icon>
<lay-tooltip <lay-tooltip
@ -142,11 +148,18 @@ const childrenIndentSize = props.currentIndentSize + props.indentSize;
whiteSpace: column.ellipsisTooltip ? 'nowrap' : 'normal', whiteSpace: column.ellipsisTooltip ? 'nowrap' : 'normal',
}" }"
> >
<!-- 树表占位与缩进 --> <!-- 树表占位与缩进 -->
<span v-if="expandSpace && index === 0" :style="{'margin-right': currentIndentSize + 'px'}"></span> <span
v-if="expandSpace && index === 0"
:style="{ 'margin-right': currentIndentSize + 'px' }"
></span>
<span v-if="expandSpace && (!data.children && !slot.expand) && index === 0" class="layui-table-cell-expand-icon-spaced"></span> <span
v-if="
expandSpace && !data.children && !slot.expand && index === 0
"
class="layui-table-cell-expand-icon-spaced"
></span>
<lay-icon <lay-icon
v-if="(slot.expand || data.children) && index === 0" v-if="(slot.expand || data.children) && index === 0"
@ -201,4 +214,4 @@ const childrenIndentSize = props.currentIndentSize + props.indentSize;
</table-row> </table-row>
</template> </template>
</template> </template>
</template> </template>

View File

@ -5,13 +5,13 @@ export default {
</script> </script>
<script setup lang="ts"> <script setup lang="ts">
import { StringOrNumber, CustomKey, CustomString } from "./tree.type";
import { LayIcon } from "@layui/icons-vue"; import { LayIcon } from "@layui/icons-vue";
import LayCheckbox from "../checkbox/index.vue"; import LayCheckbox from "../checkbox/index.vue";
import { Ref, useSlots } from "vue"; import { Ref, useSlots } from "vue";
import { Tree } from "./tree"; import { Tree } from "./tree";
import { Nullable } from "../../types"; import { Nullable } from "../../types";
import LayTransition from "../transition/index.vue"; import LayTransition from "../transition/index.vue";
import { StringOrNumber, CustomKey, CustomString } from "./tree.type";
export interface TreeData { export interface TreeData {
id: CustomKey; id: CustomKey;
@ -131,7 +131,12 @@ function handleTitleClick(node: TreeData) {
}" }"
@click="handleTitleClick(node)" @click="handleTitleClick(node)"
> >
{{ node.title }} <template v-if="slots.title">
<slot name="title" :data="node"></slot>
</template>
<template v-else>
{{ node.title }}
</template>
</span> </span>
</div> </div>
</div> </div>
@ -149,7 +154,11 @@ function handleTitleClick(node: TreeData) {
:tree="tree" :tree="tree"
:only-icon-control="onlyIconControl" :only-icon-control="onlyIconControl"
@node-click="recursiveNodeClick" @node-click="recursiveNodeClick"
/> >
<template v-if="slots.title" v-slot:title="{ data }">
<slot name="title" :data="data"></slot>
</template>
</tree-node>
</div> </div>
</lay-transition> </lay-transition>
</div> </div>

View File

@ -3,19 +3,16 @@ export default {
name: "LayTree", name: "LayTree",
}; };
</script> </script>
<script lang="ts" setup> <script lang="ts" setup>
import TreeNode from "./TreeNode.vue"; import TreeNode from "./TreeNode.vue";
import { computed } from "vue"; import { computed, useSlots } from "vue";
import { useTree } from "./useTree"; import { useTree } from "./useTree";
import { TreeData } from "./tree"; import { TreeData } from "./tree";
import { StringFn, StringOrNumber, KeysType, EditType } from "./tree.type";
import "./index.less"; import "./index.less";
type StringFn = () => string; export interface OriginalTreeData {
type StringOrNumber = string | number;
type KeysType = (number | string)[];
type EditType = boolean | ("add" | "update" | "delete");
interface OriginalTreeData {
title: StringFn | string; title: StringFn | string;
id: StringOrNumber; id: StringOrNumber;
field: StringFn | string; field: StringFn | string;
@ -23,7 +20,7 @@ interface OriginalTreeData {
disabled?: boolean; disabled?: boolean;
} }
interface TreeProps { export interface TreeProps {
checkedKeys?: KeysType; checkedKeys?: KeysType;
data: OriginalTreeData; data: OriginalTreeData;
showCheckbox?: boolean; showCheckbox?: boolean;
@ -61,6 +58,8 @@ const props = withDefaults(defineProps<TreeProps>(), {
}, },
}); });
const slots = useSlots();
const emit = defineEmits<TreeEmits>(); const emit = defineEmits<TreeEmits>();
const className = computed(() => { const className = computed(() => {
@ -88,6 +87,10 @@ function handleClick(node: TreeData) {
:collapse-transition="collapseTransition" :collapse-transition="collapseTransition"
:only-icon-control="onlyIconControl" :only-icon-control="onlyIconControl"
@node-click="handleClick" @node-click="handleClick"
/> >
<template v-if="slots.title" v-slot:title="{ data }">
<slot name="title" :data="data"></slot>
</template>
</tree-node>
</div> </div>
</template> </template>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.4 KiB

After

Width:  |  Height:  |  Size: 321 B

View File

@ -253,6 +253,28 @@ import { ref } from 'vue';
::: :::
::: title 定义标题
:::
::: demo 使用 `title` 插槽自定义节点标题
<template>
<lay-tree
:data="data"
collapse-transition
>
<template v-slot:title="{ data }">
{{ data.id }}
</template>
</lay-tree>
</template>
<script setup>
import { ref } from 'vue';
</script>
:::
::: title Tree 属性 ::: title Tree 属性
::: :::

View File

@ -43,7 +43,7 @@
rel="nofollow" rel="nofollow"
class="site-star" class="site-star"
> >
<i class="layui-icon"></i> Star <cite id="getStars">973</cite> <i class="layui-icon"></i> Star <cite id="getStars">1054</cite>
</a> </a>
<a <a
href="https://gitee.com/layui-vue" href="https://gitee.com/layui-vue"