Sha256: 9722dde605f90787392fee0a37743ff1a034273cfadb79e8d5ca8cf16886445b
Contents?: true
Size: 1.33 KB
Versions: 45
Compression:
Stored size: 1.33 KB
Contents
import { Controller } from '@hotwired/stimulus' // Used as a custom Stream Action <turbo-stream action="update-belongs-to" /> export default class extends Controller { static values = { polymorphic: Boolean, searchable: Boolean, targetName: String, relationName: String, } beforeStreamRender(event) { const { relationName } = event.target.dataset if (event.target.action !== 'update-belongs-to' || this.relationNameValue !== relationName) { return false } event.detail.render = (stream) => { if (this.searchableValue) { this.updateSearchable(stream) } else { this.updateNonSearchable(stream) } } } updateSearchable(stream) { // Update the id component document.querySelector(`input[name="${this.targetNameValue}"][type="hidden"]`).value = stream.dataset.targetRecordId // Update the label document.querySelector(`input[name="${this.targetNameValue}"][type="text"]`).value = stream.dataset.targetResourceLabel } updateNonSearchable(stream) { const select = document.querySelector(`select[name="${this.targetNameValue}"]`) const option = document.createElement('option') option.value = stream.dataset.targetRecordId option.text = stream.dataset.targetResourceLabel option.selected = 'selected' select.appendChild(option) } }
Version data entries
45 entries across 45 versions & 1 rubygems