Use built-in feature to delete new record row

I looked at the source code and found that we were missing one detail: data-new-record.
https://github.com/stimulus-components/stimulus-rails-nested-form/blob/master/src/index.ts#L32-L35

It's documented here, but it's easy to miss: https://www.stimulus-components.com/docs/stimulus-rails-nested-form/
This commit is contained in:
David Cook
2024-05-14 10:17:50 +10:00
parent 808f1c65f2
commit b45df8a723
2 changed files with 2 additions and 7 deletions

View File

@@ -76,8 +76,8 @@
= form.fields_for("products][#{product_index}][variants_attributes][NEW_RECORD", product.variants.build) do |new_variant_form|
%template{ 'data-nested-form-target': "template" }
%tr.condensed{ 'data-controller': "variant", 'class': "nested-form-wrapper" }
= render partial: 'variant_row', locals: { variant: new_variant_form.object, f: new_variant_form, category_options:, tax_category_options: }
%tr.condensed{ 'data-controller': "variant", 'class': "nested-form-wrapper", 'data-new-record': "true" }
= render partial: 'variant_row', locals: { variant: new_variant_form.object, f: new_variant_form, category_options:, tax_category_options: }
%tr{ 'data-nested-form-target': "target" }
%tr.condensed

View File

@@ -118,11 +118,6 @@ export default class BulkFormController extends Controller {
this.recordElements[recordId]
.splice(elmntsToDelete[0], elmntsToDelete[elmntsToDelete.length - 1] - elmntsToDelete[0] + 1);
this.toggleFormChanged();
// Otherwise, elements within tr may be re-added to recordElements.
// With the nested-form-wrapper(stimulus components) class added to a tr.
let tr = document.querySelector('.nested-form-wrapper[style="display: none;"]')
tr.remove();
}
// private