Fix filters resetting user input on redraw
This commit is contained in:
1 parent
595f0eddf1
commit
53bb4d3552
10 files changed
+129
-96
No files matched your search
@@ -51,11 +51,8 @@ export default class Autocomplete {
|
||||
|
||||
el.addEventListener("focus", () => {
|
||||
this.element = el;
|
||||
const domRect = el.getBoundingClientRect();
|
||||
this.container.style.left = `${domRect.left + window.pageXOffset}px`;
|
||||
this.container.style.width = `${domRect.width}px`;
|
||||
this.container.style.top = `${domRect.bottom + window.pageYOffset}px`;
|
||||
this.update();
|
||||
this.reposition();
|
||||
});
|
||||
|
||||
el.addEventListener("blur", () => {
|
||||
@@ -71,7 +68,8 @@ export default class Autocomplete {
|
||||
} else if (e.key === "Enter") {
|
||||
if (this.default != null) {
|
||||
el.value = this.default;
|
||||
e.preventDefault();
|
||||
e.stopImmediatePropagation();
|
||||
el.dispatchEvent(new InputEvent("input"));
|
||||
this.update();
|
||||
}
|
||||
} else if (e.key === "ArrowDown") {
|
||||
@@ -93,6 +91,19 @@ export default class Autocomplete {
|
||||
});
|
||||
}
|
||||
|
||||
public reposition(): void {
|
||||
if (!this.element) return;
|
||||
const domRect = this.element.getBoundingClientRect();
|
||||
if (!domRect.width) {
|
||||
// Element has been removed
|
||||
if (this.visible) this.hide();
|
||||
return;
|
||||
}
|
||||
this.container.style.left = `${domRect.left + window.pageXOffset}px`;
|
||||
this.container.style.width = `${domRect.width}px`;
|
||||
this.container.style.top = `${domRect.bottom + window.pageYOffset}px`;
|
||||
}
|
||||
|
||||
private hide(): void {
|
||||
this.container.style.opacity = "0";
|
||||
this.visible = false;
|
||||
@@ -159,6 +170,7 @@ export default class Autocomplete {
|
||||
e.addEventListener("mousedown", (ev) => {
|
||||
ev.preventDefault();
|
||||
el.value = suggestion.value;
|
||||
el.dispatchEvent(new InputEvent("input"));
|
||||
if (this.element === el) this.update();
|
||||
});
|
||||
this.container.appendChild(e);
|
||||
|
||||
Reference in new issue
Block a user