Created
September 17, 2021 14:01
-
-
Save SamuelAierizer/e9bad5058019b118892d65febef179e9 to your computer and use it in GitHub Desktop.
Stimulus select based toggle of input options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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