Skip to content

Commit 4ff1385

Browse files
aliceHendicottAlice Hendicott
and
Alice Hendicott
authored
fix(useCombobox): Ensure highlighted index is not selected on browser tab change (#1484)
Related issue - #1471 Co-authored-by: Alice Hendicott <[email protected]>
1 parent 9b3199a commit 4ff1385

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/hooks/useCombobox/__tests__/getInputProps.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,38 @@ describe('getInputProps', () => {
14601460
}),
14611461
)
14621462
})
1463+
1464+
test('the open menu will be closed and highlighted item will not be selected if the blur event related target is null', () => {
1465+
const stateReducer = jest.fn().mockImplementation(s => s)
1466+
const {container} = renderCombobox({
1467+
isOpen: true,
1468+
highlightedIndex: 0,
1469+
stateReducer,
1470+
})
1471+
const input = getInput()
1472+
document.body.appendChild(container)
1473+
1474+
fireEvent.blur(input, {relatedTarget: null})
1475+
1476+
expect(stateReducer).toHaveBeenCalledTimes(1)
1477+
expect(stateReducer).toHaveBeenCalledWith(
1478+
{
1479+
highlightedIndex: 0,
1480+
inputValue: '',
1481+
isOpen: true,
1482+
selectedItem: null,
1483+
},
1484+
expect.objectContaining({
1485+
type: stateChangeTypes.InputBlur,
1486+
changes: {
1487+
highlightedIndex: -1,
1488+
inputValue: '',
1489+
isOpen: false,
1490+
selectedItem: null,
1491+
},
1492+
}),
1493+
)
1494+
})
14631495
})
14641496

14651497
describe('on focus', () => {

src/hooks/useCombobox/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -416,15 +416,15 @@ function useCombobox(userProps = {}) {
416416
: event.target.value,
417417
})
418418
}
419-
const inputHandleBlur = () => {
419+
const inputHandleBlur = event => {
420420
/* istanbul ignore else */
421421
if (
422422
latestState.isOpen &&
423423
!mouseAndTouchTrackersRef.current.isMouseDown
424424
) {
425425
dispatch({
426426
type: stateChangeTypes.InputBlur,
427-
selectItem: true,
427+
selectItem: event.relatedTarget !== null,
428428
})
429429
}
430430
}

0 commit comments

Comments
 (0)