Class: NitroKit::FormSection

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

Defined Under Namespace

Classes: Child

Constant Summary

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Method Summary collapse

Constructor Details

#initialize(title: nil, description: nil, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ FormSection

Returns a new instance of FormSection.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/components/nitro_kit/form_section.rb', line 7

def initialize(
  title: nil,
  description: nil,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @title_content = content_from_keyword(:title, title)
  @description_content = content_from_keyword(:description, description)
  @status = nil
  @form = nil
  @title_id = "#{id || "nk-form-section-#{SecureRandom.hex(4)}"}-title"

  super(
    component: :form_section,
    attributes: { id:, aria: { labelledby: @title_id } }.compact,
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Method Details

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



56
57
58
59
# File 'app/components/nitro_kit/form_section.rb', line 56

def description(text = nil, &block)
  @description_content = declare_content(:description, @description_content, text, &block)
  nil
end

#form(&content) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
# File 'app/components/nitro_kit/form_section.rb', line 71

def form(&content)
  raise ArgumentError, "FormSection form requires a block" unless content
  raise ArgumentError, "FormSection accepts exactly one form" if @form

  @form = content
  nil
end

#status(component, &content) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
# File 'app/components/nitro_kit/form_section.rb', line 61

def status(component, &content)
  unless component.is_a?(NitroKit::Alert)
    raise ArgumentError, "FormSection status must be a NitroKit::Alert"
  end
  raise ArgumentError, "FormSection accepts at most one status" if @status

  @status = Child.new(component:, content:)
  nil
end

#title(text = nil, &block) ⇒ Object



51
52
53
54
# File 'app/components/nitro_kit/form_section.rb', line 51

def title(text = nil, &block)
  @title_content = declare_content(:title, @title_content, text, &block)
  nil
end

#view_template {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/components/nitro_kit/form_section.rb', line 32

def view_template
  yield self if block_given?
  require_content!("FormSection", :title, @title_content)
  raise ArgumentError, "FormSection requires exactly one form" unless @form

  section(**root_attributes) do
    header(**slot_attributes(:header)) do
      h2(**slot_attributes(:title, attributes: { id: @title_id })) do
        render_deferred_content(@title_content)
      end
      if @description_content
        p(**slot_attributes(:description)) { render_deferred_content(@description_content) }
      end
    end
    render_in_slot(@status.component, :status, &@status.content) if @status
    div(**slot_attributes(:form), &@form)
  end
end