Class: NitroKit::Field

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/field.rb

Instance Attribute Summary collapse

Attributes inherited from Component

#attrs

Instance Method Summary collapse

Methods inherited from Component

#builder, from_template

Constructor Details

#initialize(form = nil, field_name = nil, as: :string, label: nil, description: nil, errors: nil, wrapper: {}, options: nil, include_blank: nil, prompt: nil, **attrs) ⇒ Field



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/components/nitro_kit/field.rb', line 5

def initialize(
  form = nil,
  field_name = nil,
  as: :string,
  label: nil,
  description: nil,
  errors: nil,
  wrapper: {},
  options: nil,
  include_blank: nil,
  prompt: nil,
  **attrs
)
  @form = form
  @field_name = field_name.to_s
  @as = as.is_a?(String) ? as.to_sym : as

  @name = attrs[:name] || form&.field_name(field_name)
  @id = attrs[:id] || form&.field_id(field_name)

  # select, radio group
  @options = options
  @include_blank = include_blank
  @prompt = prompt

  @field_attrs = attrs
  @field_label = label.nil? ? field_name.to_s.humanize : label
  @field_description = description
  @field_error_messages = errors

  @wrapper = wrapper

  super(
    wrapper,
    data: { as: @as.to_s },
    class: base_class
  )
end

Instance Attribute Details

#asObject (readonly)

Returns the value of attribute as.



44
45
46
# File 'app/components/nitro_kit/field.rb', line 44

def as
  @as
end

#field_attrsObject (readonly)

Returns the value of attribute field_attrs.



44
45
46
# File 'app/components/nitro_kit/field.rb', line 44

def field_attrs
  @field_attrs
end

#field_descriptionObject (readonly)

Returns the value of attribute field_description.



44
45
46
# File 'app/components/nitro_kit/field.rb', line 44

def field_description
  @field_description
end

#field_error_messagesObject (readonly)

Returns the value of attribute field_error_messages.



44
45
46
# File 'app/components/nitro_kit/field.rb', line 44

def field_error_messages
  @field_error_messages
end

#field_labelObject (readonly)

Returns the value of attribute field_label.



44
45
46
# File 'app/components/nitro_kit/field.rb', line 44

def field_label
  @field_label
end

#formObject (readonly)

Returns the value of attribute form.



44
45
46
# File 'app/components/nitro_kit/field.rb', line 44

def form
  @form
end

#idObject (readonly)

Returns the value of attribute id.



44
45
46
# File 'app/components/nitro_kit/field.rb', line 44

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



44
45
46
# File 'app/components/nitro_kit/field.rb', line 44

def name
  @name
end

Instance Method Details

#control(**attrs) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/components/nitro_kit/field.rb', line 105

def control(**attrs)
  builder do
    case as
    when :string
      input(**attrs)
    when
        :button,
        :color,
        :date,
        :datetime,
        :datetime_local,
        :email,
        :file,
        :hidden,
        :month,
        :number,
        :password,
        :range,
        :search,
        :tel,
        :text,
        :time,
        :url,
        :week
      input(type: as, **attrs)
    when :select
      select(**attrs)
    when :textarea
      textarea(**attrs)
    when :checkbox
      checkbox(**attrs)
    when :combobox
      combobox(**attrs)
    when :radio, :radio_button, :radio_group
      radio_group(**attrs)
    when :switch
      switch(**attrs)
    when Class
      component(**attrs)
    else
      raise ArgumentError, "Invalid field type `#{as}'"
    end
  end
end

#description(text = nil, **attrs, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'app/components/nitro_kit/field.rb', line 79

def description(text = nil, **attrs, &block)
  builder do
    text ||= field_description

    return unless text || block_given?

    div(**mattr(attrs, data: { slot: "description" }, class: description_class)) do
      text_or_block(text, &block)
    end
  end
end

#errors(error_messages = nil, **attrs) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/components/nitro_kit/field.rb', line 91

def errors(error_messages = nil, **attrs)
  builder do
    error_messages ||= field_error_messages

    return unless error_messages&.any?

    ul(**mattr(attrs, data: { slot: "error" }, class: error_class)) do |msg|
      error_messages.each do |msg|
        li { msg }
      end
    end
  end
end

#html_labelObject



65
# File 'app/components/nitro_kit/field.rb', line 65

alias :html_label :label

#label(text = nil, **attrs) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'app/components/nitro_kit/field.rb', line 67

def label(text = nil, **attrs)
  builder do
    text ||= field_label

    return unless text

    render(Label.new(**mattr(attrs, for: id, data: { slot: "label" }))) do
      text
    end
  end
end

#view_templateObject



55
56
57
58
59
60
61
62
63
# File 'app/components/nitro_kit/field.rb', line 55

def view_template
  div(**attrs) do
    if block_given?
      yield
    else
      default_field
    end
  end
end