mirror of
https://github.com/element-plus/element-plus.git
synced 2026-03-13 07:51:17 +08:00
* refactor(menu): rename to kebab-case * refactor(menu): rename RootMenuProvider to MenuProvider * refactor(menu): rename menu.type to types * refactor(menu): extract menu props and emits * refactor(menu): change sub-menu.vue to ts * refactor(menu): extract menu-item-group props and emits * refactor(menu): extract menu-item props and emits * refactor(menu): extract sub-menu props and emits * refactor(menu): rename type RegisterMenuItem to MenuItemRegistered * refactor(menu): MenuProvider ref to reactive * refactor(menu): MenuProvider remove methods * refactor(menu): change submenus to subMenus * refactor(menu): remove RootMenuProps type * refactor(menu): MenuProvider improve types & rename submenu to subMenu * refactor(menu): menu add block to provide * refactor(menu): menu improve expose * refactor(menu): menu improve render types * refactor(menu): menu refactor types & change handle(Sub)MenuItemClick params * refactor(menu): menu refactor types * refactor(menu): menu-item-group improve types * refactor(menu): menu-item improve types * refactor(menu): sub-menu improve types * refactor(menu): use-menu improve types * refactor(menu): sub-menu fix types * refactor(menu): menu-collapse-transition improve types * refactor(menu): menu-item-group improve template * refactor(menu): menu-item rename emit param * refactor(menu): finally improve types * fix lint * chore: re-order import * chore: remove reactive
58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import { computed, inject } from 'vue'
|
|
import { throwError } from '@element-plus/utils/error'
|
|
|
|
import type { ComponentInternalInstance, Ref, CSSProperties } from 'vue'
|
|
import type { MenuProvider } from './types'
|
|
|
|
export default function useMenu(
|
|
instance: ComponentInternalInstance,
|
|
currentIndex: Ref<string>
|
|
) {
|
|
const rootMenu = inject<MenuProvider>('rootMenu')
|
|
if (!rootMenu) throwError('useMenu', 'can not inject root menu')
|
|
|
|
const indexPath = computed(() => {
|
|
let parent = instance.parent!
|
|
const path = [currentIndex.value]
|
|
while (parent.type.name !== 'ElMenu') {
|
|
if (parent.props.index) {
|
|
path.unshift(parent.props.index as string)
|
|
}
|
|
parent = parent.parent!
|
|
}
|
|
return path
|
|
})
|
|
|
|
const parentMenu = computed(() => {
|
|
let parent = instance.parent
|
|
while (parent && !['ElMenu', 'ElSubMenu'].includes(parent.type.name!)) {
|
|
parent = parent.parent
|
|
}
|
|
return parent!
|
|
})
|
|
const paddingStyle = computed<CSSProperties>(() => {
|
|
let parent = instance.parent
|
|
if (rootMenu.props.mode !== 'vertical') return {}
|
|
|
|
let padding = 20
|
|
|
|
if (rootMenu.props.collapse) {
|
|
padding = 20
|
|
} else {
|
|
while (parent && parent.type.name !== 'ElMenu') {
|
|
if (parent.type.name === 'ElSubMenu') {
|
|
padding += 20
|
|
}
|
|
parent = parent.parent
|
|
}
|
|
}
|
|
return { paddingLeft: `${padding}px` }
|
|
})
|
|
|
|
return {
|
|
parentMenu,
|
|
paddingStyle,
|
|
indexPath,
|
|
}
|
|
}
|