Skip to content

Commit

Permalink
Handle states_required? in admin address component
Browse files Browse the repository at this point in the history
Previously, this component would just disable the state_id field when
the selected country did not require states, but this meant that the
user would never be able to self-fill the state_name field, which is
something that some stores can require with the
`Spree::Config[:address_requires_state]` preference.

The component now takes this into account and will swap between a
state_id dropdown select field, or a blank state_name text input
depending on the country the user has selected.

This brings this new admin component up to par with the previous backend
address forms.
  • Loading branch information
MadelineCollier committed Oct 11, 2024
1 parent 489e527 commit 5de4878
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,33 @@
"data-action": "change->#{stimulus_id}#loadStates"
) %>

<%= render component("ui/forms/field").select(
@name,
:state_id,
state_options,
object: @address,
value: @address.try(:state_id),
disabled: @address.country&.states&.empty?,
"data-#{stimulus_id}-target": "state"
) %>
<%= content_tag(:div,
data: { "#{stimulus_id}-target": "stateNameWrapper" },
class: (@address.country&.states&.empty? ? "flex flex-col gap-2 w-full" : "hidden flex flex-col gap-2 w-full")
) do %>
<%= render component("ui/forms/field").text_field(
@name,
:state_name,
object: @address,
value: @address.try(:state_name),
"data-#{stimulus_id}-target": "stateName"
) %>
<% end %>
<input autocomplete="off" type="hidden" name=<%= "#{@name}[state_id]" %>>

<%= content_tag(:div,
data: { "#{stimulus_id}-target": "stateWrapper" },
class: (@address.country&.states&.empty? ? "hidden flex flex-col gap-2 w-full" : "flex flex-col gap-2 w-full")
) do %>
<%= render component("ui/forms/field").select(
@name,
:state_id,
state_options,
object: @address,
value: @address.try(:state_id),
"data-#{stimulus_id}-target": "state"
) %>
<% end %>

<%= render component("ui/forms/field").text_field(@name, :phone, object: @address) %>
</div>
Expand Down
51 changes: 38 additions & 13 deletions admin/app/components/solidus_admin/ui/forms/address/component.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
static targets = ["country", "state"]
static targets = ["country", "state", "stateName", "stateWrapper", "stateNameWrapper"]

loadStates() {
const countryId = this.countryTarget.value
Expand All @@ -13,22 +13,47 @@ export default class extends Controller {
})
}

updateStateOptions(data) {
updateStateOptions(states) {
if (states.length === 0) {
// Show state name text input if no states to choose from.
this.toggleStateFields(false)
} else {
// Show state select dropdown.
this.toggleStateFields(true)
this.populateStateSelect(states)
}
}

toggleStateFields(showSelect) {
const stateWrapper = this.stateWrapperTarget
const stateNameWrapper = this.stateNameWrapperTarget
const stateSelect = this.stateTarget
const stateName = this.stateNameTarget

stateSelect.innerHTML = ""

if (data.length === 0) {
stateSelect.disabled = true
} else {
if (showSelect) {
// Show state select dropdown.
stateSelect.disabled = false

data.forEach((state) => {
const option = document.createElement("option")
option.value = state.id
option.innerText = state.name
stateSelect.appendChild(option)
})
stateName.value = ""
stateWrapper.classList.remove('hidden')
stateNameWrapper.classList.add('hidden')
} else {
// Show state name text input if no states to choose from.
stateSelect.disabled = true
stateWrapper.classList.add("hidden")
stateNameWrapper.classList.remove("hidden")
}
}

populateStateSelect(states) {
const stateSelect = this.stateTarget
stateSelect.innerHTML = ""

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

0 comments on commit 5de4878

Please sign in to comment.