Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Use form builder in more places in admin (#13290)
Damian Legawiec18f5f8039e5d
diff --git a/admin/app/models/spree/admin/form_builder.rb b/admin/app/models/spree/admin/form_builder.rbindex a77d63befb..21e7ae0d46 100644--- a/admin/app/models/spree/admin/form_builder.rb+++ b/admin/app/models/spree/admin/form_builder.rb@@ -24,12 +24,17 @@ module Spree# - :required [Boolean] whether field is required# - :help [String] help text to display below the field# - :help_bubble [String] help bubble text+ # - :prepend [String] text to prepend before the input field+ # - :append [String] text to append after the input field# @return [String] HTML string containing the complete form groupdef spree_text_field(method, options = {})options[:class] ||= 'form-control'+ prepend = options.delete(:prepend)+ append = options.delete(:append)+@template.content_tag(:div, class: 'form-group') do@template.label(@object_name, method, get_label(method, options)) +- @template.text_field(@object_name, method, objectify_options(options)) ++ wrap_with_input_group(@template.text_field(@object_name, method, objectify_options(options)), prepend, append) +@template.error_message_on(@object_name, method) + spree_field_help(method, options.merge(class: 'form-text mt-2'))endend@@ -41,9 +46,12 @@ module Spree# @return [String] HTML string containing the complete form groupdef spree_number_field(method, options = {})options[:class] ||= 'form-control'+ prepend = options.delete(:prepend)+ append = options.delete(:append)+@template.content_tag(:div, class: 'form-group') do@template.label(@object_name, method, get_label(method, options)) +- @template.number_field(@object_name, method, objectify_options(options)) ++ wrap_with_input_group(@template.number_field(@object_name, method, objectify_options(options)), prepend, append) +@template.error_message_on(@object_name, method) + spree_field_help(method, options.merge(class: 'form-text mt-2'))endend@@ -266,6 +274,36 @@ module Spree@template.raw(translated_label + required_label + help_bubble)end++ # Wrap a field with an input group if prepend or append is specified+ #+ # @param field_html [String] the HTML for the field+ # @param prepend [String, nil] text to prepend before the input field+ # @param append [String, nil] text to append after the input field+ # @return [String] HTML string with input group wrapper or the original field+ def wrap_with_input_group(field_html, prepend = nil, append = nil)+ return field_html if prepend.nil? && append.nil?++ @template.content_tag(:div, class: 'input-group') do+ prepend_html = if prepend.present?+ @template.content_tag(:div, class: 'input-group-prepend') do+ @template.content_tag(:span, prepend, class: 'input-group-text')+ end+ else+ ''.html_safe+ end++ append_html = if append.present?+ @template.content_tag(:div, class: 'input-group-append') do+ @template.content_tag(:span, append, class: 'input-group-text')+ end+ else+ ''.html_safe+ end++ prepend_html + field_html + append_html+ end+ endendendenddiff --git a/admin/app/views/spree/admin/custom_domains/_form.html.erb b/admin/app/views/spree/admin/custom_domains/_form.html.erbindex 6bd150a36d..7e7d2d3ad2 100644--- a/admin/app/views/spree/admin/custom_domains/_form.html.erb+++ b/admin/app/views/spree/admin/custom_domains/_form.html.erb@@ -1,19 +1,7 @@<div class="card mb-3"><div class="card-body">- <div class="form-group">- <%= f.label :url, Spree.t(:domain) %>- <div class="form-control d-flex align-items-center py-0 focus-shadow focus-border">- <span class="text-muted">https://</span>- <%= f.text_field :url, class: 'form-control-plaintext pr-0 pl-1', autofocus: f.object.new_record?, required: true %>- </div>- <%= f.error_message_on :url %>- </div>+ <%= f.spree_text_field :url, label: Spree.t(:domain), prepend: 'https://', autofocus: f.object.new_record?, required: true %>- <div class="custom-control custom-checkbox">- <%= f.check_box :default, class: 'custom-control-input' %>- <%= f.label :default, class: 'custom-control-label font-weight-semibold' %>- <p class="form-text mb-0">We will use this domain in emails to customers.</p>- <%= f.error_message_on :default %>- </div>+ <%= f.spree_check_box :default, help: 'We will use this domain in emails to customers.' %></div></div>diff --git a/admin/app/views/spree/admin/page_links/_form.html.erb b/admin/app/views/spree/admin/page_links/_form.html.erbindex 1d99feefd7..5ed0135a9c 100644--- a/admin/app/views/spree/admin/page_links/_form.html.erb+++ b/admin/app/views/spree/admin/page_links/_form.html.erb@@ -1,19 +1,10 @@<% data_action = (@page_link.persisted? && !@page_link.parent.is_a?(Spree::Store) ? { action: 'blur->auto-submit#submit' } : {}) %>-<div class="form-group">- <%= f.label :linkable_type, 'Link' %>- <%= f.select :linkable_type, @page_link.parent.allowed_linkable_types, { include_blank: false }, { class: 'custom-select mb-3', data: { action: 'auto-submit#submit' } } %>- <%= render 'spree/admin/page_links/linkable_type_dropdown', page_link: @page_link, form_name: 'page_link' %>-</div>+<%= f.spree_select :linkable_type, @page_link.parent.allowed_linkable_types, { label: 'Link', include_blank: false }, { class: 'custom-select mb-3', data: { action: 'auto-submit#submit' } } %>+<%= render 'spree/admin/page_links/linkable_type_dropdown', page_link: @page_link, form_name: 'page_link' %><hr class="my-4" />-<div class="form-group">- <%= f.label :label, Spree.t(:name) %>- <%= f.text_field :label, class: 'form-control', data: data_action, required: true %>-</div>+<%= f.spree_text_field :label, label: Spree.t(:name), data: data_action, required: true %>-<div class="custom-control custom-checkbox">- <%= f.check_box :open_in_new_tab, class: 'custom-control-input', data: data_action %>- <%= f.label :open_in_new_tab, class: 'custom-control-label' %>-</div>+<%= f.spree_check_box :open_in_new_tab, data: data_action %>diff --git a/admin/app/views/spree/admin/promotions/form/_settings.html.erb b/admin/app/views/spree/admin/promotions/form/_settings.html.erbindex 6c93c2b980..36a445dc31 100644--- a/admin/app/views/spree/admin/promotions/form/_settings.html.erb+++ b/admin/app/views/spree/admin/promotions/form/_settings.html.erb@@ -2,17 +2,6 @@<%= f.spree_number_field :usage_limit, min: 0, step: 1, help: 'Leave this field blank for unlimited usage.' %><% end %>-<div class="form-group">- <%= f.label :starts_at %>- <%= f.datetime_field :starts_at, class: 'form-control' %>- <%= f.error_message_on :starts_at %>-</div>+<%= f.spree_datetime_field :starts_at %>-<div class="form-group">- <%= f.label :expires_at %>- <%= f.datetime_field :expires_at, class: 'form-control' %>- <%= f.error_message_on :expires_at %>- <span class="form-text mt-2">- Leave this field blank for no expiration.- </span>-</div>+<%= f.spree_datetime_field :expires_at, help: 'Leave this field blank for no expiration.' %>diff --git a/admin/app/views/spree/admin/refunds/_form.html.erb b/admin/app/views/spree/admin/refunds/_form.html.erbindex d6732a5211..055ea3a742 100644--- a/admin/app/views/spree/admin/refunds/_form.html.erb+++ b/admin/app/views/spree/admin/refunds/_form.html.erb@@ -1,13 +1,5 @@<% if f.object.new_record? %>- <div class="form-group">- <%= f.label :amount, Spree.t(:amount) %>- <div class="input-group">- <%= f.text_field :amount, class: 'form-control', required: true %>- <div class="input-group-append">- <span class="input-group-text"><%= currency_symbol(@order.currency) %></span>- </div>- </div>- </div>+ <%= f.spree_text_field :amount, label: Spree.t(:amount), append: currency_symbol(@order.currency), required: true %><% end %><%= f.spree_collection_select :refund_reason_id, refund_reasons, :id, :name, { include_blank: true, label: Spree.t(:reason), required: true } %>\ No newline at end of filediff --git a/admin/app/views/spree/admin/tax_rates/_form.html.erb b/admin/app/views/spree/admin/tax_rates/_form.html.erbindex 2bfac334a1..3c4bf0e229 100644--- a/admin/app/views/spree/admin/tax_rates/_form.html.erb+++ b/admin/app/views/spree/admin/tax_rates/_form.html.erb@@ -8,16 +8,7 @@<div class="card-body"><%= f.spree_text_field :name, required: true %>- <div class="form-group">- <%= f.label :amount_percentage, Spree.t(:rate) %>- <div class="input-group">- <%= f.number_field :amount_percentage, class: 'form-control', min: 0, step: 0.1, required: true %>- <div class="input-group-append">- <span class="input-group-text">%</span>- </div>- </div>- <%= f.error_message_on :amount %>- </div>+ <%= f.spree_number_field :amount_percentage, label: Spree.t(:rate), append: '%', min: 0, step: 0.1, required: true %><%= f.spree_collection_select :zone_id, @available_zones, :id, :name, label: Spree.t(:zone) %><%= f.spree_collection_select :tax_category_id, @available_categories, :id, :name, label: Spree.t(:tax_category) %>diff --git a/admin/app/views/spree/admin/translations/translation_rows/_permalink_field_row.html.erb b/admin/app/views/spree/admin/translations/translation_rows/_permalink_field_row.html.erbindex adf3c0552c..8344c00b72 100644--- a/admin/app/views/spree/admin/translations/translation_rows/_permalink_field_row.html.erb+++ b/admin/app/views/spree/admin/translations/translation_rows/_permalink_field_row.html.erb@@ -10,15 +10,4 @@translated_value = f.object.send("#{field}_#{locale}")&.delete_prefix(base_permalink_translated)%>-<div class="form-group">- <%= f.label field, Spree.t(field) %>- <div class="input-group">- <% if base_permalink_translated.present? %>- <div class="input-group-prepend">- <span class="input-group-text"><%= base_permalink_translated %></span>- </div>- <% end %>-- <%= f.text_field "#{field}_#{locale}", value: translated_value, class: 'form-control' %>- </div>-</div>+<%= f.spree_text_field "#{field}_#{locale}", label: Spree.t(field), value: translated_value, prepend: base_permalink_translated.presence %>diff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex 1248def54e..775cfb34f5 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -105,6 +105,8 @@ en:phone: Shipping address phonestate: Shipping address statezipcode: Shipping address zipcode+ spree/page_link:+ open_in_new_tab: Open in new tabspree/payment:amount: Amountnumber: Number