From b12ad4abc414d8d00c8d5a48ed444286b26c7883 Mon Sep 17 00:00:00 2001 From: Felipe Renan Date: Thu, 8 Mar 2018 22:50:21 -0300 Subject: [PATCH] Add API to register custom components With this API, Simple Form will be possible to add custom components. Methods defined in a component will be exposed to be used in the wrapper as Simple::Components Examples # The application needs to tell where the components will be. Dir[Rails.root.join('lib/components/**/*.rb')].each { |f| require f } # Create a custom component in the path specified above. # lib/components/input_group_component.rb module InpoutGroupComponent def preprend ... end def append ... end end SimpleForm.setup do |config| # Create a wrapper using the custom component. config.wrappers :input_group, tag: :div, error_class: :error do |b| b.use :label b.optional :prepend b.use :input b.use :append end end # Using the custom component in the form. <%= simple_form_for @blog, wrapper: input_group do |f| %> <%= f.input :title, prepend: true %> <% end %> --- CHANGELOG.md | 3 +- README.md | 86 +++++++++++++++++++ .../config/initializers/simple_form.rb | 7 ++ .../initializers/simple_form_bootstrap.rb | 7 ++ .../initializers/simple_form_foundation.rb | 7 ++ lib/simple_form.rb | 43 ++++++++++ test/components/custom_components_test.rb | 62 +++++++++++++ 7 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 test/components/custom_components_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 566750bd..4c175355 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Unreleased -* Allow custom errors classes to inputs . [@feliperenan](https://github.com/feliperenan) +* Add API to register custom components.[@feliperenan](https://github.com/feliperenan) +* Allow custom errors classes to inputs.[@feliperenan](https://github.com/feliperenan) * Remove support from Rails 4.0, 4.1 and 4.2. [@feliperenan](https://github.com/feliperenan) * Add support for citext, hstore, json & jsonb column types. [@swrobel](https://github.com/swrobel) diff --git a/README.md b/README.md index ee2c5a4f..aa2dc812 100644 --- a/README.md +++ b/README.md @@ -989,6 +989,92 @@ when the content is present. end ``` +## Custom Components + +When you use custom wrappers, you might also be looking for a way to add custom components to your +wrapper. The default components are: + +```ruby +:label # The