Module: HakumiComponents::Concerns::FormField

Instance Method Summary collapse

Instance Method Details

#describedby_idsObject



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

Returns:

  • (Boolean)


65
66
67
# File 'app/components/hakumi_components/concerns/form_field.rb', line 65

def error?
  form_field_errors.any?
end

#error_messageObject



103
104
105
# File 'app/components/hakumi_components/concerns/form_field.rb', line 103

def error_message
  form_field_errors.first
end

#form_field_captionObject



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_contractObject



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_errorsObject



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_optionsObject



55
56
57
# File 'app/components/hakumi_components/concerns/form_field.rb', line 55

def form_field_html_options
  form_field_contract.html_options
end

#form_field_labelObject



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_nameObject



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_requiredObject



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_rulesObject



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_standaloneObject



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_classesObject



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_idObject



75
76
77
78
79
80
81
# File 'app/components/hakumi_components/concerns/form_field.rb', line 75

def input_id
  html_options = form_field_html_options
  id = html_options[:id]
  return id if id.is_a?(String) || id.is_a?(Symbol)

  form_field_name
end

#render_captionObject



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?

  (:div, caption, class: "hakumi-form-item-extra", id: "#{form_field_name}_caption")
end

#render_errorObject



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?

  (
    :div,
    error_message,
    class: "hakumi-form-item-explain-error",
    id: "#{form_field_name}_error",
    role: "alert"
  )
end

#render_explainObject



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?

  (: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?

  (:div, class: "hakumi-form-item-label") do
    (:label, for: field_id || form_field_name) do
      safe_join([
        (form_field_required ? (:span, "*", class: "hakumi-form-item-required-mark") : nil),
        label
      ].compact)
    end
  end
end

#standalone?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/components/hakumi_components/concerns/form_field.rb', line 70

def standalone?
  form_field_standalone
end