Skip to content

Commit 3473c11

Browse files
authored
Revert "Autocomplete: Only open menu on click (#4771)"
This reverts commit 92e05f7.
1 parent b85d505 commit 3473c11

File tree

4 files changed

+34
-41
lines changed

4 files changed

+34
-41
lines changed

.changeset/four-shoes-yell.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/react/src/Autocomplete/Autocomplete.docs.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
{
2424
"name": "openOnFocus",
2525
"type": "boolean",
26-
"defaultValue": "false",
27-
"description": "Whether the associated autocomplete menu should open on an input focus event",
28-
"deprecated": true
26+
"defaultValue": "true",
27+
"description": "Whether the associated autocomplete menu should open on an input focus event"
2928
}
3029
],
3130
"passthrough": {

packages/react/src/Autocomplete/AutocompleteInput.tsx

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ import useSafeTimeout from '../hooks/useSafeTimeout'
1010
type InternalAutocompleteInputProps = {
1111
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1212
as?: React.ComponentType<React.PropsWithChildren<any>>
13-
14-
/**
15-
* @deprecated `openOnFocus` is deprecated and will be removed in v38.
16-
* When `true`, autocomplete menu will show on focus or click.
17-
*/
13+
// When false, the autocomplete menu will not render either on mouse click or
14+
// keyboard focus.
1815
openOnFocus?: boolean
1916
}
2017

@@ -31,7 +28,7 @@ const AutocompleteInput = React.forwardRef(
3128
onKeyUp,
3229
onKeyPress,
3330
value,
34-
openOnFocus = false,
31+
openOnFocus = true,
3532
...props
3633
},
3734
forwardedRef,
@@ -55,12 +52,15 @@ const AutocompleteInput = React.forwardRef(
5552
const [highlightRemainingText, setHighlightRemainingText] = useState<boolean>(true)
5653
const {safeSetTimeout} = useSafeTimeout()
5754

58-
const handleInputFocus: FocusEventHandler<HTMLInputElement> = event => {
59-
onFocus?.(event)
60-
if (openOnFocus) {
61-
setShowMenu(true)
62-
}
63-
}
55+
const handleInputFocus: FocusEventHandler<HTMLInputElement> = useCallback(
56+
event => {
57+
if (openOnFocus) {
58+
onFocus?.(event)
59+
setShowMenu(true)
60+
}
61+
},
62+
[onFocus, setShowMenu, openOnFocus],
63+
)
6464

6565
const handleInputBlur: FocusEventHandler<HTMLInputElement> = useCallback(
6666
event => {
@@ -78,13 +78,16 @@ const AutocompleteInput = React.forwardRef(
7878
[onBlur, setShowMenu, inputRef, safeSetTimeout],
7979
)
8080

81-
const handleInputChange: ChangeEventHandler<HTMLInputElement> = event => {
82-
onChange && onChange(event)
83-
setInputValue(event.currentTarget.value)
84-
if (!showMenu) {
85-
setShowMenu(true)
86-
}
87-
}
81+
const handleInputChange: ChangeEventHandler<HTMLInputElement> = useCallback(
82+
event => {
83+
onChange && onChange(event)
84+
setInputValue(event.currentTarget.value)
85+
if (!showMenu) {
86+
setShowMenu(true)
87+
}
88+
},
89+
[onChange, setInputValue, setShowMenu, showMenu],
90+
)
8891

8992
const handleInputKeyDown: KeyboardEventHandler<HTMLInputElement> = useCallback(
9093
event => {
@@ -119,6 +122,7 @@ const AutocompleteInput = React.forwardRef(
119122
const onInputKeyPress: KeyboardEventHandler<HTMLInputElement> = useCallback(
120123
event => {
121124
onKeyPress && onKeyPress(event)
125+
122126
if (showMenu && event.key === 'Enter' && activeDescendantRef.current) {
123127
event.preventDefault()
124128
event.nativeEvent.stopImmediatePropagation()

packages/react/src/__tests__/Autocomplete.test.tsx

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {render as HTMLRender, fireEvent, screen, waitFor} from '@testing-library/react'
1+
import {render as HTMLRender, fireEvent, waitFor, screen} from '@testing-library/react'
22
import userEvent from '@testing-library/user-event'
33
import React from 'react'
44
import type {AutocompleteInputProps} from '../Autocomplete'
@@ -130,15 +130,14 @@ describe('Autocomplete', () => {
130130
expect(onKeyPressMock).toHaveBeenCalled()
131131
})
132132

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

139139
expect(inputNode.getAttribute('aria-expanded')).not.toBe('true')
140-
fireEvent.click(inputNode)
141-
fireEvent.keyDown(inputNode, {key: 'ArrowDown'})
140+
fireEvent.focus(inputNode)
142141
expect(inputNode.getAttribute('aria-expanded')).toBe('true')
143142
})
144143

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

151150
expect(inputNode.getAttribute('aria-expanded')).not.toBe('true')
152-
fireEvent.click(inputNode)
153-
fireEvent.keyDown(inputNode, {key: 'ArrowDown'})
154-
151+
fireEvent.focus(inputNode)
155152
expect(inputNode.getAttribute('aria-expanded')).toBe('true')
153+
// eslint-disable-next-line github/no-blur
154+
fireEvent.blur(inputNode)
156155

157-
await userEvent.tab()
158-
159-
expect(inputNode.getAttribute('aria-expanded')).not.toBe('true')
156+
// wait a tick for blur to finish
157+
await waitFor(() => expect(inputNode.getAttribute('aria-expanded')).not.toBe('true'))
160158
})
161159

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

314312
expect(onSelectedChangeMock).toHaveBeenCalledWith([mockItems[0]])
@@ -331,8 +329,6 @@ describe('Autocomplete', () => {
331329
if (inputNode) {
332330
expect(inputNode.getAttribute('aria-expanded')).not.toBe('true')
333331
await user.click(inputNode)
334-
335-
fireEvent.keyDown(inputNode, {key: 'ArrowDown'})
336332
expect(inputNode.getAttribute('aria-expanded')).toBe('true')
337333
await user.click(getByText(mockItems[1].text))
338334
expect(inputNode.getAttribute('aria-expanded')).toBe('true')
@@ -356,7 +352,6 @@ describe('Autocomplete', () => {
356352
if (inputNode) {
357353
expect(inputNode.getAttribute('aria-expanded')).not.toBe('true')
358354
await user.click(inputNode)
359-
fireEvent.keyDown(inputNode, {key: 'ArrowDown'})
360355
expect(inputNode.getAttribute('aria-expanded')).toBe('true')
361356
await user.click(getByText(mockItems[1].text))
362357
expect(inputNode.getAttribute('aria-expanded')).not.toBe('true')

0 commit comments

Comments
 (0)