(component): 多层级菜单支持配置缩进,默认为不缩进,可通过<lay-menu :indent="true">开启

This commit is contained in:
傅子湘 2022-10-11 16:16:23 +08:00
parent 9b14cb524c
commit 9121124179
5 changed files with 46 additions and 7 deletions

View File

@ -183,6 +183,7 @@
.layui-nav-tree .layui-nav-item {
width: 100%;
display: block;
box-sizing: border-box;
line-height: 42px;
}
@ -196,12 +197,6 @@
padding: 5px 23px 5px 23px;
}
/* 嵌套垂直导航缩进 */
.layui-nav-child > .layui-nav-item > div > dl.layui-nav-child > li {
box-sizing: border-box;
padding-left: 10px;
}
.layui-nav-tree .layui-nav-item * {
color: rgba(255, 255, 255, 0.7);
}

View File

@ -18,6 +18,7 @@ export interface LayMenuProps {
level?: boolean | string;
collapse?: boolean | string;
collapseTransition?: boolean | string;
indent?: boolean | string; //
}
const emit = defineEmits([
@ -36,6 +37,7 @@ const props = withDefaults(defineProps<LayMenuProps>(), {
level: true,
collapse: false,
collapseTransition: true,
indent: false,
});
const isTree: ComputedRef<boolean> = computed(() => props.tree);
@ -68,6 +70,15 @@ const selectedKey = computed({
},
});
const indent = computed({
get() {
return props.indent;
},
set(val) {
// emit("update:indent", val);
},
});
watch(
() => props.collapse,
() => {
@ -87,6 +98,7 @@ provide("openKeys", openKeys);
provide("isCollapse", isCollapse);
provide("isCollapseTransition", isCollapseTransition);
provide("menuTheme", menuTheme);
provide("indent", indent);
</script>
<template>

View File

@ -0,0 +1,17 @@
// 处理菜单栏缩进
export function indentHandle(obj: {
indent: boolean | string;
level: number;
basePadding?: number;
}) {
const { indent, level, basePadding = 0 } = obj;
const least: number = level - 1; // 第一层不缩进
if (indent && least > 0) {
const px =
typeof indent === "boolean"
? `${basePadding + 10 * least}px` // css样式表对<a>设定了23基础边距
: indent.replace(/\d+/g, (s) => (basePadding + least * +s).toString());
return `padding-left: ${px}`;
}
return "";
}

View File

@ -8,6 +8,7 @@ export default {
import { computed, ComputedRef, inject, ref, Ref, useSlots } from "vue";
import { DropdownContext, dropdownInjectionKey } from "../dropdown/interface";
import useLevel from "../menu/useLevel";
import { indentHandle } from "../menu/utils";
import LayTooltip from "../tooltip/index.vue";
export interface LayMenuItemProps {
@ -22,6 +23,7 @@ const selectedKey: Ref<string> = inject("selectedKey") as Ref<string>;
const isTree = inject("isTree") as ComputedRef<boolean>;
const isCollapse = inject("isCollapse") as ComputedRef<boolean | string>;
const theme = inject("menuTheme") as Ref<string>;
const indent = inject("indent") as Ref<string | boolean>;
const dropdownCtx = inject<DropdownContext | undefined>(
dropdownInjectionKey,
undefined
@ -44,6 +46,7 @@ const needTooltip = computed(
<li
class="layui-nav-item"
:class="[selectedKey === id ? 'layui-this' : '']"
:style="isTree && indentHandle({ indent, level })"
@click="selectHandle()"
>
<template v-if="needTooltip">

View File

@ -8,6 +8,7 @@ export default {
import { computed, inject, ref, Ref, useSlots, watchEffect } from "vue";
import LayTransition from "../transition/index.vue";
import SubMenuPopup from "./SubMenuPopup.vue";
import { indentHandle } from "../menu/utils";
import { provideLevel, default as useLevel } from "../menu/useLevel";
export interface LaySubMenuProps {
@ -22,6 +23,7 @@ const { level } = useLevel();
const isTree: Ref<boolean> = inject("isTree") as Ref<boolean>;
const selectedKey: Ref<string> = inject("selectedKey") as Ref<string>;
const openKeys: Ref<string[]> = inject("openKeys") as Ref<string[]>;
const indent = inject("indent") as Ref<string | boolean>;
const isCollapse: Ref<boolean | string> = inject("isCollapse") as Ref<
boolean | string
>;
@ -70,7 +72,17 @@ const openHandle = function () {
<template>
<li v-if="!needPopup" class="layui-nav-item">
<a href="javascript:void(0)" @click="openHandle()">
<a
href="javascript:void(0)"
@click="openHandle()"
:style="
indentHandle({
indent,
level,
basePadding: 23,
})
"
>
<!-- 图标 -->
<i v-if="slots.icon" class="layui-sub-menu-icon">
<slot name="icon"></slot>