Skip to content

Commit

Permalink
Issue fix for the menu has no menu items #1223 (#1279)
Browse files Browse the repository at this point in the history
* fix issue #1223

* fix empty menu items without useState
  • Loading branch information
JeevantheDev authored Jan 15, 2022
1 parent 77c3141 commit 71904a1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
10 changes: 8 additions & 2 deletions docs/documentation/components/menu.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function MenuOptionsGroupExample() {
{ label: 'Descending', value: 'desc' },
]}
selected={selectedOrder}
onChange={(selected) => setSelectedOrder(selected)}
onChange={selected => setSelectedOrder(selected)}
/>
<Menu.Divider />
<Menu.OptionsGroup
Expand All @@ -194,7 +194,7 @@ function MenuOptionsGroupExample() {
{ label: 'Type', value: 'type' },
]}
selected={selectedField}
onChange={(selected) => setSelectedField(selected)}
onChange={selected => setSelectedField(selected)}
/>
</Menu>
)
Expand Down Expand Up @@ -237,3 +237,9 @@ The focus styles are currently not working in the docs, sorry for the inconvenie
<Button>Custom Menu Items</Button>
</Popover>
```

## Menu without items

```jsx
<Menu></Menu>
```
30 changes: 14 additions & 16 deletions src/menu/src/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,17 @@ import MenuOptionsGroup from './MenuOptionsGroup'

const Menu = memo(function Menu(props) {
const menuRef = useRef(null)

const firstItem = useRef()
const lastItem = useRef()

const menuItems = useRef()

useEffect(() => {
const currentMenuRef = menuRef.current

menuItems.current = currentMenuRef
? [
...currentMenuRef.querySelectorAll(
'[role="menuitemradio"]:not([disabled]), [role="menuitem"]:not([disabled])'
)
]
: []

if (menuItems.current.length === 0) {
throw new Error('The menu has no menu items')
}
menuItems.current = [
...currentMenuRef.querySelectorAll('[role="menuitemradio"]:not([disabled]), [role="menuitem"]:not([disabled])')
]

firstItem.current = menuItems.current[0]
lastItem.current = menuItems.current[menuItems.current.length - 1]
Expand Down Expand Up @@ -68,9 +60,7 @@ const Menu = memo(function Menu(props) {
const { target } = e
const menuItem = menuItems.current && menuItems.current.find(item => item === target)

if (!menuItem) {
return
}
if (!menuItem) return

if (e.key === 'ArrowDown') {
e.preventDefault()
Expand Down Expand Up @@ -101,9 +91,17 @@ const Menu = memo(function Menu(props) {
}, [menuRef])

const { children } = props

const renderEmptyChildren = () => {
return (
<MenuGroup>
<MenuItem disabled>No items...</MenuItem>
</MenuGroup>
)
}
return (
<Pane is="nav" ref={menuRef} role="menu" outline="none">
{children}
{children || renderEmptyChildren()}
</Pane>
)
})
Expand Down
3 changes: 3 additions & 0 deletions src/menu/stories/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ storiesOf('menu', module)
>
<Button>Hover menus</Button>
</Popover>
<Popover position={Position.BOTTOM_LEFT} content={<Menu></Menu>}>
<Button marginTop={16}>Without Menu Items</Button>
</Popover>

<UnorderedList marginTop={24}>
<ListItem>Arrow down on a button will bring focus inside the popover.</ListItem>
Expand Down

0 comments on commit 71904a1

Please sign in to comment.