simple_form icon indicating copy to clipboard operation
simple_form copied to clipboard

Allow to define multiple wrapper mappings

Open JanStevens opened this issue 6 years ago • 8 comments
trafficstars

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,

JanStevens avatar Jan 25 '19 09:01 JanStevens

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

feliperenan avatar Feb 05 '19 13:02 feliperenan

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)

sliminas avatar Nov 12 '19 10:11 sliminas

Multiple wrapper mappings would be really helpful.

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

diesl avatar Sep 29 '20 21:09 diesl

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

JanStevens avatar Sep 30 '20 16:09 JanStevens

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

seballot avatar Mar 18 '21 06:03 seballot

Anyone working on this? I might take a stab otherwise

braindeaf avatar Oct 22 '21 07:10 braindeaf

I did a stab...

https://github.com/braindeaf/simple_form/commit/c08810bc81813e1bc7ef6cb4ebde78f4bfbbf7ff https://github.com/heartcombo/simple_form/pull/1751

It's pretty basic really.

braindeaf avatar Nov 26 '21 15:11 braindeaf

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.

ldonnet avatar Jan 12 '23 21:01 ldonnet