import { Controller } from "@hotwired/stimulus"; export default class extends Controller { static targets = ["container", "field"]; static values = { nextIndex: Number } add(event) { event.preventDefault(); const fields_wrapper = this.element.dataset.nestedFieldsWrapper; const field_index_pattern = `[${fields_wrapper}_attributes][0]` const new_field_index = `[${fields_wrapper}_attributes][${this.nextIndexValue}]` const position = this.containerTarget.children.length + 1; const content = this.element.dataset.nestedFieldsTemplate.replace( /NEW_RECORD/g, new Date().getTime() ).replace( /NEW_POSITION/g, position ).replaceAll( field_index_pattern, new_field_index ); this.containerTarget.insertAdjacentHTML("beforeend", content); this.nextIndexValue = this.nextIndexValue + 1; } remove(event) { event.preventDefault(); const field = event.target.closest("[data-nested-fields-target='field']"); const destroyInput = field.querySelector("[data-destroy-input]"); if (destroyInput) { destroyInput.value = 1; // Mark for deletion field.style.display = "none"; } else { field.remove(); } } }

AltStyle によって変換されたページ (->オリジナル) /