Skip to content

Commit c78a9de

Browse files
Merge pull request #5871 from MadelineCollier/admin-user-creation-modification
[Admin] Handle states_required? in admin address component
2 parents 7593683 + d46369f commit c78a9de

File tree

2 files changed

+65
-22
lines changed

2 files changed

+65
-22
lines changed

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,33 @@
2121
"data-action": "change->#{stimulus_id}#loadStates"
2222
) %>
2323

24-
<%= render component("ui/forms/field").select(
25-
@name,
26-
:state_id,
27-
state_options,
28-
object: @address,
29-
value: @address.try(:state_id),
30-
disabled: @address.country&.states&.empty?,
31-
"data-#{stimulus_id}-target": "state"
32-
) %>
24+
<%= content_tag(:div,
25+
data: { "#{stimulus_id}-target": "stateNameWrapper" },
26+
class: (@address.country&.states&.empty? ? "flex flex-col gap-2 w-full" : "hidden flex flex-col gap-2 w-full")
27+
) do %>
28+
<%= render component("ui/forms/field").text_field(
29+
@name,
30+
:state_name,
31+
object: @address,
32+
value: @address.try(:state_name),
33+
"data-#{stimulus_id}-target": "stateName"
34+
) %>
35+
<% end %>
36+
<input autocomplete="off" type="hidden" name=<%= "#{@name}[state_id]" %>>
37+
38+
<%= content_tag(:div,
39+
data: { "#{stimulus_id}-target": "stateWrapper" },
40+
class: (@address.country&.states&.empty? ? "hidden flex flex-col gap-2 w-full" : "flex flex-col gap-2 w-full")
41+
) do %>
42+
<%= render component("ui/forms/field").select(
43+
@name,
44+
:state_id,
45+
state_options,
46+
object: @address,
47+
value: @address.try(:state_id),
48+
"data-#{stimulus_id}-target": "state"
49+
) %>
50+
<% end %>
3351

3452
<%= render component("ui/forms/field").text_field(@name, :phone, object: @address) %>
3553
</div>
Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Controller } from '@hotwired/stimulus'
22

33
export default class extends Controller {
4-
static targets = ["country", "state"]
4+
static targets = ["country", "state", "stateName", "stateWrapper", "stateNameWrapper"]
55

66
loadStates() {
77
const countryId = this.countryTarget.value
@@ -13,22 +13,47 @@ export default class extends Controller {
1313
})
1414
}
1515

16-
updateStateOptions(data) {
16+
updateStateOptions(states) {
17+
if (states.length === 0) {
18+
// Show state name text input if no states to choose from.
19+
this.toggleStateFields(false)
20+
} else {
21+
// Show state select dropdown.
22+
this.toggleStateFields(true)
23+
this.populateStateSelect(states)
24+
}
25+
}
26+
27+
toggleStateFields(showSelect) {
28+
const stateWrapper = this.stateWrapperTarget
29+
const stateNameWrapper = this.stateNameWrapperTarget
1730
const stateSelect = this.stateTarget
31+
const stateName = this.stateNameTarget
1832

19-
stateSelect.innerHTML = ""
2033

21-
if (data.length === 0) {
22-
stateSelect.disabled = true
23-
} else {
34+
if (showSelect) {
35+
// Show state select dropdown.
2436
stateSelect.disabled = false
25-
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-
})
37+
stateName.value = ""
38+
stateWrapper.classList.remove('hidden')
39+
stateNameWrapper.classList.add('hidden')
40+
} else {
41+
// Show state name text input if no states to choose from.
42+
stateSelect.disabled = true
43+
stateWrapper.classList.add("hidden")
44+
stateNameWrapper.classList.remove("hidden")
3245
}
3346
}
47+
48+
populateStateSelect(states) {
49+
const stateSelect = this.stateTarget
50+
stateSelect.innerHTML = ""
51+
52+
states.forEach((state) => {
53+
const option = document.createElement("option")
54+
option.value = state.id
55+
option.innerText = state.name
56+
stateSelect.appendChild(option)
57+
})
58+
}
3459
}

0 commit comments

Comments
 (0)