Class: NitroKit::DataSection
- Defined in:
- app/components/nitro_kit/data_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
- #actions(component, &content) ⇒ Object
- #description(text = nil, &block) ⇒ Object
- #empty_state(component, &content) ⇒ Object
- #html_table ⇒ Object
-
#initialize(title: nil, description: nil, level: 2, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ DataSection
constructor
A new instance of DataSection.
- #table(component, &content) ⇒ Object
- #title(text = nil, &block) ⇒ Object
- #view_template(&block) ⇒ Object
Constructor Details
#initialize(title: nil, description: nil, level: 2, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ DataSection
Returns a new instance of DataSection.
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/data_section.rb', line 8 def initialize( title: nil, description: nil, level: 2, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil ) @title_content = content_from_keyword(:title, title) @description_content = content_from_keyword(:description, description) @level = validate_choice!(:level, level, TITLE_LEVELS) @actions = nil @content = nil @title_id = "#{id || "nk-data-section-#{SecureRandom.hex(4)}"}-title" super( component: :data_section, attributes: { id:, aria: { labelledby: @title_id } }.compact, html:, aria:, data:, desperately_need_a_class: ) end |
Instance Method Details
#actions(component, &content) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'app/components/nitro_kit/data_section.rb', line 69 def actions(component, &content) ensure_collecting! unless component.is_a?(NitroKit::ButtonGroup) || component.is_a?(NitroKit::Button) raise ArgumentError, "DataSection actions must be a NitroKit::ButtonGroup or NitroKit::Button" end raise ArgumentError, "DataSection accepts at most one actions group" if @actions @actions = Child.new(component:, content:) nil end |
#description(text = nil, &block) ⇒ Object
62 63 64 65 |
# File 'app/components/nitro_kit/data_section.rb', line 62 def description(text = nil, &block) @description_content = declare_content(:description, @description_content, text, &block) nil end |
#empty_state(component, &content) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'app/components/nitro_kit/data_section.rb', line 89 def empty_state(component, &content) unless component.is_a?(NitroKit::EmptyState) raise ArgumentError, "DataSection empty_state must be a NitroKit::EmptyState" end raise ArgumentError, "DataSection EmptyState must use level: 3" if component.level != 3 assign_content(component, &content) end |
#html_table ⇒ Object
67 |
# File 'app/components/nitro_kit/data_section.rb', line 67 alias :html_table :table |
#table(component, &content) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'app/components/nitro_kit/data_section.rb', line 80 def table(component, &content) ensure_collecting! unless table_component?(component) raise ArgumentError, "DataSection table must be a NitroKit::Table or NitroKit::DetailsTable" end assign_content(component, &content) end |
#title(text = nil, &block) ⇒ Object
56 57 58 59 60 |
# File 'app/components/nitro_kit/data_section.rb', line 56 def title(text = nil, &block) ensure_collecting! @title_content = declare_content(:title, @title_content, text, &block) nil end |
#view_template(&block) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/components/nitro_kit/data_section.rb', line 35 def view_template(&block) collect_declarations(&block) require_content!("DataSection", :title, @title_content) raise ArgumentError, "DataSection requires exactly one table or EmptyState" unless @content section(**root_attributes) do header(**slot_attributes(:header)) do div(**slot_attributes(:heading)) do public_send(:"h#{@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(@actions.component, :actions, &@actions.content) if @actions end render_in_slot(@content.component, content_slot, &@content.content) end end |