Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StimulusJs integration example #686

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* [Single-Value Only](#single-value)
* [React wrapper](#react)
* [Angular wrapper](#angular)
* [StimulusJs Example](#stimulusjs)
* [Vue Example](https://codesandbox.io/s/tagify-tags-component-vue-example-l8ok4)
* [jQuery version](#jquery-version)
* [FAQ](#FAQ)
Expand Down Expand Up @@ -516,6 +517,70 @@ export class AppComponent implements OnDestroy {
</details>


## StimulusJs

Loading remote server data using StimulusJs. Below is a basic example using the `fetch` API

See live [Demo](https://codesandbox.io/s/quizzical-mirzakhani-ss8k2)


<details>
<summary>Example:</summary>

```typescript
import { Controller } from 'stimulus';
import Tagify from '@yaireo/tagify';

export default class extends Controller {

connect() {
this.countries();
}

countries() {
var that = this;
const url = 'https://get_suggestions.com/countries.json';

const countryInput = document.querySelector('input');
const tagify = new Tagify(countryInput, {
whitelist: [],
maxTags: 10,
dropdown: {
maxItems: 20, // <- mixumum allowed rendered suggestions
classname: 'tags-look', // <- custom classname for this dropdown, so it could be targeted
enabled: 0, // <- show suggestions on focus
closeOnSelect: false, // <- do not hide the suggestions dropdown once an item has been selected
},
});

tagify.on('focus', function(e) {
that.loadWhiteList(e, tagify, url)
})
}

loadWhiteList(e, tagify, url) {
let controller;
const value = e.detail.value;
tagify.settings.whitelist.length = 0; // reset the whitelist

controller && controller.abort();
controller = new AbortController();

tagify.loading(true).dropdown.hide.call(tagify)

fetch(`${url}?value=${value}`, {signal:controller.signal})
.then(RES => RES.json())
.then(function(whitelist){
tagify.settings.whitelist.splice(0, whitelist.length, ...whitelist)
tagify.loading(false).dropdown.show.call(tagify, value);
})
}

}

```
</details>


## jQuery version

Expand Down