Skip to content

Commit

Permalink
fix(menu): pass key to <MenuDivider /> (uber#5218)
Browse files Browse the repository at this point in the history
When `{ divider: true }` was passed as an item to the menu component
would not pass the key prop causing the react warning for missing keys.

With these changes the current scope's iterator `itemIndex` is passed as
it is passed to the `<Option />` component.

Co-authored-by: Chase Starr <[email protected]>
  • Loading branch information
V-Mann-Nick and chasestarr authored Nov 17, 2022
1 parent d8a9819 commit 8537953
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/menu/__tests__/menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,19 @@ describe('Menu Stateless Component', () => {
const options = getAllByTestId(container, 'option');
expect(options.length).toBe(2);
});

it('renders dividers without react key warning', () => {
const original = console.error;
console.error = jest.fn();
const itemsWithDivider = [{ label: 'item1' }, { divider: true }, { label: 'item2' }];
// @ts-expect-error todo(flow->ts)
render(<Menu {...getSharedProps()} items={itemsWithDivider} />);
expect(console.error).not.toHaveBeenCalledWith(
expect.stringContaining('Each child in a list should have a unique "key" prop.'),
expect.anything(),
expect.anything(),
expect.anything()
);
console.error = original;
});
});
2 changes: 1 addition & 1 deletion src/menu/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default function Menu(props: StatelessMenuProps) {
const { getRequiredItemProps = (item, index) => ({} as RenderItemProps) } = props;

if (item.divider === true) {
return <MenuDivider {...menuDividerProps} />;
return <MenuDivider key={itemIndex} {...menuDividerProps} />;
}

const {
Expand Down

0 comments on commit 8537953

Please sign in to comment.