Skip to content

Commit f2af94c

Browse files
authored
Update README.md
1 parent fc08ffe commit f2af94c

File tree

1 file changed

+152
-2
lines changed

1 file changed

+152
-2
lines changed

README.md

Lines changed: 152 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,152 @@
1-
# stimulus-multiselect
2-
Multiselect component built on the back of stimulus and html select tag
1+
# Stimulus Multiselect controller
2+
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.
3+
4+
![multiselect](https://user-images.githubusercontent.com/31761693/196924970-eb19e850-62f9-4af0-ab2d-488bde0a9ea9.gif)
5+
6+
## Installation
7+
8+
If you are using a js bundler with `node_modules` support (such as esbuild, rollup.js or Webpack) install the package from npm:
9+
10+
```plain
11+
yarn add stimulus-autocomplete
12+
```
13+
14+
If you're using [importmap-rails](https://github.com/rails/importmap-rails), you'll need to pin `stimulus-multiselect`:
15+
16+
```plain
17+
./bin/importmap pin stimulus-multiselect
18+
```
19+
20+
## Usage
21+
22+
Load your stimulus application as usual and the register the multiselect
23+
controller with it:
24+
25+
```javascript
26+
import { Application } from '@hotwired/stimulus'
27+
import { Multiselect } from 'stimulus-multiselect'
28+
29+
const application = Application.start()
30+
application.register('multiselect', Multiselect)
31+
```
32+
33+
### Remote search
34+
35+
To use the multiselect as a remote search component, you need some the following markup:
36+
37+
```html
38+
<div data-controller="multiselect" data-multiselect-search-url-value="/cars" data-placeholder="Search for cars...">
39+
<select multiple="multiple" class="multiselect__hidden" data-multiselect-target="hidden" name="form[test_ids][]" id="form_test_ids"></select>
40+
</div>
41+
```
42+
43+
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:
44+
45+
```json
46+
[
47+
{
48+
"value": "toyota",
49+
"text": "Jaris 🚗"
50+
},
51+
{
52+
"value": "ambulance",
53+
"text": "Ambulance 🚑"
54+
},
55+
{
56+
"value": "police",
57+
"text": "Police 🚓"
58+
},
59+
{
60+
"value": "taxi",
61+
"text": "Taxi 🚕"
62+
},
63+
{
64+
"value": "truck",
65+
"text": "Truck 🚚"
66+
}
67+
]
68+
```
69+
70+
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 ",").
71+
72+
### Preload
73+
74+
To preload items after rendering the page, you need some the following markup:
75+
76+
```html
77+
<div data-controller="multiselect" data-multiselect-preload-url-value="/cars" data-placeholder="Search for cars...">
78+
<select multiple="multiple" class="multiselect__hidden" data-multiselect-target="hidden" name="form[test_ids][]" id="form_test_ids"></select>
79+
</div>
80+
```
81+
82+
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.
83+
84+
Note: the server response for the preload url needs to provide a json array just like in the example above.
85+
86+
### Static collection
87+
88+
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 %>"`).
89+
90+
```html
91+
<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...">
92+
<select multiple="multiple" class="multiselect__hidden" data-multiselect-target="hidden" name="form[test_ids][]" id="form_test_ids"></select>
93+
</div>
94+
```
95+
96+
## Good to know
97+
98+
- The initial html markup contains a hidden select which serves as a hook for any forms that contain the multiselect. This select will reflect all changes on the multiselect. As an additional helper, if the multiselect is used outside of Stimulus scope, it is possible to get all the selected values via the `values` property:
99+
100+
```js
101+
document.getElementById("multiselect_id").values
102+
```
103+
104+
- The multiselect comes with a .css file with some styles but feel free to change it when using it in your app.
105+
106+
TODO: Explain addable
107+
108+
### Accessibility
109+
110+
- Arrow navigation on the elements in the dropdown
111+
- Selecting elements with Enter key
112+
- Close dropdown on Escape
113+
- If search is empty Backspace deletes the last item
114+
115+
116+
## Events
117+
118+
- `multiselect-change` whenever an element is added or removed
119+
- `multiselect-removed` whenever an element is removed. This event contains the value of the removed element in the events `detail` under `id`:
120+
121+
```javascript
122+
myFunction(event) {
123+
console.log(event.detail.id) // Should print the value of the item removed - 13
124+
}
125+
```
126+
127+
- `multiselect-added` whenever an element is added. This event contains the added object in the events `detail` under `item`:
128+
129+
```javascript
130+
myFunction(event) {
131+
console.log(event.detail.item) // Should print the added object - { "value": "test", "text": "Test" }
132+
}
133+
```
134+
- `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.
135+
136+
## Examples
137+
138+
The current examples are contained in the [examples folder](https://github.com/WizardComputer/stimulus-multiselect/tree/main/examples). You can use the included http-server to test the examples.
139+
140+
## Contributing
141+
142+
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](https://www.contributor-covenant.org/).
143+
144+
## Releasing
145+
146+
1. Update the version number in `package.json`. Try to follow semantic versioning guidelines as much as possible.
147+
148+
2. Publish the package to npmjs.com with `yarn run release`
149+
150+
## License
151+
152+
This package is available as open source under the terms of the MIT License.

0 commit comments

Comments
 (0)