fix: preserve menu item actions in admin dropdowns. Closes #4453.
This commit is contained in:
1 parent
0a77e50da1
commit
dd6b5ddf3a
2 files changed
+35
-1
No files matched your search
@@ -0,0 +1,31 @@
|
||||
import { Icon, MenuItem, Row, Text } from '@umami/react-zen';
|
||||
import { expect, test, vi } from 'vitest';
|
||||
import { Edit } from '@/components/icons';
|
||||
import { render, screen } from '@/test/render';
|
||||
import { MenuButton } from './MenuButton';
|
||||
|
||||
test('preserves child menu item actions while still notifying the parent handler', async () => {
|
||||
const onChildAction = vi.fn();
|
||||
const onMenuAction = vi.fn();
|
||||
const { user } = render(
|
||||
<MenuButton onAction={onMenuAction}>
|
||||
<MenuItem id="edit" onAction={onChildAction}>
|
||||
<Row alignItems="center" gap>
|
||||
<Icon>
|
||||
<Edit />
|
||||
</Icon>
|
||||
<Text>Edit</Text>
|
||||
</Row>
|
||||
</MenuItem>
|
||||
</MenuButton>,
|
||||
);
|
||||
|
||||
const button = screen.getByRole('button');
|
||||
|
||||
button.focus();
|
||||
await user.keyboard('{Enter}');
|
||||
await user.click(screen.getByRole('menuitem', { name: 'Edit' }));
|
||||
|
||||
expect(onChildAction).toHaveBeenCalledWith('edit');
|
||||
expect(onMenuAction).toHaveBeenCalledWith('edit');
|
||||
});
|
||||
@@ -34,7 +34,10 @@ export function MenuButton({
|
||||
{Children.map(children, child =>
|
||||
isValidElement(child)
|
||||
? cloneElement(child as ReactElement<{ onAction?: (key: Key) => void }>, {
|
||||
onAction: handleAction,
|
||||
onAction: (key: Key) => {
|
||||
child.props.onAction?.(key);
|
||||
handleAction(key);
|
||||
},
|
||||
})
|
||||
: child,
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user