Class: NitroKit::SettingsSection

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

Defined Under Namespace

Classes: Child

Constant Summary collapse

TITLE_LEVELS =
(1..6).freeze

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, level: 2, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ SettingsSection

Returns a new instance of SettingsSection.



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
# File 'app/components/nitro_kit/settings_section.rb', line 8

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

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

Instance Method Details

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



63
64
65
66
# File 'app/components/nitro_kit/settings_section.rb', line 63

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

#form(&content) ⇒ Object

Raises:

  • (ArgumentError)


78
79
80
81
82
83
84
# File 'app/components/nitro_kit/settings_section.rb', line 78

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

  @form = content
  nil
end

#status(component, &content) ⇒ Object

Raises:

  • (ArgumentError)


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

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

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

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



57
58
59
60
61
# File 'app/components/nitro_kit/settings_section.rb', line 57

def title(text = nil, level: nil, &block)
  @title_level = validate_choice!(:level, level, TITLE_LEVELS) if level
  @title_content = declare_content(:title, @title_content, text, &block)
  nil
end

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

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/components/nitro_kit/settings_section.rb', line 35

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

  section(**root_attributes) do
    header(**slot_attributes(:header)) do
      public_send(
        :"h#{@title_level}",
        **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