import { Controller } from "stimulus"; import TomSelect from "tom-select/dist/esm/tom-select.complete"; export default class extends Controller { static values = { options: Object }; connect(options = {}) { this.control = new TomSelect(this.element, { maxItems: 1, maxOptions: null, plugins: ["dropdown_input"], allowEmptyOption: !this.#placeholder(), closeAfterSelect: true, placeholder: this.#placeholder(), onItemAdd: function () { this.setTextboxValue(""); }, ...this.optionsValue, ...options, }); } disconnect() { if (this.control) this.control.destroy(); } // private #placeholder() { const optionsArray = [...this.element.options]; return optionsArray.find((option) => [null, ""].includes(option.value))?.text; } }