Last active
March 28, 2023 16:25
-
-
Save birchestx/79ffb3d3c3016cb4c6d0 to your computer and use it in GitHub Desktop.
simple_form bootstrap with tooltips and locales
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
In simple_form_bootstrap.rb: | |
b.use :tooltip # enable the tooltip example from below. | |
I added these everywhere I saw word placeholder. | |
Add simple_form_components.rb: | |
module SimpleForm | |
module Components | |
module Tooltips | |
def tooltip(wrapper_options = nil) | |
unless tooltip_text.nil? | |
input_html_options[:rel] ||= 'tooltip' | |
input_html_options['data-toggle'] ||= 'tooltip' | |
input_html_options['data-placement'] ||= tooltip_position | |
input_html_options['data-trigger'] ||= 'hover' | |
input_html_options['data-original-title'] ||= tooltip_text | |
nil | |
end | |
end | |
def tooltip_text | |
tooltip = options[:tooltip] || tooltip_translation | |
if tooltip.is_a?(String) | |
tooltip | |
elsif tooltip.is_a?(Array) | |
tooltip[1] | |
else | |
nil | |
end | |
end | |
def tooltip_position | |
tooltip = options[:tooltip] | |
tooltip.is_a?(Array) ? tooltip[0] : "right" | |
end | |
# First check labels translation and then human attribute name. | |
def tooltip_translation #:nodoc: | |
if translated_tooltip = translate_from_namespace(:tooltips) | |
translated_tooltip | |
end | |
end | |
end | |
end | |
end | |
SimpleForm::Inputs::Base.send(:include, SimpleForm::Components::Tooltips) | |
This also supports translation under tooltips: in simple_form.en.yml | |
In .js file put: | |
$('[data-toggle="tooltip"]').tooltip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment