Skip to content

Instantly share code, notes, and snippets.

@SamuelAierizer
Created September 17, 2021 14:01
Show Gist options
  • Save SamuelAierizer/e9bad5058019b118892d65febef179e9 to your computer and use it in GitHub Desktop.
Save SamuelAierizer/e9bad5058019b118892d65febef179e9 to your computer and use it in GitHub Desktop.
Stimulus select based toggle of input options
<div data-controller="select">
<%= select_tag(:question_type, options_for_select([['Multiple choice with 1 answer', 'radial'], ['Multiple choice with multiple answers', 'checkbox'], ['Open answer', 'open']], 'open'), {:class => 'form-field', "data-select-target" => "select", "data-action" => "change->select#changed"}) %>
<div data-select-target="single">
<p> Insert here for single option which will ocure if choosen 'open' </p>
</div>
<div data-select-target="multi">
<p> Insert here for multi option which will ocure if choosen enethyng else </p>
</div>
</div
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = ["single", "multi", "select"]
connect() {
if (this.selectTarget.value === "open") {
this.set(false, true);
} else {
this.set(true, false);
}
}
changed(event) {
if (this.selectTarget.value === "open") {
this.set(false, true);
} else {
this.set(true, false);
}
}
set(single, multi) {
this.singleTarget.hidden = single;
this.multiTarget.hidden = multi;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment