Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
63 changes: 63 additions & 0 deletions src/__tests__/downshift.lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jest.mock('../utils', () => {

afterEach(() => {
utils.scrollIntoView.mockReset()
// Ensure any pending timers are cleared if not already handled by tests
jest.clearAllTimers()
})

test('do not set state after unmount', () => {
Expand Down Expand Up @@ -256,6 +258,67 @@ test('controlled highlighted index change scrolls the item into view', () => {
)
})

test('input blur when menu is open does not reset state', () => {
const handleStateChange = jest.fn()
const items = ['apple', 'banana']

render(
<Downshift onStateChange={handleStateChange} itemToString={i => String(i)}>
{({
getInputProps,
getToggleButtonProps,
getMenuProps,
getItemProps,
getRootProps,
}) => (
<div {...getRootProps()}>
<input {...getInputProps({'data-testid': 'downshift-input'})} />
<button {...getToggleButtonProps({'data-testid': 'toggle-button'})} />
{
<ul {...getMenuProps({'data-testid': 'downshift-menu'})}>
{items.map((item, index) => (
<li
key={item}
{...getItemProps({
index,
item,
})}
>
{item}
</li>
))}
</ul>
}
</div>
)}
</Downshift>,
)

const input = screen.getByTestId('downshift-input')
const toggleButton = screen.getByTestId('toggle-button')

fireEvent.click(toggleButton)
jest.runAllTimers()

expect(screen.getByTestId('downshift-menu')).toBeInTheDocument()
expect(handleStateChange).toHaveBeenCalledWith(
expect.objectContaining({
type: Downshift.stateChangeTypes.clickButton,
isOpen: true,
}),
expect.anything(),
)

act(() => {
input.focus()
})
fireEvent.blur(input)

jest.runAllTimers()

expect(handleStateChange).toHaveBeenCalledTimes(1)
})

function mouseDownAndUp(node) {
fireEvent.mouseDown(node)
fireEvent.mouseUp(node)
Expand Down
8 changes: 7 additions & 1 deletion src/downshift.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,13 @@ class Downshift extends Component {
this._rootNode &&
this._rootNode.contains(activeElement)

if (!downshiftButtonIsActive) {
// We check for the containment of the newly activeElement in the _menuNode here due to iOS emitting a blur event right away when the menu is opened (maybe due to not showing the keyboard?)
const activeElementWithinMenu =
this._menuNode &&
activeElement &&
this._menuNode.contains(activeElement)

if (!downshiftButtonIsActive && !activeElementWithinMenu) {
this.reset({type: stateChangeTypes.blurInput})
}
})
Expand Down