Class: NitroKit::DetailsTable

Inherits:
Component
  • Object
show all
Includes:
Phlex::Rails::Helpers::Routes
Defined in:
app/components/nitro_kit/details_table.rb

Defined Under Namespace

Classes: Field

Constant Summary

Constants inherited from Component

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, caption: nil, label: nil, empty_text: nil, boolean_labels: nil, route_base: nil, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ DetailsTable

Returns a new instance of DetailsTable.



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/details_table.rb', line 11

def initialize(
  record,
  caption: nil,
  label: nil,
  empty_text: nil,
  boolean_labels: nil,
  route_base: nil,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @record = record
  @route_base = route_base
  @caption = caption.nil? ? nil : validate_text!("caption", caption)
  @empty_text = validate_text!("empty_text", empty_text || I18n.t("nitro_kit.details_table.empty"))
  @boolean_labels = validate_boolean_labels!(boolean_labels || default_boolean_labels)
  @fields = []
  @table = Table.new(
    table_aria: label.nil? ? {} : { label: validate_text!("label", label) }
  )

  super(
    component: :details_table,
    attributes: { id: }.compact,
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



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

def record
  @record
end

#route_baseObject (readonly)

Returns the value of attribute route_base.



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

def route_base
  @route_base
end

Instance Method Details

#field(attribute, label: nil, value: UNSET, &content) ⇒ Object



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

def field(attribute, label: nil, value: UNSET, &content)
  attribute = validate_attribute!(attribute)
  if @fields.any? { |field| field.attribute.to_s == attribute.to_s }
    raise ArgumentError, "DetailsTable field keys must be unique: #{attribute.inspect}"
  end
  label = label.nil? ? attribute.to_s.humanize : validate_text!("field label", label)
  resolved_value = value.equal?(UNSET) ? resolve_attribute(attribute) : value

  @fields << Field.new(attribute:, label:, value: resolved_value, content:)
  nil
end

#fields(*attributes) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
65
# File 'app/components/nitro_kit/details_table.rb', line 60

def fields(*attributes)
  raise ArgumentError, "DetailsTable fields requires at least one attribute" if attributes.empty?

  attributes.each { |attribute| field(attribute) }
  nil
end

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

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/components/nitro_kit/details_table.rb', line 46

def view_template
  yield self if block_given?
  raise ArgumentError, "DetailsTable requires at least one field" if @fields.empty?

  div(**root_attributes) do
    render_in_slot(@table, :table) do
      @table.caption(@caption) if @caption
      @table.tbody do
        @fields.each { |field| render_field(field) }
      end
    end
  end
end