Skip to content

Commit c3c7245

Browse files
cgood92Clint Goodman
andauthored
feat: dom attributes for <Item/> in ActionGroup's collapsed menu can now be passed down (#4499)
Co-authored-by: Clint Goodman <[email protected]>
1 parent 59b0313 commit c3c7245

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

packages/@react-spectrum/actiongroup/src/ActionGroup.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ function ActionGroupMenu<T>({state, isDisabled, isEmphasized, staticColor, items
474474
disallowEmptySelection={state.selectionManager.disallowEmptySelection}
475475
onSelectionChange={(keys) => state.selectionManager.setSelectedKeys(keys)}
476476
onAction={onAction}>
477-
{node => <Item textValue={node.textValue}>{node.rendered}</Item>}
477+
{node => <Item textValue={node.textValue} {...filterDOMProps(node.props)}>{node.rendered}</Item>}
478478
</Menu>
479479
</MenuTrigger>
480480
);

packages/@react-spectrum/actiongroup/stories/ActionGroup.stories.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,23 +276,23 @@ function renderOverflow(props) {
276276
return (
277277
<div style={{padding: '10px', resize: 'both', overflow: 'auto', width: 250, backgroundColor: 'var(--spectrum-global-color-gray-50)'}}>
278278
<ActionGroup {...props} summaryIcon={<TextIcon />} maxHeight="100%">
279-
<Item key="1">
279+
<Item key="1" data-testid="edit">
280280
<DrawIcon />
281281
<Text>Edit</Text>
282282
</Item>
283-
<Item key="2">
283+
<Item key="2" data-testid="copy">
284284
<CopyIcon />
285285
<Text>Copy</Text>
286286
</Item>
287-
<Item key="3">
287+
<Item key="3" data-testid="delete">
288288
<DeleteIcon />
289289
<Text>Delete</Text>
290290
</Item>
291-
<Item key="4">
291+
<Item key="4" data-testid="move">
292292
<MoveIcon />
293293
<Text>Move</Text>
294294
</Item>
295-
<Item key="5">
295+
<Item key="5" data-testid="duplicate">
296296
<DuplicateIcon />
297297
<Text>Duplicate</Text>
298298
</Item>

packages/@react-spectrum/actiongroup/test/ActionGroup.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,29 @@ describe('ActionGroup', function () {
744744
expect(onAction).toHaveBeenCalledWith('three');
745745
});
746746

747+
it('collapsed menu items can have DOM attributes passed to them', function () {
748+
let onAction = jest.fn();
749+
let tree = render(
750+
<Provider theme={theme}>
751+
<ActionGroup overflowMode="collapse" onAction={onAction}>
752+
<Item key="one">One</Item>
753+
<Item key="two" data-element="two">Two</Item>
754+
<Item key="three">Three</Item>
755+
<Item key="four">Four</Item>
756+
</ActionGroup>
757+
</Provider>
758+
);
759+
760+
let actiongroup = tree.getByRole('toolbar');
761+
let buttons = within(actiongroup).getAllByRole('button');
762+
763+
triggerPress(buttons[1]);
764+
765+
let menu = tree.getByRole('menu');
766+
let items = within(menu).getAllByRole('menuitem');
767+
expect(items[0]).toHaveAttribute('data-element', 'two');
768+
});
769+
747770
it('handles keyboard focus management properly', function () {
748771
let onAction = jest.fn();
749772
let tree = render(

packages/@react-spectrum/menu/src/MenuItem.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
import CheckmarkMedium from '@spectrum-icons/ui/CheckmarkMedium';
1414
import {classNames, ClearSlots, SlotProvider} from '@react-spectrum/utils';
1515
import {DOMAttributes, Node} from '@react-types/shared';
16+
import {filterDOMProps, mergeProps, useSlotId} from '@react-aria/utils';
1617
import {FocusRing} from '@react-aria/focus';
1718
import {Grid} from '@react-spectrum/layout';
1819
import InfoOutline from '@spectrum-icons/workflow/InfoOutline';
1920
// @ts-ignore
2021
import intlMessages from '../intl/*.json';
21-
import {mergeProps, useSlotId} from '@react-aria/utils';
2222
import React, {Key, useRef} from 'react';
2323
import styles from '@adobe/spectrum-css-temp/components/menu/vars.css';
2424
import {Text} from '@react-spectrum/text';
@@ -51,6 +51,8 @@ export function MenuItem<T>(props: MenuItemProps<T>) {
5151
if (isMenuDialogTrigger) {
5252
isUnavailable = menuDialogContext.isUnavailable;
5353
}
54+
55+
let domProps = filterDOMProps(item.props);
5456

5557
let {
5658
onClose,
@@ -104,7 +106,7 @@ export function MenuItem<T>(props: MenuItemProps<T>) {
104106
return (
105107
<FocusRing focusRingClass={classNames(styles, 'focus-ring')}>
106108
<li
107-
{...mergeProps(menuItemProps)}
109+
{...mergeProps(menuItemProps, domProps)}
108110
ref={ref}
109111
className={classNames(
110112
styles,

0 commit comments

Comments
 (0)