From 17066eef199c0ce19cbc6147d9bbcca23c96d4d3 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Thu, 24 Apr 2025 10:04:16 +0200 Subject: [PATCH 1/2] Update custom select to allow empty option `choices` param is now forced to be an Array, as it will be easier to include blank option in an array. Previously it implicitly allowed it to be html safe string containing markup for options as well, but using arrays is a lot easier. --- .../solidus_admin/ui/forms/select/component.rb | 10 ++++++++++ .../solidus_admin/web_components/solidus_select.js | 1 + .../solidus_admin/ui/forms/select/component_preview.rb | 10 +++++++--- .../forms/select/component_preview/overview.html.erb | 4 ++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/admin/app/components/solidus_admin/ui/forms/select/component.rb b/admin/app/components/solidus_admin/ui/forms/select/component.rb index a7aaae408e2..349e2429d2a 100644 --- a/admin/app/components/solidus_admin/ui/forms/select/component.rb +++ b/admin/app/components/solidus_admin/ui/forms/select/component.rb @@ -50,6 +50,9 @@ class SolidusAdmin::UI::Forms::Select::Component < SolidusAdmin::BaseComponent # loading next page of results. Default: "Loading more results". # @option attributes [String] :"data-no-results-message" which text to show when there are no search results returned. # Default: "No results found". + # @option attributes [true, String] :include_blank if passed, an empty option will be prepended to the list of options. + # Pass +true+ for empty option with no text, or +String+ for the text to be shown as empty option. + # @raise [ArgumentError] if +choices+ is not an array def initialize(label:, name:, choices:, src: nil, size: :m, hint: nil, tip: nil, error: nil, **attributes) @label = label @hint = hint @@ -76,10 +79,17 @@ def before_render end def prepare_options(choices:, src:) + raise ArgumentError, "`choices` must be an array" unless choices.is_a?(Array) + if src.present? @attributes[:"data-src"] = src end + if (blank_option = @attributes.delete(:include_blank)) + blank_option = "" if blank_option == true + choices.unshift([blank_option, ""]) + end + @options_collection = options_for_select(choices, @attributes.delete(:value)) end diff --git a/admin/app/javascript/solidus_admin/web_components/solidus_select.js b/admin/app/javascript/solidus_admin/web_components/solidus_select.js index b6cff8680cd..5dc1a066927 100644 --- a/admin/app/javascript/solidus_admin/web_components/solidus_select.js +++ b/admin/app/javascript/solidus_admin/web_components/solidus_select.js @@ -44,6 +44,7 @@ class SolidusSelect extends HTMLSelectElement { dropdownContentClass: "dropdown-content", optionClass: "option", wrapperClass: "wrapper", + allowEmptyOption: true, maxOptions: null, refreshThrottle: 0, plugins: { diff --git a/admin/spec/components/previews/solidus_admin/ui/forms/select/component_preview.rb b/admin/spec/components/previews/solidus_admin/ui/forms/select/component_preview.rb index 02890b5b363..1f0bb73bdfb 100644 --- a/admin/spec/components/previews/solidus_admin/ui/forms/select/component_preview.rb +++ b/admin/spec/components/previews/solidus_admin/ui/forms/select/component_preview.rb @@ -39,12 +39,15 @@ def remote_with_pagination(multiple: false, latency: false, loading_message: nil # @param disabled toggle # @param error toggle # @param include_blank toggle + # @param blank_option text # @param placeholder text # @param hint text # @param tip text - def playground(size: "m", options: 3, multiple: false, selected: false, disabled: false, error: false, include_blank: true, placeholder: nil, hint: nil, tip: nil) + def playground(size: "m", options: 3, multiple: false, selected: false, disabled: false, error: false, include_blank: false, blank_option: nil, placeholder: nil, hint: nil, tip: nil) options = (1..options).map { |i| ["Option #{i}", i] } - options.unshift(["None", ""]) if include_blank + if include_blank && blank_option.present? + include_blank = blank_option + end render component("ui/forms/select").new( label: "Label", @@ -57,7 +60,8 @@ def playground(size: "m", options: 3, multiple: false, selected: false, disabled value: (multiple && [1, 2] || 1 if selected), multiple:, disabled:, - placeholder: + placeholder:, + include_blank: ) end end diff --git a/admin/spec/components/previews/solidus_admin/ui/forms/select/component_preview/overview.html.erb b/admin/spec/components/previews/solidus_admin/ui/forms/select/component_preview/overview.html.erb index ba544f1b1be..71b48288a94 100644 --- a/admin/spec/components/previews/solidus_admin/ui/forms/select/component_preview/overview.html.erb +++ b/admin/spec/components/previews/solidus_admin/ui/forms/select/component_preview/overview.html.erb @@ -3,7 +3,7 @@

Single

- <% default_params = { label: "Country", name: "country", choices: ["", "Denmark", "Sweden"] } %> + <% default_params = { label: "Country", name: "country", choices: %w[Denmark Sweden] } %>
Default
@@ -51,7 +51,7 @@

Multiple

