Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fieldset legend class configuration #1333

Open
pduersteler opened this issue Jan 28, 2021 · 2 comments
Open

Fieldset legend class configuration #1333

pduersteler opened this issue Jan 28, 2021 · 2 comments

Comments

@pduersteler
Copy link

I'm not sure if I just could not find it or if it is indeed something that I'd consider to be a missing thing, so I though I just put it up here and see what happens.

Right now, the formtastic configurations and custom input class possibilities allow for customizing almost anything, which is great. What I am currently looking for is an option to define a default class for the fieldset/legend that is being generated.

<%= f.inputs "My fieldset title" do %>
  ...
<% end %>

The code above will render the following:

<fieldset>
  <legend>
    <span>My fieldset title</span>
  </legend>
  ...
</fieldset>

As my SCSS is BEM-oriented, a configurable option to give it a default class would be very nice. There are already options in the initializer for various things like Formtastic::Helpers::FormHelper.default_form_class. Maybe something along these lines for a default legend class would be an option? I'm not sure if this would collide with the fieldset that is generated for e.g. a checkbox collection, though.

If there is already a way to modify this part, I'd happy to get a hint.

@mikz
Copy link
Contributor

mikz commented Jun 1, 2021

I know it is a poor substitute, but could you create your own semantic form builder and override:

def field_set_legend(html_options)
legend = (html_options[:name] || '').to_s
# only applying if String includes '%i' avoids argument error when $DEBUG is true
legend %= parent_child_index(html_options[:parent]) if html_options[:parent] && legend.include?('%i')
legend = template.content_tag(:legend, template.content_tag(:span, legend.html_safe)) unless legend.blank?
legend
end

And the same applies for fieldset/ol

def field_set_and_list_wrapping(*args, &block) # @private
contents = args[-1].is_a?(::Hash) ? '' : args.pop.flatten
html_options = args.extract_options!
if block_given?
contents = if template.respond_to?(:is_haml?) && template.is_haml?
template.capture_haml(&block)
else
template.capture(&block)
end
end
# Work-around for empty contents block
contents ||= ""
# Ruby 1.9: String#to_s behavior changed, need to make an explicit join.
contents = contents.join if contents.respond_to?(:join)
legend = field_set_legend(html_options)
fieldset = template.content_tag(:fieldset,
legend.html_safe << template.content_tag(:ol, contents.html_safe),
html_options.except(:builder, :parent, :name)
)
fieldset
end

Maybe give it a try in your app and see if that fixes the problem for you.

Possibly the solution could be to make it easier to modify these methods and make them a bit cleaner.

@pduersteler
Copy link
Author

Thanks, this works so far!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants