simple_form
simple_form copied to clipboard
Allow to define multiple wrapper mappings
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,
I'm ok with this proposal. @rafaelfranca any thoughts?
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)
Multiple wrapper mappings would be really helpful.
@JanStevens Any news on this issue? Are you still planning to submit a pull request?
Sadly no, I've moved on and not actively working with rails anymore 😬
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
Anyone working on this? I might take a stab otherwise
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.
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.