Skip to content

Instantly share code, notes, and snippets.

@frznk-tank
Last active July 18, 2018 00:01
Show Gist options
  • Save frznk-tank/1c8071f533815cc42e2e6ee849b2af4e to your computer and use it in GitHub Desktop.
Save frznk-tank/1c8071f533815cc42e2e6ee849b2af4e to your computer and use it in GitHub Desktop.
SimpleForm Initializer for Foundation 6 with additional markup to allow styling of checkboxes, radio buttons and file controls
# lib/components/control_indicator_helper.rb
module ControlIndicatorComponent
def select_arrow(wrapper_options=nil)
@arrow_icon ||= begin
if options[:arrow_icon].nil? || options[:arrow_icon].to_s == 'default'
''
else
options[:arrow_icon].to_s.html_safe
end
end
end
def checkbox_radio_indicator(wrapper_options=nil)
@check_icon ||= begin
if options[:check_icon].nil? || options[:check_icon].to_s == 'default'
''
else
options[:check_icon].to_s.html_safe
end
end
end
# https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/inputs/collection_radio_buttons_input.rb#L4
def checkbox_radio_collection(wrapper_options=nil)
label_method, value_method = detect_collection_methods
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
input_options.merge!(
item_wrapper_tag: nil,
item_label_class: SimpleFormConfig.config.control_class,
boolean_style: :nested
)
template.capture do
template.concat template.content_tag(:legend, label_text)
template.concat template.content_tag(:div, @builder.send(:"collection_#{input_type}",
attribute_name,
collection,
value_method,
label_method,
input_options,
merged_input_options,
&ovrd_collection_block_for_nested_boolean_style
), class: SimpleFormConfig.config.control_group_class)
end
end
private
def ovrd_collection_block_for_nested_boolean_style
proc { |builder| ovrd_build_nested_boolean_style_item_tag(builder) }
end
def ovrd_build_nested_boolean_style_item_tag(collection_builder)
template.capture do
template.concat collection_builder.send(:"#{input_type.to_s.singularize}")
template.concat template.content_tag(:span, select_arrow, class: SimpleFormConfig.config.indicator_class)
template.concat template.content_tag(:span, collection_builder.text, class: SimpleFormConfig.config.label_class)
end
end
end
SimpleForm.include_component(ControlIndicatorComponent)
# config/initializers/simple_form.rb
# frozen_string_literal: true
# See https://github.com/plataformatec/simple_form#custom-components
Dir[Rails.root.join('lib/components/**/*.rb')].each { |f| require f }
module SimpleFormConfig
include ActiveSupport::Configurable
class << self
def configure
yield config
end
end
end
SimpleFormConfig.configure do |config|
config.button_class = 'button'
config.default_form_class = 'form'
config.default_input_class = 'form-input'
config.label_class = 'form-control__label'
config.indicator_class = 'form-control__indicator'
config.collection_wrapper_class = 'form-control__collection'
config.error_notification_class = 'form__error-notification'
config.control_class = 'form-control'
config.select_control_class = 'form-control-select'
config.control_group_class = 'form-control-group'
config.control_group_label_class = 'form-control-group__label'
config.control_error_class = 'form-control__error'
config.control_hint_class = 'form-control__hint'
config.input_field_valid_class = 'is-valid'
config.input_field_error_class = 'is-invalid'
config.wrapper_hint_class = 'has-hint'
config.wrapper_error_class = 'has-error'
end
SimpleForm.setup do |config|
config.button_class = SimpleFormConfig.config.button_class
config.default_form_class = SimpleFormConfig.config.default_form_class
config.label_class = SimpleFormConfig.config.label_class
config.collection_wrapper_class = SimpleFormConfig.config.collection_wrapper_class
config.error_notification_class = SimpleFormConfig.config.error_notification_class
config.input_field_valid_class = SimpleFormConfig.config.input_field_valid_class
config.input_field_error_class = SimpleFormConfig.config.input_field_error_class
default_wrapper_options = {
hint_class: SimpleFormConfig.config.wrapper_hint_class,
error_class: SimpleFormConfig.config.wrapper_error_class,
class: SimpleFormConfig.config.default_input_class
}
default_label_wrapper_options = {
tag: 'label', class: SimpleFormConfig.config.control_class
}
default_hint_options = {
tag: 'p', class: SimpleFormConfig.config.control_hint_class
}
default_error_options = {
tag: 'p', class: SimpleFormConfig.config.control_error_class
}
default_label_options = {
tag: 'span', class: config.label_class
}
default_indicator_options = {
tag: 'span', class: SimpleFormConfig.config.indicator_class
}
# ==== Default
config.wrappers :default, default_wrapper_options do |b|
# Extensions
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :min_max
b.optional :minlength
b.optional :pattern
b.optional :readonly
# Components
b.use :label_input
b.use :hint, wrap_with: default_hint_options
b.use :full_error, wrap_with: default_error_options
end
# ==== Text Input
config.wrappers :f6_text, default_wrapper_options do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :min_max
b.optional :minlength
b.optional :pattern
b.optional :readonly
b.wrapper default_label_wrapper_options do |bb|
bb.use :label_text, wrap_with: default_label_options
bb.use :input
end
b.use :hint, wrap_with: default_hint_options
b.use :full_error, wrap_with: default_error_options
end
# ==== Select Input
config.wrappers :f6_select, default_wrapper_options do |b|
b.use :html5
b.optional :readonly
b.wrapper default_label_wrapper_options do |bb|
bb.use :label_text, wrap_with: default_label_options
bb.wrapper tag: :div, class: SimpleFormConfig.config.select_control_class do |bbb|
bbb.use :input
bbb.use :select_arrow, wrap_with: default_indicator_options
end
end
b.use :hint, wrap_with: default_hint_options
b.use :full_error, wrap_with: default_error_options
end
# ==== Boolean Input
config.wrappers :f6_boolean, default_wrapper_options do |b|
b.use :html5
b.optional :readonly
b.wrapper default_label_wrapper_options do |bb|
bb.use :input, boolean_style: :inline
bb.use :checkbox_radio_indicator, wrap_with: default_indicator_options
bb.use :label_text, wrap_with: default_label_options
end
b.use :hint, wrap_with: default_hint_options
b.use :full_error, wrap_with: default_error_options
end
# ==== Checkbox Collection
config.wrappers :f6_check_boxes, default_wrapper_options do |b|
b.use :html5
b.optional :readonly
b.wrapper tag: :fieldset, class: config.collection_wrapper_class do |bb|
bb.use :checkbox_radio_collection
end
b.use :hint, wrap_with: default_hint_options
b.use :full_error, wrap_with: default_error_options
end
# ==== Radio Button Collection
config.wrappers :f6_radio_buttons, default_wrapper_options do |b|
b.use :html5
b.optional :readonly
b.wrapper tag: :fieldset, class: config.collection_wrapper_class do |bb|
bb.use :checkbox_radio_collection
end
b.use :hint, wrap_with: default_hint_options
b.use :full_error, wrap_with: default_error_options
end
config.default_wrapper = :f6_text
# Define the way to render check boxes / radio buttons with labels.
# Defaults to :nested for bootstrap config.
# inline: input + label
# nested: label > input
config.boolean_style = :inline
# Method used to tidy up errors. Specify any Rails Array method.
# :first lists the first message for each field.
# Use :to_sentence to list all errors for each field.
config.error_method = :first
config.error_notification_tag = :div
config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
config.collection_value_methods = [ :id, :to_s ]
# You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
# config.collection_wrapper_tag = nil
# You can wrap each item in a collection of radio/check boxes with a tag,
# defaulting to :span.
# config.item_wrapper_tag = :span
# You can define a class to use in all item wrappers. Defaulting to none.
config.item_wrapper_class = '123'
# How the label text should be generated altogether with the required text.
config.label_text = lambda { |label, required, explicit_label| "#{label} #{required}" }
# You can define which elements should obtain additional classes
# config.generate_additional_classes_for = [:wrapper, :label, :input]
# Whether attributes are required by default (or not). Default is true.
config.required_by_default = false
# Tell browsers whether to use the native HTML5 validations (novalidate form option).
# These validations are enabled in SimpleForm's internal config but disabled by default
# in this configuration, which is recommended due to some quirks from different browsers.
# To stop SimpleForm from generating the novalidate option, enabling the HTML5 validations,
# change this configuration to true.
config.browser_validations = false
# Collection of methods to detect if a file type was given.
config.file_methods = [ :mounted_as, :file?, :public_filename, :attached? ]
# Custom mappings for input types. This should be a hash containing a regexp
# to match as key, and the input type that will be used when the field name
# matches the regexp as value.
# config.input_mappings = { /count/ => :integer }
# Custom wrappers for input types. This should be a hash containing an input
# type as key and the wrapper that will be used for all inputs with specified type.
config.wrapper_mappings = {
string: :f6_text,
text: :f6_text,
select: :f6_select,
boolean: :f6_boolean,
check_boxes: :f6_check_boxes,
radio_buttons: :f6_radio_buttons
}
# Namespaces where SimpleForm should look for custom input classes that
# override default inputs.
# config.custom_inputs_namespaces << "CustomInputs"
# Default priority for time_zone inputs.
# config.time_zone_priority = nil
# Default priority for country inputs.
# config.country_priority = nil
# When false, do not use translations for labels.
# config.translate_labels = true
# Automatically discover new inputs in Rails' autoload path.
# config.inputs_discovery = true
# Cache SimpleForm inputs discovery
# config.cache_discovery = !Rails.env.development?
# Default class for inputs
config.input_class = nil
# Define the default class of the input wrapper of the boolean input.
config.boolean_label_class = nil
# Defines if the default input wrapper class should be included in radio
# collection wrappers.
# config.include_default_input_wrapper_class = true
config.i18n_scope = 'simple_form'
end
ruby:
collection_options = ['Options 1', 'Options 2', 'Options 3', 'Options 4']
= simple_form_for :styleguide do |f|
fieldset.form__inputs
legend String
.grid-x.grid-margin-x
.cell.auto = f.input :string
.cell.auto = f.input :disabled_string, disabled: true
fieldset.form__inputs
legend Text
.grid-x.grid-margin-x
.cell.auto = f.input :text, as: :text
.cell.auto = f.input :disabled_text, as: :text, disabled: true
fieldset.form__inputs
legend Select
.grid-x.grid-margin-x
.cell.auto = f.input :select, as: :select, collection: collection_options
.cell.auto = f.input :disabled_select, as: :select, collection: collection_options, disabled: true
fieldset.form__inputs
legend File
.grid-x.grid-margin-x
.cell.auto = f.input :file, as: :file
.cell.auto = f.input :disabled_file, as: :file, disabled: true
fieldset.form__inputs
legend Boolean
.grid-x.grid-margin-x
.cell.auto = f.input :boolean, as: :boolean, label: 'Boolean label'
.cell.auto = f.input :disabled_boolean, as: :boolean, disabled: true
fieldset.form__inputs
legend Checkbox Collection
.grid-x.grid-margin-x
.cell.auto = f.input :check_boxes, as: :check_boxes, collection: collection_options
.cell.auto = f.input :disabled_check_boxes, as: :check_boxes, collection: collection_options, disabled: true
fieldset.form__inputs
legend Radio Button Collection
.grid-x.grid-margin-x
.cell.auto = f.input :basic_radio_buttons, as: :radio_buttons, collection: collection_options
.cell.auto = f.input :disabled_radio_buttons, as: :radio_buttons, collection: collection_options, disabled: true
fieldset.form__actions
= button_tag 'Submit', class: 'button'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment