mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
Factorize js controller between Selectore and SuperSelector components
+ add a computeItemsHeight on afterReflex callback
This commit is contained in:
@@ -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)`;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user