Files
openfoodnetwork/app/webpacker/controllers/producers_select_controller.js
cyrillefr dab49f3e89 Requested changes on Producers selects are to be populated on focus
- comments
- id of template as a parameter
- little clean-up
2024-06-03 22:45:01 +02:00

40 lines
1.1 KiB
JavaScript

import TomSelect from "./tom_select_controller";
const minimumOptionsForSearchField = 11;
export default class extends TomSelect {
static values = { templateId: String, placeholder: String };
connect() {
const template = this.template(this.templateIdValue );
let additional_options = {};
const lines_of_option = template.content.children.length;
// Disable search input ("dropdown_input" plugin) from tom-select
if (lines_of_option < minimumOptionsForSearchField) {
additional_options.plugins = [];
}
super.connect(additional_options);
// focus event available on tom-select , not on html select
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(this.templateIdValue );
Array.from(template.content.children).forEach((option) => {
this.control.addOption({value: option.value, text: option.text});
});
}
template(id) {
return document.querySelector(`#${id}`);
}
}