Class: LightningUiKit::TableComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/lightning_ui_kit/table_component.rb

Instance Method Summary collapse

Constructor Details

#initialize(data:, selectable: false, select_name: "ids[]", row_id: :id, row_url: nil, sort_key: nil, sort_direction: nil, sort_param: :sort, direction_param: :direction, selected_label: "selected", actions_title: "Actions", empty_message: "No data available") ⇒ TableComponent

Returns a new instance of TableComponent.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/components/lightning_ui_kit/table_component.rb', line 10

def initialize(
  data:,
  selectable: false,
  select_name: "ids[]",
  row_id: :id,
  row_url: nil,
  sort_key: nil,
  sort_direction: nil,
  sort_param: :sort,
  direction_param: :direction,
  selected_label: "selected",
  actions_title: "Actions",
  empty_message: "No data available"
)
  @data = data
  @selectable = selectable
  @select_name = select_name
  @row_id = row_id
  @row_url = row_url
  @sort_key = sort_key
  @sort_direction = sort_direction
  @sort_param = sort_param
  @direction_param = direction_param
  @selected_label = selected_label
  @actions_title = actions_title
  @empty_message = empty_message
end

Instance Method Details

#aria_sort(column) ⇒ Object



84
85
86
87
88
# File 'app/components/lightning_ui_kit/table_component.rb', line 84

def aria_sort(column)
  return unless sorted_by?(column)

  (current_sort_direction == "desc") ? "descending" : "ascending"
end

#checkbox_icon_classesObject



116
117
118
119
120
121
122
# File 'app/components/lightning_ui_kit/table_component.rb', line 116

def checkbox_icon_classes
  <<~CLASSES.squish
    lui:pointer-events-none lui:absolute lui:inset-0 lui:m-auto lui:size-4
    lui:stroke-foreground-invert lui:opacity-0
    lui:peer-checked:opacity-100 lui:peer-indeterminate:opacity-100 lui:sm:size-3.5
  CLASSES
end

#checkbox_input_classesObject



104
105
106
107
108
109
110
111
112
113
114
# File 'app/components/lightning_ui_kit/table_component.rb', line 104

def checkbox_input_classes
  <<~CLASSES.squish
    lui:peer lui:size-[1.125rem] lui:shrink-0 lui:cursor-pointer lui:appearance-none
    lui:rounded-[0.3125rem] lui:border lui:border-border-strong lui:bg-surface-input lui:shadow-sm
    lui:hover:border-border-hover lui:sm:size-4
    lui:checked:border-border-invert lui:checked:bg-surface-invert lui:checked:hover:border-border-invert
    lui:indeterminate:border-border-invert lui:indeterminate:bg-surface-invert lui:indeterminate:hover:border-border-invert
    lui:focus-visible:outline lui:focus-visible:outline-2 lui:focus-visible:outline-offset-2 lui:focus-visible:outline-focus
    lui:disabled:cursor-not-allowed lui:disabled:opacity-50
  CLASSES
end

#colspanObject



100
101
102
# File 'app/components/lightning_ui_kit/table_component.rb', line 100

def colspan
  columns.size + (selectable? ? 1 : 0) + (actions.present? ? 1 : 0)
end

#current_sort_directionObject



96
97
98
# File 'app/components/lightning_ui_kit/table_component.rb', line 96

def current_sort_direction
  sort_state[1]
end

#current_sort_keyObject

Current sort state: the sort/direction request params when present (validated), otherwise the explicit sort_key:/sort_direction: defaults.



92
93
94
# File 'app/components/lightning_ui_kit/table_component.rb', line 92

def current_sort_key
  sort_state[0]
end

#interactive?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/components/lightning_ui_kit/table_component.rb', line 42

def interactive?
  selectable? || !@row_url.nil?
end

#row_url_for(row) ⇒ Object



61
62
63
# File 'app/components/lightning_ui_kit/table_component.rb', line 61

def row_url_for(row)
  @row_url&.call(row)
end

#row_value(row) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/components/lightning_ui_kit/table_component.rb', line 46

def row_value(row)
  case @row_id
  when Proc
    @row_id.call(row)
  else
    if row.is_a?(Hash)
      row[@row_id] || row[@row_id.to_s]
    elsif row.respond_to?(@row_id)
      row.public_send(@row_id)
    elsif row.respond_to?(:[])
      row[@row_id] || row[@row_id.to_s]
    end
  end
end

#selectable?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/components/lightning_ui_kit/table_component.rb', line 38

def selectable?
  @selectable
end

#sort_icon(column) ⇒ Object



78
79
80
81
82
# File 'app/components/lightning_ui_kit/table_component.rb', line 78

def sort_icon(column)
  return "chevron-up-down" unless sorted_by?(column)

  (current_sort_direction == "desc") ? "chevron-down" : "chevron-up"
end

#sort_url(column) ⇒ Object



69
70
71
72
73
74
75
76
# File 'app/components/lightning_ui_kit/table_component.rb', line 69

def sort_url(column)
  direction = (sorted_by?(column) && current_sort_direction == "asc") ? "desc" : "asc"
  query = current_query_parameters.merge(
    @sort_param.to_s => column.sort_key.to_s,
    @direction_param.to_s => direction
  )
  "#{current_path}?#{query.to_query}"
end

#sorted_by?(column) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/components/lightning_ui_kit/table_component.rb', line 65

def sorted_by?(column)
  column.sortable? && column.sort_key.to_s == current_sort_key
end