UNPKG

stimulus-rails-nested-form

Version:

A Stimulus controller to create new fields on the fly to populate your Rails relationship.

2 lines (1 loc) 830 B
(function(t,e){typeof exports=="object"&&typeof module<"u"?module.exports=e(require("@hotwired/stimulus")):typeof define=="function"&&define.amd?define(["@hotwired/stimulus"],e):(t=typeof globalThis<"u"?globalThis:t||self,t.StimulusRailsNestedForm=e(t.Stimulus))})(this,function(t){"use strict";class e extends t.Controller{add(n){n.preventDefault();const r=this.templateTarget.innerHTML.replace(/NEW_RECORD/g,new Date().getTime().toString());this.targetTarget.insertAdjacentHTML("beforebegin",r)}remove(n){n.preventDefault();const r=n.target.closest(this.wrapperSelectorValue);if(r.dataset.newRecord==="true")r.remove();else{r.style.display="none";const s=r.querySelector("input[name*='_destroy']");s.value="1"}}}return e.targets=["target","template"],e.values={wrapperSelector:{type:String,default:".nested-form-wrapper"}},e});