Based off the bootstrap helpers mentioned in the simple_form wiki, with the following, I was able to simply add a tooltip like so:
<%= f.input :phone, placeholder: "Phone Number", tooltip: 'We need your phone number because...' %>
I just needed to add the tooltip component below:
# config/initializers/simple_form_components.rb:
module SimpleForm
module Components
module Tooltips
def tooltip(wrapper_options = nil)
@tooltip ||= begin
content = tooltip_text
options[:tooltip_html] ||= {}
options[:tooltip_html][:title] = content
options[:tooltip_html]['data-toggle'] = 'tooltip'
options[:tooltip_html]['data-placement'] = tooltip_position
options[:tooltip_html]['tabindex'] = '0'
content ? '' : nil
end
end
def tooltip_text
tooltip = options[:tooltip]
if tooltip.is_a?(String)
html_escape(tooltip)
elsif tooltip.is_a?(Array)
if tooltip[1].is_a?(String)
html_escape(tooltip[1])
else
content = translate_from_namespace(:tooltips)
content.html_safe if content
end
else
content = translate_from_namespace(:tooltips)
content.html_safe if content
end
end
def tooltip_position
tooltip = options[:tooltip]
tooltip.is_a?(Array) ? tooltip[0] : "right"
end
end
end
end
SimpleForm::Inputs::Base.send(:include, SimpleForm::Components::Tooltips)
And change the config/initializers/simple_form_bootstrap.rb
file to include the use :tooltip
component after each use :label
, like so:
# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
config.error_notification_class = 'alert alert-danger'
config.button_class = 'btn btn-default'
config.boolean_label_class = nil
config.wrappers :vertical_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :minlength
b.optional :pattern
b.optional :min_max
b.optional :readonly
b.use :label, class: 'control-label'
b.use :tooltip, wrap_with: { tag: 'span', class: 'glyphicon glyphicon-question-sign text-muted' }
b.use :input, class: 'form-control'
b.use :error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
config.wrappers :vertical_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :minlength
b.optional :readonly
b.use :label, class: 'control-label'
b.use :tooltip, wrap_with: { tag: 'span', class: 'glyphicon glyphicon-question-sign text-muted' }
b.use :input
b.use :error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
... rest of file omitted for brevity...
And with a little css to space it correctly:
span[data-toggle='tooltip'] {
margin-left: .25em;
}
And the javascript that bootstrap requires to activate it:
$('[data-toggle="tooltip"]').tooltip();
It worked great. I'm using bootstrap 3, rails 5 and simple_form 4.