Factorize js controller between Selectore and SuperSelector components

+ add a computeItemsHeight on afterReflex callback
This commit is contained in:
Jean-Baptiste Bellet
2022-03-22 16:07:48 +01:00
parent 4fa88b9c18
commit 224daf2591
2 changed files with 14 additions and 17 deletions

View File

@@ -1,10 +1,14 @@
import ApplicationController from "./application_controller";
export default class extends ApplicationController {
reflex = "SelectorComponent";
connect() {
super.connect();
window.addEventListener("click", this.closeOnClickOutside);
this.computeItemsHeight();
}
disconnect() {
super.disconnect();
window.removeEventListener("click", this.closeOnClickOutside);
@@ -12,7 +16,13 @@ export default class extends ApplicationController {
closeOnClickOutside = (event) => {
if (!this.element.contains(event.target)) {
this.stimulate("SelectorComponent#close", this.element);
this.stimulate(`${this.reflex}#close`, this.element);
}
};
computeItemsHeight = () => {
const items = this.element.querySelector(".selector-items");
const rect = items.getBoundingClientRect();
items.style.maxHeight = `calc(100vh - ${rect.height}px)`;
};
}

View File

@@ -1,18 +1,5 @@
import ApplicationController from "./application_controller";
import SelectorController from "./selector_controller";
export default class extends ApplicationController {
connect() {
super.connect();
window.addEventListener("click", this.handleClick);
}
disconnect() {
super.disconnect();
window.removeEventListener("click", this.handleClick);
}
handleClick = (event) => {
if (!this.element.contains(event.target)) {
this.stimulate("SuperSelectorComponent#close", this.element);
}
};
export default class extends SelectorController {
reflex = "SuperSelectorComponent";
}