Class: Pulse::Table

Inherits:
Component
  • Object
show all
Defined in:
app/components/pulse/table.rb,
app/components/pulse/table/cell.rb,
app/components/pulse/table/column.rb

Overview

Renders a table Inspiration taken from https://github.com/baoagency/polaris_view_components/

Defined Under Namespace

Classes: Cell, Column

Constant Summary collapse

DEFAULT_TABLE_CLASSES =
'pulse-min-w-full pulse-divide-y ' \
'pulse-divide-gray-300'
DEFAULT_HEAD_CLASSES =
'pulse-bg-gray-100'
DEFAULT_TH_CLASSES =
'pulse-text-start pulse-text-gray pulse-uppercase ' \
'pulse-tracking-wider pulse-px-4 print:pulse-px-2 ' \
'pulse-py-4 print:pulse-py-3 text-xs pulse-font-medium'
DEFAULT_BODY_CLASSES =
'pulse-bg-white pulse-divide-y pulse-text-sm ' \
'pulse-leading-extra-snug pulse-divide-gray-300 ' \
'print:pulse-divide-gray-400 pulse-tracking-tight ' \
''
DEFAULT_ROW_CLASSES =
'pulse-border-secondary-300'
DEFAULT_TD_CLASSES =
'pulse-px-4 print:pulse-px-2 pulse-py-4'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, additional_params: {}, pagination: true, striped: false, current_sort_column: nil, current_sort_direction: nil, hoverable: false, row_classes: nil, header: true, scroll: true, **options) ⇒ Table

Returns a new instance of Table.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/components/pulse/table.rb', line 32

def initialize(collection, additional_params: {}, pagination: true,
               striped: false, current_sort_column: nil,
               current_sort_direction: nil, hoverable: false,
               row_classes: nil, header: true, scroll: true, **options)
  @collection = collection
  @additional_params = additional_params
  @pagination = pagination
  @striped = striped
  @current_sort_column = current_sort_column
  @current_sort_direction = current_sort_direction
  @hoverable = hoverable
  @row_classes = row_classes
  @header = header
  @scroll = scroll
  @options = options
end

Instance Attribute Details

#additional_paramsObject (readonly)

Returns the value of attribute additional_params.



28
29
30
# File 'app/components/pulse/table.rb', line 28

def additional_params
  @additional_params
end

#collectionObject (readonly)

Returns the value of attribute collection.



28
29
30
# File 'app/components/pulse/table.rb', line 28

def collection
  @collection
end

#current_sort_columnObject (readonly)

Returns the value of attribute current_sort_column.



28
29
30
# File 'app/components/pulse/table.rb', line 28

def current_sort_column
  @current_sort_column
end

#current_sort_directionObject (readonly)

Returns the value of attribute current_sort_direction.



28
29
30
# File 'app/components/pulse/table.rb', line 28

def current_sort_direction
  @current_sort_direction
end

#full_widthObject (readonly)

Returns the value of attribute full_width.



28
29
30
# File 'app/components/pulse/table.rb', line 28

def full_width
  @full_width
end

#headerObject (readonly)

Returns the value of attribute header.



28
29
30
# File 'app/components/pulse/table.rb', line 28

def header
  @header
end

#hoverableObject (readonly)

Returns the value of attribute hoverable.



28
29
30
# File 'app/components/pulse/table.rb', line 28

def hoverable
  @hoverable
end

#paginationObject (readonly)

Returns the value of attribute pagination.



28
29
30
# File 'app/components/pulse/table.rb', line 28

def pagination
  @pagination
end

#row_classesObject (readonly)

Returns the value of attribute row_classes.



28
29
30
# File 'app/components/pulse/table.rb', line 28

def row_classes
  @row_classes
end

#scrollObject (readonly)

Returns the value of attribute scroll.



28
29
30
# File 'app/components/pulse/table.rb', line 28

def scroll
  @scroll
end

#stripedObject (readonly)

Returns the value of attribute striped.



28
29
30
# File 'app/components/pulse/table.rb', line 28

def striped
  @striped
end

Instance Method Details

#body_classesObject



81
82
83
84
85
86
87
# File 'app/components/pulse/table.rb', line 81

def body_classes
  merge_classes(
    DEFAULT_BODY_CLASSES,
    '[&>tr:nth-child(even)]:pulse-bg-gray-50': striped,
    'pulse-border-b [&>tr]:pulse-border-t [&>tr:first-child]:pulse-border-t-0': !striped # rubocop:disable Layout/LineLength
  )
end

#footer?Boolean

Returns:

  • (Boolean)


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

def footer?
  columns.any?(&:footer)
end

#head_cell_classesObject



75
76
77
78
79
# File 'app/components/pulse/table.rb', line 75

def head_cell_classes
  merge_classes(
    DEFAULT_TH_CLASSES
  )
end

#head_classesObject



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

def head_classes
  merge_classes(
    DEFAULT_HEAD_CLASSES,
    @options[:head_classes]
  )
end

#pagination?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'app/components/pulse/table.rb', line 49

def pagination?
  pagination
end

#render_cell(**arguments) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'app/components/pulse/table.rb', line 101

def render_cell(**arguments, &)
  render(
    Pulse::Table::Cell.new(
      additional_params:,
      **arguments
    ),
    &
  )
end

#row_arguments(row) ⇒ Object



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

def row_arguments(row)
  { tag: 'tr' }.tap do |args|
    args[:classes] = merge_classes(
      DEFAULT_ROW_CLASSES,
      { 'hover:pulse-bg-dark-charcoal-50': hoverable },
      row_classes
    )
    args[:id] = dom_id(row) if row.respond_to?(:to_key)
    args[:data] ||= {}
  end
end

#table_optionsObject



57
58
59
60
61
62
63
64
65
66
# File 'app/components/pulse/table.rb', line 57

def table_options
  { tag: 'table' }.deep_merge(@options).tap do |args|
    args[:classes] = merge_classes(
      DEFAULT_TABLE_CLASSES,
      'pulse-container pulse-mx-auto',
      args[:classes]
    )
    args[:data] ||= {}
  end
end