Skip to content

Commit d973713

Browse files
committed
Disable state field for countries without states
Modify the address form logic to accommodate countries without associated states. The state select field is now automatically disabled when a country with no states is selected. Additionally, this ensure state select field is disabled for preloaded selected country without states.
1 parent b563b62 commit d973713

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

admin/app/components/solidus_admin/orders/show/component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def format_address(address)
2121
address.address2,
2222
address.city,
2323
address.zipcode,
24-
address.state.name,
24+
address.state&.name,
2525
tag.br,
2626
address.country.name,
2727
tag.br,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
:state_id,
2626
state_options,
2727
value: @form.object.try(:state_id),
28+
disabled: @form.object.country&.states&.empty?,
2829
"data-#{stimulus_id}-target": "state"
2930
) %>
3031

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ export default class extends Controller {
1818

1919
stateSelect.innerHTML = ""
2020

21-
data.forEach(state => {
22-
const option = document.createElement("option")
21+
if (data.length === 0) {
22+
stateSelect.disabled = true
23+
} else {
24+
stateSelect.disabled = false
2325

24-
option.value = state.id
25-
option.innerText = state.name
26-
stateSelect.appendChild(option)
27-
})
26+
data.forEach((state) => {
27+
const option = document.createElement("option")
28+
option.value = state.id
29+
option.innerText = state.name
30+
stateSelect.appendChild(option)
31+
})
32+
}
2833
}
2934
}

0 commit comments

Comments
 (0)