Skip to content

Fix dropdown field change API to sync selection with model updates#309

Open
vivekjyani wants to merge 2 commits intomainfrom
NO-2072
Open

Fix dropdown field change API to sync selection with model updates#309
vivekjyani wants to merge 2 commits intomainfrom
NO-2072

Conversation

@vivekjyani
Copy link
Copy Markdown
Contributor

@vivekjyani vivekjyani commented Apr 14, 2026

Context / Spec

When a dropdown field value is updated externally via the change API (for example, programmatic model updates or mirrored-field updates), DropdownView could show stale selection state because its internal @State (selectedDropdownValueID) only captured the initial model value.

Ticket: NO-2072

What Changed

1) State sync fix in DropdownView
File: Sources/JoyfillUI/View/Fields/DropdownView.swift

Added an observer to sync internal selection state whenever dropdownDataModel.dropdownValue changes externally:

.onChange(of: dropdownDataModel.dropdownValue) { newValue in
    if selectedDropdownValueID != newValue {
        selectedDropdownValueID = newValue
    }
}

The guard prevents redundant writes and avoids re-triggering the existing onChange flow unnecessarily.

2) UI regression test for mirrored dropdown behavior
File: JoyfillSwiftUIExample/JoyfillUITests/ChangeUITests/OnChangeHandlerUITests.swift

Added testDropdownCoordinatess() (iPad-only) that:

  • navigates to the relevant page setup,
  • selects "No" in the left dropdown,
  • asserts the right dropdown reflects the same value.

This validates that external/model-driven dropdown updates are now reflected in UI state.

Design Decisions

  • Why .onChange on model value? It is the minimal fix that preserves current architecture while correctly re-syncing local @State on external updates.
  • Why guard equality before assignment? Prevents unnecessary state writes and avoids duplicate event propagation.
  • Why iPad-only test guard? The test scenario relies on the iPad-specific layout/navigation path used by existing change-handler UI tests.

Public API Impact

No public API changes. This is an internal state-sync and test coverage update.

Test Coverage

Added UI coverage via:

  • testDropdownCoordinatess() in OnChangeHandlerUITests

Reviewer Notes

  • Scope is focused: 2 files changed (runtime fix + regression test).
  • Fix addresses a SwiftUI state-sync edge case where @State initialized in init does not automatically track subsequent model updates.

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.

1 participant