Skip to content

fix: readOnly for PersonLookup and OrganisationLookup#4181

Open
walldenfilippa wants to merge 4 commits intomainfrom
fix/4119-read-only-look-up-components
Open

fix: readOnly for PersonLookup and OrganisationLookup#4181
walldenfilippa wants to merge 4 commits intomainfrom
fix/4119-read-only-look-up-components

Conversation

@walldenfilippa
Copy link
Copy Markdown
Contributor

@walldenfilippa walldenfilippa commented May 7, 2026

Description

FIxes readOnly for PersonLookup and OrganisationLookup

lookUpReadOnly.mp4

Related Issue(s)

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
    • Lookup components correctly enforce read-only: inputs are disabled when read-only or during active lookup, action buttons are hidden in read-only mode, and Enter-key submission is disabled when read-only. Existing interactive lookup, validation, and submit/clear behavior remain unchanged.

@walldenfilippa walldenfilippa added kind/feature-request New feature or request backport-ignore This PR is a new feature and should not be cherry-picked onto release branches labels May 7, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 7, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

OrganisationLookupComponent and PersonLookupComponent are updated to respect a readOnly property from the useItemWhenType hook. Input fields become read-only when a lookup succeeds or the component is externally marked read-only. Submit/clear buttons are hidden entirely when readOnly is true, preventing user modifications.

Changes

Read-Only State Support in Lookup Components

Layer / File(s) Summary
State Extraction from Hook
src/layout/OrganisationLookup/OrganisationLookupComponent.tsx, src/layout/PersonLookup/PersonLookupComponent.tsx
Both components now destructure readOnly from useItemWhenType to expose external read-only state.
Input Field Read-Only Conditions
src/layout/OrganisationLookup/OrganisationLookupComponent.tsx, src/layout/PersonLookup/PersonLookupComponent.tsx
ORG number, SSN, and name input fields are marked read-only when either a lookup succeeds, a lookup is in progress, or the component's readOnly state is true.
Submit/Clear Button Visibility
src/layout/OrganisationLookup/OrganisationLookupComponent.tsx, src/layout/PersonLookup/PersonLookupComponent.tsx
Action button containers are conditionally rendered only when !readOnly, hiding buttons entirely when in read-only mode; submit-vs-clear behavior otherwise unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 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 clearly and concisely summarizes the main change: fixing readOnly behavior for both PersonLookup and OrganisationLookup components.
Description check ✅ Passed The description follows the template structure, includes the related issue #4119, and documents verification/QA status with manual testing completed and appropriate support/sprint decisions.
Linked Issues check ✅ Passed The changes implement the core requirement from #4119: making PersonLookup non-interactive when readOnly is true by disabling buttons and input fields, while extending the fix to OrganisationLookup.
Out of Scope Changes check ✅ Passed All changes are scoped to PersonLookupComponent and OrganisationLookupComponent readOnly handling, directly addressing the linked issue #4119 with no extraneous modifications.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/4119-read-only-look-up-components

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Microsoft Presidio Analyzer (2.2.362)
src/layout/OrganisationLookup/OrganisationLookupComponent.tsx

Microsoft Presidio Analyzer failed to scan this file


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.

@walldenfilippa walldenfilippa changed the title fix readOnly for PersonLookup and OrganisationLookup fix: readOnly for PersonLookup and OrganisationLookup May 7, 2026
Copy link
Copy Markdown
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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/layout/PersonLookup/PersonLookupComponent.tsx (1)

235-239: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Enter-key handlers fire unconditionally in read-only mode, producing spurious validation errors.

Same root cause as in OrganisationLookupComponent: both onKeyDown handlers call handleSubmit() without checking readOnly. With readOnly=true, pressing Enter on either input triggers handleValidateSsn('') / handleValidateName(''), sets error state, and renders error UI beneath read-only fields.

🐛 Proposed fix (apply to both handlers)
-              onKeyDown={async (ev) => {
-                if (ev.key === 'Enter') {
-                  await handleSubmit();
-                }
-              }}
+              onKeyDown={async (ev) => {
+                if (ev.key === 'Enter' && !readOnly) {
+                  await handleSubmit();
+                }
+              }}

