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

[MenuItem][Joy] Add internal id to DOM #37361

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions packages/mui-base/src/useMenuItem/useMenuItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export default function useMenuItem(params: UseMenuItemParameters): UseMenuItemR
...resolvedMenuItemProps,
role: 'menuitem',
ref: handleRef,
id,
};
};

Expand Down
14 changes: 14 additions & 0 deletions packages/mui-joy/src/MenuItem/MenuItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,20 @@ describe('Joy <MenuItem />', () => {
expect(menuitem).to.have.attribute('aria-disabled', 'true');
});

it('should add external id to DOM and override internal id', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is passing in master too but i just wanted to add a test which tests adding extrenal id to DOM when available

render(<MenuItem id="test-id" />);
const menuitem = screen.getByRole('menuitem');

expect(menuitem).to.have.attribute('id', 'test-id');
});

it('should add internal id to DOM when external id is not provided', () => {
render(<MenuItem />);
const menuitem = screen.getByRole('menuitem');

expect(menuitem).to.have.attribute('id');
});

it('can be selected', () => {
render(<MenuItem selected />);
const menuitem = screen.getByRole('menuitem');
Expand Down
6 changes: 6 additions & 0 deletions packages/mui-joy/src/MenuItem/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const MenuItem = React.forwardRef(function MenuItem(inProps, ref) {
selected = false,
color: colorProp = selected ? 'primary' : 'neutral',
orientation = 'horizontal',
id,
variant = 'plain',
slots = {},
slotProps = {},
Expand All @@ -72,6 +73,7 @@ const MenuItem = React.forwardRef(function MenuItem(inProps, ref) {
const { getRootProps, disabled, focusVisible } = useMenuItem({
disabled: disabledProp,
rootRef: ref,
id,
});

const ownerState = {
Expand Down