Select2 integration for Stimulus (Hotwired)
npm install stimulus-select2Lightweight controller that wires Select2 to @hotwired/stimulus.
``bash`
npm install stimulus-select2 @hotwired/stimulus jquery select2
Make sure Select2 is loaded (via a bundler import or script tag) before the controller connects.
`js
import { Application } from "@hotwired/stimulus";
import Select2Controller from "stimulus-select2";
import "select2";
import "select2/dist/css/select2.css";
const application = Application.start();
application.register("select2", Select2Controller);
`
`html`
data-controller="select2"
data-select2-placeholder="Pick a color"
data-select2-width="style"
>
Any data-select2- attributes are converted to Select2 options using the same camelCasing that jQuery applies to data- keys.
- npm run build – generate dist bundles (CJS, ESM, and UMD)npm test
- – run the Vitest suitenpm run dev
- – watch and rebuild the bundlesnpm run demo
- – build and open a local browser demo at /demo`