Class: BetterUi::General::Table::Component

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/better_ui/general/table/component.rb

Constant Summary collapse

TABLE_BASE_CLASSES =

Classi base sempre presenti

"w-full table-auto border-collapse"
TABLE_THEME =

Temi di colore con classi Tailwind dirette

{
  default: "bg-gray-50 text-gray-900",
  white: "bg-white text-gray-900", 
  red: "bg-red-50 text-red-900",
  rose: "bg-rose-50 text-rose-900",
  orange: "bg-orange-50 text-orange-900",
  green: "bg-green-50 text-green-900",
  blue: "bg-blue-50 text-blue-900",
  yellow: "bg-yellow-50 text-yellow-900",
  violet: "bg-violet-50 text-violet-900"
}.freeze
TABLE_RADIUS =

Opzioni di bordi arrotondati con classi Tailwind dirette

{
  none: "rounded-none",
  small: "rounded-md",
  medium: "rounded-lg", 
  large: "rounded-xl",
  full: "rounded-full"
}.freeze
CONTAINER_BASE_CLASSES =

Classi per container

"overflow-x-auto"
THEAD_CLASSES =

Classi per elementi della tabella

"bg-gray-100 border-b border-gray-200"
TBODY_CLASSES =
""
TFOOT_CLASSES =
"bg-gray-50 border-t border-gray-200"
TR_CLASSES =
"border-b border-gray-100 hover:bg-gray-50"
TH_CLASSES =
"px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
TD_CLASSES =
"px-4 py-3 text-sm text-gray-900"
TF_CLASSES =
"px-4 py-3 text-sm font-medium text-gray-900"
CAPTION_CLASSES =
"mb-2 text-sm text-gray-600 text-left"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data: nil, headers: nil, caption: nil, theme: :default, radius: :small, striped: false, hoverable: false, bordered: false, compact: false, minimal: false, footer: nil, header_rows_partial: nil, body_row_partial: nil, footer_rows_partial: nil, thead_partial: nil, tfoot_partial: nil, **html_options) ⇒ Component

Returns a new instance of Component.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/components/better_ui/general/table/component.rb', line 47

def initialize(
  data: nil,
  headers: nil, 
  caption: nil, 
  theme: :default, 
  radius: :small, 
  striped: false, 
  hoverable: false, 
  bordered: false, 
  compact: false,
  minimal: false,
  footer: nil, 
  header_rows_partial: nil, 
  body_row_partial: nil, 
  footer_rows_partial: nil,
  thead_partial: nil,
  tfoot_partial: nil,
  **html_options
)
  @data = data || []
  @headers = headers
  @caption = caption
  @theme = theme.to_sym
  @radius = radius.to_sym
  # Flag boolean combinabili
  @striped = !!striped
  @hoverable = !!hoverable
  @bordered = !!bordered
  @compact = !!compact
  @minimal = !!minimal
  @footer = footer.is_a?(Array) ? footer : nil
  @header_rows_partial = header_rows_partial
  @body_row_partial = body_row_partial
  @footer_rows_partial = footer_rows_partial
  @thead_partial = thead_partial
  @tfoot_partial = tfoot_partial
  @html_options = html_options
  
  validate_params
end

Instance Attribute Details

#body_row_partialObject (readonly)

Returns the value of attribute body_row_partial.



5
6
7
# File 'app/components/better_ui/general/table/component.rb', line 5

def body_row_partial
  @body_row_partial
end

#borderedObject (readonly)

Returns the value of attribute bordered.



5
6
7
# File 'app/components/better_ui/general/table/component.rb', line 5

def bordered
  @bordered
end

#captionObject (readonly)

Returns the value of attribute caption.



5
6
7
# File 'app/components/better_ui/general/table/component.rb', line 5

def caption
  @caption
end

#compactObject (readonly)

Returns the value of attribute compact.



5
6
7
# File 'app/components/better_ui/general/table/component.rb', line 5

def compact
  @compact
end

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'app/components/better_ui/general/table/component.rb', line 5

def data
  @data
end

Returns the value of attribute footer.



5
6
7
# File 'app/components/better_ui/general/table/component.rb', line 5

def footer
  @footer
end

Returns the value of attribute footer_rows_partial.



5
6
7
# File 'app/components/better_ui/general/table/component.rb', line 5

def footer_rows_partial
  @footer_rows_partial
end

#header_rows_partialObject (readonly)

Returns the value of attribute header_rows_partial.



5
6
7
# File 'app/components/better_ui/general/table/component.rb', line 5

def header_rows_partial
  @header_rows_partial
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'app/components/better_ui/general/table/component.rb', line 5

def headers
  @headers
end

#hoverableObject (readonly)

Returns the value of attribute hoverable.



5
6
7
# File 'app/components/better_ui/general/table/component.rb', line 5

def hoverable
  @hoverable
end

#minimalObject (readonly)

