Added ability to limit the results if wanted. Added a on enter callback and prevented enter from adding new lines.

This commit is contained in:
MattMo 2023-10-25 13:42:37 -07:00
parent 18901d2115
commit 7a950f307d

View File

@ -23,7 +23,9 @@ class SearchSelect extends IgniteElement {
searchDelay: 200, searchDelay: 200,
searchMaxHeight: "15em", searchMaxHeight: "15em",
searching: false, searching: false,
documentListener: null documentListener: null,
maxResults: -1,
onEnter: null
}; };
} }
@ -58,6 +60,24 @@ class SearchSelect extends IgniteElement {
.ref(this.inputElement) .ref(this.inputElement)
.onFocus(() => this.onFocus()) .onFocus(() => this.onFocus())
.on("keydown", (e) => { .on("keydown", (e) => {
//If the enter key is pressed, prevent it from doing anything.
if (e.key == "Enter") {
e.preventDefault();
e.stopPropagation();
if (this.onEnter) {
this.onEnter(e.target.textContent);
}
this.searching = false;
this.inputElement.blur();
this.inputElement.textContent = null;
e.preventDefault();
e.stopPropagation();
return;
}
//If the escape key is pressed stop searching until something else happens. //If the escape key is pressed stop searching until something else happens.
if (e.key == "Escape") { if (e.key == "Escape") {
this.searching = false; this.searching = false;
@ -132,6 +152,7 @@ class SearchSelect extends IgniteElement {
this.searching = false; this.searching = false;
//Dispatch native change event so this acts like a native control
this.dispatchEvent(new Event("change")); this.dispatchEvent(new Event("change"));
}) })
) )
@ -216,8 +237,13 @@ class SearchSelect extends IgniteElement {
}); });
} }
//Limit the results if needed
if (this.maxResults != -1) {
this.results = results.slice(0, this.maxResults);
} else {
this.results = results; this.results = results;
} }
}
} }
class Template extends IgniteTemplate { class Template extends IgniteTemplate {