Skip to content

Commit 05ffa9c

Browse files
authored
fix(useSelect): preventDefault on item props onMouseDown (#1547)
* fix(useSelect): preventDefault on item props onMouseDown * add another test
1 parent b24f2e6 commit 05ffa9c

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/hooks/useCombobox/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ function useCombobox(userProps = {}) {
334334
index,
335335
})
336336
}
337-
const itemHandleMouseDown = e => e.preventDefault()
337+
const itemHandleMouseDown = e => e.preventDefault() // keep focus on the input after item click select.
338338

339339
return {
340340
[refKey]: handleRefs(ref, itemNode => {

src/hooks/useSelect/__tests__/getToggleButtonProps.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,24 @@ describe('getToggleButtonProps', () => {
17091709
})
17101710

17111711
describe('blur', () => {
1712+
test('with the menu closed does nothing', async () => {
1713+
const initialHighlightedIndex = 2
1714+
renderSelect({initialHighlightedIndex}, ui => {
1715+
return (
1716+
<>
1717+
{ui}
1718+
<div tabIndex={0}>Second element</div>
1719+
</>
1720+
)
1721+
})
1722+
1723+
await tab() // focus the button
1724+
screen.getByText(/Second element/).focus()
1725+
1726+
expect(getItems()).toHaveLength(0)
1727+
expect(getToggleButton()).not.toHaveTextContent()
1728+
})
1729+
17121730
test('by focusing another element should behave as a normal blur', async () => {
17131731
const initialHighlightedIndex = 2
17141732
renderSelect({initialIsOpen: true, initialHighlightedIndex}, ui => {

src/hooks/useSelect/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ function useSelect(userProps = {}) {
490490
index,
491491
})
492492
}
493-
const itemHandleMouseDown = e => e.preventDefault()
493+
const itemHandleMouseDown = e => e.preventDefault() // keep focus on the toggle after item click select.
494494

495495
const itemProps = {
496496
[refKey]: handleRefs(ref, itemNode => {

0 commit comments

Comments
 (0)