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

Allow to define multiple wrapper mappings #1632

Open
JanStevens opened this issue Jan 25, 2019 · 8 comments
Open

Allow to define multiple wrapper mappings #1632

JanStevens opened this issue Jan 25, 2019 · 8 comments

Comments

@JanStevens
Copy link

Environment

  • Ruby 2.5.3
  • Rails 5.2.2
  • Simple Form 4.1.0

Proposal

The current wrapper mappings allow to specify how every field type should be mapped and this allows to have a consistent style across your application. However there is a need to have additional styles / wrappers for different kinds of search inputs, a typical example is search forms. For example in our application we have Vertical Forms, using custom bootstrap wrapper mappings but for the search filters (using ransack) above our tables we would more like an inline style where labels are hidden by default.

Current solution is to always pass in the wrapper_mappings in the simple_form_tag but even with a helper method around it, this is a bit error prone. I propose to add the concept of wrapper sets (as originally proposed in #1229).

For backwards compatibility the default configuration would still be possible

config.wrapper_mappings = {
    boolean:       :custom_boolean,
    check_boxes:   :custom_collection,
    date:          :custom_multi_select,
    datetime:      :custom_multi_select,
    file:          :custom_file,
    radio_buttons: :custom_collection,
    range:         :custom_range,
    time:          :custom_multi_select,
    select:        :custom_multi_select
  }

Internally this will be set on wrapper_mappings[:default] and used across the board, advance usage could then also define a new set of wrappers

config.wrapper_mappings[:search] = {
    boolean:       :custom_boolean_search,
    check_boxes:   :custom_collection_search,
    date:          :custom_multi_select_search,
    datetime:      :custom_multi_select_search,
    file:          :custom_file_search,
    radio_buttons: :custom_collection_search,
    range:         :custom_range_search,
    time:          :custom_multi_select_search,
    select:        :custom_multi_select_search
  }

This could then be used in the form asfollowed

simple_form_for(animal, wrapper_mappings: :search)
OR
simple_form_for(animal, wrapper_set: :search)

I'm willing to spend some time on this if it will be accepted as a MR

Regards,

@feliperenan
Copy link
Collaborator

I'm ok with this proposal. @rafaelfranca any thoughts?

@sliminas
Copy link

One additional thought on this:
From my point of view it would be very helpful to set the wrapper mapping per form wrapper.

If you have multiple form wrappers defined according to the proposal of @JanStevens it would be still necessary to select the wrapper mapping every time I render a form with an explicit wrapper instead of setting a default once when the form wrapper is defined.

I think it would be helpful to end up with a result like this:

config.wrappers :my_special_form do |b|
  b.wrapper_mapping :my_special_mapping
  b.use :input
  # ...
end

config.wrapper_mappings[:my_special_mapping] = {
  boolean: :my_special_boolean,
  # ....
}

Then when using the form in the view like this it would know which form wrapper to use and also automatically which mapping to use for this wrapper:

simple_form_for(animal, wrapper: :my_special_form)

@diesl
Copy link

diesl commented Sep 29, 2020

Multiple wrapper mappings would be really helpful.

@JanStevens Any news on this issue? Are you still planning to submit a pull request?

@JanStevens
Copy link
Author

Sadly no, I've moved on and not actively working with rails anymore 😬

@seballot
Copy link

seballot commented Mar 18, 2021

As a quick workaround, you can create a custom simple_form helper and add some logic there :

# app/helpers/application_helper.rb
module ApplicationHelper
    def my_simple_form_for(record, options, &block)
      if options[:wrapper] == :search && options[:wrapper_mappings].nil?
          options[:wrapper_mappings] = {
            boolean:       :custom_boolean_search,
            check_boxes:   :custom_collection_search,
            .....
          }
      end
      simple_form_for(record, options, &block)
    end
  end

@braindeaf
Copy link

Anyone working on this? I might take a stab otherwise

braindeaf pushed a commit to braindeaf/simple_form that referenced this issue Nov 26, 2021
@braindeaf
Copy link

braindeaf commented Nov 26, 2021

I did a stab...

braindeaf@c08810b
#1751

It's pretty basic really.

@ldonnet
Copy link

ldonnet commented Jan 12, 2023

It could be so helpful if this PR could be merged because it could be very useful in many use cases. On my side, I want to migrate from bootstrap to tailwind with specific simple form wrappers and this PR permits to improve gem uses.

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

No branches or pull requests

7 participants