comfy_bootstrap_form 4.0.1 → 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +1 -2
- data/README.md +12 -2
- data/lib/bootstrap_form/bootstrap_options.rb +7 -0
- data/lib/bootstrap_form/form_builder.rb +76 -25
- data/lib/bootstrap_form/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf622e9dea142e6cd18d422f30446995fabff88afb88d60b92a5e1604d684f48
|
4
|
+
data.tar.gz: 5f9679e1bdd4605e79a4d59c374d45c8511bc700ee689fd7c0d3245f26ae01a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f01ad97aa7ec33468d219cf2ff492aba96bb8ea7d65d4a177db7b9d31a0207dfe183bed2b67b4702a646b310b2c756098331667769e788455e763d906dd78fb
|
7
|
+
data.tar.gz: 7d7dac488acd94aae48fba8acbf7cab0be698211ac721896a9425fd92ffc51a59d89f4e594a56b1296e0ba9f522764a0f750b4d5eb5e1b36ff131faee660e74b
|
data/Gemfile
CHANGED
@@ -3,7 +3,7 @@ source "http://rubygems.org"
|
|
3
3
|
gemspec
|
4
4
|
|
5
5
|
# Uncomment and change rails version for testing purposes
|
6
|
-
|
6
|
+
gem "rails", "~> 5.2.0.rc1"
|
7
7
|
|
8
8
|
group :development do
|
9
9
|
gem "rubocop", require: false
|
@@ -13,7 +13,6 @@ group :test do
|
|
13
13
|
gem "coveralls", require: false
|
14
14
|
gem "diffy"
|
15
15
|
gem "equivalent-xml"
|
16
|
-
# gem "minitest", "~> 5.10.3"
|
17
16
|
gem "minitest"
|
18
17
|
gem "sqlite3"
|
19
18
|
end
|
data/README.md
CHANGED
@@ -70,7 +70,7 @@ for all kinds of different form configurations you can have.
|
|
70
70
|
|
71
71
|
## Form helpers
|
72
72
|
|
73
|
-
####
|
73
|
+
#### bootstrap_form_with
|
74
74
|
|
75
75
|
Wrapper around `form_with` helper that's available in Rails 5.1 and above.
|
76
76
|
Here's an example:
|
@@ -286,6 +286,16 @@ If you want to use something like a button, or other html content do this:
|
|
286
286
|
<%= form.text_field :value, bootstrap: {append: {html: button_html}} %>
|
287
287
|
```
|
288
288
|
|
289
|
+
#### Custom Forms
|
290
|
+
|
291
|
+
Bootstrap can replace native browser form elements with custom ones for checkboxes,
|
292
|
+
radio buttons and file input field. Example usage:
|
293
|
+
|
294
|
+
```erb
|
295
|
+
<%= form.file_field :photo, bootstrap: {custom_control: true} %>
|
296
|
+
<%= form.collection_radio_buttons :choice, %w[yes no], :to_s, :to_s, bootstrap: {custom_control: true} %>
|
297
|
+
```
|
298
|
+
|
289
299
|
### Gotchas
|
290
300
|
|
291
301
|
- In Rails 5.1 `form_with` does not generate ids for inputs. If you want them
|
@@ -314,4 +324,4 @@ If you want to use something like a button, or other html content do this:
|
|
314
324
|
|
315
325
|
---
|
316
326
|
|
317
|
-
Copyright 2018 Oleg Khabarov, Released under the [MIT License](
|
327
|
+
Copyright 2018 Oleg Khabarov, Released under the [MIT License](LICENSE.md)
|
@@ -58,6 +58,12 @@ module BootstrapForm
|
|
58
58
|
#
|
59
59
|
attr_reader :check_inline
|
60
60
|
|
61
|
+
# Enables special input styling for file_field, radio and checkboxes. Example:
|
62
|
+
#
|
63
|
+
# form.file_file :photo, bootstrap: {custom_control: true}
|
64
|
+
#
|
65
|
+
attr_reader :custom_control
|
66
|
+
|
61
67
|
def initialize(options = {})
|
62
68
|
set_defaults
|
63
69
|
set_options(options)
|
@@ -109,6 +115,7 @@ module BootstrapForm
|
|
109
115
|
@prepend = nil
|
110
116
|
@help = nil
|
111
117
|
@check_inline = false
|
118
|
+
@custom_control = false
|
112
119
|
end
|
113
120
|
|
114
121
|
end
|
@@ -4,7 +4,7 @@ module BootstrapForm
|
|
4
4
|
class FormBuilder < ActionView::Helpers::FormBuilder
|
5
5
|
|
6
6
|
FIELD_HELPERS = %w[
|
7
|
-
color_field date_field datetime_field email_field
|
7
|
+
color_field date_field datetime_field email_field month_field
|
8
8
|
number_field password_field phone_field range_field search_field text_area
|
9
9
|
text_field time_field url_field week_field
|
10
10
|
].freeze
|
@@ -49,6 +49,31 @@ module BootstrapForm
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
+
# Wrapper for file_field helper. It can accept `custom_control` option.
|
53
|
+
#
|
54
|
+
# file_field :photo, bootstrap: {custom_control: true}
|
55
|
+
#
|
56
|
+
def file_field(method, options = {})
|
57
|
+
bootstrap = form_bootstrap.scoped(options.delete(:bootstrap))
|
58
|
+
|
59
|
+
draw_form_group(bootstrap, method, options) do
|
60
|
+
if bootstrap.custom_control
|
61
|
+
content_tag(:div, class: "custom-file") do
|
62
|
+
add_css_class!(options, "custom-file-input")
|
63
|
+
remove_css_class!(options, "form-control")
|
64
|
+
label_text = options.delete(:placeholder)
|
65
|
+
concat super(method, options)
|
66
|
+
|
67
|
+
label_options = { class: "custom-file-label" }
|
68
|
+
label_options[:for] = options[:id] if options[:id].present?
|
69
|
+
concat label(method, label_text, label_options)
|
70
|
+
end
|
71
|
+
else
|
72
|
+
super(method, options)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
52
77
|
# Wrapper around checkbox. Example usage:
|
53
78
|
#
|
54
79
|
# checkbox :agree, bootstrap: {label: {text: "Do you agree?"}}
|
@@ -73,11 +98,22 @@ module BootstrapForm
|
|
73
98
|
|
74
99
|
content_tag(:fieldset, class: fieldset_css_class) do
|
75
100
|
draw_control_column(bootstrap, offset: true) do
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
101
|
+
if bootstrap.custom_control
|
102
|
+
content_tag(:div, class: "custom-control custom-checkbox") do
|
103
|
+
add_css_class!(options, "custom-control-input")
|
104
|
+
remove_css_class!(options, "form-check-input")
|
105
|
+
concat super(method, options, checked_value, unchecked_value)
|
106
|
+
concat label(method, label_text, class: "custom-control-label")
|
107
|
+
concat errors if errors.present?
|
108
|
+
concat help_text if help_text.present?
|
109
|
+
end
|
110
|
+
else
|
111
|
+
content_tag(:div, class: "form-check") do
|
112
|
+
concat super(method, options, checked_value, unchecked_value)
|
113
|
+
concat label(method, label_text, class: "form-check-label")
|
114
|
+
concat errors if errors.present?
|
115
|
+
concat help_text if help_text.present?
|
116
|
+
end
|
81
117
|
end
|
82
118
|
end
|
83
119
|
end
|
@@ -97,7 +133,7 @@ module BootstrapForm
|
|
97
133
|
def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {})
|
98
134
|
bootstrap = form_bootstrap.scoped(options.delete(:bootstrap))
|
99
135
|
|
100
|
-
args = [bootstrap, method, collection, value_method, text_method, options, html_options]
|
136
|
+
args = [bootstrap, :radio_button, method, collection, value_method, text_method, options, html_options]
|
101
137
|
draw_choices(*args) do |m, v, opts|
|
102
138
|
radio_button(m, v, opts)
|
103
139
|
end
|
@@ -116,7 +152,7 @@ module BootstrapForm
|
|
116
152
|
content << hidden_field(method, multiple: true, value: "")
|
117
153
|
end
|
118
154
|
|
119
|
-
args = [bootstrap, method, collection, value_method, text_method, options, html_options]
|
155
|
+
args = [bootstrap, :check_box, method, collection, value_method, text_method, options, html_options]
|
120
156
|
content << draw_choices(*args) do |m, v, opts|
|
121
157
|
opts[:multiple] = true
|
122
158
|
opts[:include_hidden] = false
|
@@ -223,7 +259,7 @@ module BootstrapForm
|
|
223
259
|
|
224
260
|
# form group wrapper for input fields
|
225
261
|
def draw_form_group(bootstrap, method, options)
|
226
|
-
label = draw_label(bootstrap, method)
|
262
|
+
label = draw_label(bootstrap, method, for_attr: options[:id])
|
227
263
|
errors = draw_errors(method)
|
228
264
|
|
229
265
|
control = draw_control(bootstrap, errors, method, options) do
|
@@ -258,16 +294,13 @@ module BootstrapForm
|
|
258
294
|
#
|
259
295
|
# text_field(:value, bootstrap: {label: {text: "Custom", class: "custom"}})
|
260
296
|
#
|
261
|
-
def draw_label(bootstrap, method)
|
262
|
-
|
263
|
-
|
297
|
+
def draw_label(bootstrap, method, for_attr: nil)
|
298
|
+
options = bootstrap.label.dup
|
299
|
+
text = options.delete(:text)
|
264
300
|
|
265
|
-
|
266
|
-
text = custom_text
|
267
|
-
end
|
301
|
+
options[:for] = for_attr if for_attr.present?
|
268
302
|
|
269
|
-
add_css_class!(options,
|
270
|
-
add_css_class!(options, "sr-only") if bootstrap.label[:hide]
|
303
|
+
add_css_class!(options, "sr-only") if options.delete(:hide)
|
271
304
|
add_css_class!(options, bootstrap.inline_margin_class) if bootstrap.inline?
|
272
305
|
|
273
306
|
if bootstrap.horizontal?
|
@@ -356,12 +389,30 @@ module BootstrapForm
|
|
356
389
|
end
|
357
390
|
|
358
391
|
# Rendering of choices for checkboxes and radio buttons
|
359
|
-
def draw_choices(bootstrap, method, collection, value_method, text_method, _options, html_options)
|
360
|
-
|
392
|
+
def draw_choices(bootstrap, type, method, collection, value_method, text_method, _options, html_options)
|
393
|
+
draw_form_group_fieldset(bootstrap, method) do
|
394
|
+
if bootstrap.custom_control
|
395
|
+
label_css_class = "custom-control-label"
|
396
|
+
|
397
|
+
form_check_css_class = "custom-control"
|
398
|
+
form_check_css_class <<
|
399
|
+
case type
|
400
|
+
when :radio_button then " custom-radio"
|
401
|
+
when :check_box then " custom-checkbox"
|
402
|
+
end
|
361
403
|
|
362
|
-
|
363
|
-
|
364
|
-
|
404
|
+
form_check_css_class << " custom-control-inline" if bootstrap.check_inline
|
405
|
+
|
406
|
+
add_css_class!(html_options, "custom-control-input")
|
407
|
+
|
408
|
+
else
|
409
|
+
label_css_class = "form-check-label"
|
410
|
+
|
411
|
+
form_check_css_class = "form-check"
|
412
|
+
form_check_css_class << " form-check-inline" if bootstrap.check_inline
|
413
|
+
|
414
|
+
add_css_class!(html_options, "form-check-input")
|
415
|
+
end
|
365
416
|
|
366
417
|
errors = draw_errors(method)
|
367
418
|
help_text = draw_help(bootstrap.help)
|
@@ -369,13 +420,13 @@ module BootstrapForm
|
|
369
420
|
add_css_class!(html_options, "is-invalid") if errors.present?
|
370
421
|
|
371
422
|
content = "".html_safe
|
372
|
-
collection.each_with_index
|
423
|
+
collection.each_with_index do |item, index|
|
373
424
|
item_value = item.send(value_method)
|
374
425
|
item_text = item.send(text_method)
|
375
426
|
|
376
427
|
content << content_tag(:div, class: form_check_css_class) do
|
377
428
|
concat yield method, item_value, html_options
|
378
|
-
concat label(method, item_text, value: item_value, class:
|
429
|
+
concat label(method, item_text, value: item_value, class: label_css_class)
|
379
430
|
if ((collection.count - 1) == index) && !bootstrap.check_inline
|
380
431
|
concat errors if errors.present?
|
381
432
|
concat help_text if help_text.present?
|
@@ -393,7 +444,7 @@ module BootstrapForm
|
|
393
444
|
end
|
394
445
|
|
395
446
|
# Wrapper for collections of radio buttons and checkboxes
|
396
|
-
def draw_form_group_fieldset(bootstrap, method
|
447
|
+
def draw_form_group_fieldset(bootstrap, method)
|
397
448
|
options = {}
|
398
449
|
|
399
450
|
unless bootstrap.label[:hide]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: comfy_bootstrap_form
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.0.
|
4
|
+
version: 4.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oleg Khabarov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-02
|
11
|
+
date: 2018-03-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|