Module: HakumiComponents::Concerns::FormField
- Extended by:
- T::Helpers, T::Sig
- Includes:
- FormFieldInterface
- Included in:
- HakumiComponents::Checkbox::Component, HakumiComponents::ColorPicker::Component, InputControl, SelectionControl, DatePicker::Component, DatePicker::RangePicker, Radio::Component, Radio::Group::Component, Rate::Component, Slider::Component, Switch::Component, TimePicker::Component, Transfer::Component, Upload::Component
- Defined in:
- app/components/hakumi_components/concerns/form_field.rb
Instance Method Summary collapse
- #describedby_ids ⇒ Object
- #error? ⇒ Boolean
- #error_message ⇒ Object
- #form_field_caption ⇒ Object
- #form_field_contract ⇒ Object
- #form_field_errors ⇒ Object
- #form_field_html_options ⇒ Object
- #form_field_label ⇒ Object
- #form_field_name ⇒ Object
- #form_field_required ⇒ Object
- #form_field_rules ⇒ Object
- #form_field_standalone ⇒ Object
- #form_item_attributes(additional_data: {}) ⇒ Object
- #form_item_classes ⇒ Object
- #input_id ⇒ Object
- #render_caption ⇒ Object
- #render_error ⇒ Object
- #render_explain ⇒ Object
- #render_label(field_id: nil) ⇒ Object
- #standalone? ⇒ Boolean
Instance Method Details
#describedby_ids ⇒ Object
95 96 97 98 99 100 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 95 def describedby_ids ids = T.let([], T::Array[String]) ids << "#{form_field_name}_caption" unless caption_blank? ids << "#{form_field_name}_error" if error? ids.empty? ? nil : ids.join(" ") end |
#error? ⇒ Boolean
65 66 67 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 65 def error? form_field_errors.any? end |
#error_message ⇒ Object
103 104 105 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 103 def form_field_errors.first end |
#form_field_caption ⇒ Object
35 36 37 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 35 def form_field_caption form_field_contract.caption end |
#form_field_contract ⇒ Object
17 18 19 20 21 22 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 17 def form_field_contract contract = T.let(@form_field_contract, T.nilable(HakumiComponents::Concerns::FormFieldContract)) return contract unless contract.nil? Kernel.raise "form_field_contract must be initialized" end |
#form_field_errors ⇒ Object
40 41 42 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 40 def form_field_errors form_field_contract.errors end |
#form_field_html_options ⇒ Object
55 56 57 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 55 def form_field_contract. end |
#form_field_label ⇒ Object
30 31 32 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 30 def form_field_label form_field_contract.label end |
#form_field_name ⇒ Object
25 26 27 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 25 def form_field_name form_field_contract.name end |
#form_field_required ⇒ Object
50 51 52 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 50 def form_field_required form_field_contract.required end |
#form_field_rules ⇒ Object
60 61 62 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 60 def form_field_rules form_field_contract.rules end |
#form_field_standalone ⇒ Object
45 46 47 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 45 def form_field_standalone form_field_contract.standalone end |
#form_item_attributes(additional_data: {}) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 158 def form_item_attributes(additional_data: {}) data_attrs = additional_data.dup rules = form_field_rules if rules && !rules.empty? data_attrs[:controller] = "hakumi--form-item" data_attrs[:hakumi__form_item_field_name_value] = form_field_name.to_s data_attrs[:hakumi__form_item_rules_value] = rules.to_json end { class: form_item_classes, data: data_attrs.empty? ? nil : data_attrs }.compact end |
#form_item_classes ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 84 def form_item_classes class_names( "form-item", { "has-error": error?, "with-help": error? || !caption_blank? } ) end |
#input_id ⇒ Object
75 76 77 78 79 80 81 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 75 def input_id = id = [:id] return id if id.is_a?(String) || id.is_a?(Symbol) form_field_name end |
#render_caption ⇒ Object
123 124 125 126 127 128 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 123 def render_caption caption = form_field_caption return nil if caption.nil? || caption.strip.empty? content_tag(:div, caption, class: "hakumi-form-item-extra", id: "#{form_field_name}_caption") end |
#render_error ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 131 def render_error return nil unless error? content_tag( :div, , class: "hakumi-form-item-explain-error", id: "#{form_field_name}_error", role: "alert" ) end |
#render_explain ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 144 def render_explain return nil if caption_blank? && !error? explain_content = if error? render_error else render_caption end return nil if explain_content.nil? content_tag(:div, class: "hakumi-form-item-explain") { explain_content } end |
#render_label(field_id: nil) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 108 def render_label(field_id: nil) label = form_field_label return nil if label.nil? || label.strip.empty? content_tag(:div, class: "hakumi-form-item-label") do content_tag(:label, for: field_id || form_field_name) do safe_join([ (form_field_required ? content_tag(:span, "*", class: "hakumi-form-item-required-mark") : nil), label ].compact) end end end |
#standalone? ⇒ Boolean
70 71 72 |
# File 'app/components/hakumi_components/concerns/form_field.rb', line 70 def standalone? form_field_standalone end |