-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Menu: Arrow key navigation updates #1761
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,74 +26,47 @@ | |
|
||
import 'jest-styled-components' | ||
import React from 'react' | ||
import { assertSnapshot, renderWithTheme } from '@looker/components-test-utils' | ||
import { renderWithTheme } from '@looker/components-test-utils' | ||
|
||
import { Box } from '../Layout' | ||
import { MenuGroup } from './MenuGroup' | ||
import { MenuItem } from './MenuItem' | ||
import { MenuList } from './MenuList' | ||
|
||
test('MenuGroup', () => { | ||
assertSnapshot( | ||
<MenuGroup> | ||
<MenuItem>who!</MenuItem> | ||
</MenuGroup> | ||
) | ||
}) | ||
|
||
test('MenuGroup - label', () => { | ||
assertSnapshot( | ||
<MenuGroup label="Questions"> | ||
<MenuItem>what?</MenuItem> | ||
<MenuItem>who?</MenuItem> | ||
<MenuItem>where?</MenuItem> | ||
</MenuGroup> | ||
) | ||
}) | ||
|
||
test('MenuGroup - JSX label', () => { | ||
assertSnapshot( | ||
<MenuGroup label={<Box>Questions</Box>}> | ||
<MenuItem>what?</MenuItem> | ||
<MenuItem>who?</MenuItem> | ||
<MenuItem>where?</MenuItem> | ||
</MenuGroup> | ||
) | ||
}) | ||
|
||
test('MenuGroup - indents MenuItem with no icon when a sibling within the same group has an icon', () => { | ||
const { getByTestId } = renderWithTheme( | ||
<MenuGroup> | ||
<MenuItem icon="Calendar">Gouda</MenuItem> | ||
<MenuItem id="cheddar">Cheddar</MenuItem> | ||
</MenuGroup> | ||
) | ||
|
||
getByTestId('menu-item-cheddar-icon-placeholder') | ||
}) | ||
|
||
test('MenuGroup - indents MenuItem with no icon when a sibling outside of the group has an icon', () => { | ||
const { getByTestId } = renderWithTheme( | ||
<MenuList> | ||
<MenuItem icon="Calendar">Gouda</MenuItem> | ||
describe('MenuGroup', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing all the snapshots since they're covered by image snapshots and otherwise this PR would need to update them. Also added the |
||
test('indents MenuItem with no icon when a sibling within the same group has an icon', () => { | ||
const { getByTestId } = renderWithTheme( | ||
<MenuGroup> | ||
<MenuItem icon="Calendar">Gouda</MenuItem> | ||
<MenuItem id="cheddar">Cheddar</MenuItem> | ||
</MenuGroup> | ||
</MenuList> | ||
) | ||
|
||
getByTestId('menu-item-cheddar-icon-placeholder') | ||
}) | ||
|
||
test('MenuGroup - does not indent MenuItems with no icon if all siblings do not have icons', () => { | ||
const { queryByTestId } = renderWithTheme( | ||
<MenuGroup> | ||
<MenuItem>Gouda</MenuItem> | ||
<MenuItem id="cheddar">Cheddar</MenuItem> | ||
</MenuGroup> | ||
) | ||
) | ||
|
||
getByTestId('menu-item-cheddar-icon-placeholder') | ||
}) | ||
|
||
test('indents MenuItem with no icon when a sibling outside of the group has an icon', () => { | ||
const { getByTestId } = renderWithTheme( | ||
<MenuList> | ||
<MenuItem icon="Calendar">Gouda</MenuItem> | ||
<MenuGroup> | ||
<MenuItem id="cheddar">Cheddar</MenuItem> | ||
</MenuGroup> | ||
</MenuList> | ||
) | ||
|
||
getByTestId('menu-item-cheddar-icon-placeholder') | ||
}) | ||
|
||
test('does not indent MenuItems with no icon if all siblings do not have icons', () => { | ||
const { queryByTestId } = renderWithTheme( | ||
<MenuGroup> | ||
<MenuItem>Gouda</MenuItem> | ||
<MenuItem id="cheddar">Cheddar</MenuItem> | ||
</MenuGroup> | ||
) | ||
|
||
expect( | ||
queryByTestId('menu-item-cheddar-icon-placeholder') | ||
).not.toBeInTheDocument() | ||
expect( | ||
queryByTestId('menu-item-cheddar-icon-placeholder') | ||
).not.toBeInTheDocument() | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,7 +86,11 @@ const MenuItemInternal: FC<MenuItemProps> = (props) => { | |
} = props | ||
|
||
const [isFocusVisible, setFocusVisible] = useState(false) | ||
const { compact: contextCompact } = useContext(MenuItemContext) | ||
const { | ||
compact: contextCompact, | ||
renderIconPlaceholder, | ||
setRenderIconPlaceholder, | ||
} = useContext(MenuItemContext) | ||
const compact = propCompact === undefined ? contextCompact : propCompact | ||
|
||
const handleOnBlur = (event: React.FocusEvent<HTMLLIElement>) => { | ||
|
@@ -95,12 +99,6 @@ const MenuItemInternal: FC<MenuItemProps> = (props) => { | |
} | ||
|
||
const { closeModal } = useContext(DialogContext) | ||
const { | ||
renderIconPlaceholder, | ||
setRenderIconPlaceholder, | ||
handleArrowDown, | ||
handleArrowUp, | ||
} = useContext(MenuItemContext) | ||
|
||
const handleOnClick = (event: React.MouseEvent<HTMLLIElement>) => { | ||
setFocusVisible(false) | ||
|
@@ -111,17 +109,6 @@ const MenuItemInternal: FC<MenuItemProps> = (props) => { | |
} | ||
} | ||
|
||
const handleOnKeyDown = (event: React.KeyboardEvent<HTMLLIElement>) => { | ||
switch (event.key) { | ||
case 'ArrowUp': | ||
handleArrowUp && handleArrowUp(event) | ||
break | ||
case 'ArrowDown': | ||
handleArrowDown && handleArrowDown(event) | ||
break | ||
} | ||
} | ||
|
||
const handleOnKeyUp = (event: React.KeyboardEvent<HTMLLIElement>) => { | ||
onKeyUp && onKeyUp(event) | ||
setFocusVisible(true) | ||
|
@@ -174,7 +161,13 @@ const MenuItemInternal: FC<MenuItemProps> = (props) => { | |
: props.rel | ||
|
||
const menuItemContent = ( | ||
<Component href={href} rel={rel} role="menuitem" target={target}> | ||
<Component | ||
href={href} | ||
rel={rel} | ||
role="menuitem" | ||
target={target} | ||
tabIndex={-1} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
> | ||
{renderedIcon} | ||
<span> | ||
{children} | ||
|
@@ -197,7 +190,6 @@ const MenuItemInternal: FC<MenuItemProps> = (props) => { | |
onBlur={handleOnBlur} | ||
onClick={disabled ? undefined : handleOnClick} | ||
onKeyUp={handleOnKeyUp} | ||
onKeyDown={handleOnKeyDown} | ||
className={className} | ||
> | ||
{tooltip ? ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this image snapshot to make up for a jest snapshot I removed.