Generate labels for attribute automatically

I'm not sure why it isn't already done by foundation's form builder. It
doesn't make any sense to change an ID of a form field without changing
the `for` attribute of its label.
This commit is contained in:
Javi Martín
2019-10-03 19:09:28 +02:00
parent 8d9cb4d8e3
commit aec84f6522
5 changed files with 22 additions and 11 deletions

View File

@@ -6,8 +6,7 @@
<%= f.text_area :body,
id: "comment-body-#{css_id}",
maxlength: Comment.body_max_length,
label: leave_comment_text(commentable),
label_options: { for: "comment-body-#{css_id}" } %>
label: leave_comment_text(commentable) %>
<%= f.hidden_field :commentable_type, value: commentable.class.name %>
<%= f.hidden_field :commentable_id, value: commentable.id %>
@@ -20,16 +19,14 @@
<div class="float-right">
<%= f.check_box :as_moderator,
label: t("comments.form.comment_as_moderator"),
id: "comment-as-moderator-#{css_id}",
label_options: { for: "comment-as-moderator-#{css_id}" } %>
id: "comment-as-moderator-#{css_id}" %>
</div>
<% end %>
<% if can? :comment_as_administrator, commentable %>
<div class="float-right">
<%= f.check_box :as_administrator,
label: t("comments.form.comment_as_admin"),
id: "comment-as-administrator-#{css_id}",
label_options: { for: "comment-as-administrator-#{css_id}" } %>
id: "comment-as-administrator-#{css_id}" %>
</div>
<% end %>

View File

@@ -25,8 +25,11 @@
<% css_id = parent_or_commentable_dom_id(nil, annotation) %>
<div id="js-comment-form-annotation-<%= annotation.id %>" style="display:none" class="comment-form js-comment-form-annotation">
<%= form_for @comment, url: legislation_process_draft_version_annotation_new_comment_path(annotation.draft_version.process, annotation.draft_version, annotation), remote: true do |f| %>
<%= label_tag "comment-body-#{css_id}", leave_comment_text(annotation) %>
<%= f.text_area :body, id: "comment-body-#{css_id}", maxlength: Comment.body_max_length, label: false, rows: 8 %>
<%= f.text_area :body,
id: "comment-body-#{css_id}",
maxlength: Comment.body_max_length,
label: leave_comment_text(annotation),
rows: 8 %>
<%= f.submit comment_button_text(nil, annotation), class: "button" %>
<% end %>
</div>

View File

@@ -19,8 +19,10 @@
<% progress_options = { min: ProgressBar::RANGE.min, max: ProgressBar::RANGE.max, step: 1 } %>
<div class="row">
<div class="small-12 medium-6 large-2 column">
<%= f.label :percentage %>
<%= f.text_field :percentage, { type: :range,
id: "percentage_range",
label: false,
class: "column" }.merge(progress_options) %>
</div>

View File

@@ -77,7 +77,6 @@
<div class="small-12 medium-8 column">
<%= f.check_box :valuation_finished,
label: t("valuation.budget_investments.edit.valuation_finished"),
label_options: { for: "js-investment-report-alert" },
id: "js-investment-report-alert",
"data-alert": t("valuation.budget_investments.edit.valuation_finished_alert"),
"data-not-feasible-alert": t("valuation.budget_investments.edit.not_feasible_alert") %>

View File

@@ -9,7 +9,7 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder
%i[text_field text_area cktext_area number_field password_field email_field].each do |field|
define_method field do |attribute, options = {}|
label_with_hint(attribute, options) +
label_with_hint(attribute, options.merge(label_options: label_options_for(options))) +
super(attribute, options.merge(
label: false, hint: false,
aria: { describedby: help_text_id(attribute, options) }
@@ -21,7 +21,7 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder
if options[:label] != false
label = content_tag(:span, label_text(object, attribute, options[:label]), class: "checkbox")
super(attribute, options.merge(label: label))
super(attribute, options.merge(label: label, label_options: label_options_for(options)))
else
super
end
@@ -42,6 +42,16 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder
end
end
def label_options_for(options)
label_options = options[:label_options] || {}
if options[:id]
{ for: options[:id] }.merge(label_options)
else
label_options
end
end
def help_text(attribute, options)
if options[:hint]
content_tag :span, options[:hint], class: "help-text", id: help_text_id(attribute, options)