Skip to content
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

Autocomplete: Only open menu on click #4771

Merged
merged 10 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/four-shoes-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

Set `openOnFocus` default to `false`, making the menu closed initially rather than opening on focus of input
2 changes: 1 addition & 1 deletion packages/react/src/Autocomplete/Autocomplete.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
{
"name": "openOnFocus",
"type": "boolean",
"defaultValue": "true",
"defaultValue": "false",
"description": "Whether the associated autocomplete menu should open on an input focus event"
}
],
Expand Down
35 changes: 14 additions & 21 deletions packages/react/src/Autocomplete/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const AutocompleteInput = React.forwardRef(
onKeyUp,
onKeyPress,
value,
openOnFocus = true,
openOnFocus = false,
...props
},
forwardedRef,
Expand All @@ -52,15 +52,12 @@ const AutocompleteInput = React.forwardRef(
const [highlightRemainingText, setHighlightRemainingText] = useState<boolean>(true)
const {safeSetTimeout} = useSafeTimeout()

const handleInputFocus: FocusEventHandler<HTMLInputElement> = useCallback(
event => {
if (openOnFocus) {
onFocus?.(event)
setShowMenu(true)
}
},
[onFocus, setShowMenu, openOnFocus],
)
const handleInputFocus: FocusEventHandler<HTMLInputElement> = event => {
onFocus?.(event)
if (openOnFocus) {
setShowMenu(true)
}
}

const handleInputBlur: FocusEventHandler<HTMLInputElement> = useCallback(
event => {
Expand All @@ -78,16 +75,13 @@ const AutocompleteInput = React.forwardRef(
[onBlur, setShowMenu, inputRef, safeSetTimeout],
)

const handleInputChange: ChangeEventHandler<HTMLInputElement> = useCallback(
event => {
onChange && onChange(event)
setInputValue(event.currentTarget.value)
if (!showMenu) {
setShowMenu(true)
}
},
[onChange, setInputValue, setShowMenu, showMenu],
)
const handleInputChange: ChangeEventHandler<HTMLInputElement> = event => {
onChange && onChange(event)
setInputValue(event.currentTarget.value)
if (!showMenu) {
setShowMenu(true)
}
}

const handleInputKeyDown: KeyboardEventHandler<HTMLInputElement> = useCallback(
event => {
Expand Down Expand Up @@ -122,7 +116,6 @@ const AutocompleteInput = React.forwardRef(
const onInputKeyPress: KeyboardEventHandler<HTMLInputElement> = useCallback(
event => {
onKeyPress && onKeyPress(event)

if (showMenu && event.key === 'Enter' && activeDescendantRef.current) {
event.preventDefault()
event.nativeEvent.stopImmediatePropagation()
Expand Down
23 changes: 14 additions & 9 deletions packages/react/src/__tests__/Autocomplete.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {render as HTMLRender, fireEvent, waitFor, screen} from '@testing-library/react'
import {render as HTMLRender, fireEvent, screen, waitFor} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import React from 'react'
import type {AutocompleteInputProps} from '../Autocomplete'
Expand Down Expand Up @@ -130,14 +130,15 @@ describe('Autocomplete', () => {
expect(onKeyPressMock).toHaveBeenCalled()
})

it('opens the menu when the input is focused', () => {
it('opens the menu when the input is focused and arrow key is pressed', () => {
const {getByLabelText} = HTMLRender(
<LabelledAutocomplete menuProps={{items: [], selectedItemIds: [], ['aria-labelledby']: 'autocompleteLabel'}} />,
)
const inputNode = getByLabelText(AUTOCOMPLETE_LABEL)

expect(inputNode.getAttribute('aria-expanded')).not.toBe('true')
fireEvent.focus(inputNode)
fireEvent.click(inputNode)
fireEvent.keyDown(inputNode, {key: 'ArrowDown'})
expect(inputNode.getAttribute('aria-expanded')).toBe('true')
})

Expand All @@ -148,13 +149,14 @@ describe('Autocomplete', () => {
const inputNode = getByLabelText(AUTOCOMPLETE_LABEL)

expect(inputNode.getAttribute('aria-expanded')).not.toBe('true')
fireEvent.focus(inputNode)
fireEvent.click(inputNode)
fireEvent.keyDown(inputNode, {key: 'ArrowDown'})

expect(inputNode.getAttribute('aria-expanded')).toBe('true')
// eslint-disable-next-line github/no-blur
fireEvent.blur(inputNode)

// wait a tick for blur to finish
await waitFor(() => expect(inputNode.getAttribute('aria-expanded')).not.toBe('true'))
await userEvent.tab()

expect(inputNode.getAttribute('aria-expanded')).not.toBe('true')
})

it('sets the input value to the suggested item text and highlights the untyped part of the word', async () => {
Expand Down Expand Up @@ -306,7 +308,7 @@ describe('Autocomplete', () => {
expect(onSelectedChangeMock).not.toHaveBeenCalled()
if (inputNode) {
fireEvent.focus(inputNode)
await user.type(inputNode, '{enter}')
await user.type(inputNode, '{arrowdown}{enter}')
}

expect(onSelectedChangeMock).toHaveBeenCalledWith([mockItems[0]])
Expand All @@ -329,6 +331,8 @@ describe('Autocomplete', () => {
if (inputNode) {
expect(inputNode.getAttribute('aria-expanded')).not.toBe('true')
await user.click(inputNode)

fireEvent.keyDown(inputNode, {key: 'ArrowDown'})
expect(inputNode.getAttribute('aria-expanded')).toBe('true')
await user.click(getByText(mockItems[1].text))
expect(inputNode.getAttribute('aria-expanded')).toBe('true')
Expand All @@ -352,6 +356,7 @@ describe('Autocomplete', () => {
if (inputNode) {
expect(inputNode.getAttribute('aria-expanded')).not.toBe('true')
await user.click(inputNode)
fireEvent.keyDown(inputNode, {key: 'ArrowDown'})
expect(inputNode.getAttribute('aria-expanded')).toBe('true')
await user.click(getByText(mockItems[1].text))
expect(inputNode.getAttribute('aria-expanded')).not.toBe('true')
Expand Down
Loading