Skip to content

Commit 7afc4f7

Browse files
committed
select option by pressing enter
1 parent 94f8c9b commit 7afc4f7

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

src/components/DropdownSelect.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ class DropdownSelect {
66
this.$dropdownSelect = null
77
this.$input = null
88
this.$optionItems = []
9-
this.keyword = null
109

1110
this._buildDropdownSelect()
1211
this._buildSearchInput()
@@ -22,8 +21,12 @@ class DropdownSelect {
2221
'shadow'
2322
)
2423

25-
this.$container.$root.$store.on('keywordChange', (keyword) => {
26-
this.keyword = keyword
24+
this.$container.$root.$store.on('keywordChange', () => {
25+
this._buildOptionItems()
26+
this._rerenderOptionsItems()
27+
})
28+
29+
this.$container.$root.$store.on('selectedItemsChange', () => {
2730
this._buildOptionItems()
2831
this._rerenderOptionsItems()
2932
})
@@ -135,13 +138,7 @@ class DropdownSelect {
135138
}
136139

137140
get filteredItems () {
138-
return this.$container.$root.$store.items.filter(item => {
139-
if (this.keyword) {
140-
return item.label.toLowerCase().includes(this.keyword.toLowerCase())
141-
}
142-
143-
return true
144-
})
141+
return this.$container.$root.$store.filteredItems
145142
}
146143
}
147144

src/components/SearchInput.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ class SearchInput {
3535
}
3636

3737
_registerEventListeners () {
38-
// prevent default action if arrow up and down is pressed
38+
// prevent default action for some key when pressed
3939
// navigating between options
4040
this._input.addEventListener('keydown', (e) => {
41-
if (e.code === 'ArrowUp' || e.code === 'ArrowDown') {
41+
if (e.code === 'ArrowUp' || e.code === 'ArrowDown' || e.code === 'Enter') {
4242
e.preventDefault()
4343

4444
if (e.code === 'ArrowUp') {
@@ -62,6 +62,32 @@ class SearchInput {
6262
this._input.addEventListener('keyup', (e) => {
6363
if (e.code !== 'ArrowUp' && e.code !== 'ArrowDown') {
6464
this.$root.$store.keyword = this._input.value
65+
}
66+
67+
// toggle select option
68+
if (e.code === 'Enter') {
69+
if (this.$root.$store.hoveredItemIndex !== null) {
70+
let selectedItem = this.$root.$store.filteredItems[this.$root.$store.hoveredItemIndex]
71+
let selectedItems = this.$root.$store.selectedItems
72+
let index = selectedItems.findIndex(_selectedItem => _selectedItem.value === selectedItem.value)
73+
74+
if (index > -1) {
75+
selectedItems.splice(index, 1)
76+
} else {
77+
if (this.$root.$store.isMultiple) {
78+
selectedItems.push(selectedItem)
79+
} else {
80+
selectedItems = [selectedItem]
81+
82+
// close the dropdown because it is not `<select multiple>`
83+
this.$root.$store.isOpened = false
84+
}
85+
}
86+
87+
this.$root.$store.selectedItems = selectedItems
88+
89+
console.log(selectedItem, { index })
90+
}
6591
}
6692
})
6793
}

src/store/Store.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ class Store extends EventEmitter {
2828
this.emit('itemsChange', items)
2929
}
3030

31+
get filteredItems () {
32+
return this._items.filter(item => {
33+
if (this._keyword) {
34+
return item.label.toLowerCase().includes(this._keyword.toLowerCase())
35+
}
36+
37+
return true
38+
})
39+
}
40+
3141
get selectedItems () {
3242
return this._selectedItems || []
3343
}
@@ -47,9 +57,11 @@ class Store extends EventEmitter {
4757
}
4858

4959
set keyword (keyword) {
50-
this._keyword = keyword
51-
this._hoveredItemIndex = null
52-
this.emit('keywordChange', keyword)
60+
if (keyword && this._keyword !== keyword) {
61+
this._keyword = keyword
62+
this._hoveredItemIndex = null
63+
this.emit('keywordChange', keyword)
64+
}
5365
}
5466

5567
/**

0 commit comments

Comments
 (0)