Skip to content

Commit 66d7668

Browse files
committed
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.
1 parent 6d66dad commit 66d7668

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

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/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)