- <% default_params = { label: "Countries", name: "countries", choices: ["", "Denmark", "Sweden", "United Kingdom"], multiple: true } %> + <% default_params = { label: "Countries", name: "countries", choices: ["Denmark", "Sweden", "United Kingdom"], multiple: true } %>
Default
From 9e32dade7d4063a8d0d1d8693338694ec17ec219 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Mon, 12 May 2025 17:52:00 +0200 Subject: [PATCH 2/2] Fix usage of "ui/forms/select" in few components Following the change in UI select component, forcing `choices` to be an array, needed to change few store credit components passing their options as a string. Also removes blank option for currency selector, since there is a default selected value, and currency cannot be blank, so there is no reason to allow selecting a blank option there. --- .../users/store_credits/edit_amount/component.html.erb | 4 ++-- .../users/store_credits/edit_amount/component.rb | 5 ----- .../users/store_credits/edit_validity/component.html.erb | 4 ++-- .../users/store_credits/edit_validity/component.rb | 5 ----- .../users/store_credits/new/component.html.erb | 9 +++++---- .../solidus_admin/users/store_credits/new/component.rb | 9 --------- 6 files changed, 9 insertions(+), 27 deletions(-) diff --git a/admin/app/components/solidus_admin/users/store_credits/edit_amount/component.html.erb b/admin/app/components/solidus_admin/users/store_credits/edit_amount/component.html.erb index 61ff2d48d82..bcdb44d7299 100644 --- a/admin/app/components/solidus_admin/users/store_credits/edit_amount/component.html.erb +++ b/admin/app/components/solidus_admin/users/store_credits/edit_amount/component.html.erb @@ -6,8 +6,8 @@ <%= render component("ui/forms/field").select( f, :store_credit_reason_id, - store_credit_reasons_select_options.html_safe, - include_blank: t('spree.choose_reason'), + @store_credit_reasons.map { [_1.name, _1.id] }, + include_blank: t('.choose_reason'), html: { required: true } ) %>
diff --git a/admin/app/components/solidus_admin/users/store_credits/edit_amount/component.rb b/admin/app/components/solidus_admin/users/store_credits/edit_amount/component.rb index 06a61d8c318..6abdec0d017 100644 --- a/admin/app/components/solidus_admin/users/store_credits/edit_amount/component.rb +++ b/admin/app/components/solidus_admin/users/store_credits/edit_amount/component.rb @@ -14,9 +14,4 @@ def form_id def form_url solidus_admin.update_amount_user_store_credit_path(@user, @store_credit, **search_filter_params) end - - def store_credit_reasons_select_options - # Placeholder + Store Credit Reasons - "" + options_from_collection_for_select(@store_credit_reasons, :id, :name) - end end diff --git a/admin/app/components/solidus_admin/users/store_credits/edit_validity/component.html.erb b/admin/app/components/solidus_admin/users/store_credits/edit_validity/component.html.erb index fd91a48fb88..051eb18c78d 100644 --- a/admin/app/components/solidus_admin/users/store_credits/edit_validity/component.html.erb +++ b/admin/app/components/solidus_admin/users/store_credits/edit_validity/component.html.erb @@ -5,8 +5,8 @@ <%= render component("ui/forms/field").select( f, :store_credit_reason_id, - store_credit_reasons_select_options.html_safe, - include_blank: t('spree.choose_reason'), + @store_credit_reasons.map { [_1.name, _1.id] }, + include_blank: t('.choose_reason'), html: { required: true } ) %>
diff --git a/admin/app/components/solidus_admin/users/store_credits/edit_validity/component.rb b/admin/app/components/solidus_admin/users/store_credits/edit_validity/component.rb index c1bacfc26d3..359b2150e45 100644 --- a/admin/app/components/solidus_admin/users/store_credits/edit_validity/component.rb +++ b/admin/app/components/solidus_admin/users/store_credits/edit_validity/component.rb @@ -14,9 +14,4 @@ def form_id def form_url solidus_admin.invalidate_user_store_credit_path(@user, @store_credit, **search_filter_params) end - - def store_credit_reasons_select_options - # Placeholder + Store Credit Reasons - "" + options_from_collection_for_select(@store_credit_reasons, :id, :name) - end end diff --git a/admin/app/components/solidus_admin/users/store_credits/new/component.html.erb b/admin/app/components/solidus_admin/users/store_credits/new/component.html.erb index df37ce9b238..e5cc0ed6623 100644 --- a/admin/app/components/solidus_admin/users/store_credits/new/component.html.erb +++ b/admin/app/components/solidus_admin/users/store_credits/new/component.html.erb @@ -6,15 +6,16 @@ <%= render component("ui/forms/field").select( f, :currency, - currency_select_options.html_safe, - include_blank: t("spree.currency"), + Spree::Config.available_currencies.map { [_1.iso_code, _1.iso_code] }, + placeholder: t("spree.currency"), + value: Spree::Config.currency, html: { required: true } ) %> <%= render component("ui/forms/field").select( f, :category_id, - store_credit_categories_select_options.html_safe, - include_blank: t("spree.category"), + @store_credit_categories.map { [_1.name, _1.id] }, + include_blank: t(".choose_category"), html: { required: true } ) %> <%= render component("ui/forms/field").text_field(f, :memo) %> diff --git a/admin/app/components/solidus_admin/users/store_credits/new/component.rb b/admin/app/components/solidus_admin/users/store_credits/new/component.rb index 5f3d0945d59..07bceccfcc9 100644 --- a/admin/app/components/solidus_admin/users/store_credits/new/component.rb +++ b/admin/app/components/solidus_admin/users/store_credits/new/component.rb @@ -10,13 +10,4 @@ def initialize(user:, store_credit:, categories:) def form_url solidus_admin.user_store_credits_path(@user, **search_filter_params) end - - def currency_select_options - options_from_collection_for_select(Spree::Config.available_currencies, :iso_code, :iso_code, Spree::Config.currency) - end - - def store_credit_categories_select_options - # Placeholder + Store Credit Categories - "" + options_from_collection_for_select(@store_credit_categories, :id, :name) - end end