Skip to content

Commit

Permalink
Fix FormBuilder to correctly forward block to TextField (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
mborromeo authored Oct 24, 2024
1 parent 31b6ee1 commit 64fa275
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/helpers/polaris/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def polaris_text_field(method, **options, &block)
if options[:error_hidden] && options[:error]
options[:error] = !!options[:error]
end
render Polaris::TextFieldComponent.new(form: self, attribute: method, **options, &block)
render Polaris::TextFieldComponent.new(form: self, attribute: method, **options), &block
end

def polaris_select(method, **options, &block)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
<%= form_with(model: product, builder: Polaris::FormBuilder) do |form| %>
<%= form.polaris_text_field(:title, help_text: "Note about text field") %>
<% end %>

<%= polaris_spacer(vertical: :base) %>

<%= form_with(model: product, builder: Polaris::FormBuilder) do |form| %>
<%= form.polaris_text_field(:title, help_text: "Note about text field") do |c| %>
<% c.with_connected_left do %>
<%= polaris_select(
name: :weight_unit,
label: "Weight unit",
label_hidden: true,
options: { "kd" => "kg", "lb" => "lb" },
selected: "kg",
) %>
<% end %>
<% c.with_connected_right do %>
<%= polaris_button { "Submit" } %>
<% end %>
<% end %>
<% end %>
19 changes: 19 additions & 0 deletions test/helpers/polaris/form_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ class Product
end
end

test "#polaris_text_field_with_block" do
@rendered_content = @builder.polaris_text_field(:title, help_text: "Help Text") do |c|
c.with_connected_right do
polaris_button { "Submit" }
end
end

assert_selector ".Polaris-Label" do
assert_selector "label", text: "Title"
end
assert_selector ".Polaris-TextField" do
assert_selector %(input[name="product[title]"])
assert_selector ".Polaris-Labelled__HelpText", text: "Help Text"
end
assert_selector ".Polaris-Button" do
assert_selector "div", text: "Submit"
end
end

test "#polaris_select" do
@rendered_content = @builder.polaris_select(:status, options: {"Active" => "active", "Draft" => "draft"})

Expand Down

0 comments on commit 64fa275

Please sign in to comment.