Skip to content

Commit

Permalink
fix(useSelect): preventDefault on item props onMouseDown
Browse files Browse the repository at this point in the history
  • Loading branch information
silviuaavram committed Sep 25, 2023
1 parent 7d8c32d commit b24f2e6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/hooks/useSelect/__tests__/getItemProps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,44 @@ describe('getItemProps', () => {
expect(result.current.highlightedIndex).toBe(1)
})

test('event handler onMouseDown is called along with downshift handler', () => {
const userOnMouseDown = jest.fn()
const preventDefault = jest.fn()
const {result} = renderUseSelect({initialIsOpen: true})

act(() => {
const {onMouseDown} = result.current.getItemProps({
index: 1,
onMouseDown: userOnMouseDown,
})

onMouseDown({preventDefault})
})

expect(userOnMouseDown).toHaveBeenCalledTimes(1)
expect(preventDefault).toHaveBeenCalledTimes(1)
})

test("event handler onMouseDown is called without downshift handler if 'preventDownshiftDefault' is passed in user event", () => {
const userOnMouseDown = jest.fn(event => {
event.preventDownshiftDefault = true
})
const preventDefault = jest.fn()
const {result} = renderUseSelect({initialIsOpen: true})

act(() => {
const {onMouseDown} = result.current.getItemProps({
index: 0,
onMouseDown: userOnMouseDown,
})

onMouseDown({preventDefault})
})

expect(userOnMouseDown).toHaveBeenCalledTimes(1)
expect(preventDefault).not.toHaveBeenCalled()
})

test("event handler onClick is called without downshift handler if 'preventDownshiftDefault' is passed in user event", () => {
const userOnClick = jest.fn(event => {
event.preventDownshiftDefault = true
Expand Down
6 changes: 6 additions & 0 deletions src/hooks/useSelect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ function useSelect(userProps = {}) {
index: indexProp,
onMouseMove,
onClick,
onMouseDown,
onPress,
refKey = 'ref',
disabled: disabledProp,
Expand Down Expand Up @@ -489,6 +490,7 @@ function useSelect(userProps = {}) {
index,
})
}
const itemHandleMouseDown = e => e.preventDefault()

const itemProps = {
[refKey]: handleRefs(ref, itemNode => {
Expand Down Expand Up @@ -516,6 +518,10 @@ function useSelect(userProps = {}) {
onMouseMove,
itemHandleMouseMove,
)
itemProps.onMouseDown = callAllEventHandlers(
onMouseDown,
itemHandleMouseDown,
)

return itemProps
},
Expand Down
2 changes: 1 addition & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ export interface UseSelectGetItemPropsOptions<Item>
GetPropsWithRefKey {}

export interface UseSelectGetItemPropsReturnValue
extends Omit<GetItemPropsReturnValue, 'onMouseDown'> {
extends GetItemPropsReturnValue {
'aria-disabled': boolean
ref?: React.RefObject<any>
}
Expand Down

0 comments on commit b24f2e6

Please sign in to comment.