Skip to content

Commit a26bd92

Browse files
committed
Add select specific options to Flowbite select input component
This adds :include_blank and :multiple options to the Flowbite select input component, and updates the corresponding InputField component and tests accordingly. I am omitting the :prompt option, as it is not clear it works with the select helper. Tests seems to suggest it does not, but maybe with a select_tag helper it would.
1 parent a5cc789 commit a26bd92

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

app/components/flowbite/input/select.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ class Select < Field
1414
lg: ["px-4", "py-3", "text-base"]
1515
}.freeze
1616

17-
def initialize(form:, attribute:, collection: [], disabled: false, options: {}, size: :default)
17+
def initialize(form:, attribute:, collection: [], disabled: false, include_blank: false, multiple: false, options: {}, size: :default)
1818
super(form: form, attribute: attribute, disabled: disabled, options: options, size: size)
1919
@collection = collection
20+
@include_blank = include_blank
21+
@multiple = multiple
2022
end
2123

2224
# Returns the HTML to use for the actual input field element.
@@ -25,7 +27,7 @@ def call
2527
input_field_type,
2628
@attribute,
2729
@collection,
28-
{},
30+
select_options,
2931
html_options
3032
)
3133
end
@@ -37,6 +39,13 @@ def input_field_type
3739

3840
private
3941

42+
def select_options
43+
{
44+
include_blank: @include_blank,
45+
multiple: @multiple
46+
}
47+
end
48+
4049
# Returns the html_options argument for the select method
4150
def html_options
4251
{

app/components/flowbite/input_field/select.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
module Flowbite
44
class InputField
55
class Select < InputField
6-
def initialize(attribute:, form:, collection: [], disabled: false, hint: nil, input: {}, label: {}, size: :default)
6+
def initialize(attribute:, form:, collection: [], disabled: false, hint: nil, include_blank: false, input: {}, label: {}, multiple: false, size: :default)
77
super(attribute: attribute, disabled: disabled, form: form, hint: hint, input: input, label: label, size: size)
88
@collection = collection
9+
@include_blank = include_blank
10+
@multiple = multiple
911
end
1012

1113
def input
@@ -15,6 +17,8 @@ def input
1517
collection: @collection,
1618
disabled: @disabled,
1719
form: @form,
20+
include_blank: @include_blank,
21+
multiple: @multiple,
1822
options: input_options,
1923
size: @size
2024
)

test/components/input/select_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ def test_renders_in_disabled_state
4444
assert_selector("select[name='article[category_id]'][disabled].bg-gray-100.border.border-gray-300.text-gray-900.cursor-not-allowed")
4545
end
4646

47+
def test_renders_with_include_blank
48+
render_inline(Flowbite::Input::Select.new(form: @form, attribute: :category_id, collection: @categories.map { |c| [c.name, c.id] }, include_blank: true))
49+
50+
assert_selector("select[name='article[category_id]'] option[value='']", text: "")
51+
end
52+
53+
def test_renders_with_multiple_select
54+
render_inline(Flowbite::Input::Select.new(form: @form, attribute: :category_id, collection: @categories.map { |c| [c.name, c.id] }, multiple: true))
55+
56+
assert_selector("select[name='article[category_id][]'][multiple]")
57+
end
58+
4759
def test_renders_with_empty_collection
4860
render_inline(Flowbite::Input::Select.new(form: @form, attribute: :category_id, collection: []))
4961

0 commit comments

Comments
 (0)