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