Skip to content

Fix GlFormFields rendering issue

What does this MR do?

Related to #2418

There are two rendering issues with GlFormFields:

  1. When the component is re-rendered the id attribute of inputs is incremented. e.g gl-form-field-1 becomes gl-form-field-2.
  2. There is no key on the v-for so when the fields are re-rendered they can lose focus. See #2418

This MR does a few things:

  1. Uses field name for id attribute instead of uniqueId to avoid incrementing every time component is re-rendered
  2. Adds support for passing a custom id to a field input
  3. Adds a key to v-for to fix focus/rendering issue when adding fields dynamically

Screenshots or screen recordings

Before After
Screen_Recording_2025-10-16_at_1.46.50_PM Screen_Recording_2025-10-23_at_11.08.24_AM

How to test locally

  1. Apply this patch
diff --git a/packages/gitlab-ui/src/components/base/form/form_fields/form_fields.stories.js b/packages/gitlab-ui/src/components/base/form/form_fields/form_fields.stories.js
index f323f037d..86f1c1fc9 100644
--- a/packages/gitlab-ui/src/components/base/form/form_fields/form_fields.stories.js
+++ b/packages/gitlab-ui/src/components/base/form/form_fields/form_fields.stories.js
@@ -89,6 +89,9 @@ const Template = () => ({
       return null;
     },
   },
+  created() {
+    setInterval(this.addField, 5000);
+  },
   methods: {
     onInputField({ name }) {
       this.$delete(this.serverValidations, name);
@@ -113,6 +116,17 @@ const Template = () => ({
 
       this.$refs.modal.show();
     },
+    addField() {
+      const fieldId = uniqueId();
+
+      this.fields = {
+        [`field${fieldId}`]: {
+          label: `Extra Field ${fieldId}`,
+          id: `custom-form-field-id-${fieldId}`,
+        },
+        ...this.fields,
+      };
+    },
   },
   template: `
     <div>
  1. Run yarn storybook
  2. Go to http://localhost:9001/iframe.html?args=&id=base-form-form-fields--default&viewMode=story

Integrations

Does this MR meet the acceptance criteria?

This checklist encourages the authors, reviewers, and maintainers of merge requests (MRs) to confirm changes were analyzed for conformity with the project's guidelines, security and accessibility.

Toggle the acceptance checklist

Conformity

  • The “What does this MR do?” section in the MR description is filled out, explaining the reasons for and scope of the proposed changes, per “Say why not just what”.
    • For example, if the MR is focused on usage guidelines, addressing accessibility challenges could be added in a separate MR.
  • Relevant label(s) are applied to the MR.
  • The MR is added to a milestone.
  • Added the ~"component:*" label(s) if applicable.
  • A changeset is added when this MR will do a patch, minor, or major update. More information can be found in doc/publishing-packages.md.

Components

Documentation

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • Security reports checked/validated by a reviewer from the AppSec team

Accessibility

If this MR adds or modifies a component, take a few moments to review the following:

  • All actions and functionality can be done with a keyboard.
  • Links, buttons, and controls have a visible focus state.
  • All content is presented in text or with a text equivalent. For example, alt text for SVG, or aria-label for icons that have meaning or perform actions.
  • Changes in a component’s state are announced by a screen reader. For example, changing aria-expanded="false" to aria-expanded="true" when an accordion is expanded.
  • Color combinations have sufficient contrast.
Edited by Peter Hegman

Merge request reports

Loading