PrimeReact menus components share a common api to specify the menuitems and submenus.
Core of the API is the MenuItem class that defines various options such as the label, icon and children of an item in a menu.
const items = [
{
label: 'Options',
items: [{label: 'New', icon: 'pi pi-fw pi-plus',command:()=>{ window.location.hash="/fileupload"; }},
{label: 'Delete', icon: 'pi pi-fw pi-trash', url: 'http://primetek.com.tr'}]
},
{
label: 'Account',
items: [{label: 'Options', icon: 'pi pi-fw pi-cog',command:()=>{ window.location.hash="/"; }},
{label: 'Sign Out', icon: 'pi pi-fw pi-power-off'} ]
}
]
MenuItem provides the following properties. Note that not all of them may be utilized by the corresponding menu component.
Name | Type | Default | Description |
---|---|---|---|
label | string | null | Text of the item. |
icon | any | null | Icon of the item. It can be a string, JSX.Element or method. |
command | function | null | Callback to execute when item is clicked. |
url | string | null | External link to navigate when item is clicked. |
items | array | null | An array of children menuitems. |
expanded | boolean | false | Visibility of submenu. |
disabled | boolean | false | When set as true, disables the menuitem. |
visible | boolean | true | When set as false, hides the menu item. |
target | string | null | Specifies where to open the linked document. |
separator | boolean | false | Defines the item as a separator. |
style | object | null | Inline style of the menuitem. |
className | string | null | Style class of the menuitem. |
template | any | null | Template of the menuitem. |
Used to create custom menuitem elements.
const items =
[
{
label: 'New',
template: (item, options) => {
return (
/* custom element */
<a className={options.className} target={item.target} onClick={options.onClick}>
<span className={classNames(options.iconClassName, 'pi pi-home')}></span>;
<span className={options.labelClassName}>{item.label}</span>;
</a>
);
}
}
];
template: (item, options) => {
// item: Current item object.
// options.onClick: Click event for the default element.
// options.className: Style class of the default element.
// options.labelClassName: Style class of the default label element.
// options.iconClassName: Style class of the default icon element.
// options.element: Default element created by the component.
// options.props: component props.
// Note: Extra options may come according to the components.
}
The function to invoke when an item is clicked is defined using the command property.
const items =
[
{
label: 'New',
icon: 'pi pi-plus',
command: (event) => {
// event.originalEvent: Browser event
// event.item: MenuItem instance
}
}
];
Navigation is specified using url property for external links or using command function for internal router.
const items =
[
{
label: 'New',
icon: 'pi pi-plus',
command: (event) => {
window.location.hash = "/fileupload";
}
},
{
label: 'Link',
icon: 'pi pi-check',
url: 'https://www.primefaces.org/primereact'
}
];
Built-in component themes created by the PrimeReact Theme Designer.
Premium themes are only available exclusively for PrimeReact Theme Designer subscribers and therefore not included in PrimeReact core.
Beautifully crafted premium create-react-app application templates by the PrimeTek design team.