Class: OkonomiUiKit::Components::TableBuilder

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::CaptureHelper, ActionView::Helpers::TagHelper
Defined in:
app/helpers/okonomi_ui_kit/components/table.rb

Instance Method Summary collapse

Constructor Details

#initialize(template, style_provider, variant = :default) ⇒ TableBuilder

Returns a new instance of TableBuilder.



51
52
53
54
55
56
57
58
# File 'app/helpers/okonomi_ui_kit/components/table.rb', line 51

def initialize(template, style_provider, variant = :default)
  @template = template
  @style_provider = style_provider
  @variant = variant
  @current_row_cells = []
  @in_header = false
  @in_body = false
end

Instance Method Details

#body(&block) ⇒ Object



68
69
70
71
72
73
74
# File 'app/helpers/okonomi_ui_kit/components/table.rb', line 68

def body(&block)
  @in_header = false
  @in_body = true
  result = tag.tbody(class: style(:body, :base), &block)
  @in_body = false
  result
end

#empty_state(title: "No records found", icon: "heroicons/outline/document", colspan: nil, &block) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/helpers/okonomi_ui_kit/components/table.rb', line 124

def empty_state(title: "No records found", icon: "heroicons/outline/document", colspan: nil, &block)
  content = if block_given?
    capture(&block)
  else
    tag.div(class: style(:empty_state, :wrapper)) do
      icon_content = if @template.respond_to?(:ui)
        @template.ui.icon(icon, class: style(:empty_state, :icon))
      else
        tag.div(class: style(:empty_state, :icon))
      end

      icon_content + tag.p(title, class: style(:empty_state, :title)) +
      tag.p("Get started by creating a new record.", class: style(:empty_state, :subtitle))
    end
  end

  tr do
    td(colspan: colspan, class: style(:empty_state, :cell)) do
      content
    end
  end
end

#head(&block) ⇒ Object



60
61
62
63
64
65
66
# File 'app/helpers/okonomi_ui_kit/components/table.rb', line 60

def head(&block)
  @in_header = true
  @in_body = false
  result = tag.thead(&block)
  @in_header = false
  result
end

#td(align: :left, **options, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'app/helpers/okonomi_ui_kit/components/table.rb', line 113

def td(align: :left, **options, &block)
  content = capture(&block) if block_given?

  # Store cell data for later processing in tr
  cell = { type: :td, align: align, options: options, content: content }
  @current_row_cells << cell

  # Return empty string for now, actual rendering happens in tr
  ""
end

#th(scope: "col", align: :left, **options, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'app/helpers/okonomi_ui_kit/components/table.rb', line 102

def th(scope: "col", align: :left, **options, &block)
  content = capture(&block) if block_given?

  # Store cell data for later processing in tr
  cell = { type: :th, scope: scope, align: align, options: options, content: content }
  @current_row_cells << cell

  # Return empty string for now, actual rendering happens in tr
  ""
end

#tr(&block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/helpers/okonomi_ui_kit/components/table.rb', line 76

def tr(&block)
  @current_row_cells = []

  # Collect all cells first
  yield if block_given?

  # Now render each cell with proper first/last detection
  rendered_cells = @current_row_cells.map.with_index do |cell, index|
    is_first = index == 0
    is_last = index == @current_row_cells.length - 1

    if cell[:type] == :th
      render_th(cell, is_first, is_last)
    else
      render_td(cell, is_first, is_last)
    end
  end

  result = tag.tr do
    @template.safe_join(rendered_cells)
  end

  @current_row_cells = []
  result
end