Skip to content

Commit abb6ffe

Browse files
committed
Implement server-Side state preloading for address form
Enhance the address form's state field by ensuring the correct state is selected during asynchronous loading. This adjustment addresses a timing issue where Stimulus did not populate the state select promptly, leading to mismatched selections.
1 parent 045017d commit abb6ffe

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

admin/app/components/solidus_admin/ui/forms/address/component.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<%= render component("ui/forms/field").select(
2424
@form,
2525
:state_id,
26-
[],
26+
state_options,
2727
value: @form.object.try(:state_id),
2828
"data-#{stimulus_id}-target": "state"
2929
) %>

admin/app/components/solidus_admin/ui/forms/address/component.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import { Controller } from '@hotwired/stimulus'
33
export default class extends Controller {
44
static targets = ["country", "state"]
55

6-
connect() {
7-
this.loadStates()
8-
}
9-
106
loadStates() {
117
const countryId = this.countryTarget.value
128

admin/app/components/solidus_admin/ui/forms/address/component.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ def initialize(form:, disabled: false)
55
@form = form
66
@disabled = disabled
77
end
8+
9+
def state_options
10+
return [] unless @form.object.country
11+
@form.object.country.states.map { |s| [s.name, s.id] }
12+
end
813
end

0 commit comments

Comments
 (0)