Class: Forms::Label

Inherits:
Phlex::HTML
  • Object
show all
Defined in:
lib/forms/label.rb

Overview

A standalone form label: <label for><span class="label">text</span></label> with an optional required marker. A block, when given, is yielded directly inside the <label> so checkboxes/toggles can nest as direct children.

For the daisyui v5 "text/icon inside the field" wrapper (

Instance Method Summary collapse

Constructor Details

#initialize(text: nil, for: nil, required: false, **attributes) ⇒ Label

Returns a new instance of Label.



11
12
13
14
15
16
17
# File 'lib/forms/label.rb', line 11

def initialize(text: nil, for: nil, required: false, **attributes)
  @text = text
  @for = grab(for:)
  @required = required
  @attributes = attributes
  super()
end

Instance Method Details

#view_template(&block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/forms/label.rb', line 19

def view_template(&block)
  label(for: @for, class: @attributes[:class], **@attributes.except(:class)) do
    if block
      yield
    elsif @text
      span(class: "label") do
        plain @text
        span(class: "text-error ml-1") { "*" } if @required
      end
    end
  end
end