Refactor #3907 - For MegaMenu

pull/3913/head
Tuğçe Küçükoğlu 2023-04-26 12:57:55 +03:00
parent 9e1903bbbe
commit f6c1efe793
4 changed files with 170 additions and 18 deletions

View File

@ -16,6 +16,12 @@ const MegaMenuProps = [
type: 'boolean',
default: 'true',
description: "Whether to apply 'router-link-active-exact' class if route exactly matches the item path."
},
{
name: 'pt',
type: 'any',
default: 'null',
description: 'Uses to pass attributes to DOM elements inside the component.'
}
];

View File

@ -11,6 +11,133 @@ import { VNode } from 'vue';
import { MenuItem } from '../menuitem';
import { ClassComponent, GlobalComponentConstructor } from '../ts-helpers';
export declare type MegaMenuPassThroughOptionType = MegaMenuPassThroughAttributes | ((options: MegaMenuPassThroughMethodOptions) => MegaMenuPassThroughAttributes) | null | undefined;
/**
* Custom passthrough(pt) option method.
*/
export interface MegaMenuPassThroughMethodOptions {
props: MegaMenuProps;
state: MegaMenuState;
}
/**
* Custom passthrough(pt) options.
* @see {@link MegaMenuProps.pt}
*/
export interface MegaMenuPassThroughOptions {
/**
* Uses to pass attributes to the root's DOM element.
*/
root?: MegaMenuPassThroughOptionType;
/**
* Uses to pass attributes to the list's DOM element.
*/
menu?: MegaMenuPassThroughOptionType;
/**
* Uses to pass attributes to the submenu header's DOM element.
*/
submenuHeader?: MegaMenuPassThroughOptionType;
/**
* Uses to pass attributes to the list item's DOM element.
*/
menuitem?: MegaMenuPassThroughOptionType;
/**
* Uses to pass attributes to the content's DOM element.
*/
content?: MegaMenuPassThroughOptionType;
/**
* Uses to pass attributes to the action's DOM element.
*/
action?: MegaMenuPassThroughOptionType;
/**
* Uses to pass attributes to the icon's DOM element.
*/
icon?: MegaMenuPassThroughOptionType;
/**
* Uses to pass attributes to the label's DOM element.
*/
label?: MegaMenuPassThroughOptionType;
/**
* Uses to pass attributes to the submenu icon's DOM element.
*/
submenuicon?: MegaMenuPassThroughOptionType;
/**
* Uses to pass attributes to the panel's DOM element.
*/
panel?: MegaMenuPassThroughOptionType;
/**
* Uses to pass attributes to the grid's DOM element.
*/
grid?: MegaMenuPassThroughOptionType;
/**
* Uses to pass attributes to the column's DOM element.
*/
column?: MegaMenuPassThroughOptionType;
/**
* Uses to pass attributes to the separator's DOM element.
*/
separator?: MegaMenuPassThroughOptionType;
/**
* Uses to pass attributes to the start of the component.
*/
start?: MegaMenuPassThroughOptionType;
/**
* Uses to pass attributes to the end of the component.
*/
end?: MegaMenuPassThroughOptionType;
}
/**
* Custom passthrough attributes for each DOM elements
*/
export interface MegaMenuPassThroughAttributes {
[key: string]: any;
}
/**
* Defines focused item info
*/
export interface MegaMenuFocusedItemInfo {
/**
* Active item index
*/
index: number;
/**
* Active item level
*/
level: number;
/**
* Parent key info
*/
parentKey: string;
}
/**
* Defines current inline state in MegaMenu component.
*/
export interface MegaMenuState {
/**
* Current id state as a string.
*/
id: string;
/**
* Current focus state as a boolean.
* @defaultValue false
*/
focused: boolean;
/**
* Current focused item info.
* @type {MegaMenuFocusedItemInfo}
*/
focusedItemInfo: MegaMenuFocusedItemInfo;
/**
* Active item path.
* @type {MenuItem}
*/
activeItem: MenuItem;
}
/**
* Defines valid properties in MegaMenu component.
*/
@ -46,6 +173,11 @@ export interface MegaMenuProps {
* Identifier of the underlying menu element.
*/
'aria-labelledby'?: string | undefined;
/**
* Uses to pass attributes to DOM elements inside the component.
* @type {MegaMenuPassThroughOptions}
*/
pt?: MegaMenuPassThroughOptions;
}
/**

View File

@ -1,6 +1,6 @@
<template>
<div :ref="containerRef" :id="id" :class="containerClass">
<div v-if="$slots.start" class="p-megamenu-start">
<div :ref="containerRef" :id="id" :class="containerClass" v-bind="ptm('root')">
<div v-if="$slots.start" class="p-megamenu-start" v-bind="ptm('start')">
<slot name="start"></slot>
</div>
<MegaMenuSub
@ -22,24 +22,27 @@
:activeItem="activeItem"
:exact="exact"
:level="0"
:pt="pt"
@focus="onFocus"
@blur="onBlur"
@keydown="onKeyDown"
@item-click="onItemClick"
@item-mouseenter="onItemMouseEnter"
/>
<div v-if="$slots.end" class="p-megamenu-end">
<div v-if="$slots.end" class="p-megamenu-end" v-bind="ptm('end')">
<slot name="end"></slot>
</div>
</div>
</template>
<script>
import BaseComponent from 'primevue/basecomponent';
import { DomHandler, ObjectUtils, UniqueComponentId } from 'primevue/utils';
import MegaMenuSub from './MegaMenuSub.vue';
export default {
name: 'MegaMenu',
extends: BaseComponent,
emits: ['focus', 'blur'],
props: {
model: {

View File

@ -1,6 +1,6 @@
<template>
<ul>
<li v-if="submenu" :class="getSubmenuHeaderClass(submenu)" :style="getItemProp(submenu, 'style')" role="presentation">{{ getItemLabel(submenu) }}</li>
<ul v-bind="ptm('menu')">
<li v-if="submenu" :class="getSubmenuHeaderClass(submenu)" :style="getItemProp(submenu, 'style')" role="presentation" v-bind="ptm('submenuHeader')">{{ getItemLabel(submenu) }}</li>
<template v-for="(processedItem, index) of items" :key="getItemKey(processedItem)">
<li
v-if="isItemVisible(processedItem) && !getItemProp(processedItem, 'separator')"
@ -15,31 +15,32 @@
:aria-level="level + 1"
:aria-setsize="getAriaSetSize()"
:aria-posinset="getAriaPosInset(index)"
v-bind="ptm('menuitem')"
>
<div class="p-menuitem-content" @click="onItemClick($event, processedItem)" @mouseenter="onItemMouseEnter($event, processedItem)">
<div class="p-menuitem-content" @click="onItemClick($event, processedItem)" @mouseenter="onItemMouseEnter($event, processedItem)" v-bind="ptm('content')">
<template v-if="!templates.item">
<router-link v-if="getItemProp(processedItem, 'to') && !isItemDisabled(processedItem)" v-slot="{ navigate, href, isActive, isExactActive }" :to="getItemProp(processedItem, 'to')" custom>
<a v-ripple :href="href" :class="getItemActionClass(processedItem, { isActive, isExactActive })" tabindex="-1" aria-hidden="true" @click="onItemActionClick($event, navigate)">
<a v-ripple :href="href" :class="getItemActionClass(processedItem, { isActive, isExactActive })" tabindex="-1" aria-hidden="true" @click="onItemActionClick($event, navigate)" v-bind="ptm('action')">
<component v-if="templates.itemicon" :is="templates.itemicon" :item="processedItem.item" :class="getItemIconClass(processedItem)" />
<span v-else-if="getItemProp(processedItem, 'icon')" :class="getItemIconClass(processedItem)" />
<span class="p-menuitem-text">{{ getItemLabel(processedItem) }}</span>
<span v-else-if="getItemProp(processedItem, 'icon')" :class="getItemIconClass(processedItem)" v-bind="ptm('icon')" />
<span class="p-menuitem-text" v-bind="ptm('label')">{{ getItemLabel(processedItem) }}</span>
</a>
</router-link>
<a v-else v-ripple :href="getItemProp(processedItem, 'url')" :class="getItemActionClass(processedItem)" :target="getItemProp(processedItem, 'target')" tabindex="-1" aria-hidden="true">
<a v-else v-ripple :href="getItemProp(processedItem, 'url')" :class="getItemActionClass(processedItem)" :target="getItemProp(processedItem, 'target')" tabindex="-1" aria-hidden="true" v-bind="ptm('action')">
<component v-if="templates.itemicon" :is="templates.itemicon" :item="processedItem.item" :class="getItemIconClass(processedItem)" />
<span v-else-if="getItemProp(processedItem, 'icon')" :class="getItemIconClass(processedItem)" />
<span class="p-menuitem-text">{{ getItemLabel(processedItem) }}</span>
<span v-else-if="getItemProp(processedItem, 'icon')" :class="getItemIconClass(processedItem)" v-bind="ptm('icon')" />
<span class="p-menuitem-text" v-bind="ptm('label')">{{ getItemLabel(processedItem) }}</span>
<template v-if="isItemGroup(processedItem)">
<component v-if="templates.submenuicon" :is="templates.submenuicon" :active="isItemActive(processedItem)" class="p-submenu-icon" />
<component v-else :is="horizontal ? 'AngleDownIcon' : 'AngleRightIcon'" class="p-submenu-icon" />
<component v-if="templates.submenuicon" :is="templates.submenuicon" :active="isItemActive(processedItem)" class="p-submenu-icon" v-bind="ptm('submenuIcon')" />
<component v-else :is="horizontal ? 'AngleDownIcon' : 'AngleRightIcon'" class="p-submenu-icon" v-bind="ptm('submenuIcon')" />
</template>
</a>
</template>
<component v-else :is="templates.item" :item="processedItem.item"></component>
</div>
<div v-if="isItemVisible(processedItem) && isItemGroup(processedItem)" class="p-megamenu-panel">
<div class="p-megamenu-grid">
<div v-for="col of processedItem.items" :key="getItemKey(col)" :class="getColumnClass(processedItem)">
<div v-if="isItemVisible(processedItem) && isItemGroup(processedItem)" class="p-megamenu-panel" v-bind="ptm('panel')">
<div class="p-megamenu-grid" v-bind="ptm('grid')">
<div v-for="col of processedItem.items" :key="getItemKey(col)" :class="getColumnClass(processedItem)" v-bind="ptm('column')">
<MegaMenuSub
v-for="submenu of col"
:key="getSubListKey(submenu)"
@ -53,6 +54,7 @@
:templates="templates"
:exact="exact"
:level="level + 1"
:pt="pt"
@item-click="$emit('item-click', $event)"
@item-mouseenter="$emit('item-mouseenter', $event)"
/>
@ -60,12 +62,20 @@
</div>
</div>
</li>
<li v-if="isItemVisible(processedItem) && getItemProp(processedItem, 'separator')" :id="getItemId(processedItem)" :style="getItemProp(processedItem, 'style')" :class="getSeparatorItemClass(processedItem)" role="separator"></li>
<li
v-if="isItemVisible(processedItem) && getItemProp(processedItem, 'separator')"
:id="getItemId(processedItem)"
:style="getItemProp(processedItem, 'style')"
:class="getSeparatorItemClass(processedItem)"
role="separator"
v-bind="ptm('separator')"
></li>
</template>
</ul>
</template>
<script>
import BaseComponent from 'primevue/basecomponent';
import AngleDownIcon from 'primevue/icons/angledown';
import AngleRightIcon from 'primevue/icons/angleright';
import Ripple from 'primevue/ripple';
@ -73,6 +83,7 @@ import { ObjectUtils } from 'primevue/utils';
export default {
name: 'MegaMenuSub',
extends: BaseComponent,
emits: ['item-click', 'item-mouseenter'],
props: {
menuId: {