Last active
January 4, 2022 08:21
-
-
Save leastbad/0f7117c418c9efc0bf62a511045602d8 to your computer and use it in GitHub Desktop.
TomSelect + StimulusReflex 3.5 (w/payload)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<select placeholder="Select a customer..." autocomplete="off" multiple data-controller="tom-select" data-tom-select-reflex-value="Customer#lookup"> | |
</select> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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