Skip to content

Instantly share code, notes, and snippets.

@andrewaguiar
Last active October 17, 2019 04:09
Show Gist options
  • Save andrewaguiar/39ca5e9a906b01f1bd00e27b3030c768 to your computer and use it in GitHub Desktop.
Save andrewaguiar/39ca5e9a906b01f1bd00e27b3030c768 to your computer and use it in GitHub Desktop.
A FormBuilder based on tachyons with (trix editor, toggle, react support and more)
# frozen_string_literal: true
class TachyonsFormBuilder < ActionView::Helpers::FormBuilder
include ActionView::Helpers::TagHelper
include ActionView::Context
include React::Rails::ViewHelper
def input_classes
'input-reset ba b--black-40 pa3 mt2 mb3 db w-100 f5 dark-gray'
end
def button_submit_classes
'f5 link ph4 pv3 mv2 dib white pointer lh-copy w-100 white bg-primary-blue hover-bg-primary-blue bb--primary-blue'
end
def color_input_classes
'input-reset ba b--black-40 mt2 mb3 db w-10 f5 dark-gray pa0 h4'
end
def label(method, options = {})
new_options = options.merge('class': 'f5 b normal black-60')
super(method, new_options)
end
def explanation(_field, text)
content_tag :span, "(#{text})", class: 'f6 i fw1'
end
def email_field(method, options = {})
new_options = options.merge('class': input_classes)
super(method, new_options)
end
def color_field(method, options = {})
new_options = options.merge('class': color_input_classes)
super(method, new_options)
end
def text_field(method, options = {})
new_options = options.merge('class': input_classes)
super(method, new_options)
end
def number_field(method, options = {})
new_options = options.merge('class': input_classes)
super(method, new_options)
end
def select(method, choices = nil, options = {}, html_options = {}, &block)
new_html_options = html_options.merge('class': input_classes)
super(method, choices, options, new_html_options, &block)
end
def collection_select(method,
collection,
value_method,
text_method,
options = {},
html_options = {})
new_html_options = html_options.merge('class': input_classes)
super(method,
collection,
value_method,
text_method,
options,
new_html_options)
end
def text_area(method, options = {})
new_options = options.merge('class': input_classes)
super(method, new_options)
end
def toggle_field(method, options = {})
new_options = options.merge('class': 'toggle')
content_tag(:div, class: 'w-100 db mt2 mb3') do
check_box(method, new_options)
end
end
def trix_editor(method, options = {})
# name and value must stay in hidden field
options = options.except(:value, :name)
input_id = options[:id] || "#{object_name}_#{method}"
id = "#{input_id}_trix"
content_tag(:'trix-editor', '', options.merge(id: id, input: input_id))
end
def trix_editor_field(method, options = {})
[
hidden_field(method, options),
trix_editor(method, options)
].join.html_safe
end
def password_field(method, options = {})
new_options = options.merge('class': input_classes)
super(method, new_options)
end
def submit(value = nil, options = {})
options = options.merge('class': button_submit_classes)
super(value, options)
end
end
ActionView::Base.default_form_builder = TachyonsFormBuilder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment