Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

fix: preserve menu item actions in admin dropdowns. Closes #4453.

Francis Caodd6b5ddf3a1f
diff --git a/src/components/input/MenuButton.test.tsx b/src/components/input/MenuButton.test.tsx
new file mode 100644
index 000000000..342490f46
--- /dev/null
+++ b/src/components/input/MenuButton.test.tsx
@@ -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');
+});
diff --git a/src/components/input/MenuButton.tsx b/src/components/input/MenuButton.tsx
index e99e7dfb9..ec2efa1b1 100644
--- a/src/components/input/MenuButton.tsx
+++ b/src/components/input/MenuButton.tsx
@@ -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,
)}