Class: TableComponent

Inherits:
Component show all
Defined in:
app/components/table_component.rb

Overview

Table — data tables with column accumulator pattern.

Usage:

Table(striped: true, rows: @users) { |c|
  c.column(:name, heading: "Name") { |user|
    Text(weight: :bold) { text user.name }
  }
  c.column(:email) { |user|
    Text(color: :grey) { text user.email }
  }
}

Or with manual content:

Table(celled: true) { |c|
  c.header {
    TableRow {
      TableCell(heading: true) { text "Name" }
      TableCell(heading: true) { text "Age" }
    }
  }
  TableRow {
    TableCell { text "Alice" }
    TableCell { text "30" }
  }
}

Constant Summary

Constants inherited from Component

Component::HTML_OPTIONS

Instance Method Summary collapse

Methods inherited from Component

default, #initialize, #render_in, slot

Constructor Details

This class inherits a constructor from Component

Instance Method Details

#column(key, heading: key.to_s.titleize, &block) ⇒ Object



57
58
59
# File 'app/components/table_component.rb', line 57

def column(key, heading: key.to_s.titleize, &block)
  columns << { key: key, heading: heading, block: block }
end

#columnsObject



53
54
55
# File 'app/components/table_component.rb', line 53

def columns
  @columns ||= []
end

#to_sObject



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
87
88
89
90
# File 'app/components/table_component.rb', line 61

def to_s
  classes = class_names(
    "ui",
    size,
    color,
    basic_class,
    compact_class,
    padded_class,
    attached_class,
    { "definition" => definition,
      "structured" => structured,
      "single line" => single_line,
      "fixed" => fixed,
      "selectable" => selectable,
      "striped" => striped,
      "sortable" => sortable,
      "celled" => celled,
      "collapsing" => collapsing,
      "inverted" => inverted,
      "unstackable" => unstackable,
      "stackable" => stackable },
    "table"
  )

  if columns.any? && rows
    column_table(classes)
  else
    manual_table(classes)
  end
end