Skip to content

Commit bbca5cc

Browse files
authored
Merge pull request #6279 from chaimann/admin-ui-select-include-blank
[Admin][UI] Allow select to include blank option
2 parents fe858ab + 9e32dad commit bbca5cc

File tree

10 files changed

+29
-32
lines changed

10 files changed

+29
-32
lines changed

admin/app/components/solidus_admin/ui/forms/select/component.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class SolidusAdmin::UI::Forms::Select::Component < SolidusAdmin::BaseComponent
5050
# loading next page of results. Default: "Loading more results".
5151
# @option attributes [String] :"data-no-results-message" which text to show when there are no search results returned.
5252
# Default: "No results found".
53+
# @option attributes [true, String] :include_blank if passed, an empty option will be prepended to the list of options.
54+
# Pass +true+ for empty option with no text, or +String+ for the text to be shown as empty option.
55+
# @raise [ArgumentError] if +choices+ is not an array
5356
def initialize(label:, name:, choices:, src: nil, size: :m, hint: nil, tip: nil, error: nil, **attributes)
5457
@label = label
5558
@hint = hint
@@ -76,10 +79,17 @@ def before_render
7679
end
7780

7881
def prepare_options(choices:, src:)
82+
raise ArgumentError, "`choices` must be an array" unless choices.is_a?(Array)
83+
7984
if src.present?
8085
@attributes[:"data-src"] = src
8186
end
8287

88+
if (blank_option = @attributes.delete(:include_blank))
89+
blank_option = "" if blank_option == true
90+
choices.unshift([blank_option, ""])
91+
end
92+
8393
@options_collection = options_for_select(choices, @attributes.delete(:value))
8494
end
8595

admin/app/components/solidus_admin/users/store_credits/edit_amount/component.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<%= render component("ui/forms/field").select(
77
f,
88
:store_credit_reason_id,
9-
store_credit_reasons_select_options.html_safe,
10-
include_blank: t('spree.choose_reason'),
9+
@store_credit_reasons.map { [_1.name, _1.id] },
10+
include_blank: t('.choose_reason'),
1111
html: { required: true }
1212
) %>
1313
</div>

admin/app/components/solidus_admin/users/store_credits/edit_amount/component.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,4 @@ def form_id
1414
def form_url
1515
solidus_admin.update_amount_user_store_credit_path(@user, @store_credit, **search_filter_params)
1616
end
17-
18-
def store_credit_reasons_select_options
19-
# Placeholder + Store Credit Reasons
20-
"<option value>#{t('.choose_reason')}</option>" + options_from_collection_for_select(@store_credit_reasons, :id, :name)
21-
end
2217
end

admin/app/components/solidus_admin/users/store_credits/edit_validity/component.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<%= render component("ui/forms/field").select(
66
f,
77
:store_credit_reason_id,
8-
store_credit_reasons_select_options.html_safe,
9-
include_blank: t('spree.choose_reason'),
8+
@store_credit_reasons.map { [_1.name, _1.id] },
9+
include_blank: t('.choose_reason'),
1010
html: { required: true }
1111
) %>
1212
</div>

admin/app/components/solidus_admin/users/store_credits/edit_validity/component.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,4 @@ def form_id
1414
def form_url
1515
solidus_admin.invalidate_user_store_credit_path(@user, @store_credit, **search_filter_params)
1616
end
17-
18-
def store_credit_reasons_select_options
19-
# Placeholder + Store Credit Reasons
20-
"<option value>#{t('.choose_reason')}</option>" + options_from_collection_for_select(@store_credit_reasons, :id, :name)
21-
end
2217
end

