Skip to content

Commit

Permalink
fix(useCombobox): prevent selection on mouse/touch blur (#1109)
Browse files Browse the repository at this point in the history
  • Loading branch information
silviuaavram authored Jul 4, 2020
1 parent b20f351 commit 8d60a76
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
40 changes: 34 additions & 6 deletions src/hooks/useCombobox/__tests__/getInputProps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,13 +845,14 @@ describe('getInputProps', () => {
await changeInputValue(inputValue)
blurInput()

expect(input.value).toBe(inputValue)
expect(input).toHaveValue(inputValue)
})

test('by mouse is not triggered if target is within downshift', () => {
const stateReducer = jest.fn().mockImplementation(s => s)
const {input, container} = renderCombobox({
isOpen: true,
highlightedIndex: 0,
stateReducer,
})
document.body.appendChild(container)
Expand All @@ -866,15 +867,29 @@ describe('getInputProps', () => {

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

test('by touch is not triggered if target is within downshift', () => {
const stateReducer = jest.fn().mockImplementation(s => s)
const {container, input} = renderCombobox({
isOpen: true,
highlightedIndex: 0,
stateReducer,
})
document.body.appendChild(container)
Expand All @@ -890,8 +905,21 @@ describe('getInputProps', () => {

expect(stateReducer).toHaveBeenCalledTimes(1)
expect(stateReducer).toHaveBeenCalledWith(
expect.objectContaining({}),
expect.objectContaining({type: stateChangeTypes.InputBlur}),
{
highlightedIndex: 0,
inputValue: '',
isOpen: true,
selectedItem: null,
},
expect.objectContaining({
type: stateChangeTypes.InputBlur,
changes: {
highlightedIndex: -1,
inputValue: '',
isOpen: false,
selectedItem: null,
},
}),
)
})
})
Expand Down Expand Up @@ -919,7 +947,7 @@ describe('getInputProps', () => {
})
getMenuProps({}, {suppressRefError: true})
getComboboxProps({}, {suppressRefError: true})

if (firstRender) {
firstRender = false
getInputProps({}, {suppressRefError: true})
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useCombobox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ function useCombobox(userProps = {}) {
() => {
dispatch({
type: stateChangeTypes.InputBlur,
selectItem: false,
})
},
)
Expand Down Expand Up @@ -425,6 +426,7 @@ function useCombobox(userProps = {}) {
if (!mouseAndTouchTrackersRef.current.isMouseDown) {
dispatch({
type: stateChangeTypes.InputBlur,
selectItem: true,
})
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/hooks/useCombobox/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ export default function downshiftUseComboboxReducer(state, action) {
case stateChangeTypes.InputBlur:
changes = {
isOpen: false,
...(state.highlightedIndex >= 0 && {
selectedItem: props.items[state.highlightedIndex],
inputValue: props.itemToString(props.items[state.highlightedIndex]),
highlightedIndex: -1,
}),
highlightedIndex: -1,
...(state.highlightedIndex >= 0 &&
action.selectItem && {
selectedItem: props.items[state.highlightedIndex],
inputValue: props.itemToString(props.items[state.highlightedIndex]),
}),
}
break
case stateChangeTypes.InputChange:
Expand Down Expand Up @@ -157,7 +158,7 @@ export default function downshiftUseComboboxReducer(state, action) {
case stateChangeTypes.FunctionSelectItem:
changes = {
selectedItem: action.selectedItem,
inputValue: props.itemToString(action.selectedItem)
inputValue: props.itemToString(action.selectedItem),
}
break
case stateChangeTypes.ControlledPropUpdatedSelectedItem:
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ export interface UseComboboxDispatchAction<Item> {
index?: number
highlightedIndex?: number
selectedItem?: Item | null
selectItem?: boolean
}

export interface UseComboboxStateChange<Item>
Expand Down

0 comments on commit 8d60a76

Please sign in to comment.