Skip to content

Commit

Permalink
Fix <Menu> onAction being double called
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Oct 22, 2024
1 parent 1d77a3b commit 5a285f8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-cats-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystatic/core': patch
---

Fix menus items like inserting/removing columns/rows in tables in editors and inserting custom components triggering twice when clicked
5 changes: 5 additions & 0 deletions .changeset/khaki-elephants-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystar/ui': patch
---

Fix `Menu` `onAction` being double called
18 changes: 2 additions & 16 deletions design-system/pkg/src/menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,10 @@ function Menu<T extends object>(
>
{[...state.collection].map(item => {
if (item.type === 'section') {
return (
<MenuSection
key={item.key}
item={item}
state={state}
onAction={completeProps.onAction}
/>
);
return <MenuSection key={item.key} item={item} state={state} />;
}

let menuItem = (
<MenuItem
key={item.key}
item={item}
state={state}
onAction={completeProps.onAction}
/>
);
let menuItem = <MenuItem key={item.key} item={item} state={state} />;

if (item.wrapper) {
menuItem = item.wrapper(menuItem);
Expand Down
6 changes: 2 additions & 4 deletions design-system/pkg/src/menu/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useMenuItem } from '@react-aria/menu';
import { mergeProps } from '@react-aria/utils';
import { TreeState } from '@react-stately/tree';
import { Node } from '@react-types/shared';
import { Key, useRef } from 'react';
import { useRef } from 'react';

import { ListItem } from '@keystar/ui/listbox';
import { Text } from '@keystar/ui/typography';
Expand All @@ -16,12 +16,11 @@ type MenuItemProps<T> = {
item: Node<T>;
state: TreeState<T>;
isVirtualized?: boolean;
onAction?: (key: Key) => void;
};

/** @private */
export function MenuItem<T>(props: MenuItemProps<T>) {
let { item, state, isVirtualized, onAction } = props;
let { item, state, isVirtualized } = props;
let { onClose, closeOnSelect } = useMenuContext();

let { rendered, key } = item;
Expand All @@ -40,7 +39,6 @@ export function MenuItem<T>(props: MenuItemProps<T>) {
onClose,
closeOnSelect,
isVirtualized,
onAction,
},
state,
ref
Expand Down
14 changes: 3 additions & 11 deletions design-system/pkg/src/menu/MenuSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useSeparator } from '@react-aria/separator';
import { getChildNodes } from '@react-stately/collections';
import { TreeState } from '@react-stately/tree';
import { Node } from '@react-types/shared';
import { Fragment, Key } from 'react';
import { Fragment } from 'react';

import { Divider } from '@keystar/ui/layout';
import { css, tokenSchema } from '@keystar/ui/style';
Expand All @@ -14,12 +14,11 @@ import { MenuItem } from './MenuItem';
interface MenuSectionProps<T> {
item: Node<T>;
state: TreeState<T>;
onAction?: (key: Key) => void;
}

/** @private */
export function MenuSection<T>(props: MenuSectionProps<T>) {
let { item, state, onAction } = props;
let { item, state } = props;
let { itemProps, headingProps, groupProps } = useMenuSection({
heading: item.rendered,
'aria-label': item['aria-label'],
Expand Down Expand Up @@ -50,14 +49,7 @@ export function MenuSection<T>(props: MenuSectionProps<T>) {
)}
<div {...groupProps}>
{[...getChildNodes(item, state.collection)].map(node => {
let item = (
<MenuItem
key={node.key}
item={node}
state={state}
onAction={onAction}
/>
);
let item = <MenuItem key={node.key} item={node} state={state} />;

if (node.wrapper) {
item = node.wrapper(item);
Expand Down
6 changes: 6 additions & 0 deletions design-system/pkg/src/menu/test/Menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -567,14 +567,17 @@ describe('menu/Menu', () => {

firePress(item1);
expect(onAction).toHaveBeenCalledWith('One');
expect(onAction).toHaveBeenCalledTimes(1);
expect(onSelectionChange).toHaveBeenCalledTimes(0);

firePress(item2);
expect(onAction).toHaveBeenCalledWith('Two');
expect(onAction).toHaveBeenCalledTimes(2);
expect(onSelectionChange).toHaveBeenCalledTimes(0);

firePress(item3);
expect(onAction).toHaveBeenCalledWith('Three');
expect(onAction).toHaveBeenCalledTimes(3);
expect(onSelectionChange).toHaveBeenCalledTimes(0);
});

Expand Down Expand Up @@ -607,14 +610,17 @@ describe('menu/Menu', () => {

firePress(item1);
expect(onAction).toHaveBeenCalledWith('One');
expect(onAction).toHaveBeenCalledTimes(1);
expect(onSelectionChange).toHaveBeenCalledTimes(0);

firePress(item2);
expect(onAction).toHaveBeenCalledWith('Two');
expect(onAction).toHaveBeenCalledTimes(2);
expect(onSelectionChange).toHaveBeenCalledTimes(0);

firePress(item3);
expect(onAction).toHaveBeenCalledWith('Three');
expect(onAction).toHaveBeenCalledTimes(3);
expect(onSelectionChange).toHaveBeenCalledTimes(0);
});
});
Expand Down

0 comments on commit 5a285f8

Please sign in to comment.