Skip to content

Commit

Permalink
Merge pull request #6488 from peconomou929/feature/add-context-to-menu
Browse files Browse the repository at this point in the history
Fix #6489: add menu context to menu
  • Loading branch information
nitrogenous authored Apr 30, 2024
2 parents 935f481 + 68cb21c commit a6973cf
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
34 changes: 34 additions & 0 deletions components/doc/common/apidoc/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -34403,6 +34403,40 @@
"optional": false,
"readonly": false,
"type": "MenuState"
},
{
"name": "context",
"optional": false,
"readonly": false,
"type": "MenuContext"
}
],
"callbacks": []
},
"MenuContext": {
"description": "Defines current options in Menu component.",
"relatedProp": "",
"props": [
{
"name": "item",
"optional": false,
"readonly": false,
"type": "MenuItem",
"description": "Current menuitem"
},
{
"name": "index",
"optional": false,
"readonly": false,
"type": "number",
"description": "Index of the menuitem"
},
{
"name": "parentId",
"optional": false,
"readonly": false,
"type": "null | string",
"description": "Id of the submenu header containing the menuitem"
}
],
"callbacks": []
Expand Down
15 changes: 10 additions & 5 deletions components/lib/menu/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export const Menu = React.memo(
}
});

const getMenuItemPTOptions = (key, menuContext) => {
return ptm(key, { context: menuContext });
};

useHandleStyle(MenuBase.css.styles, isUnstyled, { name: 'menu' });
const menuRef = React.useRef(null);
const listRef = React.useRef(null);
Expand Down Expand Up @@ -335,20 +339,21 @@ export const Menu = React.memo(
return null;
}

const menuContext = { item, index, parentId };
const linkClassName = classNames('p-menuitem-link', { 'p-disabled': item.disabled });
const iconClassName = classNames('p-menuitem-icon', item.icon);
const iconProps = mergeProps(
{
className: cx('icon')
},
ptm('icon')
getMenuItemPTOptions('icon', menuContext)
);
const icon = IconUtils.getJSXIcon(item.icon, { ...iconProps }, { props });
const labelProps = mergeProps(
{
className: cx('label')
},
ptm('label')
getMenuItemPTOptions('label', menuContext)
);
const label = item.label && <span {...labelProps}>{item.label}</span>;
const key = item.id || (parentId || idState) + '_' + index;
Expand All @@ -358,7 +363,7 @@ export const Menu = React.memo(
onMouseMove: (event) => onItemMouseMove(event, key),
className: cx('content')
},
ptm('content')
getMenuItemPTOptions('content', menuContext)
);

const actionProps = mergeProps(
Expand All @@ -373,7 +378,7 @@ export const Menu = React.memo(
'aria-disabled': item.disabled,
'data-p-disabled': item.disabled
},
ptm('action')
getMenuItemPTOptions('action', menuContext)
);

let content = (
Expand Down Expand Up @@ -412,7 +417,7 @@ export const Menu = React.memo(
'data-p-focused': focusedOptionId() === key,
'data-p-disabled': item.disabled || false
},
ptm('menuitem')
getMenuItemPTOptions('menuitem', menuContext)
);

return <li {...menuitemProps}>{content}</li>;
Expand Down
19 changes: 19 additions & 0 deletions components/lib/menu/menu.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ export declare type MenuPassThroughTransitionType = ReactCSSTransitionProps | ((
export interface MenuPassThroughMethodOptions {
props: MenuProps;
state: MenuState;
context: MenuContext;
}

/**
* Defines current options in Menu component.
*/
export interface MenuContext {
/**
* Current menuitem
*/
item: MenuItem;
/**
* Index of the menuitem
*/
index: number;
/**
* Id of the submenu header containing the menuitem
*/
parentId: string | null;
}

/**
Expand Down

0 comments on commit a6973cf

Please sign in to comment.