Skip to content

fix: fixed navigation to the relevant input field#4026

Open
JamalAlabdullah wants to merge 6 commits intomainfrom
3967-navigasjon-via-feilmedlingslink-feiler-for-inputfelt-med-markdown-lister
Open

fix: fixed navigation to the relevant input field#4026
JamalAlabdullah wants to merge 6 commits intomainfrom
3967-navigasjon-via-feilmedlingslink-feiler-for-inputfelt-med-markdown-lister

Conversation

@JamalAlabdullah
Copy link
Contributor

@JamalAlabdullah JamalAlabdullah commented Feb 27, 2026

Description

Related Issue(s)

Screen.Recording.2026-02-27.at.12.40.44.mov

Verification/QA

  • Manual functionality testing
    • I have tested these changes manually
    • Creator of the original issue (or service owner) has been contacted for manual testing (or will be contacted when released in alpha)
    • No testing done/necessary
  • Automated tests
    • Unit test(s) have been added/updated
    • Cypress E2E test(s) have been added/updated
    • No automatic tests are needed here (no functional changes/additions)
    • I want someone to help me make some tests
  • UU/WCAG (follow these guidelines until we have our own)
    • I have tested with a screen reader/keyboard navigation/automated wcag validator
    • No testing done/necessary (no DOM/visual changes)
    • I want someone to help me perform accessibility testing
  • User documentation @ altinn-studio-docs
    • Has been added/updated
    • No functionality has been changed/added, so no documentation is needed
    • I will do that later/have created an issue
  • Support in Altinn Studio
    • Issue(s) created for support in Studio
    • This change/feature does not require any changes to Altinn Studio
  • Sprint board
    • The original issue (or this PR itself) has been added to the Team Apps project and to the current sprint board
    • I don't have permissions to do that, please help me out
  • Labels
    • I have added a kind/* and backport* label to this PR for proper release notes grouping
    • I don't have permissions to add labels, please help me out

Summary by CodeRabbit

  • Bug Fixes
    • Improved keyboard focus selection: detects a broader set of focusable elements, returns no focus for empty containers, honors an optional binding preference, and uses a clear fallback order (preferred binding → first input-like → first available).
  • Tests
    • Added comprehensive tests covering focus selection behaviors and fallback paths.

@JamalAlabdullah JamalAlabdullah added kind/bug Something isn't working backport This PR should be cherry-picked onto older release branches labels Feb 27, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 27, 2026

📝 Walkthrough

Walkthrough

Rewrote and exported findElementToFocus in src/layout/GenericComponent.tsx: returns undefined for null container or no focusable targets, collects targets via a single query (input, textarea, select, button, [tabindex]:not([tabindex="-1"]), [contenteditable="true"]), and prioritizes elements matching data-bindingkey when a binding is provided.

Changes

Cohort / File(s) Summary
Core logic & export
src/layout/GenericComponent.tsx
Refactored findElementToFocus into a single-pass lookup, added early null check, broadened selector set, consolidated fallback order, and changed function to a named exported function. (+38/-13)
Tests
src/layout/GenericComponent.test.tsx
Added tests for the new exported findElementToFocus: no-focusable result, preference for input-like elements, preference for input-like matching data-bindingkey, fallback to any binding match, and fallback to first focusable element. (+61/-1)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing navigation to the relevant input field, which aligns with the core purpose of the PR addressing issue #3967.
Description check ✅ Passed The description follows the template structure with issue reference, verification checklist items completed, and accessibility testing confirmed. However, the description section itself lacks detailed explanation of the technical changes made.
Linked Issues check ✅ Passed The PR successfully addresses issue #3967 by rewriting the findElementToFocus function to properly handle input field navigation with broader selector coverage and improved binding-matching logic.
Out of Scope Changes check ✅ Passed All changes are focused and in-scope: the findElementToFocus function rewrite directly addresses the navigation issue, and test additions support the core functionality without extraneous modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 3967-navigasjon-via-feilmedlingslink-feiler-for-inputfelt-med-markdown-lister

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@JamalAlabdullah JamalAlabdullah added the squad/utforming Issues that belongs to the named squad. label Feb 27, 2026
@JamalAlabdullah JamalAlabdullah moved this to 🔎 In review in Team Altinn Studio Feb 27, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/layout/GenericComponent.tsx (1)

274-289: Consider filtering non-interactive candidates before fallback focus.

If fallback selects a disabled/hidden/inert element, focus can silently fail even though a valid input exists later in the list.

Suggested hardening
   const targetElements = Array.from(
     div.querySelectorAll<HTMLElement>(
       ['input', 'textarea', 'select', 'button', '[tabindex]:not([tabindex="-1"])', '[contenteditable="true"]'].join(
         ',',
       ),
     ),
-  );
+  ).filter(
+    (element) =>
+      !element.matches(':disabled, [hidden], [aria-hidden="true"], [inert], input[type="hidden"]'),
+  );
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/layout/GenericComponent.tsx` around lines 274 - 289, The current focus
fallback can pick non-interactive elements; update the selection to filter out
disabled/hidden/inert/non-visible candidates before choosing a fallback: after
building targetElements (from querySelectorAll), run a filter that removes
elements with hidden attribute, aria-hidden="true", inert attribute,
style.display === 'none' or style.visibility === 'hidden' or
getClientRects().length === 0 (or offsetParent === null for robustness), and for
form controls check the disabled property; keep the binding lookup
(elementWithBinding) using the same dataset.bindingkey check but apply the same
filter so the returned element (elementWithBinding ?? first) is guaranteed
interactive. Ensure you still return undefined when no interactive candidates
remain.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/layout/GenericComponent.tsx`:
- Around line 274-289: The current focus fallback can pick non-interactive
elements; update the selection to filter out disabled/hidden/inert/non-visible
candidates before choosing a fallback: after building targetElements (from
querySelectorAll), run a filter that removes elements with hidden attribute,
aria-hidden="true", inert attribute, style.display === 'none' or
style.visibility === 'hidden' or getClientRects().length === 0 (or offsetParent
=== null for robustness), and for form controls check the disabled property;
keep the binding lookup (elementWithBinding) using the same dataset.bindingkey
check but apply the same filter so the returned element (elementWithBinding ??
first) is guaranteed interactive. Ensure you still return undefined when no
interactive candidates remain.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ce114bd and fd1fd48.

📒 Files selected for processing (1)
  • src/layout/GenericComponent.tsx

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/layout/GenericComponent.test.tsx (1)

78-136: Add one regression test for the real error-link flow (markdown list + intro text).

These helper tests are strong, but they don’t directly cover the bug’s full path (clicking error message link and focusing the target input when description has leading text + markdown list). Please add one component-level regression test for that interaction.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/layout/GenericComponent.test.tsx` around lines 78 - 136, Add a
component-level regression test that renders GenericComponent (or the component
that uses findElementToFocus) with an error message containing leading intro
text plus a markdown list (e.g., "Intro text\n\n- item") and an error link that
targets a field by data-bindingkey; use the existing test helpers
(createContainer or test renderer) to render the DOM, simulate a user click on
the error link (fireEvent.click or userEvent.click), then assert that the
input/textarea with the matching data-bindingkey receives focus
(expect(document.activeElement).toBe(targetElement) or
expect(targetElement).toHaveFocus()). Ensure the test exercises the same code
path as findElementToFocus so it verifies focusing works when the description
contains both intro text and a markdown list.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/layout/GenericComponent.test.tsx`:
- Around line 78-136: Add a component-level regression test that renders
GenericComponent (or the component that uses findElementToFocus) with an error
message containing leading intro text plus a markdown list (e.g., "Intro
text\n\n- item") and an error link that targets a field by data-bindingkey; use
the existing test helpers (createContainer or test renderer) to render the DOM,
simulate a user click on the error link (fireEvent.click or userEvent.click),
then assert that the input/textarea with the matching data-bindingkey receives
focus (expect(document.activeElement).toBe(targetElement) or
expect(targetElement).toHaveFocus()). Ensure the test exercises the same code
path as findElementToFocus so it verifies focusing works when the description
contains both intro text and a markdown list.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b32282f and bbfabfd.

📒 Files selected for processing (2)
  • src/layout/GenericComponent.test.tsx
  • src/layout/GenericComponent.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/layout/GenericComponent.tsx

…eilmedlingslink-feiler-for-inputfelt-med-markdown-lister
@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 4, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport This PR should be cherry-picked onto older release branches kind/bug Something isn't working squad/utforming Issues that belongs to the named squad.

Projects

Status: 🔎 In review

Development

Successfully merging this pull request may close these issues.

Navigasjon via feilmedlingslink feiler for inputfelt med markdown-lister

1 participant