Skip to content

Commit

Permalink
fix: do not hijack input when inside a menu (#1551)
Browse files Browse the repository at this point in the history
  • Loading branch information
kabaros authored Jul 4, 2024
1 parent 975764f commit 7a27d3d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
21 changes: 21 additions & 0 deletions components/menu/src/menu/__tests__/menu.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Input } from '@dhis2-ui/input'
import { render } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { mount } from 'enzyme'
Expand Down Expand Up @@ -180,4 +181,24 @@ describe('Menu Component', () => {
// non menu items do not receive focus
expect(plainListItem.parentElement).not.toHaveFocus()
})

it('does not hijack input change value if space entered [bug]', () => {
const onChange = jest.fn()
const { getByPlaceholderText } = render(
<Menu dataTest={menuDataTest} dense={false}>
<MenuItem value="myValue" label="Click menu item" />
<Input onChange={onChange} placeholder="test"></Input>
</Menu>
)

const inputField = getByPlaceholderText('test')
inputField.focus()
userEvent.keyboard('t')
userEvent.keyboard('e')
userEvent.keyboard(' ')
userEvent.keyboard('st')

expect(inputField.value).toBe('te st')
expect(onChange).toHaveBeenCalled()
})
})
4 changes: 2 additions & 2 deletions components/menu/src/menu/use-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export const useMenuNavigation = (children) => {
break
case 'Enter':
case ' ':
event.preventDefault()
if (event.target.nodeName === 'LI') {
event.target.children[0].click()
event.preventDefault()
event.target.children?.[0]?.click()
}
break
default:
Expand Down

0 comments on commit 7a27d3d

Please sign in to comment.