Class: Superform::Rails::Field

Inherits:
Field show all
Defined in:
lib/superform/rails/field.rb

Overview

The Field class is designed to be extended to create custom forms. To override, in your subclass you may have something like this:

“‘ruby class MyForm < Superform::Rails::Form

class MyLabel < Superform::Rails::Components::Label
  def view_template(&content)
    label(form: @field.dom.name, class: "text-bold", &content)
  end
end

class Field < Field
  def label(**, &)
    MyLabel.new(self, **, &)
  end

  def input(class: nil, **)
    super(class: ["input input-outline", grab(class:)])
  end
end

end “‘

Now all calls to ‘label` will have the `text-bold` class applied to it.

Instance Attribute Summary

Attributes inherited from Field

#dom

Attributes inherited from Node

#key, #parent

Instance Method Summary collapse

Methods inherited from Field

#assign, #collection, #field, inherited, #initialize, #kit, method_added, #value

Methods inherited from Node

#initialize

Constructor Details

This class inherits a constructor from Superform::Field

Instance Method Details

#button(**attributes) ⇒ Object



28
29
30
# File 'lib/superform/rails/field.rb', line 28

def button(**attributes)
  Components::Button.new(field, attributes:)
end

#checkbox(**attributes) ⇒ Object Also known as: check_box



36
37
38
# File 'lib/superform/rails/field.rb', line 36

def checkbox(**attributes)
  Components::Checkbox.new(field, attributes:)
end

#colorObject



112
113
114
# File 'lib/superform/rails/field.rb', line 112

def color(*, **, &)
  input(*, **, type: :color, &)
end

#dateObject



92
93
94
# File 'lib/superform/rails/field.rb', line 92

def date(*, **, &)
  input(*, **, type: :date, &)
end

#datetimeObject



100
101
102
# File 'lib/superform/rails/field.rb', line 100

def datetime(*, **, &)
  input(*, **, type: :"datetime-local", &)
end

#emailObject



71
72
73
# File 'lib/superform/rails/field.rb', line 71

def email(*, **, &)
  input(*, **, type: :email, &)
end

#fileObject



120
121
122
# File 'lib/superform/rails/field.rb', line 120

def file(*, **, &)
  input(*, **, type: :file, &)
end

#hiddenObject



63
64
65
# File 'lib/superform/rails/field.rb', line 63

def hidden(*, **, &)
  input(*, **, type: :hidden, &)
end

#input(**attributes) ⇒ Object



32
33
34
# File 'lib/superform/rails/field.rb', line 32

def input(**attributes)
  Components::Input.new(field, attributes:)
end

#label(**attributes) ⇒ Object



40
41
42
# File 'lib/superform/rails/field.rb', line 40

def label(**attributes, &)
  Components::Label.new(field, attributes:, &)
end

#monthObject



104
105
106
# File 'lib/superform/rails/field.rb', line 104

def month(*, **, &)
  input(*, **, type: :month, &)
end

#numberObject



84
85
86
# File 'lib/superform/rails/field.rb', line 84

def number(*, **, &)
  input(*, **, type: :number, &)
end

#passwordObject



67
68
69
# File 'lib/superform/rails/field.rb', line 67

def password(*, **, &)
  input(*, **, type: :password, &)
end

#radio(value) ⇒ Object



124
125
126
# File 'lib/superform/rails/field.rb', line 124

def radio(value, *, **, &)
  input(*, **, type: :radio, value: value, &)
end

#rangeObject



88
89
90
# File 'lib/superform/rails/field.rb', line 88

def range(*, **, &)
  input(*, **, type: :range, &)
end

#searchObject



116
117
118
# File 'lib/superform/rails/field.rb', line 116

def search(*, **, &)
  input(*, **, type: :search, &)
end

#select(*collection, **attributes) ⇒ Object



48
49
50
# File 'lib/superform/rails/field.rb', line 48

def select(*collection, **attributes, &)
  Components::Select.new(field, attributes:, collection:, &)
end

#telObject Also known as: phone



79
80
81
# File 'lib/superform/rails/field.rb', line 79

def tel(*, **, &)
  input(*, **, type: :tel, &)
end

#textObject

HTML5 input type convenience methods - clean API without _field suffix Examples:

field(:email).email(class: "form-input")
field(:age).number(min: 18, max: 99)
field(:birthday).date
field(:secret).hidden(value: "token123")
field(:gender).radio("male", id: "user_gender_male")


59
60
61
# File 'lib/superform/rails/field.rb', line 59

def text(*, **, &)
  input(*, **, type: :text, &)
end

#textarea(**attributes) ⇒ Object Also known as: text_area



44
45
46
# File 'lib/superform/rails/field.rb', line 44

def textarea(**attributes)
  Components::Textarea.new(field, attributes:)
end

#timeObject



96
97
98
# File 'lib/superform/rails/field.rb', line 96

def time(*, **, &)
  input(*, **, type: :time, &)
end

#titleObject



132
133
134
# File 'lib/superform/rails/field.rb', line 132

def title
  key.to_s.titleize
end

#urlObject



75
76
77
# File 'lib/superform/rails/field.rb', line 75

def url(*, **, &)
  input(*, **, type: :url, &)
end

#weekObject



108
109
110
# File 'lib/superform/rails/field.rb', line 108

def week(*, **, &)
  input(*, **, type: :week, &)
end