Refactor #3907 - For ContextMenu

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

View File

@ -34,6 +34,12 @@ const ContextMenuProps = [
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

@ -12,6 +12,119 @@ import { VNode } from 'vue';
import { MenuItem } from '../menuitem';
import { ClassComponent, GlobalComponentConstructor } from '../ts-helpers';
export declare type ContextMenuPassThroughOptionType = ContextMenuPassThroughAttributes | ((options: ContextMenuPassThroughMethodOptions) => ContextMenuPassThroughAttributes) | null | undefined;
/**
* Custom passthrough(pt) option method.
*/
export interface ContextMenuPassThroughMethodOptions {
props: ContextMenuProps;
state: ContextMenuState;
}
/**
* Custom passthrough(pt) options.
* @see {@link ContextMenuProps.pt}
*/
export interface ContextMenuPassThroughOptions {
/**
* Uses to pass attributes to the root's DOM element.
*/
root?: ContextMenuPassThroughOptionType;
/**
* Uses to pass attributes to the list's DOM element.
*/
menu?: ContextMenuPassThroughOptionType;
/**
* Uses to pass attributes to the list item's DOM element.
*/
menuitem?: ContextMenuPassThroughOptionType;
/**
* Uses to pass attributes to the content's DOM element.
*/
content?: ContextMenuPassThroughOptionType;
/**
* Uses to pass attributes to the action's DOM element.
*/
action?: ContextMenuPassThroughOptionType;
/**
* Uses to pass attributes to the icon's DOM element.
*/
icon?: ContextMenuPassThroughOptionType;
/**
* Uses to pass attributes to the label's DOM element.
*/
label?: ContextMenuPassThroughOptionType;
/**
* Uses to pass attributes to the submenu icon's DOM element.
*/
submenuicon?: ContextMenuPassThroughOptionType;
/**
* Uses to pass attributes to the separator's DOM element.
*/
separator?: ContextMenuPassThroughOptionType;
}
/**
* Custom passthrough attributes for each DOM elements
*/
export interface ContextMenuPassThroughAttributes {
[key: string]: any;
}
/**
* Defines focused item info
*/
export interface ContextMenuFocusedItemInfo {
/**
* Active item index
*/
index: number;
/**
* Active item level
*/
level: number;
/**
* Parent key info
*/
parentKey: string;
}
/**
* Defines current inline state in ContextMenu component.
*/
export interface ContextMenuState {
/**
* Current id state as a string.
*/
id: string;
/**
* Current focus state as a boolean.
* @defaultValue false
*/
focused: boolean;
/**
* Current focused item info.
* @type {ContextMenuFocusedItemInfo}
*/
focusedItemInfo: ContextMenuFocusedItemInfo;
/**
* Active item path.
* @type {ContextMenuFocusedItemInfo}
*/
activeItemPath: ContextMenuFocusedItemInfo[];
/**
* Current visible state as a boolean.
* @defaultValue false
*/
visible: boolean;
/**
* Current submenu visible state as a boolean.
* @defaultValue false
*/
submenuVisible: boolean;
}
/**
* Defines valid properties in ContextMenu component.
*/
@ -57,6 +170,11 @@ export interface ContextMenuProps {
* Identifier of the underlying menu element.
*/
'aria-labelledby'?: string | undefined;
/**
* Uses to pass attributes to DOM elements inside the component.
* @type {ContextMenuPassThroughOptions}
*/
pt?: ContextMenuPassThroughOptions;
}
/**

View File

@ -1,7 +1,7 @@
<template>
<Portal :appendTo="appendTo">
<transition name="p-contextmenu" @enter="onEnter" @after-enter="onAfterEnter" @leave="onLeave" @after-leave="onAfterLeave">
<div v-if="visible" :ref="containerRef" :class="containerClass" v-bind="$attrs">
<div v-if="visible" :ref="containerRef" :class="containerClass" v-bind="{ ...$attrs, ...ptm('root') }">
<ContextMenuSub
:ref="listRef"
:id="id + '_list'"
@ -21,6 +21,7 @@
:aria-label="ariaLabel"
:level="0"
:visible="submenuVisible"
:pt="pt"
@focus="onFocus"
@blur="onBlur"
@keydown="onKeyDown"
@ -33,12 +34,14 @@
</template>
<script>
import BaseComponent from 'primevue/basecomponent';
import Portal from 'primevue/portal';
import { DomHandler, ObjectUtils, UniqueComponentId, ZIndexUtils } from 'primevue/utils';
import ContextMenuSub from './ContextMenuSub.vue';
export default {
name: 'ContextMenu',
extends: BaseComponent,
inheritAttrs: false,
emits: ['focus', 'blur', 'show', 'hide'],
props: {

View File

@ -1,6 +1,6 @@
<template>
<transition name="p-contextmenusub" @enter="onEnter">
<ul v-if="root ? true : visible" ref="container">
<ul v-if="root ? true : visible" ref="container" v-bind="ptm('menu')">
<template v-for="(processedItem, index) of items" :key="getItemKey(processedItem)">
<li
v-if="isItemVisible(processedItem) && !getItemProp(processedItem, 'separator')"
@ -15,23 +15,24 @@
: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="getItemProp(processedItem, 'items')">
<component v-if="templates.submenuicon" :is="templates.submenuicon" :active="isItemActive(processedItem)" class="p-submenu-icon" />
<AngleRightIcon v-else class="p-submenu-icon" />
<AngleRightIcon v-else class="p-submenu-icon" v-bind="ptm('submenuicon')" />
</template>
</a>
</template>
@ -49,24 +50,34 @@
:activeItemPath="activeItemPath"
:exact="exact"
:level="level + 1"
:pt="pt"
:visible="isItemActive(processedItem) && isItemGroup(processedItem)"
@item-click="$emit('item-click', $event)"
@item-mouseenter="$emit('item-mouseenter', $event)"
/>
</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>
</transition>
</template>
<script>
import BaseComponent from 'primevue/basecomponent';
import AngleRightIcon from 'primevue/icons/angleright';
import Ripple from 'primevue/ripple';
import { DomHandler, ObjectUtils } from 'primevue/utils';
export default {
name: 'ContextMenuSub',
extends: BaseComponent,
emits: ['item-click', 'item-mouseenter'],
props: {
items: {