Skip to content

Commit bd0b81c

Browse files
authored
Improve searchbox event handling (#695)
1 parent 72b0285 commit bd0b81c

2 files changed

Lines changed: 38 additions & 48 deletions

File tree

js/menu.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ function Search(menu) {
99

1010
document.addEventListener('keydown', this.documentKeydown.bind(this));
1111

12-
this.$searchBox.addEventListener(
13-
'keydown',
14-
debounce(this.searchBoxKeydown.bind(this), { stopPropagation: true }),
15-
);
16-
this.$searchBox.addEventListener(
17-
'keyup',
18-
debounce(this.searchBoxKeyup.bind(this), { stopPropagation: true }),
19-
);
12+
let requestHandleSearchBoxEvent = debounce(this.handleSearchBoxEvent.bind(this));
13+
14+
this.$searchBox.addEventListener('keydown', e => {
15+
e.stopPropagation();
16+
if (e.key === 'Enter') e.preventDefault();
17+
requestHandleSearchBoxEvent(e);
18+
});
19+
this.$searchBox.addEventListener('input', e => {
20+
e.stopPropagation();
21+
requestHandleSearchBoxEvent(e);
22+
});
2023

2124
// Perform an initial search if the box is not empty.
2225
if (this.$searchBox.value) {
@@ -54,23 +57,15 @@ Search.prototype.documentKeydown = function (e) {
5457
}
5558
};
5659

57-
Search.prototype.searchBoxKeydown = function (e) {
58-
e.stopPropagation();
59-
e.preventDefault();
60-
if (e.keyCode === 191 && e.target.value.length === 0) {
61-
e.preventDefault();
62-
} else if (e.keyCode === 13) {
63-
e.preventDefault();
64-
this.selectResult();
65-
}
66-
};
67-
68-
Search.prototype.searchBoxKeyup = function (e) {
69-
if (e.keyCode === 13 || e.keyCode === 9) {
70-
return;
60+
Search.prototype.handleSearchBoxEvent = function (e) {
61+
switch (e.type) {
62+
case 'keydown':
63+
if (e.key === 'Enter') this.selectResult();
64+
break;
65+
case 'input':
66+
this.search(e.target.value);
67+
break;
7168
}
72-
73-
this.search(e.target.value);
7469
};
7570

7671
Search.prototype.triggerSearch = function () {

test/baselines/generated-reference/assets-inline.html

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,17 @@
9393
9494
document.addEventListener('keydown', this.documentKeydown.bind(this));
9595
96-
this.$searchBox.addEventListener(
97-
'keydown',
98-
debounce(this.searchBoxKeydown.bind(this), { stopPropagation: true }),
99-
);
100-
this.$searchBox.addEventListener(
101-
'keyup',
102-
debounce(this.searchBoxKeyup.bind(this), { stopPropagation: true }),
103-
);
96+
let requestHandleSearchBoxEvent = debounce(this.handleSearchBoxEvent.bind(this));
97+
98+
this.$searchBox.addEventListener('keydown', e => {
99+
e.stopPropagation();
100+
if (e.key === 'Enter') e.preventDefault();
101+
requestHandleSearchBoxEvent(e);
102+
});
103+
this.$searchBox.addEventListener('input', e => {
104+
e.stopPropagation();
105+
requestHandleSearchBoxEvent(e);
106+
});
104107
105108
// Perform an initial search if the box is not empty.
106109
if (this.$searchBox.value) {
@@ -138,23 +141,15 @@
138141
}
139142
};
140143
141-
Search.prototype.searchBoxKeydown = function (e) {
142-
e.stopPropagation();
143-
e.preventDefault();
144-
if (e.keyCode === 191 && e.target.value.length === 0) {
145-
e.preventDefault();
146-
} else if (e.keyCode === 13) {
147-
e.preventDefault();
148-
this.selectResult();
149-
}
150-
};
151-
152-
Search.prototype.searchBoxKeyup = function (e) {
153-
if (e.keyCode === 13 || e.keyCode === 9) {
154-
return;
144+
Search.prototype.handleSearchBoxEvent = function (e) {
145+
switch (e.type) {
146+
case 'keydown':
147+
if (e.key === 'Enter') this.selectResult();
148+
break;
149+
case 'input':
150+
this.search(e.target.value);
151+
break;
155152
}
156-
157-
this.search(e.target.value);
158153
};
159154
160155
Search.prototype.triggerSearch = function () {

0 commit comments

Comments
 (0)