-
-
Save sjava/1457742 to your computer and use it in GitHub Desktop.
SimpleForm Nested Label Component for Twitter Bootstrap CSS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# <rails_root>/lib/simple_form/label_nested_input.rb | |
module SimpleForm | |
module Components | |
module LabelInput | |
extend ActiveSupport::Concern | |
included do | |
include SimpleForm::Components::Labels | |
end | |
# Create a html label with nested input and span (containing label text). | |
# Used to encapsulate the html layout required to support | |
# Twitter's Bootstrap checkbox and radio button approach. | |
# | |
# Example: | |
# | |
# <label for="foo"> | |
# <input name="foo" type="hidden" value="0" /> | |
# <input id="foo" name="foo" type="checkbox" value="1" /> | |
# <span>This is the label text</span> | |
# </label> | |
# | |
# Note: A bug somewhere between Rails & HAML prevents the use of | |
# a block assignment of the content to the label. So, we are | |
# just verbosely assigning it inline as a method param. | |
# | |
def label_nested_input | |
@builder.label(label_target, | |
input + template.content_tag('span', label_text), | |
label_html_options) | |
end | |
end | |
end | |
end | |
# include this config/initializers/simple_form.rb | |
config.wrappers :label_wrapped, :class => "clearfix", :error_class => :error do |b| | |
b.use :placeholder | |
b.use :tag => 'div', :class => 'input inputs-list' do |div| | |
div.use :label_nested_input | |
div.use :error, :tag => :span, :class => :'help-inline' | |
end | |
end | |
# use in erb/haml like this | |
= form.input :remember_me, :as => :boolean, :wrapper => :label_wrapped, :label => 'Remember me on this computer' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment