-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5468 from nebulab/rainerd/admin/add-ui-forms-addr…
…ess-component [Admin] Introduce `ui/forms/address` component for order admin checkout
- Loading branch information
Showing
9 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
admin/app/components/solidus_admin/ui/forms/address/component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<fieldset class="<%= stimulus_id %>" | ||
data-controller="<%= stimulus_id %>" | ||
<%= :disabled if @disabled %> | ||
> | ||
<div class="<%= stimulus_id %>--address-form flex flex-wrap gap-4 pb-4"> | ||
<%= render component("ui/forms/field").text_field(@form, :name) %> | ||
<%= render component("ui/forms/field").text_field(@form, :address1) %> | ||
<%= render component("ui/forms/field").text_field(@form, :address2) %> | ||
<div class="flex w-full space-x-4"> | ||
<%= render component("ui/forms/field").text_field(@form, :city, class: "flex-1") %> | ||
<%= render component("ui/forms/field").text_field(@form, :zipcode, class: "flex-1") %> | ||
</div> | ||
|
||
<%= render component("ui/forms/field").select( | ||
@form, | ||
:country_id, | ||
Spree::Country.all.map { |c| [c.name, c.id] }, | ||
value: @form.object.try(:country_id), | ||
"data-#{stimulus_id}-target": "country", | ||
"data-action": "change->#{stimulus_id}#loadStates" | ||
) %> | ||
|
||
<%= render component("ui/forms/field").select( | ||
@form, | ||
:state_id, | ||
[], | ||
value: @form.object.try(:state_id), | ||
"data-#{stimulus_id}-target": "state" | ||
) %> | ||
|
||
<%= render component("ui/forms/field").text_field(@form, :phone) %> | ||
</div> | ||
</fieldset> |
33 changes: 33 additions & 0 deletions
33
admin/app/components/solidus_admin/ui/forms/address/component.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Controller } from '@hotwired/stimulus' | ||
|
||
export default class extends Controller { | ||
static targets = ["country", "state"] | ||
|
||
connect() { | ||
this.loadStates() | ||
} | ||
|
||
loadStates() { | ||
const countryId = this.countryTarget.value | ||
|
||
fetch(`/admin/countries/${countryId}/states`) | ||
.then(response => response.json()) | ||
.then(data => { | ||
this.updateStateOptions(data) | ||
}) | ||
} | ||
|
||
updateStateOptions(data) { | ||
const stateSelect = this.stateTarget | ||
|
||
stateSelect.innerHTML = "" | ||
|
||
data.forEach(state => { | ||
const option = document.createElement("option") | ||
|
||
option.value = state.id | ||
option.innerText = state.name | ||
stateSelect.appendChild(option) | ||
}) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
admin/app/components/solidus_admin/ui/forms/address/component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::UI::Forms::Address::Component < SolidusAdmin::BaseComponent | ||
def initialize(form:, disabled: false) | ||
@form = form | ||
@disabled = disabled | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
admin/app/controllers/solidus_admin/countries_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin | ||
class CountriesController < SolidusAdmin::BaseController | ||
skip_before_action :authorize_solidus_admin_user! | ||
|
||
def states | ||
@states = Spree::State.where(country_id: params[:country_id]) | ||
render json: @states.select(:id, :name) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
admin/spec/components/previews/solidus_admin/ui/forms/address/component_preview.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
# @component "ui/forms/address" | ||
class SolidusAdmin::UI::Forms::Address::ComponentPreview < ViewComponent::Preview | ||
include SolidusAdmin::Preview | ||
|
||
def overview | ||
render_with_template | ||
end | ||
|
||
# @param disabled toggle | ||
def playground(disabled: false) | ||
view = ActionView::Base.new(ActionView::LookupContext.new([]), {}, nil) | ||
render component("ui/forms/address").new( | ||
form: ActionView::Helpers::FormBuilder.new(:address, Spree::Address.new, view, {}), | ||
disabled: disabled | ||
) | ||
end | ||
end |
3 changes: 3 additions & 0 deletions
3
...ec/components/previews/solidus_admin/ui/forms/address/component_preview/overview.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<%= form_for Spree::Address.new, url: '#' do |f| %> | ||
<%= render current_component.new(form: f, disabled: false) %> | ||
<% end %> |
9 changes: 9 additions & 0 deletions
9
admin/spec/components/solidus_admin/ui/forms/address/component_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe SolidusAdmin::UI::Forms::Address::Component, type: :component do | ||
it "renders the overview preview" do | ||
render_preview(:overview) | ||
end | ||
end |