Skip to content

Commit

Permalink
bugfix: set radio button checked according to form field (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
k0va1 authored Sep 9, 2024
1 parent 517bead commit 6a51583
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
14 changes: 11 additions & 3 deletions app/components/polaris/base_radio_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def initialize(
form: nil,
attribute: nil,
name: nil,
checked: false,
checked: nil,
disabled: false,
value: nil,
**system_arguments
Expand All @@ -22,9 +22,17 @@ def system_arguments
@system_arguments.tap do |opts|
opts[:disabled] = true if @disabled
opts[:aria] ||= {}
opts[:aria][:checked] = @checked
opts[:checked] = @checked
opts[:class] = opts.delete(:classes)

if @form.present? && @attribute.present?
unless @checked.nil?
opts[:aria][:checked] = !!@checked
opts[:checked] = !!@checked
end
else
opts[:aria][:checked] = !!@checked
opts[:checked] = !!@checked
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/components/polaris/radio_button_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(
name: nil,
label: nil,
label_hidden: false,
checked: false,
checked: nil,
disabled: false,
help_text: nil,
value: nil,
Expand Down
2 changes: 1 addition & 1 deletion demo/app/models/product.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Product
include ActiveModel::Model

attr_accessor :title, :status, :country, :selected_markets, :tags
attr_accessor :title, :status, :country, :selected_markets, :tags, :access
end
2 changes: 1 addition & 1 deletion demo/app/previews/form_builder_component_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def check_box
end

def radio_button
product = Product.new
product = Product.new(access: :allow)
render_with_template(locals: {product: product})
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<%= form_with(model: product, builder: Polaris::FormBuilder) do |form| %>
<%= form.polaris_radio_button(:access,
value: :allow,
label: "Allow access",
checked: true,
label: "Allow access"
) %>
<%= form.polaris_radio_button(:access,
value: :deny,
label: "Deny access",
checked: false,
label: "Deny access"
) %>
<% end %>
24 changes: 24 additions & 0 deletions test/helpers/polaris/form_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ class Product
end
end

test "#polaris_radio_button checked" do
@product = Product.new(access: :allow)
@builder = Polaris::FormBuilder.new(:product, @product, self, {})
@rendered_content = @builder.polaris_radio_button(:access, value: :allow, label: "Radio Label")

assert_selector "label.Polaris-Choice" do
assert_selector ".Polaris-Choice__Label", text: "Radio Label"
assert_selector ".Polaris-RadioButton" do
assert_selector %(input[name="product[access]"][value="allow"][type="radio"][checked="checked"])
end
end
end

test "#polaris_radio_button checked manually" do
@rendered_content = @builder.polaris_radio_button(:access, value: :allow, checked: true, label: "Radio Label")

assert_selector "label.Polaris-Choice" do
assert_selector ".Polaris-Choice__Label", text: "Radio Label"
assert_selector ".Polaris-RadioButton" do
assert_selector %(input[name="product[access]"][value="allow"][type="radio"][checked="checked"])
end
end
end

test "#polaris_dropzone" do
@rendered_content = @builder.polaris_dropzone(:image, label: "Dropzone Label")

Expand Down

0 comments on commit 6a51583

Please sign in to comment.