Skip to content

Commit

Permalink
Disable state field for countries without states
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rainerdema committed Nov 8, 2023
1 parent b58646b commit 2dd113b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def format_address(address)
address.address2,
address.city,
address.zipcode,
address.state.name,
address.state&.name,
tag.br,
address.country.name,
tag.br,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
:state_id,
state_options,
value: @form.object.try(:state_id),
disabled: @form.object.country&.states&.empty?,
"data-#{stimulus_id}-target": "state"
) %>

Expand Down
17 changes: 11 additions & 6 deletions admin/app/components/solidus_admin/ui/forms/address/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ export default class extends Controller {

stateSelect.innerHTML = ""

data.forEach(state => {
const option = document.createElement("option")
if (data.length === 0) {
stateSelect.disabled = true
} else {
stateSelect.disabled = false

option.value = state.id
option.innerText = state.name
stateSelect.appendChild(option)
})
data.forEach((state) => {
const option = document.createElement("option")
option.value = state.id
option.innerText = state.name
stateSelect.appendChild(option)
})
}
}
}

0 comments on commit 2dd113b

Please sign in to comment.