Files
openfoodnetwork/app/webpacker/controllers/tom_select_controller.js
Matt-Yorkley 310577c49d Close dropdown after selection with multiselect
This conforms with the previous select2 multiselect behaviour
2023-05-21 00:15:32 +01:00

41 lines
949 B
JavaScript

import { Controller } from "stimulus";
import TomSelect from "tom-select/dist/esm/tom-select.complete";
export default class extends Controller {
static values = { options: Object };
static defaults = {
maxItems: 1,
maxOptions: null,
plugins: ["dropdown_input"],
allowEmptyOption: true,
closeAfterSelect: true,
onItemAdd: function () {
this.setTextboxValue("");
},
};
connect(options = {}) {
if (this.#placeholder()) {
options.allowEmptyOption = false;
options.placeholder = this.#placeholder();
}
this.control = new TomSelect(this.element, {
...this.constructor.defaults,
...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;
}
}