Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin] Handle states_required? in admin address component #5871

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}
}
Loading