Skip to content

UoE/fix: prevent multi-value form fields from losing input and 'Add more'#3

Open
milanmajchrak wants to merge 2 commits intodatashare-UoEMainLibrary-dspace-8_xfrom
uoe/item-issue-add-more-removed
Open

UoE/fix: prevent multi-value form fields from losing input and 'Add more'#3
milanmajchrak wants to merge 2 commits intodatashare-UoEMainLibrary-dspace-8_xfrom
uoe/item-issue-add-more-removed

Conversation

@milanmajchrak
Copy link
Copy Markdown
Collaborator

@milanmajchrak milanmajchrak commented Apr 8, 2026

Problem

In submission metadata form, multi-value fields (e.g. Type, Funder, Data Creator)
have an "Add more" button to add additional rows. When a user rapidly removed
all rows, the "Add more" button disappeared entirely, making it impossible
to add new values without reloading the page.

Root cause

The removeItem() method in form.component.ts unconditionally called
removeFormArrayGroup() for every removal — including the last remaining item.
When the last item was removed, the underlying FormArray became empty,
Angular destroyed the array's DOM container, and the "Add more" button
(rendered inside the array template) was destroyed along with it.

Fix

Ported the upstream DSpace fix pattern: when the user removes the last
remaining item (index 0, array length 1), the method now clears its value
instead of removing the row. This keeps the FormArray at length >= 1,
preserving the DOM container and the "Add more" button.

if (index === 0 && formArrayControl.value?.length === 1) {
  // Clear value instead of removing the row
  if (event.model instanceof DynamicConcatModel) {
    formArrayControl.at(0).get(fieldId).reset();
  } else {
    formArrayControl.at(0).get(fieldId).setValue(null);
  }
} else {
  this.formBuilderService.removeFormArrayGroup(index, formArrayControl, arrayContext);
}

Origin of fix

Adapted from the upstream DSpace Angular main branch approach. The pattern
ensures multi-value form arrays never reach length 0.

Changed files

File Change
src/app/shared/form/form.component.ts Guard last-item removal: clear value instead of removing row
src/app/shared/form/form.component.spec.ts Added 4 unit tests covering last-item, clear-value, multi-item removal, and existing test adjustments
src/app/datashare/datashare-submission.service.spec.ts Fixed pre-existing broken import (DatashareCustomisedSubmissionServiceDatashareSubmissionService)

Video of fixed feature:
fix-addmore-button-persists.webm

Copilot AI review requested due to automatic review settings April 8, 2026 14:49
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the shared dynamic form component’s array-item removal behavior to avoid fully removing the final remaining entry in a repeatable field, which helps preserve the UI state (e.g., “Add more”) and prevents losing user input unexpectedly.

Changes:

  • Update FormComponent.removeItem to clear the last remaining array entry instead of removing it.
  • Expand FormComponent specs to reflect the new “clear last item” behavior and adjust prior removal tests to remove a non-last item.
  • Rename the Datashare submission service spec to match the actual service class name.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/app/shared/form/form.component.ts Changes array item removal logic to clear the final entry rather than removing it.
src/app/shared/form/form.component.spec.ts Updates/remaps existing array-removal tests and adds new tests for “last item is cleared” behavior.
src/app/datashare/datashare-submission.service.spec.ts Fixes spec naming/injection to use DatashareSubmissionService.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/app/shared/form/form.component.ts Outdated
Comment thread src/app/shared/form/form.component.ts Outdated
Comment thread src/app/shared/form/form.component.ts
Comment thread src/app/shared/form/form.component.spec.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants