Files
openfoodnetwork/app/webpacker/controllers/producers_select_controller.js
cyrillefr 1a4a33227e Producers selects are to be populated on focus
- added a new Stimulus controller to display one line selects
 - that are to be populated when getting focus to avoid repeating
 - 10 times same option lists
 - on focus, select is populated by the data stored in template.
 - data that used to be source for each select (and so repeated) is
 - now stored once in a template
2024-06-03 22:45:01 +02:00

38 lines
996 B
JavaScript

import TomSelect from "./tom_select_controller";
const minimumOptionsForSearchField = 11;
export default class extends TomSelect {
static values = { options: Object, placeholder: String };
connect(options = {}) {
const template = this.template();
let additional_options = {};
const lines_of_option = template.content.children.length;
if (lines_of_option < minimumOptionsForSearchField) {
additional_options.plugins = [];
}
super.connect(additional_options);
this.control.on('focus', this.fetchProducers.bind(this));
}
disconnect() {
if (this.control) this.control.destroy();
}
fetchProducers() {
if ((Object.keys(this.control.options).length) > 1) return false;
const template = this.template();
Array.from(template.content.children).forEach((option) => {
this.control.addOption({value: option.value, text: option.text});
});
}
template() {
return document.querySelector("#producer_options");
}
}