Sha256: 264a9e330f5c5717c8d0eb95da0016fd57d0e346e8b46559d7519c6d409892e9

Contents?: true

Size: 1.65 KB

Versions: 33

Compression:

Stored size: 1.65 KB

Contents

import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="nested-resource-form-fields"
// Copied from https://github.com/stimulus-components/stimulus-rails-nested-form/blob/master/src/index.ts
export default class extends Controller {
  static targets = ["target", "template", "addButton"]

  static values = {
    wrapperSelector: {
      type: String,
      default: ".nested-resource-form-fields",
    },
    limit: Number,
  }

  connect() {
    this.updateState()
  }

  add(e) {
    e.preventDefault()

    const content = this.templateTarget.innerHTML.replace(/NEW_RECORD/g, new Date().getTime().toString())
    this.targetTarget.insertAdjacentHTML("beforebegin", content)

    const event = new CustomEvent("nested-resource-form-fields:add", { bubbles: true })
    this.element.dispatchEvent(event)

    this.updateState()
  }

  remove(e) {
    e.preventDefault()

    const wrapper = e.target.closest(this.wrapperSelectorValue)

    if (wrapper.dataset.newRecord === "true") {
      wrapper.remove()
    } else {
      wrapper.style.display = "none"

      const input = wrapper.querySelector("input[name*='_destroy']")
      input.value = "1"
    }

    const event = new CustomEvent("nested-resource-form-fields:remove", { bubbles: true })
    this.element.dispatchEvent(event)

    this.updateState()
  }

  updateState() {
    if (!this.hasAddButtonTarget || this.limitValue == 0) return

    if (this.childCount >= this.limitValue)
      this.addButtonTarget.style.display = "none"
    else
      this.addButtonTarget.style.display = "initial"
  }

  get childCount() {
    return this.element.querySelectorAll(this.wrapperSelectorValue).length
  }
}

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
plutonium-0.15.6 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.15.5 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.15.4 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.15.3 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.15.2 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.15.1 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.15.0 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.15.0.pre.rc3 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.15.0.pre.rc2 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.15.0.pre.rc1 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.14.1 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.14.0 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.13.3 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.13.2 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.13.1 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.13.0 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.12.14 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.12.13 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.12.12 src/js/controllers/nested_resource_form_fields_controller.js
plutonium-0.12.11 src/js/controllers/nested_resource_form_fields_controller.js