Created
January 2, 2021 14:48
-
-
Save kylekeesling/98a3727eb1cb8b2a7eb86d80b6f494d4 to your computer and use it in GitHub Desktop.
A Simple Form custom input using stimulus-flatpickr and Bootstrap
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
# frozen_string_literal: true | |
class FlatpickrInput < SimpleForm::Inputs::StringInput | |
def input(wrapper_options) | |
template.content_tag(:div, class: "input-group", | |
data: {controller: "flatpickr", flatpickr_disable_mobile: true}) do | |
template.concat input_addon(calendar_button, data: {toggle: true}) | |
template.concat @builder.text_field(attribute_name, input_html_options) | |
template.concat input_addon(close_button, data: {clear: true}) | |
end | |
end | |
def input_html_options | |
super.merge({class: "form-control", data: {input: ""}}) | |
end | |
def input_addon(addon, **options) | |
template.content_tag(:a, class: "input-group-addon", data: options[:data]) do | |
template.concat addon | |
end | |
end | |
def calendar_button | |
'<i class="fa fa-calendar"></i>'.html_safe | |
end | |
def close_button | |
'<i class="fa fa-close"></i>'.html_safe | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment