Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #6489: add menu context to menu #6488

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -329,20 +333,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 @@ -351,7 +356,7 @@ export const Menu = React.memo(
onClick: (event) => onItemClick(event, item, key),
className: cx('content')
},
ptm('content')
getMenuItemPTOptions('content', menuContext)
);

const actionProps = mergeProps(
Expand All @@ -366,7 +371,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 @@ -405,7 +410,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
Loading