Skip to content

Commit 6ad095f

Browse files
committed
TEF-94 Add dropdown component
1 parent 175f8d6 commit 6ad095f

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

Gemfile.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ PLATFORMS
343343
aarch64-linux-musl
344344
arm-linux-gnu
345345
arm-linux-musl
346+
arm64-darwin-23
346347
arm64-darwin-24
347348
x86_64-linux
348349
x86_64-linux-gnu
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="space-y-4">
2+
<%= @form.label @method, @label, class: "label block #{required_class}" %>
3+
<%= @form.collection_select @method, @collection, @item_value_method, @item_label_method, prompt: "- Select -" %>
4+
<%= render(FormErrorsComponent.new(form: @form, method: @method)) %>
5+
</div>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
class DropdownComponent < ViewComponent::Base
4+
def initialize(form:, method:, collection:, item_value_method:, item_label_method:, required: false)
5+
@form = form
6+
@method = method
7+
@collection = collection
8+
@item_value_method = item_value_method
9+
@item_label_method = item_label_method
10+
@required = required
11+
end
12+
13+
private
14+
15+
def required_class
16+
@required ? "required" : ""
17+
end
18+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class DropdownComponentTest < ViewComponent::TestCase
6+
def test_component_renders_something_useful
7+
# assert_equal(
8+
# %(<span>Hello, components!</span>),
9+
# render_inline(DropdownComponent.new(message: "Hello, components!")).css("span").to_html
10+
# )
11+
end
12+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class DropdownComponentPreview < FormComponentPreview
2+
def default
3+
render(DropdownComponent.new(form: form, method: :favorite_fruits, collection: self.class.fruit_options, item_value_method: :value, item_label_method: :label))
4+
end
5+
6+
def prefilled
7+
custom_model = TestModel.new(favorite_fruits: [ "apple" ])
8+
render(DropdownComponent.new(form: form(model: custom_model), method: :favorite_fruits, collection: self.class.fruit_options, item_value_method: :value, item_label_method: :label))
9+
end
10+
11+
def with_errors
12+
custom_model = TestModel.new
13+
custom_model.valid?
14+
render(DropdownComponent.new(form: form(model: custom_model), method: :favorite_fruits, collection: self.class.fruit_options, item_value_method: :value, item_label_method: :label))
15+
end
16+
17+
def required
18+
render(DropdownComponent.new(form: form, method: :favorite_fruits, collection: self.class.fruit_options, item_value_method: :value, item_label_method: :label, required: true))
19+
end
20+
end

0 commit comments

Comments
 (0)