Skip to content

Commit a4b8757

Browse files
honzajerabeklayershifter
authored andcommitted
fix(Dropdown): closing the search menu on spacebar press (#3766)
* Fix closing the search menu on spacebar press * Update Dropdown.js
1 parent 81d3cd1 commit a4b8757

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/modules/Dropdown/Dropdown.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,12 @@ export default class Dropdown extends Component {
581581
debug('selectItemOnEnter()', keyboardKey.getKey(e))
582582
const { search } = this.props
583583

584-
if (
585-
keyboardKey.getCode(e) !== keyboardKey.Enter &&
586-
keyboardKey.getCode(e) !== keyboardKey.Spacebar
587-
)
588-
return
584+
const shouldSelect =
585+
keyboardKey.getCode(e) === keyboardKey.Enter ||
586+
// https://github.com/Semantic-Org/Semantic-UI-React/pull/3766
587+
(!search && keyboardKey.getCode(e) === keyboardKey.Spacebar)
588+
589+
if (!shouldSelect) return
589590
e.preventDefault()
590591

591592
const optionSize = _.size(this.getMenuOptions())

test/specs/modules/Dropdown/Dropdown-test.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ describe('Dropdown', () => {
10781078
.at(1)
10791079
.should.have.props({ selected: true, active: true })
10801080
})
1081-
it('closes the menu', () => {
1081+
it('closes the menu on ENTER key', () => {
10821082
wrapperMount(<Dropdown options={options} selection />).simulate('click')
10831083

10841084
dropdownMenuIsOpen()
@@ -1087,6 +1087,33 @@ describe('Dropdown', () => {
10871087
domEvent.keyDown(document, { key: 'Enter' })
10881088
dropdownMenuIsClosed()
10891089
})
1090+
it('closes the menu on SPACE key', () => {
1091+
wrapperMount(<Dropdown options={options} selection />).simulate('click')
1092+
1093+
dropdownMenuIsOpen()
1094+
1095+
// choose an item closes
1096+
domEvent.keyDown(document, { key: 'Spacebar' })
1097+
dropdownMenuIsClosed()
1098+
})
1099+
it('closes the Search menu on ENTER key', () => {
1100+
wrapperMount(<Dropdown options={options} selection search />).simulate('click')
1101+
1102+
dropdownMenuIsOpen()
1103+
1104+
// choose an item closes
1105+
domEvent.keyDown(document, { key: 'Enter' })
1106+
dropdownMenuIsClosed()
1107+
})
1108+
it('does not close the Search menu on SPACE key', () => {
1109+
wrapperMount(<Dropdown options={options} selection search />).simulate('click')
1110+
1111+
dropdownMenuIsOpen()
1112+
1113+
// choose an item closes
1114+
domEvent.keyDown(document, { key: 'Spacebar' })
1115+
dropdownMenuIsOpen()
1116+
})
10901117
it('keeps value of the searchQuery when selection is changed', () => {
10911118
wrapperMount(<Dropdown options={options} selection search />)
10921119

0 commit comments

Comments
 (0)