admin/app/components/solidus_admin/users/store_credits/new/component.html.erb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
<%= render component("ui/forms/field").select(
77
f,
88
:currency,
9-
currency_select_options.html_safe,
10-
include_blank: t("spree.currency"),
9+
Spree::Config.available_currencies.map { [_1.iso_code, _1.iso_code] },
10+
placeholder: t("spree.currency"),
11+
value: Spree::Config.currency,
1112
html: { required: true }
1213
) %>
1314
<%= render component("ui/forms/field").select(
1415
f,
1516
:category_id,
16-
store_credit_categories_select_options.html_safe,
17-
include_blank: t("spree.category"),
17+
@store_credit_categories.map { [_1.name, _1.id] },
18+
include_blank: t(".choose_category"),
1819
html: { required: true }
1920
) %>
2021
<%= render component("ui/forms/field").text_field(f, :memo) %>

admin/app/components/solidus_admin/users/store_credits/new/component.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,4 @@ def initialize(user:, store_credit:, categories:)
1010
def form_url
1111
solidus_admin.user_store_credits_path(@user, **search_filter_params)
1212
end
13-
14-
def currency_select_options
15-
options_from_collection_for_select(Spree::Config.available_currencies, :iso_code, :iso_code, Spree::Config.currency)
16-
end
17-
18-
def store_credit_categories_select_options
19-
# Placeholder + Store Credit Categories
20-
"<option value>#{t('.choose_category')}</option>" + options_from_collection_for_select(@store_credit_categories, :id, :name)
21-
end
2213
end

admin/app/javascript/solidus_admin/web_components/solidus_select.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class SolidusSelect extends HTMLSelectElement {
4444
dropdownContentClass: "dropdown-content",
4545
optionClass: "option",
4646
wrapperClass: "wrapper",
47+
allowEmptyOption: true,
4748
maxOptions: null,
4849
refreshThrottle: 0,
4950
plugins: {

admin/spec/components/previews/solidus_admin/ui/forms/select/component_preview.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ def remote_with_pagination(multiple: false, latency: false, loading_message: nil
3939
# @param disabled toggle
4040
# @param error toggle
4141
# @param include_blank toggle
42+
# @param blank_option text
4243
# @param placeholder text
4344
# @param hint text
4445
# @param tip text
45-
def playground(size: "m", options: 3, multiple: false, selected: false, disabled: false, error: false, include_blank: true, placeholder: nil, hint: nil, tip: nil)
46+
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)
4647
options = (1..options).map { |i| ["Option #{i}", i] }
47-
options.unshift(["None", ""]) if include_blank
48+
if include_blank && blank_option.present?
49+
include_blank = blank_option
50+
end
4851

4952
render component("ui/forms/select").new(
5053
label: "Label",
@@ -57,7 +60,8 @@ def playground(size: "m", options: 3, multiple: false, selected: false, disabled
5760
value: (multiple && [1, 2] || 1 if selected),
5861
multiple:,
5962
disabled:,
60-
placeholder:
63+
placeholder:,
64+
include_blank:
6165
)
6266
end
6367
end

admin/spec/components/previews/solidus_admin/ui/forms/select/component_preview/overview.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div class="flex flex-col flex-grow gap-2">
44
<h3>Single</h3>
55

6-
<% default_params = { label: "Country", name: "country", choices: ["", "Denmark", "Sweden"] } %>
6+
<% default_params = { label: "Country", name: "country", choices: %w[Denmark Sweden] } %>
77

88
<div class="mb-8">
99
<h6 class="text-gray-500 mb-3 mt-0">Default</h6>
@@ -51,7 +51,7 @@
5151
<div class="flex flex-col flex-grow gap-2">
5252
<h3>Multiple</h3>
5353

54-
<% default_params = { label: "Countries", name: "countries", choices: ["", "Denmark", "Sweden", "United Kingdom"], multiple: true } %>
54+
<% default_params = { label: "Countries", name: "countries", choices: ["Denmark", "Sweden", "United Kingdom"], multiple: true } %>
5555

5656
<div class="mb-8">
5757
<h6 class="text-gray-500 mb-3 mt-0">Default</h6>

0 commit comments

Comments
 (0)