Skip to content

Commit

Permalink
fix(useCombobox): Ensure highlighted index is not selected on browser…
Browse files Browse the repository at this point in the history
… tab change (#1484)

Related issue - #1471

Co-authored-by: Alice Hendicott <alice.hendicott@rea-group.com>
  • Loading branch information
aliceHendicott and Alice Hendicott authored Mar 14, 2023
1 parent 9b3199a commit 4ff1385
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/hooks/useCombobox/__tests__/getInputProps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,38 @@ describe('getInputProps', () => {
}),
)
})

test('the open menu will be closed and highlighted item will not be selected if the blur event related target is null', () => {
const stateReducer = jest.fn().mockImplementation(s => s)
const {container} = renderCombobox({
isOpen: true,
highlightedIndex: 0,
stateReducer,
})
const input = getInput()
document.body.appendChild(container)

fireEvent.blur(input, {relatedTarget: null})

expect(stateReducer).toHaveBeenCalledTimes(1)
expect(stateReducer).toHaveBeenCalledWith(
{
highlightedIndex: 0,
inputValue: '',
isOpen: true,
selectedItem: null,
},
expect.objectContaining({
type: stateChangeTypes.InputBlur,
changes: {
highlightedIndex: -1,
inputValue: '',
isOpen: false,
selectedItem: null,
},
}),
)
})
})

describe('on focus', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useCombobox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,15 @@ function useCombobox(userProps = {}) {
: event.target.value,
})
}
const inputHandleBlur = () => {
const inputHandleBlur = event => {
/* istanbul ignore else */
if (
latestState.isOpen &&
!mouseAndTouchTrackersRef.current.isMouseDown
) {
dispatch({
type: stateChangeTypes.InputBlur,
selectItem: true,
selectItem: event.relatedTarget !== null,
})
}
}
Expand Down

0 comments on commit 4ff1385

Please # to comment.