Skip to content

Commit e158106

Browse files
authored
Merge pull request #42 from substancelab/add-options-for-select
Add select specific options
2 parents 91c4af5 + 1a78fe7 commit e158106

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
88
### Added
99

1010
* Options to Flowbite::InputField::Checkbox and Flowbite::Input::Checkbox that allow you to change the submitted value.
11+
* Options added for Flowbite::Input::Select - :multiple and :include_blank. These can now be passed to either the input itself or Flowbite::InputField::Select
1112

1213
### Changes
1314

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
)

demo/test/components/previews/select_preview.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ def large_select
4040
render(Flowbite::InputField::Select.new(attribute: :company, form: form, collection: ["Option 1", "Option 2", "Option 3"], size: :lg))
4141
end
4242

43+
# @group Multiple
44+
#
45+
# @display classes w-full
46+
47+
def multiple_select
48+
render(Flowbite::InputField::Select.new(attribute: :company, form: form, collection: ["Option 1", "Option 2", "Option 3"], multiple: true))
49+
end
50+
51+
# @group Include blank
52+
#
53+
# @display classes w-full
54+
55+
def include_blank_select
56+
render(Flowbite::InputField::Select.new(attribute: :company, form: form, collection: ["Option 1", "Option 2", "Option 3"], include_blank: true))
57+
end
58+
4359
# @!endgroup
4460

4561
private

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)