Returns the value of attribute minimal.



5
6
7
# File 'app/components/better_ui/general/table/component.rb', line 5

def minimal
  @minimal
end

#stripedObject (readonly)

Returns the value of attribute striped.



5
6
7
# File 'app/components/better_ui/general/table/component.rb', line 5

def striped
  @striped
end

#tfoot_partialObject (readonly)

Returns the value of attribute tfoot_partial.



5
6
7
# File 'app/components/better_ui/general/table/component.rb', line 5

def tfoot_partial
  @tfoot_partial
end

#thead_partialObject (readonly)

Returns the value of attribute thead_partial.



5
6
7
# File 'app/components/better_ui/general/table/component.rb', line 5

def thead_partial
  @thead_partial
end

Instance Method Details

#caption_classesObject



133
134
135
# File 'app/components/better_ui/general/table/component.rb', line 133

def caption_classes
  CAPTION_CLASSES
end

#combined_classesObject

Combina tutte le classi per la tabella



89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/components/better_ui/general/table/component.rb', line 89

def combined_classes
  [
    TABLE_BASE_CLASSES,
    get_theme_class,
    @bordered ? "border border-gray-200" : nil,
    @striped ? "[&_tbody_tr:nth-child(odd)]:bg-gray-50" : nil,
    @hoverable ? "[&_tbody_tr]:hover:bg-gray-50" : nil,
    @compact ? "[&_td]:py-1 [&_th]:py-1" : nil,
    @minimal ? "border-0" : nil,
    @html_options[:class]
  ].compact.join(" ")
end

#container_attributesObject

Restituisce gli attributi HTML per il container



118
119
120
121
122
# File 'app/components/better_ui/general/table/component.rb', line 118

def container_attributes
  {
    class: table_container_classes
  }
end

#get_cell_value(row, header) ⇒ Object

Ottiene il valore di una cella in modo consistente



180
181
182
183
184
185
186
187
188
189
190
# File 'app/components/better_ui/general/table/component.rb', line 180

def get_cell_value(row, header)
  if row.is_a?(Hash)
    row[header.to_s] || row[header.to_sym] || "—"
  elsif row.respond_to?(header.to_sym)
    row.send(header.to_sym)
  elsif row.is_a?(Array) && headers_for_display.index(header)
    row[headers_for_display.index(header)] || "—"
  else
    "—"
  end
end

#get_radius_classObject



124
125
126
# File 'app/components/better_ui/general/table/component.rb', line 124

def get_radius_class
  TABLE_RADIUS[@radius] || TABLE_RADIUS[:small]
end

#get_theme_classObject



128
129
130
# File 'app/components/better_ui/general/table/component.rb', line 128

def get_theme_class
  TABLE_THEME[@theme] || TABLE_THEME[:default]
end

#headers_for_displayObject



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/components/better_ui/general/table/component.rb', line 165

def headers_for_display
  return @headers if @headers.present?
  return [] if @data.empty?
  
  case first_item = @data.first
  when Hash
    first_item.keys
  when -> (item) { item.respond_to?(:attributes) }
    first_item.attributes.keys - %w[id created_at updated_at]
  else
    []
  end
end

#render?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'app/components/better_ui/general/table/component.rb', line 192

def render?
  true
end

#table_attributesObject

Restituisce gli attributi HTML per la tabella



103
104
105
106
107
# File 'app/components/better_ui/general/table/component.rb', line 103

def table_attributes
  attrs = @html_options.except(:class)
  attrs[:class] = combined_classes
  attrs
end

#table_container_classesObject

Combina le classi per il container



110
111
112
113
114
115
# File 'app/components/better_ui/general/table/component.rb', line 110

def table_container_classes
  [
    CONTAINER_BASE_CLASSES,
    get_radius_class
  ].compact.join(" ")
end

#tbody_classesObject



141
142
143
# File 'app/components/better_ui/general/table/component.rb', line 141

def tbody_classes
  TBODY_CLASSES
end

#td_classesObject



157
158
159
# File 'app/components/better_ui/general/table/component.rb', line 157

def td_classes
  TD_CLASSES
end

#tf_classesObject



161
162
163
# File 'app/components/better_ui/general/table/component.rb', line 161

def tf_classes
  TF_CLASSES
end

#tfoot_classesObject



145
146
147
# File 'app/components/better_ui/general/table/component.rb', line 145

def tfoot_classes
  TFOOT_CLASSES
end

#th_classesObject



153
154
155
# File 'app/components/better_ui/general/table/component.rb', line 153

def th_classes
  TH_CLASSES
end

#thead_classesObject



137
138
139
# File 'app/components/better_ui/general/table/component.rb', line 137

def thead_classes
  THEAD_CLASSES
end

#tr_classes(index) ⇒ Object



149
150
151
# File 'app/components/better_ui/general/table/component.rb', line 149

def tr_classes(index)
  TR_CLASSES
end