Class: BetterUi::General::Table::ThComponent

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(theme: :default, sortable: false, sorted: false, sort_direction: :asc, scope: "col", **html_options) ⇒ ThComponent

Returns a new instance of ThComponent.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/components/better_ui/general/table/th_component.rb', line 7

def initialize(
  theme: :default,
  sortable: false,
  sorted: false,
  sort_direction: :asc,
  scope: "col",
  **html_options
)
  @theme = theme.to_sym
  @sortable = !!sortable
  @sorted = !!sorted
  @sort_direction = sort_direction.to_sym
  @scope = scope
  @html_options = html_options
end

Instance Attribute Details

#scopeObject (readonly)

Returns the value of attribute scope.



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

def scope
  @scope
end

#sort_directionObject (readonly)

Returns the value of attribute sort_direction.



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

def sort_direction
  @sort_direction
end

#sortableObject (readonly)

Returns the value of attribute sortable.



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

def sortable
  @sortable
end

#sortedObject (readonly)

Returns the value of attribute sorted.



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

def sorted
  @sorted
end

#themeObject (readonly)

Returns the value of attribute theme.



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

def theme
  @theme
end

Instance Method Details

#sort_iconObject



39
40
41
42
43
44
45
46
47
# File 'app/components/better_ui/general/table/th_component.rb', line 39

def sort_icon
  return unless @sortable

  if @sorted
    @sort_direction == :asc ? "" : ""
  else
    ""
  end
end

#th_attributesObject



32
33
34
35
36
37
# File 'app/components/better_ui/general/table/th_component.rb', line 32

def th_attributes
  attrs = @html_options.except(:class)
  attrs[:class] = th_classes
  attrs[:scope] = @scope
  attrs
end

#th_classesObject



23
24
25
26
27
28
29
30
# File 'app/components/better_ui/general/table/th_component.rb', line 23

def th_classes
  [
    "px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",
    @sortable ? "cursor-pointer hover:bg-gray-200" : nil,
    @sorted ? "bg-gray-200" : nil,
    @html_options[:class]
  ].compact.join(" ")
end