Method: SimpleForm.include_component

Defined in:
lib/simple_form.rb

.include_component(component) ⇒ Object

Includes a component to be used by Simple Form. 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 InputGroupComponent
  def prepend
    ...
  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 %>


331
332
333
334
335
336
337
# File 'lib/simple_form.rb', line 331

def self.include_component(component)
  if Module === component
    SimpleForm::Inputs::Base.include(component)
  else
    raise TypeError, "SimpleForm.include_component expects a module but got: #{component.class}"
  end
end