Skip to content

Commit 6605613

Browse files
committed
adding support for radio_button
1 parent 3160fca commit 6605613

File tree

6 files changed

+85
-8
lines changed

6 files changed

+85
-8
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,11 @@ date_field month_field range_field time_field
9898
datetime_field number_field search_field url_field
9999
email_field password_field text_area week_field
100100
date_select time_select datetime_select
101-
collection_radio_buttons collection_check_boxes
102-
rich_text_area
101+
check_box radio_button rich_text_area
102+
collection_check_boxes
103+
collection_radio_buttons
103104
```
104105

105-
Singular `radio_button` is not implemented as it doesn't make sense to wrap one
106-
radio button input in Bootstrap markup.
107-
108106
#### Radio Buttons and Checkboxes
109107

110108
To render collection of radio buttons or checkboxes we use the same helper that

demo/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- `bundle`
66
- `cd demo/`
7+
- `./bin/rails db:schema:load`
78
- `./bin/rails s`
89
- Navigate to http://localhost:3000
910
- Start changing stuff in [/demo/app/views/bootstrap/form.html.erb](/demo/app/views/bootstrap/form.html.erb)

demo/app/views/bootstrap/form.html.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@
8989
<h3>Checkboxes and Radio buttons</h3>
9090
<% @user.test = "b" %>
9191
<%= bootstrap_form_with model: @user, scope: "user_d", url: "/" do |form| %>
92+
<%= form.check_box :test, bootstrap: { label: "Singular Checkbox"} %>
93+
<%= form.radio_button :test, "value_a", bootstrap: { label: "Singular Radio A"} %>
94+
<%= form.radio_button :test, "value_b", bootstrap: { label: "Singular Radio B"} %>
9295
<%= form.collection_radio_buttons :test, [["a", "Label A"], ["b", "Label B"]], :first, :second %>
9396
<%= form.collection_check_boxes :test, [["e", "Label E"], ["f", "Label F"]], :first, :second, bootstrap: {help: "help text"} %>
9497
<%= form.collection_radio_buttons :test, ["u", "v", "w"], :to_s, :to_s, bootstrap: {check_inline: true, label: {text: "Custom Label"}} %>
@@ -99,6 +102,9 @@
99102
<h3>With Errors</h3>
100103
<% @user_with_error.test = "b" %>
101104
<%= bootstrap_form_with model: @user_with_error, scope: "user_e", url: "/" do |form| %>
105+
<%= form.check_box :test, bootstrap: { label: "Singular Checkbox"} %>
106+
<%= form.radio_button :test, "value_a", bootstrap: { label: "Singular Radio A"} %>
107+
<%= form.radio_button :test, "value_b", bootstrap: { label: "Singular Radio B"} %>
102108
<%= form.collection_radio_buttons :test, [["a", "Label A"], ["b", "Label B"]], :first, :second %>
103109
<%= form.collection_check_boxes :test, [["e", "Label E"], ["f", "Label F"]], :first, :second, bootstrap: {help: "help text"} %>
104110
<%= form.collection_radio_buttons :test, ["u", "v", "w"], :to_s, :to_s, bootstrap: {check_inline: true, label: {text: "Custom Label"}} %>

lib/comfy_bootstrap_form/form_builder.rb

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,52 @@ def file_field(method, options = {})
123123
end
124124
end
125125

126+
# Wrapper around radio button. Example usage:
127+
#
128+
# radio_button :choice, "value", bootstrap: {label: {text: "Do you agree?"}}
129+
#
130+
def radio_button(method, tag_value, options = {})
131+
bootstrap = form_bootstrap.scoped(options.delete(:bootstrap))
132+
return super if bootstrap.disabled
133+
134+
help_text = draw_help(bootstrap)
135+
errors = draw_errors(bootstrap, method)
136+
137+
add_css_class!(options, "form-check-input")
138+
add_css_class!(options, "is-invalid") if errors.present?
139+
140+
label_text = nil
141+
if (custom_text = bootstrap.label[:text]).present?
142+
label_text = custom_text
143+
end
144+
145+
fieldset_css_class = "form-group"
146+
fieldset_css_class += " row" if bootstrap.horizontal?
147+
fieldset_css_class += " #{bootstrap.inline_margin_class}" if bootstrap.inline?
148+
149+
content_tag(:fieldset, class: fieldset_css_class) do
150+
draw_control_column(bootstrap, offset: true) do
151+
if bootstrap.custom_control
152+
content_tag(:div, class: "custom-control custom-radio") do
153+
add_css_class!(options, "custom-control-input")
154+
remove_css_class!(options, "form-check-input")
155+
concat super(method, tag_value, options)
156+
concat label(method, label_text, value: tag_value, class: "custom-control-label")
157+
concat errors if errors.present?
158+
concat help_text if help_text.present?
159+
end
160+
else
161+
content_tag(:div, class: "form-check") do
162+
concat super(method, tag_value, options)
163+
concat label(method, label_text, value: tag_value, class: "form-check-label")
164+
concat errors if errors.present?
165+
concat help_text if help_text.present?
166+
end
167+
end
168+
end
169+
end
170+
end
171+
126172
# Wrapper around checkbox. Example usage:
127173
#
128174
# checkbox :agree, bootstrap: {label: {text: "Do you agree?"}}
@@ -186,7 +232,7 @@ def collection_radio_buttons(method, collection, value_method, text_method, opti
186232

187233
args = [bootstrap, :radio_button, method, collection, value_method, text_method, options, html_options]
188234
draw_choices(*args) do |m, v, opts|
189-
radio_button(m, v, opts)
235+
radio_button(m, v, opts.merge(bootstrap: { disabled: true }))
190236
end
191237
end
192238

@@ -208,7 +254,7 @@ def collection_check_boxes(method, collection, value_method, text_method, option
208254
content << draw_choices(*args) do |m, v, opts|
209255
opts[:multiple] = true
210256
opts[:include_hidden] = false
211-
ActionView::Helpers::FormBuilder.instance_method(:check_box).bind(self).call(m, opts, v)
257+
check_box(m, opts.merge(bootstrap: { disabled: true }), v)
212258
end
213259
end
214260

test/comfy_bootstrap_form/custom_fields_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ def test_file_field_with_custom_id
5151
assert_xml_equal expected, actual
5252
end
5353

54+
def test_radio_button
55+
actual = @builder.radio_button(:test, "value", bootstrap: { custom_control: true })
56+
expected = <<-HTML
57+
<fieldset class="form-group">
58+
<div class="custom-control custom-radio">
59+
<input class="custom-control-input" id="user_test_value" name="user[test]" type="radio" value="value"/>
60+
<label class="custom-control-label" for="user_test_value">Value</label>
61+
</div>
62+
</fieldset>
63+
HTML
64+
assert_xml_equal expected, actual
65+
end
66+
5467
def test_check_box
5568
actual = @builder.check_box(:test, bootstrap: { custom_control: true })
5669
expected = <<-HTML

test/comfy_bootstrap_form/radios_and_checkboxes_test.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,20 @@ def test_collection_checkboxes_without_hidden_field
8989
assert_xml_equal expected, actual
9090
end
9191

92-
def test_radio_buttons
92+
def test_radio_button
93+
actual = @builder.radio_button(:test, "value")
94+
expected = <<-HTML
95+
<fieldset class="form-group">
96+
<div class="form-check">
97+
<input class="form-check-input" id="user_test_value" name="user[test]" type="radio" value="value"/>
98+
<label class="form-check-label" for="user_test_value">Value</label>
99+
</div>
100+
</fieldset>
101+
HTML
102+
assert_xml_equal expected, actual
103+
end
104+
105+
def test_collection_radio_buttons
93106
actual = @builder.collection_radio_buttons(:test, %w[a b], :to_s, :titleize)
94107
expected = <<-HTML
95108
<fieldset class="form-group">

0 commit comments

Comments
 (0)