Skip to content

fix: sort credential fields by display_order before rendering#339

Closed
emmettownsend wants to merge 3 commits into
mxenabled:masterfrom
inrupt:fix/sort-credentials-by-display-order
Closed

fix: sort credential fields by display_order before rendering#339
emmettownsend wants to merge 3 commits into
mxenabled:masterfrom
inrupt:fix/sort-credentials-by-display-order

Conversation

@emmettownsend

Copy link
Copy Markdown

Summary

When connecting to MX Bank (test institution), the password field renders above the username field. This happens because the Credentials component renders fields in whatever order the API returns them, without sorting by display_order.

Changes

  • Sort credentials by display_order in Credentials.js before rendering, building form schema, and building initial values
  • Handles missing display_order gracefully (defaults to 0)
  • The fix is in the shared Credentials component so both CreateMemberForm and UpdateMemberForm benefit
  • Added a test verifying that credentials render in display_order regardless of API response order

Root cause

Credentials.js uses credentials.map(...) directly for rendering (line 446), buildInitialValues (line 115), buildFormSchema (line 116), and attemptConnect (line 261) — all without sorting by display_order. When the MX Platform API returns credentials with password before username in the array, the widget displays them in that order.

Fix

const sortedCredentials = [...credentials].sort(
  (a, b) => (a.display_order ?? 0) - (b.display_order ?? 0),
)

All references to credentials within the component now use sortedCredentials.

Test plan

  • Added test: credentials with reversed API order render correctly by display_order
  • Manual verification with MX Bank (test institution) — password/username should now appear in correct order

Copilot AI review requested due to automatic review settings June 10, 2026 18:05
The Credentials component renders fields in the order returned by the
API without sorting by display_order. When the API returns password
before username (as observed with MX Bank), users see password first
— a confusing UX. This sorts credentials by display_order so fields
render in the intended sequence regardless of API response ordering.
@emmettownsend emmettownsend force-pushed the fix/sort-credentials-by-display-order branch from 14036db to 1adbc27 Compare June 10, 2026 18:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR ensures credential fields render in display_order rather than relying on the API response order, and adds a regression test to lock the behavior in.

Changes:

  • Sort credentials by display_order before building form schema/values, rendering fields, focusing errored inputs, and building the connect payload.
  • Add a test covering rendering order independence from API ordering.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/views/credentials/Credentials.js Sort credentials by display_order and use the sorted list consistently across the component.
src/views/credentials/tests/Credentials-test.tsx Add a regression test asserting credential render order respects display_order.

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

Comment thread src/views/credentials/Credentials.js Outdated
Comment on lines +115 to +117
const sortedCredentials = [...credentials].sort(
(a, b) => (a.display_order ?? 0) - (b.display_order ?? 0),
)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch — changed the default to Infinity so credentials without display_order sort to the end. Skipped the index tie-breaker since this is a 2-field login form in practice.

Comment thread src/views/credentials/Credentials.js Outdated
Comment on lines +115 to +117
const sortedCredentials = [...credentials].sort(
(a, b) => (a.display_order ?? 0) - (b.display_order ?? 0),
)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Sorting 2 items on each render is effectively free — adding useMemo here would be more code for no measurable benefit.


useEffect(() => {
for (const field of credentials) {
for (const field of sortedCredentials) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The original code had the same pattern — credentials was used in this effect without being in the dependency array. This change only swapped the variable name; the dependency array behaviour is pre-existing.

@@ -258,7 +261,7 @@ export const Credentials = React.forwardRef(
}, [errors])

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Same as above — pre-existing dependency array pattern, not introduced by this change.

Comment on lines +156 to +157
const inputs = await screen.findAllByRole('textbox')
expect(inputs[0]).toHaveAccessibleName(/Username/i)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Agreed — updated the test to query fields by their accessible labels and assert relative DOM position instead of relying on array indexing.

…tions

- Use Infinity instead of 0 for missing display_order so credentials
  without the field sort to the end rather than the top
- Query test fields by accessible label and assert DOM order instead
  of relying on array indexing of all textbox roles
- Wrap sortedCredentials in useMemo to match codebase conventions
- Format test file to stay within line length limits
- Add test for credentials with missing display_order falling to end
@wesrisenmay-mx

Copy link
Copy Markdown
Collaborator

@emmettownsend how are you using this package?

@emmettownsend

emmettownsend commented Jun 11, 2026 via email

Copy link
Copy Markdown
Author

@wesrisenmay-mx

Copy link
Copy Markdown
Collaborator

@emmettownsend Could you give it another go? I believe this was a configuration problem on our MX Bank Institution within the int environment.

@emmettownsend

Copy link
Copy Markdown
Author

@emmettownsend Could you give it another go? I believe this was a configuration problem on our MX Bank Institution within the int environment.

All good now. Thanks.

@wesrisenmay-mx

Copy link
Copy Markdown
Collaborator

Fixed via configuration

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.

3 participants