Also applies to: 289-293

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/layout/PersonLookup/PersonLookupComponent.tsx` around lines 235 - 239,
The onKeyDown handlers in PersonLookupComponent are calling handleSubmit()
unconditionally, causing validation to run in read-only mode; update both
onKeyDown handlers (the ones that currently call handleSubmit()) to first check
the component's readOnly prop/state and only call await handleSubmit() when
readOnly is falsy, so Enter presses in read-only mode are ignored and do not
trigger handleValidateSsn/handleValidateName or set error state.
src/layout/OrganisationLookup/OrganisationLookupComponent.tsx (1)

165-169: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Enter-key handler fires in read-only mode and can produce spurious validation errors.

When readOnly is true, the NumericInput is still focusable and fires onKeyDown. Pressing Enter calls handleSubmit()handleValidateOrgnr(tempOrgNr). Since tempOrgNr is '' (the user cannot type into a read-only field), validation fails and setOrgNrErrors is set, rendering error UI below what should be a clean read-only input.

🐛 Proposed fix
-              onKeyDown={async (ev) => {
-                if (ev.key === 'Enter') {
-                  await handleSubmit();
-                }
-              }}
+              onKeyDown={async (ev) => {
+                if (ev.key === 'Enter' && !readOnly) {
+                  await handleSubmit();
+                }
+              }}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/layout/OrganisationLookup/OrganisationLookupComponent.tsx` around lines
165 - 169, The Enter-key handler on the NumericInput should not trigger
validation when the component is read-only; update the onKeyDown callback to
first check the readOnly prop (or the input's readOnly state) and return early
if true, so it only calls handleSubmit() when readOnly is false. Specifically,
modify the onKeyDown in OrganisationLookupComponent to guard before invoking
handleSubmit (which calls handleValidateOrgnr using tempOrgNr) to prevent
setOrgNrErrors from running for read-only inputs.
🧹 Nitpick comments (1)
src/layout/PersonLookup/PersonLookupComponent.tsx (1)

229-229: 💤 Low value

isFetching is absent from the readOnly condition — inconsistent with OrganisationLookupComponent.

OrganisationLookupComponent uses readOnly={hasSuccessfullyFetched || isFetching || readOnly}, locking the input during the in-flight API call. Both PersonLookup inputs omit isFetching, leaving the SSN and surname fields editable while a lookup is in progress.

♻️ Proposed change
-              readOnly={hasSuccessfullyFetched || readOnly}
+              readOnly={hasSuccessfullyFetched || isFetching || readOnly}

Apply the same diff to both the NumericInput (line 229) and the Input (line 283).

Also applies to: 283-283

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/layout/PersonLookup/PersonLookupComponent.tsx` at line 229, Update the
readOnly prop on PersonLookupComponent's inputs to match
OrganisationLookupComponent: include isFetching in the condition so the fields
are locked while the lookup is in-flight. Specifically, change the NumericInput
(currently using readOnly={hasSuccessfullyFetched || readOnly}) and the Input
(similarly missing isFetching) to use readOnly={hasSuccessfullyFetched ||
isFetching || readOnly} so both SSN and surname become non-editable during
loading; reference the NumericInput and Input elements and the variables
hasSuccessfullyFetched, isFetching, and readOnly when making the edits.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/layout/OrganisationLookup/OrganisationLookupComponent.tsx`:
- Around line 165-169: The Enter-key handler on the NumericInput should not
trigger validation when the component is read-only; update the onKeyDown
callback to first check the readOnly prop (or the input's readOnly state) and
return early if true, so it only calls handleSubmit() when readOnly is false.
Specifically, modify the onKeyDown in OrganisationLookupComponent to guard
before invoking handleSubmit (which calls handleValidateOrgnr using tempOrgNr)
to prevent setOrgNrErrors from running for read-only inputs.

In `@src/layout/PersonLookup/PersonLookupComponent.tsx`:
- Around line 235-239: The onKeyDown handlers in PersonLookupComponent are
calling handleSubmit() unconditionally, causing validation to run in read-only
mode; update both onKeyDown handlers (the ones that currently call
handleSubmit()) to first check the component's readOnly prop/state and only call
await handleSubmit() when readOnly is falsy, so Enter presses in read-only mode
are ignored and do not trigger handleValidateSsn/handleValidateName or set error
state.

---

Nitpick comments:
In `@src/layout/PersonLookup/PersonLookupComponent.tsx`:
- Line 229: Update the readOnly prop on PersonLookupComponent's inputs to match
OrganisationLookupComponent: include isFetching in the condition so the fields
are locked while the lookup is in-flight. Specifically, change the NumericInput
(currently using readOnly={hasSuccessfullyFetched || readOnly}) and the Input
(similarly missing isFetching) to use readOnly={hasSuccessfullyFetched ||
isFetching || readOnly} so both SSN and surname become non-editable during
loading; reference the NumericInput and Input elements and the variables
hasSuccessfullyFetched, isFetching, and readOnly when making the edits.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d21c5182-b2aa-4709-a1a0-95c90b0f1bec

📥 Commits

Reviewing files that changed from the base of the PR and between bce9f48 and c56a11d.

📒 Files selected for processing (2)
  • src/layout/OrganisationLookup/OrganisationLookupComponent.tsx
  • src/layout/PersonLookup/PersonLookupComponent.tsx

@walldenfilippa walldenfilippa added kind/product-feature Pull requests containing new features and removed kind/feature-request New feature or request labels May 7, 2026
Copy link
Copy Markdown
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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/layout/OrganisationLookup/OrganisationLookupComponent.tsx`:
- Around line 166-167: The submit and clear handlers (handleSubmit and
handleClear) still perform state mutations (e.g., calling setValue) even when
the component is read-only; add an explicit readOnly guard at the start of both
functions to return early if readOnly is true so mutations cannot occur,
mirroring the render/key gating already present (also update any helper
functions they call to respect readOnly if needed and ensure the Enter key
handler remains unchanged).

In `@src/layout/PersonLookup/PersonLookupComponent.tsx`:
- Around line 236-237: The Enter-key branch currently checks readOnly before
calling handleSubmit, but the mutation handlers themselves must defensively
refuse to mutate state; add an early guard at the start of handleSubmit and
handleClear (and any other mutation functions that call setValue) that returns
immediately if readOnly is true to prevent async/stale paths from performing
setValue or other writes; reference the handleSubmit and handleClear functions
and any places where setValue is invoked and ensure they check the readOnly flag
before mutating.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 90ad5b5d-84de-4b4e-8d9a-040cac832a27

📥 Commits

Reviewing files that changed from the base of the PR and between c56a11d and 6591527.

📒 Files selected for processing (2)
  • src/layout/OrganisationLookup/OrganisationLookupComponent.tsx
  • src/layout/PersonLookup/PersonLookupComponent.tsx

Comment on lines +166 to 167
if (ev.key === 'Enter' && !readOnly) {
await handleSubmit();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Mirror read-only enforcement inside submit/clear handlers.

The new render/key gating is correct, but handleSubmit/handleClear still allow setValue writes if called while read-only. Add explicit guards in those handlers so read-only is enforced at the mutation boundary too.

Suggested patch
@@
   async function handleSubmit() {
+    if (readOnly) {
+      return;
+    }
     const isValid = handleValidateOrgnr(tempOrgNr);
@@
     const { data } = await performLookup();
+    if (readOnly) {
+      return;
+    }
     if (data?.org) {
@@
   function handleClear() {
+    if (readOnly) {
+      return;
+    }
     setValue('organisation_lookup_orgnr', '');

Also applies to: 180-200

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/layout/OrganisationLookup/OrganisationLookupComponent.tsx` around lines
166 - 167, The submit and clear handlers (handleSubmit and handleClear) still
perform state mutations (e.g., calling setValue) even when the component is
read-only; add an explicit readOnly guard at the start of both functions to
return early if readOnly is true so mutations cannot occur, mirroring the
render/key gating already present (also update any helper functions they call to
respect readOnly if needed and ensure the Enter key handler remains unchanged).

Comment on lines +236 to 237
if (ev.key === 'Enter' && !readOnly) {
await handleSubmit();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Add hard read-only guards in mutation handlers, not only in UI triggers.

Good UI gating, but handleSubmit/handleClear can still write with setValue if invoked while readOnly is true (including async/stale paths). Add defensive guards inside those handlers to enforce no data mutation in read-only mode.

Suggested patch
@@
   async function handleSubmit() {
+    if (readOnly) {
+      return;
+    }
     const isNameValid = handleValidateName(tempName);
@@
     const { data } = await performLookup();
+    if (readOnly) {
+      return;
+    }
     if (data?.person) {
@@
   function handleClear() {
+    if (readOnly) {
+      return;
+    }
     if (dataModelBindings.person_lookup_ssn) {
       setValue('person_lookup_ssn', '');

Also applies to: 290-291, 308-328

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/layout/PersonLookup/PersonLookupComponent.tsx` around lines 236 - 237,
The Enter-key branch currently checks readOnly before calling handleSubmit, but
the mutation handlers themselves must defensively refuse to mutate state; add an
early guard at the start of handleSubmit and handleClear (and any other mutation
functions that call setValue) that returns immediately if readOnly is true to
prevent async/stale paths from performing setValue or other writes; reference
the handleSubmit and handleClear functions and any places where setValue is
invoked and ensure they check the readOnly flag before mutating.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 7, 2026

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

Labels

backport-ignore This PR is a new feature and should not be cherry-picked onto release branches kind/product-feature Pull requests containing new features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PersonLookup component does not honor readOnly property

1 participant