Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added

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

### Changes

Expand Down
13 changes: 11 additions & 2 deletions app/components/flowbite/input/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ class Select < Field
lg: ["px-4", "py-3", "text-base"]
}.freeze

def initialize(form:, attribute:, collection: [], disabled: false, options: {}, size: :default)
def initialize(form:, attribute:, collection: [], disabled: false, include_blank: false, multiple: false, options: {}, size: :default)
super(form: form, attribute: attribute, disabled: disabled, options: options, size: size)
@collection = collection
@include_blank = include_blank
@multiple = multiple
end

# Returns the HTML to use for the actual input field element.
Expand All @@ -25,7 +27,7 @@ def call
input_field_type,
@attribute,
@collection,
{},
select_options,
html_options
)
end
Expand All @@ -37,6 +39,13 @@ def input_field_type

private

def select_options
{
include_blank: @include_blank,
multiple: @multiple
}
end

# Returns the html_options argument for the select method
def html_options
{
Expand Down
6 changes: 5 additions & 1 deletion app/components/flowbite/input_field/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
module Flowbite
class InputField
class Select < InputField
def initialize(attribute:, form:, collection: [], disabled: false, hint: nil, input: {}, label: {}, size: :default)
def initialize(attribute:, form:, collection: [], disabled: false, hint: nil, include_blank: false, input: {}, label: {}, multiple: false, size: :default)
super(attribute: attribute, disabled: disabled, form: form, hint: hint, input: input, label: label, size: size)
@collection = collection
@include_blank = include_blank
@multiple = multiple
end

def input
Expand All @@ -15,6 +17,8 @@ def input
collection: @collection,
disabled: @disabled,
form: @form,
include_blank: @include_blank,
multiple: @multiple,
options: input_options,
size: @size
)
Expand Down
16 changes: 16 additions & 0 deletions demo/test/components/previews/select_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ def large_select
render(Flowbite::InputField::Select.new(attribute: :company, form: form, collection: ["Option 1", "Option 2", "Option 3"], size: :lg))
end

# @group Multiple
#
# @display classes w-full

def multiple_select
render(Flowbite::InputField::Select.new(attribute: :company, form: form, collection: ["Option 1", "Option 2", "Option 3"], multiple: true))
end

# @group Include blank
#
# @display classes w-full

def include_blank_select
render(Flowbite::InputField::Select.new(attribute: :company, form: form, collection: ["Option 1", "Option 2", "Option 3"], include_blank: true))
end

# @!endgroup

private
Expand Down
12 changes: 12 additions & 0 deletions test/components/input/select_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ def test_renders_in_disabled_state
assert_selector("select[name='article[category_id]'][disabled].bg-gray-100.border.border-gray-300.text-gray-900.cursor-not-allowed")
end

def test_renders_with_include_blank
render_inline(Flowbite::Input::Select.new(form: @form, attribute: :category_id, collection: @categories.map { |c| [c.name, c.id] }, include_blank: true))

assert_selector("select[name='article[category_id]'] option[value='']", text: "")
end

def test_renders_with_multiple_select
render_inline(Flowbite::Input::Select.new(form: @form, attribute: :category_id, collection: @categories.map { |c| [c.name, c.id] }, multiple: true))

assert_selector("select[name='article[category_id][]'][multiple]")
end

def test_renders_with_empty_collection
render_inline(Flowbite::Input::Select.new(form: @form, attribute: :category_id, collection: []))

Expand Down