Refactor #3907 - For Menubar

pull/3913/head
Tuğçe Küçükoğlu 2023-04-26 12:58:22 +03:00
parent 234ff1062f
commit 2dd290d526
4 changed files with 164 additions and 15 deletions

View File

@ -10,6 +10,12 @@ const MenubarProps = [
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,130 @@ import { ButtonHTMLAttributes, 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 MenubarPassThroughOptionType = MenubarPassThroughAttributes | ((options: MenubarPassThroughMethodOptions) => MenubarPassThroughAttributes) | null | undefined;
/**
* Custom passthrough(pt) option method.
*/
export interface MenubarPassThroughMethodOptions {
props: MenubarProps;
state: MenubarState;
}
/**
* Custom passthrough(pt) options.
* @see {@link MenubarProps.pt}
*/
export interface MenubarPassThroughOptions {
/**
* Uses to pass attributes to the root's DOM element.
*/
root?: MenubarPassThroughOptionType;
/**
* Uses to pass attributes to the list's DOM element.
*/
menu?: MenubarPassThroughOptionType;
/**
* Uses to pass attributes to the list item's DOM element.
*/
menuitem?: MenubarPassThroughOptionType;
/**
* Uses to pass attributes to the content's DOM element.
*/
content?: MenubarPassThroughOptionType;
/**
* Uses to pass attributes to the action's DOM element.
*/
action?: MenubarPassThroughOptionType;
/**
* Uses to pass attributes to the icon's DOM element.
*/
icon?: MenubarPassThroughOptionType;
/**
* Uses to pass attributes to the label's DOM element.
*/
label?: MenubarPassThroughOptionType;
/**
* Uses to pass attributes to the submenu icon's DOM element.
*/
submenuicon?: MenubarPassThroughOptionType;
/**
* Uses to pass attributes to the separator's DOM element.
*/
separator?: MenubarPassThroughOptionType;
/**
* Uses to pass attributes to the mobile popup menu button's DOM element.
*/
button?: MenubarPassThroughOptionType;
/**
* Uses to pass attributes to the mobile popup menu button icon's DOM element.
*/
popupIcon?: MenubarPassThroughOptionType;
/**
* Uses to pass attributes to the start of the component.
*/
start?: MenubarPassThroughOptionType;
/**
* Uses to pass attributes to the end of the component.
*/
end?: MenubarPassThroughOptionType;
}
/**
* Custom passthrough attributes for each DOM elements
*/
export interface MenubarPassThroughAttributes {
[key: string]: any;
}
/**
* Defines focused item info
*/
export interface MenubarFocusedItemInfo {
/**
* Active item index
*/
index: number;
/**
* Active item level
*/
level: number;
/**
* Parent key info
*/
parentKey: string;
}
/**
* Defines current inline state in Menubar component.
*/
export interface MenubarState {
/**
* Current id state as a string.
*/
id: string;
/**
* Current mobile menu active state as a boolean.
* @defaultValue false
*/
mobileActive: boolean;
/**
* Current focus state as a boolean.
* @defaultValue false
*/
focused: boolean;
/**
* Current focused item info.
* @type {MenubarFocusedItemInfo}
*/
focusedItemInfo: MenubarFocusedItemInfo;
/**
* Active item path.
* @type {MenubarFocusedItemInfo}
*/
activeItemPath: MenubarFocusedItemInfo[];
}
/** /**
* Defines valid properties in Menubar component. * Defines valid properties in Menubar component.
*/ */
@ -36,6 +160,11 @@ export interface MenubarProps {
* Identifier of the underlying input element. * Identifier of the underlying input element.
*/ */
'aria-labelledby'?: string | undefined; 'aria-labelledby'?: string | undefined;
/**
* Uses to pass attributes to DOM elements inside the component.
* @type {MenubarPassThroughOptions}
*/
pt?: MenubarPassThroughOptions;
} }
/** /**

View File

@ -1,6 +1,6 @@
<template> <template>
<div :ref="containerRef" :class="containerClass"> <div :ref="containerRef" :class="containerClass" v-bind="ptm('root')">
<div v-if="$slots.start" class="p-menubar-start"> <div v-if="$slots.start" class="p-menubar-start" v-bind="ptm('start')">
<slot name="start"></slot> <slot name="start"></slot>
</div> </div>
<a <a
@ -15,10 +15,10 @@
:aria-label="$primevue.config.locale.aria.navigation" :aria-label="$primevue.config.locale.aria.navigation"
@click="menuButtonClick($event)" @click="menuButtonClick($event)"
@keydown="menuButtonKeydown($event)" @keydown="menuButtonKeydown($event)"
v-bind="buttonProps" v-bind="{ ...buttonProps, ...ptm('button') }"
> >
<slot name="popupicon"> <slot name="popupicon">
<BarsIcon /> <BarsIcon v-bind="ptm('popupIcon')" />
</slot> </slot>
</a> </a>
<MenubarSub <MenubarSub
@ -39,25 +39,28 @@
:level="0" :level="0"
:aria-labelledby="ariaLabelledby" :aria-labelledby="ariaLabelledby"
:aria-label="ariaLabel" :aria-label="ariaLabel"
: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-menubar-end"> <div v-if="$slots.end" class="p-menubar-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 BarsIcon from 'primevue/icons/bars'; import BarsIcon from 'primevue/icons/bars';
import { DomHandler, ObjectUtils, UniqueComponentId, ZIndexUtils } from 'primevue/utils'; import { DomHandler, ObjectUtils, UniqueComponentId, ZIndexUtils } from 'primevue/utils';
import MenubarSub from './MenubarSub.vue'; import MenubarSub from './MenubarSub.vue';
export default { export default {
name: 'Menubar', name: 'Menubar',
extends: BaseComponent,
emits: ['focus', 'blur'], emits: ['focus', 'blur'],
props: { props: {
model: { model: {

View File

@ -1,5 +1,5 @@
<template> <template>
<ul> <ul v-bind="ptm('menu')">
<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')"
@ -14,23 +14,24 @@
: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="getItemProp(processedItem, 'items')"> <template v-if="getItemProp(processedItem, 'items')">
<component v-if="templates.submenuicon" :is="templates.submenuicon" :root="root" :active="isItemActive(processedItem)" class="p-submenu-icon" /> <component v-if="templates.submenuicon" :is="templates.submenuicon" :root="root" :active="isItemActive(processedItem)" class="p-submenu-icon" />
<component v-else :is="root ? 'AngleDownIcon' : 'AngleRightIcon'" class="p-submenu-icon" /> <component v-else :is="root ? 'AngleDownIcon' : 'AngleRightIcon'" class="p-submenu-icon" v-bind="ptm('submenuIcon')" />
</template> </template>
</a> </a>
</template> </template>
@ -48,16 +49,25 @@
: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)"
/> />
</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';
@ -65,6 +75,7 @@ import { ObjectUtils } from 'primevue/utils';
export default { export default {
name: 'MenubarSub', name: 'MenubarSub',
extends: BaseComponent,
emits: ['item-mouseenter', 'item-click'], emits: ['item-mouseenter', 'item-click'],
props: { props: {
items: { items: {