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

Version Path
satis-2.1.47 app/components/satis/input_array/component_controller.js
satis-2.1.46 app/components/satis/input_array/component_controller.js
satis-2.1.45 app/components/satis/input_array/component_controller.js
satis-2.1.44 app/components/satis/input_array/component_controller.js
satis-2.1.43 app/components/satis/input_array/component_controller.js
satis-2.1.42 app/components/satis/input_array/component_controller.js
satis-2.1.41 app/components/satis/input_array/component_controller.js
satis-2.1.40 app/components/satis/input_array/component_controller.js
satis-2.1.39 app/components/satis/input_array/component_controller.js
satis-2.1.38 app/components/satis/input_array/component_controller.js
satis-2.1.37 app/components/satis/input_array/component_controller.js
satis-2.1.36 app/components/satis/input_array/component_controller.js
satis-2.1.35 app/components/satis/input_array/component_controller.js
satis-2.1.33 app/components/satis/input_array/component_controller.js
satis-2.1.31 app/components/satis/input_array/component_controller.js
satis-2.1.30 app/components/satis/input_array/component_controller.js
satis-2.1.29 app/components/satis/input_array/component_controller.js
satis-2.1.28 app/components/satis/input_array/component_controller.js
satis-2.1.27 app/components/satis/input_array/component_controller.js
satis-2.1.26 app/components/satis/input_array/component_controller.js