Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
32722d4
make space on menu item not scroll the page
lucivpav Jul 30, 2019
251cb64
Merge branch 'master' into fix/menu-item-space
lucivpav Jul 30, 2019
e34e7dc
update changelog
lucivpav Jul 30, 2019
64a8127
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 5, 2019
4f46ef2
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 9, 2019
ded06cf
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 9, 2019
e0b6c50
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 13, 2019
648d2e9
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 13, 2019
1fe7b4c
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 13, 2019
2e2018f
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 13, 2019
a6e2a24
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 14, 2019
11b1de0
fix submenu not closing
lucivpav Aug 14, 2019
e90e881
update changelog
lucivpav Aug 14, 2019
d273d40
update specification of menuItemBehavior
lucivpav Aug 14, 2019
5147b11
add submenu close test
lucivpav Aug 14, 2019
d85e012
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 15, 2019
ca21749
rework keyboard click fix
lucivpav Aug 16, 2019
ad26b34
Merge branch 'fix/menu-item-space' of github.com:stardust-ui/react in…
lucivpav Aug 16, 2019
607ea95
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 16, 2019
90de93b
fix test
lucivpav Aug 16, 2019
cd6c892
Merge branch 'fix/menu-item-space' of github.com:stardust-ui/react in…
lucivpav Aug 16, 2019
d93e3d9
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 19, 2019
89c651b
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 19, 2019
9b914be
Merge branch 'fix/menu-item-space' of github.com:stardust-ui/react in…
lucivpav Aug 19, 2019
e660905
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 20, 2019
b32599e
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 21, 2019
e8b5fc0
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 21, 2019
31ac3a9
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 22, 2019
c1ac51c
Merge branch 'master' into fix/menu-item-space
lucivpav Aug 22, 2019
cb2fbcc
simplify using closeAllMenus
lucivpav Aug 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Fix switching modes in `Dropdown` @layershifter ([#1830](https://github.com/stardust-ui/react/pull/1830))
- Fix a runtime error if context is missing @layershifter ([#1837](https://github.com/stardust-ui/react/pull/1837))
- Fix RTL mode in `Dialog` @lucivpav ([#1828](https://github.com/stardust-ui/react/pull/1828))
- Do not scroll the page when spacebar is pressed on focused `MenuItem`, fix submenu not closing @lucivpav ([#1735](https://github.com/stardust-ui/react/pull/1735))

### Features
- Add `overwrite` prop to `Provider` @layershifter ([#1780](https://github.com/stardust-ui/react/pull/1780))
Expand Down
8 changes: 7 additions & 1 deletion packages/react/src/components/Menu/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ class MenuItem extends AutoControlledComponent<WithAsProp<MenuItemProps>, MenuIt
doNotNavigateNextParentItem: event => {
event.stopPropagation()
},
closeAllMenus: event => this.closeAllMenus(event),
}

outsideClickHandler = e => {
Expand Down Expand Up @@ -379,10 +380,15 @@ class MenuItem extends AutoControlledComponent<WithAsProp<MenuItemProps>, MenuIt
}
const { inSubmenu } = this.props
this.trySetMenuOpen(false, e, () => {
if (!inSubmenu && this.props.vertical) {
if (!inSubmenu) {
focusAsync(this.itemRef.current)
}
})

// avoid spacebar scrolling the page
if (!inSubmenu) {
e.preventDefault()
}
}

closeMenu = (e: Event, forceTriggerFocus?: boolean) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const menuItemBehavior: Accessibility<MenuItemBehaviorProps> = props => ({
},
},
wrapper: {
closeAllMenus: {
keyCombinations: [{ keyCode: keyboardKey.Enter }, { keyCode: keyboardKey.Spacebar }],
},
closeAllMenusAndFocusNextParentItem: {
keyCombinations: [{ keyCode: keyboardKey.ArrowRight }],
},
Expand Down
30 changes: 30 additions & 0 deletions packages/react/test/specs/components/Menu/Menu-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from 'src/lib/accessibility'
import { AccessibilityDefinition } from 'src/lib/accessibility/types'
import { ReactWrapper } from 'enzyme'
import * as keyboardKey from 'keyboard-key'

const menuImplementsCollectionShorthandProp = implementsCollectionShorthandProp(Menu)

Expand All @@ -25,6 +26,16 @@ describe('Menu', () => {
{ key: 'users', content: 'users', 'data-foo': 'something' },
]

const getNestedItems = () => [
{ key: 'home', content: 'home', onClick: jest.fn(), 'data-foo': 'something' },
{
key: 'users',
content: 'users',
'data-foo': 'something',
menu: [{ key: '1', content: 'Alice' }, { key: '2', content: 'Bob' }],
},
]

describe('items', () => {
it('renders children', () => {
const menuItems = mountWithProvider(<Menu items={getItems()} />).find('MenuItem')
Expand Down Expand Up @@ -66,6 +77,25 @@ describe('Menu', () => {
expect(menuItems.everyWhere(item => item.prop('data-foo') === 'something')).toBe(true)
})

it('closes menu when item is clicked using spacebar', () => {
const menu = mountWithProvider(<Menu items={getNestedItems()} />)
const menuItems = menu.find('MenuItem')

menuItems
.at(1)
.find('a')
.first()
.simulate('keydown', { keyCode: keyboardKey.Spacebar })
menuItems
.at(1)
.at(0)
.find('a')
.first()
.simulate('keydown', { keyCode: keyboardKey.Spacebar })

expect(menuItems.at(1).state('menuOpen')).toBe(false)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the fix, the Menu component behaves in a weird way. The menuOpen state is undefined, instead of true. Upon investigation, this.props in handleClick are not what one would expect. They seem like the props of the root component. This probably causes the menuOpen not to be defined. I spent too much time on this, trying different ways to make the value true instead of undefined, but without luck.

})

describe('activeIndex', () => {
it('should not be set by default', () => {
const menuItems = mountWithProvider(<Menu items={getItems()} />).find('MenuItem')
Expand Down