fix: never reuse a collection entry index after a removal
This commit is contained in:
1 parent
ede8fd840a
commit
972839286c
1 file changed
+4
-1
@@ -11,10 +11,13 @@ export default class extends Controller {
|
|||||||
|
|
||||||
connect() {
|
connect() {
|
||||||
this.itemsCountValue = this.fieldTargets.length
|
this.itemsCountValue = this.fieldTargets.length
|
||||||
|
// A running counter rather than the current count: after a remove, count + 1 can be an
|
||||||
|
// index a remaining entry still holds, and the two would submit into the same slot.
|
||||||
|
this.nextIndex = this.fieldTargets.length
|
||||||
}
|
}
|
||||||
|
|
||||||
addItem() {
|
addItem() {
|
||||||
const index = this.fieldTargets.length + 1
|
const index = this.nextIndex++
|
||||||
const prototype = JSON.parse(this.prototypeValue)
|
const prototype = JSON.parse(this.prototypeValue)
|
||||||
const newField = prototype.replace(/__name__/g, index)
|
const newField = prototype.replace(/__name__/g, index)
|
||||||
this.fieldsTarget.insertAdjacentHTML('beforeend', newField)
|
this.fieldsTarget.insertAdjacentHTML('beforeend', newField)
|
||||||
|
|||||||
Reference in new issue
Block a user