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', type: 'boolean',
default: 'true', default: 'true',
description: "Whether to apply 'router-link-active-exact' class if route exactly matches the item path." 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 { MenuItem } from '../menuitem';
import { ClassComponent, GlobalComponentConstructor } from '../ts-helpers'; 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. * Defines valid properties in MegaMenu component.
*/ */
@ -46,6 +173,11 @@ export interface MegaMenuProps {
* Identifier of the underlying menu element. * Identifier of the underlying menu element.
*/ */
'aria-labelledby'?: string | undefined; '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> <template>
<div :ref="containerRef" :id="id" :class="containerClass"> <div :ref="containerRef" :id="id" :class="containerClass" v-bind="ptm('root')">
<div v-if="$slots.start" class="p-megamenu-start"> <div v-if="$slots.start" class="p-megamenu-start" v-bind="ptm('start')">
<slot name="start"></slot> <slot name="start"></slot>
</div> </div>
<MegaMenuSub <MegaMenuSub
@ -22,24 +22,27 @@
:activeItem="activeItem" :activeItem="activeItem"
:exact="exact" :exact="exact"
:level="0" :level="0"
:pt="pt"
@focus="onFocus" @focus="onFocus"
@blur="onBlur" @blur="onBlur"
@keydown="onKeyDown" @keydown="onKeyDown"
@item-click="onItemClick" @item-click="onItemClick"
@item-mouseenter="onItemMouseEnter" @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> <slot name="end"></slot>
</div> </div>
</div> </div>
</template> </template>
<script> <script>
import BaseComponent from 'primevue/basecomponent';
import { DomHandler, ObjectUtils, UniqueComponentId } from 'primevue/utils'; import { DomHandler, ObjectUtils, UniqueComponentId } from 'primevue/utils';
import MegaMenuSub from './MegaMenuSub.vue'; import MegaMenuSub from './MegaMenuSub.vue';
export default { export default {
name: 'MegaMenu', name: 'MegaMenu',
extends: BaseComponent,
emits: ['focus', 'blur'], emits: ['focus', 'blur'],
props: { props: {
model: { model: {

View File

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