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

Feature Request: Add Support to Customize Input Mappings #1799

Closed klondikemarlen closed 2 months ago

klondikemarlen commented 1 year ago

Precheck

Environment

Current behavior

If I want to customize the search input, e.g. f.input(:search, as: :search), I can't do so in a comfortable way. I can do f.input(:search, as: :custom_search) and build a CustomSearchInput < SimpleForm::Inputs::StringInput, but this doesn't feel very natural. I can also do SimpleForm::FormBuilder.mappings[:search] = CustomSearchInput, but this doesn't use a public interface.

Expected behavior

To be able to tweak the default SimpleForm::FormBuilder.mappings[:search] via a config option. e.g.

# config/initializers/simple_form.rb
SimpleForm.setup do |config|
  config.form_builder_input_mappings = {
    search: CustomSearchInput,
  }
end
nashby commented 2 months ago

Hey @klondikemarlen! There's exactly what you want in README:

You can also redefine existing Simple Form inputs by creating a new class with the same name. For instance, if you want to wrap date/time/datetime in a div, you can do:


# app/inputs/date_time_input.rb
class DateTimeInput < SimpleForm::Inputs::DateTimeInput
  def input(wrapper_options)
    template.content_tag(:div, super)
  end
end
klondikemarlen commented 2 months ago

So this would be?

# app/inputs/search.rb
class Search < SimpleForm::Inputs::StringInput
  def input(wrapper_options)
    # custom input code?
  end
end

Honestly its been so long, I don't even remember the problem I was trying to solve. I'm using Vue for front-end work now.

nashby commented 2 months ago

@klondikemarlen yeah, something like that