Skip to content

Instantly share code, notes, and snippets.

@leastbad
Last active January 4, 2022 08:21
Show Gist options
  • Save leastbad/0f7117c418c9efc0bf62a511045602d8 to your computer and use it in GitHub Desktop.
Save leastbad/0f7117c418c9efc0bf62a511045602d8 to your computer and use it in GitHub Desktop.
TomSelect + StimulusReflex 3.5 (w/payload)
class CustomersReflex < ApplicationReflex
def lookup(search)
# use search string to do some kind of DB lookup here
self.payload = {} # I have NO IDEA what tom-select expects!
morph :nothing
end
end
<select placeholder="Select a customer..." autocomplete="off" multiple data-controller="tom-select" data-tom-select-reflex-value="Customer#lookup">
</select>
import { Controller } from 'stimulus'
import TomSelect from 'tom-select'
export default class extends Controller {
static values = {
reflex: String,
values: Array,
options: String
}
connect() {
this.control = new TomSelect(this.element, {
plugins: ['remove_button'],
valueField: 'value',
labelField: 'text',
searchField: [],
preload: "focus",
options: JSON.parse(this.optionsValue),
items: this.valuesValue || [],
load: this.search
})
}
search = (search, callback) =>
this.stimulate(this.reflexValue, search).then(({ payload }) =>
callback(payload)
)
disconnect() {
if (this.control) this.control.destroy()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment