Sha256: d8de3d921c5d9c05d9b5afd5375359af23f9eec6eb7dc1cd35f7c01fcf995b89
Contents?: true
Size: 1.52 KB
Versions: 40
Compression:
Stored size: 1.52 KB
Contents
import ApplicationController from "satis/controllers/application_controller" export default class InputArrayComponentController extends ApplicationController { static targets = ["rowsContainer", "inputRow", "rowTemplate", "crossButton", "plusButton"] static values = { inputName: String } connect() { super.connect() this.updateAddRemoveButtons() } disconnect() {} removeRow(event) { event.preventDefault() event.currentTarget.parentElement.parentElement.remove() this.updateAddRemoveButtons() } addRow(event) { event.preventDefault() const rowTemplate = this.rowTemplateTarget.content.firstElementChild.cloneNode(true) const inputControl = rowTemplate.querySelector("input") inputControl.setAttribute("name", this.inputNameValue) this.rowsContainerTarget.appendChild(rowTemplate) this.updateAddRemoveButtons() } input(event) { if ( Array.from(this.rowsContainerTarget.children).indexOf(event.currentTarget.parentElement.parentElement) + 1 == this.inputRowTargets.length ) { this.addRow(event) } } updateAddRemoveButtons() { this.inputRowTargets.forEach((inputRow, i) => { if (i + 1 == this.inputRowTargets.length) { inputRow.querySelector(".crossButton").classList.add("hidden") inputRow.querySelector(".plusButton").classList.remove("hidden") } else { inputRow.querySelector(".crossButton").classList.remove("hidden") inputRow.querySelector(".plusButton").classList.add("hidden") } }) } }
Version data entries
40 entries across 40 versions & 1 rubygems