forked from bootstrap-ruby/bootstrap_form
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradio_button.rb
More file actions
65 lines (56 loc) · 2.45 KB
/
radio_button.rb
File metadata and controls
65 lines (56 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true
module BootstrapForm
module Inputs
module RadioButton
extend ActiveSupport::Concern
include Base
included do
def radio_button_with_bootstrap(name, value, *args)
options = args.extract_options!.symbolize_keys!
wrapper_attributes = options[:wrapper] || {}
wrapper_attributes[:class] = radio_button_wrapper_class(options)
tag.div(**wrapper_attributes) do
html = radio_button_without_bootstrap(name, value, radio_button_options(name, options))
html << radio_button_label(name, value, options) unless options[:skip_label]
html << generate_error(name, options[:id]) if options[:error_message]
html
end
end
bootstrap_alias :radio_button
end
private
def radio_button_options(name, options)
radio_button_options = options.except(:class, :label, :label_class, :error_message, :help,
:inline, :hide_label, :skip_label, :wrapper, :wrapper_class)
radio_button_options[:class] = radio_button_classes(name, options)
radio_button_options[:aria] = { describedby: aria_feedback_id(id: options[:id], name:) } if error?(name)
radio_button_options.merge!(required_field_options(options, name))
end
def radio_button_label(name, value, options)
label_options = { value: value, class: radio_button_label_class(options) }
label_options[:for] = options[:id] if options[:id].present?
label(name, options[:label], label_options)
end
def radio_button_classes(name, options)
classes = Array(options[:class]) << "form-check-input"
classes << "is-invalid" if error?(name)
classes << "position-static" if options[:skip_label] || options[:hide_label]
classes.flatten.compact
end
def radio_button_label_class(options)
classes = ["form-check-label"]
classes << options[:label_class]
classes << hide_class if options[:hide_label]
classes.flatten.compact
end
def radio_button_wrapper_class(options)
classes = ["form-check"]
classes << "form-check-inline" if layout_inline?(options[:inline])
classes << "disabled" if options[:disabled]
classes << options.dig(:wrapper, :class).presence
classes << options[:wrapper_class].presence
classes.flatten.compact
end
end
end
end