Files
openfoodnetwork/app/webpacker/controllers/tom_select_controller.js
David Cook b97890b537 Support custom placeholder value
Hmm, but this isn't useful until we get Tom-Select to work the way we want..

To do that, I think we'd ned to hook into TS to clear the current selection when focused, then set it back upon blur (if no selection was made). Hmm, but we still want it to show slected in the dropdown list.
Can we do it with css maybe?
2023-12-14 14:24:21 +11:00

35 lines
962 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, placeholder: String };
connect(options = {}) {
console.log(this.element, this.placeholderValue);
this.control = new TomSelect(this.element, {
maxItems: 1,
maxOptions: null,
plugins: ["dropdown_input"],
allowEmptyOption: true, // Show blank option (option with empty value)
closeAfterSelect: true,
placeholder: this.placeholderValue || this.#emptyOption(),
onItemAdd: function () {
this.setTextboxValue("");
},
...this.optionsValue,
...options,
});
}
disconnect() {
if (this.control) this.control.destroy();
}
// private
#emptyOption() {
const optionsArray = [...this.element.options];
return optionsArray.find((option) => [null, ""].includes(option.value))?.text;
}
}