heartcombo / simple_form

Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.
http://blog.plataformatec.com.br/tag/simple_form
MIT License
8.21k stars 1.31k forks source link

Ability to render a blank <label> for a boolean input? #1701

Closed KyleCRat closed 2 years ago

KyleCRat commented 4 years ago

I am working on some custom styling around boolean form inputs. I would like to be able to use the default boolean input to achieve two outcomes depending on options passed.

Outcome A:

Render a inline boolean with a normal label via:

= simple_form_for :permissive_action do |f|
  = f.input action.action,
    as: :boolean,
    input_html: { checked: @user.authorized?(action) }

Outcome B:

Render an inline boolean with a blank <label> tag so I can continue to show my custom CSS checkbox.

This causes the label to be removed all together

= simple_form_for :permissive_action do |f|
  = f.input action.action,
    as: :boolean,
    label: false,
    input_html: { checked: @user.authorized?(action) }

This causes the label to pull the name from the field

= simple_form_for :permissive_action do |f|
  = f.input action.action,
    as: :boolean,
    label: '',
    input_html: { checked: @user.authorized?(action) }

I've tried

I ended up Overwriting inputs/boolean_input.rb to allow passing a blank string to render a blank tag. by adding a options[:label] == '' check to inputs/boolean_input.rb:22

21.  ...
20.        if inline_label? || options[:label] == false
21.          input(wrapper_options)
22.        elsif options[:label] == ''
23.          input(wrapper_options) +
24.            @builder.label(label_target, label_html_options) { '' }
25.        elsif nested_boolean_style?
25.  ...

This still feels heavy handed for this. I've looked for a simpler answer to this but have not been able to find anything after quite a while searching. Is there something I am missing by chance?

If I am not missing something and this is not possible, would this be a feature you be interested in adding? Or is there a reason I am not aware of as to why this is not possible?

Thank you in advance for your time and assistance.

Environment

nashby commented 2 years ago

That's how Rails works when you pass an empty string to label helper. An easy workaround for you case is label: '&nbsp'.html_safe