Fixed #367 - Ability to activate PanelMenu item declaratively or programmatically

pull/1281/head
Cagatay Civici 2021-05-17 18:44:09 +03:00
parent 62d8c7a448
commit c04098e4fe
9 changed files with 720 additions and 344 deletions

View File

@ -4,6 +4,12 @@ const PanelMenuProps = [
type: "array",
default: "null",
description: "An array of menuitems."
},
{
name: "expandedKeys",
type: "array",
default: "null",
description: "A map of keys to represent the expansion state in controlled mode."
}
];

View File

@ -9,7 +9,7 @@ const TreeProps = [
name: "expandedKeys",
type: "array",
default: "null",
description: "A map of keys to represent the state of the tree expansion state in controlled mode."
description: "A map of keys to represent the expansion state in controlled mode."
},
{
name: "selectionMode",

View File

@ -1,5 +1,6 @@
interface PanelMenuProps {
model?: any[];
expandedKeys?: any;
}
declare class PanelMenu {

View File

@ -20,10 +20,11 @@
<component v-else :is="$slots.item" :item="item"></component>
</div>
<transition name="p-toggleable-content">
<div class="p-toggleable-content" v-show="item === activeItem"
<div class="p-toggleable-content" v-show="isActive(item)"
role="region" :id="ariaId +'_content' " :aria-labelledby="ariaId +'_header'">
<div class="p-panelmenu-content" v-if="item.items">
<PanelMenuSub :model="item.items" class="p-panelmenu-root-submenu" :template="$slots.item" />
<PanelMenuSub :model="item.items" class="p-panelmenu-root-submenu" :template="$slots.item"
:expandedKeys="expandedKeys" @item-toggle="updateExpandedKeys" />
</div>
</div>
</transition>
@ -38,10 +39,15 @@ import {UniqueComponentId} from 'primevue/utils';
export default {
name: 'PanelMenu',
emits: ['update:expandedKeys'],
props: {
model: {
type: Array,
default: null
},
expandedKeys: {
type: null,
default: null
}
},
data() {
@ -68,10 +74,25 @@ export default {
else
this.activeItem = item;
this.updateExpandedKeys({item: item, expanded: this.activeItem != null});
if (item.to && navigate) {
navigate(event);
}
},
updateExpandedKeys(event) {
if (this.expandedKeys) {
let item = event.item;
let _keys = {...this.expandedKeys};
if (event.expanded)
_keys[item.key] = true;
else
delete _keys[item.key];
this.$emit('update:expandedKeys', _keys);
}
},
getPanelClass(item) {
return ['p-panelmenu-panel', item.class];
},
@ -83,7 +104,7 @@ export default {
return ['p-menuitem-icon', item.icon];
},
isActive(item) {
return item === this.activeItem;
return this.expandedKeys ? this.expandedKeys[item.key] : item === this.activeItem;
},
getHeaderClass(item) {
return ['p-component p-panelmenu-header', {'p-highlight': this.isActive(item), 'p-disabled': item.disabled}];

View File

@ -18,8 +18,9 @@
</template>
<component v-else :is="template" :item="item"></component>
<transition name="p-toggleable-content">
<div class="p-toggleable-content" v-show="item === activeItem">
<PanelMenuSub :model="item.items" v-if="visible(item) && item.items" :key="item.label + '_sub_'" :template="template" />
<div class="p-toggleable-content" v-show="isActive(item)">
<PanelMenuSub :model="item.items" v-if="visible(item) && item.items" :key="item.label + '_sub_'" :template="template"
:expandedKeys="expandedKeys" @item-toggle="$emit('item-toggle', $event)" />
</div>
</transition>
</li>
@ -31,6 +32,7 @@
<script>
export default {
name: 'PanelMenuSub',
emits: ['item-toggle'],
props: {
model: {
type: null,
@ -39,6 +41,10 @@ export default {
template: {
type: Object,
default: null
},
expandedKeys: {
type: null,
default: null
}
},
data() {
@ -65,6 +71,8 @@ export default {
else
this.activeItem = item;
this.$emit('item-toggle', {item: item, expanded: this.activeItem != null});
if (item.to && navigate) {
navigate(event);
}
@ -76,7 +84,7 @@ export default {
return ['p-menuitem-link', {'p-disabled': item.disabled}];
},
isActive(item) {
return item === this.activeItem;
return this.expandedKeys ? this.expandedKeys[item.key] : item === this.activeItem;
},
getSubmenuIcon(item) {
const active = this.isActive(item);

View File

@ -110,6 +110,12 @@ const items: [
<td>null</td>
<td>Style class of the menuitem.</td>
</tr>
<tr>
<td>key</td>
<td>object</td>
<td>null</td>
<td>Unique identifier of an item.</td>
</tr>
</tbody>
</table>
</div>

View File

@ -10,7 +10,15 @@
<div class="content-section implementation">
<div class="card">
<h5>Default</h5>
<PanelMenu :model="items" />
<h5>Programmatic</h5>
<div class="p-mb-3">
<Button type="button" icon="pi pi-plus" label="Expand All" @click="expandAll" class="p-mr-2" />
<Button type="button" icon="pi pi-minus" label="Collapse All" @click="collapseAll" />
</div>
<PanelMenu :model="items" v-model:expandedKeys="expandedKeys" />
</div>
</div>
@ -24,86 +32,97 @@ import PanelMenuDoc from './PanelMenuDoc';
export default {
data() {
return {
items: [
{
expandedKeys: {},
items: [{
key: '0',
label: 'File',
icon:'pi pi-fw pi-file',
items: [
{
icon: 'pi pi-fw pi-file',
items: [{
key: '0_0',
label: 'New',
icon:'pi pi-fw pi-plus',
items: [
{
icon: 'pi pi-fw pi-plus',
items: [{
key: '0_0_0',
label: 'Bookmark',
icon:'pi pi-fw pi-bookmark'
icon: 'pi pi-fw pi-bookmark'
},
{
key: '0_0_1',
label: 'Video',
icon:'pi pi-fw pi-video'
icon: 'pi pi-fw pi-video'
}
]
},
{
key: '0_1',
label: 'Delete',
icon:'pi pi-fw pi-trash'
icon: 'pi pi-fw pi-trash'
},
{
key: '0_2',
label: 'Export',
icon:'pi pi-fw pi-external-link'
icon: 'pi pi-fw pi-external-link'
}
]
},
{
key: '1',
label: 'Edit',
icon:'pi pi-fw pi-pencil',
items: [
{
icon: 'pi pi-fw pi-pencil',
items: [{
key: '1_0',
label: 'Left',
icon:'pi pi-fw pi-align-left'
icon: 'pi pi-fw pi-align-left'
},
{
key: '1_1',
label: 'Right',
icon:'pi pi-fw pi-align-right'
icon: 'pi pi-fw pi-align-right'
},
{
key: '1_2',
label: 'Center',
icon:'pi pi-fw pi-align-center'
icon: 'pi pi-fw pi-align-center'
},
{
key: '1_3',
label: 'Justify',
icon:'pi pi-fw pi-align-justify'
icon: 'pi pi-fw pi-align-justify'
}
]
},
{
key: '2',
label: 'Users',
icon:'pi pi-fw pi-user',
items: [
{
icon: 'pi pi-fw pi-user',
items: [{
key: '2_0',
label: 'New',
icon:'pi pi-fw pi-user-plus',
icon: 'pi pi-fw pi-user-plus',
},
{
key: '2_1',
label: 'Delete',
icon:'pi pi-fw pi-user-minus',
icon: 'pi pi-fw pi-user-minus',
},
{
key: '2_2',
label: 'Search',
icon:'pi pi-fw pi-users',
items: [
{
icon: 'pi pi-fw pi-users',
items: [{
key: '2_2_0',
label: 'Filter',
icon:'pi pi-fw pi-filter',
items: [
{
icon: 'pi pi-fw pi-filter',
items: [{
key: '2_2_0_0',
label: 'Print',
icon:'pi pi-fw pi-print'
}
]
icon: 'pi pi-fw pi-print'
}]
},
{
icon:'pi pi-fw pi-bars',
key: '2_2_1',
icon: 'pi pi-fw pi-bars',
label: 'List'
}
]
@ -111,36 +130,61 @@ export default {
]
},
{
key: '3',
label: 'Events',
icon:'pi pi-fw pi-calendar',
items: [
{
icon: 'pi pi-fw pi-calendar',
items: [{
key: '3_0',
label: 'Edit',
icon:'pi pi-fw pi-pencil',
items: [
{
icon: 'pi pi-fw pi-pencil',
items: [{
key: '3_0_0',
label: 'Save',
icon:'pi pi-fw pi-calendar-plus'
icon: 'pi pi-fw pi-calendar-plus'
},
{
key: '3_0_0',
label: 'Delete',
icon:'pi pi-fw pi-calendar-minus'
icon: 'pi pi-fw pi-calendar-minus'
}
]
},
{
key: '3_1',
label: 'Archieve',
icon:'pi pi-fw pi-calendar-times',
items: [
{
icon: 'pi pi-fw pi-calendar-times',
items: [{
key: '3_1_0',
label: 'Remove',
icon:'pi pi-fw pi-calendar-minus'
icon: 'pi pi-fw pi-calendar-minus'
}]
}
]
}
]
}
]
},
methods: {
expandAll() {
for (let node of this.items) {
this.expandNode(node);
}
this.expandedKeys = {
...this.expandedKeys
};
},
collapseAll() {
this.expandedKeys = {};
},
expandNode(node) {
if (node.items && node.items.length) {
this.expandedKeys[node.key] = true;
for (let child of node.items) {
this.expandNode(child);
}
}
}
},
components: {

View File

@ -16,6 +16,7 @@ import PanelMenu from 'primevue/panelmenu';
</code></pre>
<div style="height: 400px; overflow:auto">
<pre v-code.script><code>
export default {
data() {
@ -142,6 +143,7 @@ export default {
}
</code></pre>
</div>
<h5>Templating</h5>
<p>PanelMenu offers content customization with the <i>item</i> template that receives the menuitem instance from the model as a parameter.</p>
@ -152,6 +154,202 @@ export default {
&lt;/template&gt;
&lt;/PanelMenu&gt;
</template>
</code></pre>
<h5>Programmatic Control</h5>
<p>If the menuitem has a <i>key</i> defined, PanelMenu state can be controlled programmatically with the <i>expandedKeys</i> property that defines the keys
that are expanded. This property is a Map instance whose key is the key of a node and value is a boolean. Note that <i>expandedKeys</i> also supports two-way binding with the v-model directive.
</p>
<p>Example below expands and collapses all nodes with buttons.</p>
<pre v-code><code><template v-pre>
&lt;div&gt;
&lt;Button type="button" icon="pi pi-plus" label="Expand All" @click="expandAll" /&gt;
&lt;Button type="button" icon="pi pi-minus" label="Collapse All" @click="collapseAll" /&gt;
&lt;/div&gt;
&lt;PanelMenu :model="items" v-model:expandedKeys="expandedKeys""&gt;&lt;/PanelMenu&gt;
</template>
</code></pre>
<div style="height: 400px; overflow:auto">
<pre v-code.script><code>
export default {
data() {
return {
items: [{
key: '0',
label: 'File',
icon: 'pi pi-fw pi-file',
items: [{
key: '0_0',
label: 'New',
icon: 'pi pi-fw pi-plus',
items: [{
key: '0_0_0',
label: 'Bookmark',
icon: 'pi pi-fw pi-bookmark'
},
{
key: '0_0_1',
label: 'Video',
icon: 'pi pi-fw pi-video'
}
]
},
{
key: '0_1',
label: 'Delete',
icon: 'pi pi-fw pi-trash'
},
{
key: '0_2',
label: 'Export',
icon: 'pi pi-fw pi-external-link'
}
]
},
{
key: '1',
label: 'Edit',
icon: 'pi pi-fw pi-pencil',
items: [{
key: '1_0',
label: 'Left',
icon: 'pi pi-fw pi-align-left'
},
{
key: '1_1',
label: 'Right',
icon: 'pi pi-fw pi-align-right'
},
{
key: '1_2',
label: 'Center',
icon: 'pi pi-fw pi-align-center'
},
{
key: '1_3',
label: 'Justify',
icon: 'pi pi-fw pi-align-justify'
}
]
},
{
key: '2',
label: 'Users',
icon: 'pi pi-fw pi-user',
items: [{
key: '2_0',
label: 'New',
icon: 'pi pi-fw pi-user-plus',
},
{
key: '2_1',
label: 'Delete',
icon: 'pi pi-fw pi-user-minus',
},
{
key: '2_2',
label: 'Search',
icon: 'pi pi-fw pi-users',
items: [{
key: '2_2_0',
label: 'Filter',
icon: 'pi pi-fw pi-filter',
items: [{
key: '2_2_0_0',
label: 'Print',
icon: 'pi pi-fw pi-print'
}]
},
{
key: '2_2_1',
icon: 'pi pi-fw pi-bars',
label: 'List'
}
]
}
]
},
{
key: '3',
label: 'Events',
icon: 'pi pi-fw pi-calendar',
items: [{
key: '3_0',
label: 'Edit',
icon: 'pi pi-fw pi-pencil',
items: [{
key: '3_0_0',
label: 'Save',
icon: 'pi pi-fw pi-calendar-plus'
},
{
key: '3_0_0',
label: 'Delete',
icon: 'pi pi-fw pi-calendar-minus'
}
]
},
{
key: '3_1',
label: 'Archieve',
icon: 'pi pi-fw pi-calendar-times',
items: [{
key: '3_1_0',
label: 'Remove',
icon: 'pi pi-fw pi-calendar-minus'
}]
}
]
}
],
expandedKeys: {}
}
},
methods: {
expandAll() {
for (let node of this.items) {
this.expandNode(node);
}
this.expandedKeys = {
...this.expandedKeys
};
},
collapseAll() {
this.expandedKeys = {};
},
expandNode(node) {
if (node.items && node.items.length) {
this.expandedKeys[node.key] = true;
for (let child of node.items) {
this.expandNode(child);
}
}
}
}
}
</code></pre>
</div>
<p class="p-mt-3">In order to display some nodes as expanded by default, simply add their keys to the map.</p>
<pre v-code.script><code>
export default {
data() {
return {
items: //menu model instance,
expandedKeys: {}
}
},
mounted() {
this.expandedKeys[this.items[0.key]] = true;
}
}
</code></pre>
<h5>Properties</h5>
@ -172,6 +370,12 @@ export default {
<td>array</td>
<td>null</td>
<td>An array of menuitems.</td>
</tr>
<tr>
<td>expandedKeys</td>
<td>array</td>
<td>null</td>
<td>A map of keys to represent the expansion state in controlled mode.</td>
</tr>
</tbody>
</table>
@ -257,7 +461,15 @@ export default {
content: `
<template>
<div>
<h5>Default</h5>
<PanelMenu :model="items" />
<h5>Programmatic</h5>
<div class="p-mb-3">
<Button type="button" icon="pi pi-plus" label="Expand All" @click="expandAll" class="p-mr-2" />
<Button type="button" icon="pi pi-minus" label="Collapse All" @click="collapseAll" />
</div>
<PanelMenu :model="items" :expandedKeys="expandedKeys" />
</div>
</template>
@ -265,86 +477,97 @@ export default {
export default {
data() {
return {
items: [
{
expandedKeys: {},
items: [{
key: '0',
label: 'File',
icon:'pi pi-fw pi-file',
items: [
{
icon: 'pi pi-fw pi-file',
items: [{
key: '0_0',
label: 'New',
icon:'pi pi-fw pi-plus',
items: [
{
icon: 'pi pi-fw pi-plus',
items: [{
key: '0_0_0',
label: 'Bookmark',
icon:'pi pi-fw pi-bookmark'
icon: 'pi pi-fw pi-bookmark'
},
{
key: '0_0_1',
label: 'Video',
icon:'pi pi-fw pi-video'
icon: 'pi pi-fw pi-video'
}
]
},
{
key: '0_1',
label: 'Delete',
icon:'pi pi-fw pi-trash'
icon: 'pi pi-fw pi-trash'
},
{
key: '0_2',
label: 'Export',
icon:'pi pi-fw pi-external-link'
icon: 'pi pi-fw pi-external-link'
}
]
},
{
key: '1',
label: 'Edit',
icon:'pi pi-fw pi-pencil',
items: [
{
icon: 'pi pi-fw pi-pencil',
items: [{
key: '1_0',
label: 'Left',
icon:'pi pi-fw pi-align-left'
icon: 'pi pi-fw pi-align-left'
},
{
key: '1_1',
label: 'Right',
icon:'pi pi-fw pi-align-right'
icon: 'pi pi-fw pi-align-right'
},
{
key: '1_2',
label: 'Center',
icon:'pi pi-fw pi-align-center'
icon: 'pi pi-fw pi-align-center'
},
{
key: '1_3',
label: 'Justify',
icon:'pi pi-fw pi-align-justify'
icon: 'pi pi-fw pi-align-justify'
}
]
},
{
key: '2',
label: 'Users',
icon:'pi pi-fw pi-user',
items: [
{
icon: 'pi pi-fw pi-user',
items: [{
key: '2_0',
label: 'New',
icon:'pi pi-fw pi-user-plus',
icon: 'pi pi-fw pi-user-plus',
},
{
key: '2_1',
label: 'Delete',
icon:'pi pi-fw pi-user-minus',
icon: 'pi pi-fw pi-user-minus',
},
{
key: '2_2',
label: 'Search',
icon:'pi pi-fw pi-users',
items: [
{
icon: 'pi pi-fw pi-users',
items: [{
key: '2_2_0',
label: 'Filter',
icon:'pi pi-fw pi-filter',
items: [
{
icon: 'pi pi-fw pi-filter',
items: [{
key: '2_2_0_0',
label: 'Print',
icon:'pi pi-fw pi-print'
}
]
icon: 'pi pi-fw pi-print'
}]
},
{
icon:'pi pi-fw pi-bars',
key: '2_2_1',
icon: 'pi pi-fw pi-bars',
label: 'List'
}
]
@ -352,36 +575,61 @@ export default {
]
},
{
key: '3',
label: 'Events',
icon:'pi pi-fw pi-calendar',
items: [
{
icon: 'pi pi-fw pi-calendar',
items: [{
key: '3_0',
label: 'Edit',
icon:'pi pi-fw pi-pencil',
items: [
{
icon: 'pi pi-fw pi-pencil',
items: [{
key: '3_0_0',
label: 'Save',
icon:'pi pi-fw pi-calendar-plus'
icon: 'pi pi-fw pi-calendar-plus'
},
{
key: '3_0_0',
label: 'Delete',
icon:'pi pi-fw pi-calendar-minus'
icon: 'pi pi-fw pi-calendar-minus'
}
]
},
{
key: '3_1',
label: 'Archieve',
icon:'pi pi-fw pi-calendar-times',
items: [
{
icon: 'pi pi-fw pi-calendar-times',
items: [{
key: '3_1_0',
label: 'Remove',
icon:'pi pi-fw pi-calendar-minus'
icon: 'pi pi-fw pi-calendar-minus'
}]
}
]
}
]
}
]
},
methods: {
expandAll() {
for (let node of this.items) {
this.expandNode(node);
}
this.expandedKeys = {
...this.expandedKeys
};
},
collapseAll() {
this.expandedKeys = {};
},
expandNode(node) {
if (node.items && node.items.length) {
this.expandedKeys[node.key] = true;
for (let child of node.items) {
this.expandNode(child);
}
}
}
}
}
@ -399,7 +647,15 @@ export default {
content: `
<template>
<div>
<h5>Default</h5>
<PanelMenu :model="items" />
<h5>Programmatic</h5>
<div class="p-mb-3">
<Button type="button" icon="pi pi-plus" label="Expand All" @click="expandAll" class="p-mr-2" />
<Button type="button" icon="pi pi-minus" label="Collapse All" @click="collapseAll" />
</div>
<PanelMenu :model="items" :expandedKeys="expandedKeys" />
</div>
</template>
@ -408,85 +664,97 @@ import { ref } from 'vue';
export default {
setup() {
const items = ref([
{
const expandedKeys = ref({});
const items = ref([{
key: '0',
label: 'File',
icon:'pi pi-fw pi-file',
items: [
{
icon: 'pi pi-fw pi-file',
items: [{
key: '0_0',
label: 'New',
icon:'pi pi-fw pi-plus',
items: [
{
icon: 'pi pi-fw pi-plus',
items: [{
key: '0_0_0',
label: 'Bookmark',
icon:'pi pi-fw pi-bookmark'
icon: 'pi pi-fw pi-bookmark'
},
{
key: '0_0_1',
label: 'Video',
icon:'pi pi-fw pi-video'
icon: 'pi pi-fw pi-video'
}
]
},
{
key: '0_1',
label: 'Delete',
icon:'pi pi-fw pi-trash'
icon: 'pi pi-fw pi-trash'
},
{
key: '0_2',
label: 'Export',
icon:'pi pi-fw pi-external-link'
icon: 'pi pi-fw pi-external-link'
}
]
},
{
key: '1',
label: 'Edit',
icon:'pi pi-fw pi-pencil',
items: [
{
icon: 'pi pi-fw pi-pencil',
items: [{
key: '1_0',
label: 'Left',
icon:'pi pi-fw pi-align-left'
icon: 'pi pi-fw pi-align-left'
},
{
key: '1_1',
label: 'Right',
icon:'pi pi-fw pi-align-right'
icon: 'pi pi-fw pi-align-right'
},
{
key: '1_2',
label: 'Center',
icon:'pi pi-fw pi-align-center'
icon: 'pi pi-fw pi-align-center'
},
{
key: '1_3',
label: 'Justify',
icon:'pi pi-fw pi-align-justify'
icon: 'pi pi-fw pi-align-justify'
}
]
},
{
key: '2',
label: 'Users',
icon:'pi pi-fw pi-user',
items: [
{
icon: 'pi pi-fw pi-user',
items: [{
key: '2_0',
label: 'New',
icon:'pi pi-fw pi-user-plus',
icon: 'pi pi-fw pi-user-plus',
},
{
key: '2_1',
label: 'Delete',
icon:'pi pi-fw pi-user-minus',
icon: 'pi pi-fw pi-user-minus',
},
{
key: '2_2',
label: 'Search',
icon:'pi pi-fw pi-users',
items: [
{
icon: 'pi pi-fw pi-users',
items: [{
key: '2_2_0',
label: 'Filter',
icon:'pi pi-fw pi-filter',
items: [
{
icon: 'pi pi-fw pi-filter',
items: [{
key: '2_2_0_0',
label: 'Print',
icon:'pi pi-fw pi-print'
}
]
icon: 'pi pi-fw pi-print'
}]
},
{
icon:'pi pi-fw pi-bars',
key: '2_2_1',
icon: 'pi pi-fw pi-bars',
label: 'List'
}
]
@ -494,38 +762,60 @@ export default {
]
},
{
key: '3',
label: 'Events',
icon:'pi pi-fw pi-calendar',
items: [
{
icon: 'pi pi-fw pi-calendar',
items: [{
key: '3_0',
label: 'Edit',
icon:'pi pi-fw pi-pencil',
items: [
{
icon: 'pi pi-fw pi-pencil',
items: [{
key: '3_0_0',
label: 'Save',
icon:'pi pi-fw pi-calendar-plus'
icon: 'pi pi-fw pi-calendar-plus'
},
{
key: '3_0_0',
label: 'Delete',
icon:'pi pi-fw pi-calendar-minus'
icon: 'pi pi-fw pi-calendar-minus'
}
]
},
{
key: '3_1',
label: 'Archieve',
icon:'pi pi-fw pi-calendar-times',
items: [
{
icon: 'pi pi-fw pi-calendar-times',
items: [{
key: '3_1_0',
label: 'Remove',
icon:'pi pi-fw pi-calendar-minus'
}
]
icon: 'pi pi-fw pi-calendar-minus'
}]
}
]
}
]);
return { items }
const expandAll = () => {
for (let node of nodes.value) {
expandNode(node);
}
expandedKeys.value = {...expandedKeys.value};
};
const collapseAll = () => {
expandedKeys.value = {};
};
const expandNode = (node) => {
if (node.children && node.children.length) {
expandedKeys.value[node.key] = true;
for (let child of node.children) {
expandNode(child);
}
}
};
return { items, expandedKeys, expandAll, collapseAll, expandNode }
}
}
<\\/script>

View File

@ -593,7 +593,7 @@ export default {
<td>expandedKeys</td>
<td>array</td>
<td>null</td>
<td>A map of keys to represent the state of the tree expansion state in controlled mode.</td>
<td>A map of keys to represent the expansion state in controlled mode.</td>
</tr>
<tr>
<td>selectionMode</td>