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.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/components/lightning_ui_kit/table_component.rb', line 24

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

#__with_action_slotObject



19
# File 'app/components/lightning_ui_kit/table_component.rb', line 19

alias_method :__with_action_slot, :with_action

#__with_column_slotObject

ViewComponent funnels slot blocks through Rails' capture, which discards non-String return values (Integers, Dates, ...), rendering blank cells. Route blocks around that wrapper via the cell: argument so capture_cell can evaluate them instead.



14
# File 'app/components/lightning_ui_kit/table_component.rb', line 14

alias_method :__with_column_slot, :with_column

#aria_sort(column) ⇒ Object



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

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

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

#checkbox_icon_classesObject



130
131
132
133
134
135
136
# File 'app/components/lightning_ui_kit/table_component.rb', line 130

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



118
119
120
121
122
123
124
125
126
127
128
# File 'app/components/lightning_ui_kit/table_component.rb', line 118

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



114
115
116
# File 'app/components/lightning_ui_kit/table_component.rb', line 114

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

#current_sort_directionObject



110
111
112
# File 'app/components/lightning_ui_kit/table_component.rb', line 110

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.



106
107
108
# File 'app/components/lightning_ui_kit/table_component.rb', line 106

def current_sort_key
  sort_state[0]
end

#interactive?Boolean

Returns:

  • (Boolean)


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

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

#row_url_for(row) ⇒ Object



75
76
77
# File 'app/components/lightning_ui_kit/table_component.rb', line 75

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

#row_value(row) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/components/lightning_ui_kit/table_component.rb', line 60

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)


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

def selectable?
  @selectable
end

#sort_icon(column) ⇒ Object



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

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



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

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)


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

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

#with_action(&block) ⇒ Object



20
21
22
# File 'app/components/lightning_ui_kit/table_component.rb', line 20

def with_action(&block)
  __with_action_slot(cell: block)
end

#with_column(title, sort_key: nil, align: :left, &block) ⇒ Object



15
16
17
# File 'app/components/lightning_ui_kit/table_component.rb', line 15

def with_column(title, sort_key: nil, align: :left, &block)
  __with_column_slot(title, sort_key: sort_key, align: align, cell: block)
end