import TomSelectController from "./tom_select_controller"; // This is simalar to the "variantAutocomplete" directive that uses "select2", but it doesn't // have all the same feature // export default class extends TomSelectController { static values = { options: Object, distributor: Number }; connect() { const options = { valueField: 'id', searchField: ['name', 'sku'], load: this.#load.bind(this), shouldLoad: (query) => query.length > 2, render: { option: this.#renderOption.bind(this), item: this.#renderItem.bind(this), }, }; super.connect(options); } // private #load(query, callback) { const url = '/admin/variants/search.json?q=' + encodeURIComponent(query); fetch(url) .then(response => response.json()) .then(json => { callback(json); }).catch((error) => { console.log(error); callback(); }); } #renderOption(variant, escape) { return `
${ variant.image ? `` : "" }
${ escape(variant.name)}
`; } #renderItem(variant, escape) { return `${ escape(variant.name) }` } }