Class: Tailwinds::Form::Builder

Inherits:
Tramway::Views::FormBuilder show all
Includes:
Tramway::Utils::Field
Defined in:
app/components/tailwinds/form/builder.rb

Overview

Provides Tailwind-styled forms

Instance Attribute Summary

Attributes inherited from Tramway::Views::FormBuilder

#template

Instance Method Summary collapse

Methods included from Tramway::Utils::Field

#tramway_field

Constructor Details

#initialize(object_name, object, template, options) ⇒ Builder

Returns a new instance of Builder.



11
12
13
14
15
# File 'app/components/tailwinds/form/builder.rb', line 11

def initialize(object_name, object, template, options)
  super

  @form_size = options[:size] || options['size'] || :middle
end

Instance Method Details

#common_field(component_name, input_method, attribute, **options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/components/tailwinds/form/builder.rb', line 17

def common_field(component_name, input_method, attribute, **options, &)
  sanitized_options = sanitize_options(options)

  component_class = "Tailwinds::Form::#{component_name.to_s.camelize}Component".constantize

  render(component_class.new(
           input: input(input_method),
           value: get_value(attribute, sanitized_options),
           **default_options(attribute, sanitized_options)
         ),
         &)
end

#date_field(attribute) ⇒ Object



55
56
57
# File 'app/components/tailwinds/form/builder.rb', line 55

def date_field(attribute, **, &)
  common_field(:date_field, :date_field, attribute, **, &)
end

#email_field(attribute) ⇒ Object



34
35
36
# File 'app/components/tailwinds/form/builder.rb', line 34

def email_field(attribute, **, &)
  common_field(:text_field, :email_field, attribute, **, &)
end

#file_field(attribute, **options) ⇒ Object



59
60
61
62
63
64
# File 'app/components/tailwinds/form/builder.rb', line 59

def file_field(attribute, **options, &)
  sanitized_options = sanitize_options(options)
  input = super(attribute, **sanitized_options.merge(class: :hidden))

  render(Tailwinds::Form::FileFieldComponent.new(input:, **default_options(attribute, sanitized_options)), &)
end

#multiselect(attribute, collection, **options) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'app/components/tailwinds/form/builder.rb', line 77

def multiselect(attribute, collection, **options, &)
  sanitized_options = sanitize_options(options)

  render(Tailwinds::Form::MultiselectComponent.new(
           input: input(:text_field),
           value: sanitized_options[:value] || sanitized_options[:selected] || object.public_send(attribute),
           collection:,
           **default_options(attribute, sanitized_options)
         ), &)
end

#number_field(attribute) ⇒ Object



38
39
40
# File 'app/components/tailwinds/form/builder.rb', line 38

def number_field(attribute, **, &)
  common_field(:number_field, :number_field, attribute, **, &)
end

#password_field(attribute, **options) ⇒ Object



46
47
48
49
50
51
52
53
# File 'app/components/tailwinds/form/builder.rb', line 46

def password_field(attribute, **options, &)
  sanitized_options = sanitize_options(options)

  render(Tailwinds::Form::TextFieldComponent.new(
           input: input(:password_field),
           **default_options(attribute, sanitized_options)
         ), &)
end

#select(attribute, collection, **options) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'app/components/tailwinds/form/builder.rb', line 66

def select(attribute, collection, **options, &)
  sanitized_options = sanitize_options(options)

  render(Tailwinds::Form::SelectComponent.new(
           input: input(:select),
           value: sanitized_options[:selected] || object.public_send(attribute),
           collection: explicitly_add_blank_option(collection, sanitized_options),
           **default_options(attribute, sanitized_options)
         ), &)
end

#submit(action, **options) ⇒ Object



88
89
90
91
92
# File 'app/components/tailwinds/form/builder.rb', line 88

def submit(action, **options, &)
  sanitized_options = sanitize_options(options)

  render(Tailwinds::Form::SubmitButtonComponent.new(action, size: form_size, **sanitized_options), &)
end

#text_area(attribute) ⇒ Object



42
43
44
# File 'app/components/tailwinds/form/builder.rb', line 42

def text_area(attribute, **, &)
  common_field(:text_area, :text_area, attribute, **, &)
end

#text_field(attribute) ⇒ Object



30
31
32
# File 'app/components/tailwinds/form/builder.rb', line 30

def text_field(attribute, **, &)
  common_field(:text_field, :text_field, attribute, **, &)
end