
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@wizardhealth/stimulus-multiselect
Advanced tools
Multiselect component built on the back of stimulus and html select tag
Multiselect component built on the back of stimulus and html select tag. Works with static imbedded json inside html, preloaded items from server and searching remotely.
If you are using a js bundler with node_modules
support (such as esbuild, rollup.js or Webpack) install the package from npm:
yarn add stimulus-multiselect
If you're using importmap-rails, you'll need to pin stimulus-multiselect
:
./bin/importmap pin stimulus-multiselect
Load your stimulus application as usual and the register the multiselect controller with it:
import { Application } from '@hotwired/stimulus'
import { Multiselect } from 'stimulus-multiselect'
const application = Application.start()
application.register('multiselect', Multiselect)
To use the multiselect as a remote search component, you need some the following markup:
<div data-controller="multiselect" data-multiselect-search-url-value="/cars" data-placeholder="Search for cars...">
<select multiple="multiple" class="multiselect__hidden" data-multiselect-target="hidden" name="form[test_ids][]" id="form_test_ids"></select>
</div>
The component makes a request to the data-multiselect-search-url to fetch results for the contents of the input field. The server must answer with a json array:
[
{
"value": "toyota",
"text": "Jaris 🚗"
},
{
"value": "ambulance",
"text": "Ambulance 🚑"
},
{
"value": "police",
"text": "Police 🚓"
},
{
"value": "taxi",
"text": "Taxi 🚕"
},
{
"value": "truck",
"text": "Truck 🚚"
}
]
Note: each object has to contain value
and text
. The server will receive a q
query param that represents the search term. Another query param sent while searching is the preselects
param that contains a set of already selected values in the multiselect (a string separated by a comma ",").
To preload items after rendering the page, you need some the following markup:
<div data-controller="multiselect" data-multiselect-preload-url-value="/cars" data-placeholder="Search for cars...">
<select multiple="multiple" class="multiselect__hidden" data-multiselect-target="hidden" name="form[test_ids][]" id="form_test_ids"></select>
</div>
With this setup the component will search through the already preloaded components. In order to search remotely as well as preloading components we just need to add the data-multiselect-search-url-value
attribute.
Note: the server response for the preload url needs to provide a json array just like in the example above.
It is possible to use the data-multiselect-items-value
attribute to load static json data. This is especially useful when your html is being preprocessed before being served (Rails ERB or as a React snippet - data-multiselect-items-value="<%= @cars.to_json %>"
).
<div data-controller="multiselect" data-multiselect-items-value='[{ "value": "cuckoo", "text": "Cuckoo 🐦"}, { "value": "macaw", "text": "Macaw 🦜"}, { "value": "rooster", "text": "Rooster 🐓"}]' data-placeholder="Search for birds...">
<select multiple="multiple" class="multiselect__hidden" data-multiselect-target="hidden" name="form[test_ids][]" id="form_test_ids"></select>
</div>
values
property:document.getElementById("multiselect_id").values
TODO: Explain addable
multiselect-change
whenever an element is added or removedmultiselect-removed
whenever an element is removed. This event contains the value of the removed element in the events detail
under id
:myFunction(event) {
console.log(event.detail.id) // Should print the value of the item removed - 13
}
multiselect-added
whenever an element is added. This event contains the added object in the events detail
under item
:myFunction(event) {
console.log(event.detail.item) // Should print the added object - { "value": "test", "text": "Test" }
}
multiselect-addable
If the addable url is added to the multiselect when the search provides no results a link appears. Pressing this link fires this event. You can use this event as a hook to decide how you want to handle adding a non-existing element to the multiselect.The current examples are contained in the examples folder. You can use the included http-server to test the examples.
Bug reports and pull requests are welcome on GitHub at https://github.com/WizardComputer/stimulus-multiselect. Any contributions or suggestions are wellcome and will be considered. Please read the Contributor Covenant code of conduct.
Update the version number in package.json
. Try to follow semantic versioning guidelines as much as possible.
Publish the package to npmjs.com with yarn run release
This package is available as open source under the terms of the MIT License.
FAQs
Multiselect component built on the back of stimulus and html select tag
The npm package @wizardhealth/stimulus-multiselect receives a total of 0 weekly downloads. As such, @wizardhealth/stimulus-multiselect popularity was classified as not popular.
We found that @wizardhealth/stimulus-multiselect demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.