Loading...
Loading...
Create and register Stimulus controllers for interactive JavaScript features. Use when adding client-side interactivity, dynamic UI updates, or when the user mentions Stimulus controllers or JavaScript behavior.
npx skill4agent add rolemodel/rolemodel-skills stimulus-controllersapp/javascript/controllers/// app/javascript/controllers/example_controller.js
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = ["element"]
static values = { name: String }
connect() {
// Called when controller is connected to DOM
}
disconnect() {
// Called when controller is disconnected from DOM
}
// Action methods
handleClick(event) {
event.preventDefault()
// Your logic here
}
}bin/rails stimulus:manifest:updateapp/javascript/controllers/index.jsimport ExampleController from "./example_controller"
application.register("example", ExampleController)data-controller="example".container data-controller="example" data-example-name-value="test"
button data-action="click->example#handleClick" Click Me
div data-example-target="element" Target Elementexample_controller.jsexport default class extends Controller"example"data-controller="example"bulk_submit_controller.js"bulk-submit"static targets = ["input", "output"]
// Access in methods:
this.inputTarget // First matching element
this.inputTargets // All matching elements
this.hasInputTarget // Boolean checkstatic values = {
url: String,
count: Number,
active: Boolean,
items: Array,
config: Object
}
// Access in methods:
this.urlValue
this.countValue
// Watch for changes:
urlValueChanged(newUrl, oldUrl) {
// Called when value changes
}<!-- Basic action -->
data-action="click->example#save"
<!-- Multiple actions -->
data-action="click->example#save submit->example#submit"
<!-- Custom events -->
data-action="example:refresh->example#reload"
<!-- Event modifiers -->
data-action="submit->example#save:prevent"static classes = ["active", "hidden"]
// Use in methods:
this.element.classList.add(this.activeClass)
this.element.classList.remove(this.hiddenClass)export default class extends Controller {
static targets = ["form", "submit"]
validate() {
const isValid = this.formTarget.checkValidity()
this.submitTarget.disabled = !isValid
}
}export default class extends Controller {
static targets = ["content"]
static classes = ["hidden"]
toggle() {
this.contentTarget.classList.toggle(this.hiddenClass)
}
}export default class extends Controller {
static values = { url: String }
async refresh() {
const response = await fetch(this.urlValue)
const html = await response.text()
this.element.innerHTML = html
}
}it 'handles interaction', :js do
visit page_path
click_button 'Toggle'
expect(page).to have_css('[data-controller="example"]')
endindex.jsbin/rails stimulus:manifest:update:jsstatic targetshasXxxTarget