Module: HakumiComponents::Table::Concerns::CellRendering

Extended by:
ActiveSupport::Concern, T::Helpers, T::Sig
Included in:
HakumiComponents::Table::Component
Defined in:
app/components/hakumi_components/table/concerns/cell_rendering.rb

Constant Summary collapse

RowSource =
T.type_alias { HakumiComponents::Table::RowRecord::Source }
DragHandleCellState =
T.type_alias { { drag_handle: T::Boolean } }
SelectionCellState =
T.type_alias { { type: Symbol, row_key: RowSource, checked: T::Boolean } }
ExpandCellState =
T.type_alias { { row_key: RowSource, expanded: T::Boolean } }
CellState =
T.type_alias { T.any(DragHandleCellState, SelectionCellState, ExpandCellState) }
CellContent =
T.type_alias { T.any(CellState, RowSource, Types::ComponentRenderable) }
SpanAttributes =
T.type_alias { T::Hash[Symbol, Types::HtmlPrimitive] }

Instance Method Summary collapse

Instance Method Details

#cell_attributes(column, row, row_index, depth, row_key = nil, raw_value = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/components/hakumi_components/table/concerns/cell_rendering.rb', line 38

def cell_attributes(column, row, row_index, depth, row_key = nil, raw_value = nil)
  attrs = T.let({
    class: cell_classes(column),
    data: {
      column_key: column.key,
      row_key: row_key,
      sort_value: scalar_dom_value(extract_sort_value(column, row, raw_value)),
      value: scalar_dom_value(raw_value)
    }.compact
  }, Types::HtmlAttributes)

  attrs[:style] = "text-align: #{column.align};" if column.align
  attrs[:style] = [ attrs[:style], "width: #{column.width};" ].compact.join(" ") if column.width
  on_cell = column.on_cell
  if on_cell
    cell_attrs = on_cell.call(row.source, row_index)
    if cell_attrs.is_a?(Hash)
      attrs[:colspan] = cell_attrs[:colspan]
      attrs[:rowspan] = cell_attrs[:rowspan]
      attrs[:class] = [ attrs[:class], cell_attrs[:class] ].compact.join(" ")
      attrs[:style] = [ attrs[:style], cell_attrs[:style] ].compact.join(" ")
    end
  end
  if depth.positive? && column == cell_first_data_column
    attrs[:style] = [ attrs[:style], "padding-left: #{depth * table_indent_size}px;" ].compact.join(" ")
  end
  apply_cell_editable_attributes!(attrs, column)
  apply_cell_should_update_attributes!(attrs, column)
  apply_cell_fixed_column_attributes!(attrs, column, header: false)
  attrs
end

#cell_classes(column) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'app/components/hakumi_components/table/concerns/cell_rendering.rb', line 136

def cell_classes(column)
  classes = T.let([ "hakumi-table-cell" ], T::Array[String])
  classes.concat(responsive_classes(column))
  classes << "hakumi-table-cell-ellipsis" if cell_ellipsis_config(column)
  classes << "hakumi-table-cell-selection" if column.selection?
  classes << "hakumi-table-cell-expand" if column.expand?
  class_name = column.class_name
  classes << class_name if class_name
  classes.join(" ")
end

#cell_content(column, row, row_index, raw_value = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'app/components/hakumi_components/table/concerns/cell_rendering.rb', line 85

def cell_content(column, row, row_index, raw_value = nil)
  return selection_cell(row, row_index) if column.selection?
  return expand_cell(row, row_index) if column.expand?
  return { drag_handle: true } if column.drag_handle?

  value = raw_value
  value = row.value(column.data_index) if value.nil? && column.data_index
  renderer = column.render
  rendered_value = renderer ? renderer.call(value, row.source, row_index) : value
  rendered_value.is_a?(ViewComponent::Base) ? rendered_value : rendered_value
end

#header_cell_attributes(column, col_span:, row_span:) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/components/hakumi_components/table/concerns/cell_rendering.rb', line 71

def header_cell_attributes(column, col_span:, row_span:)
  attrs = T.let({
    class: header_cell_classes(column),
    data: header_cell_data_attributes(column)
  }, Types::HtmlAttributes)
  attrs[:colspan] = col_span if col_span > 1
  attrs[:rowspan] = row_span if row_span > 1
  attrs[:style] = "text-align: #{column.align};" if column.align
  attrs[:style] = [ attrs[:style], "width: #{column.width};" ].compact.join(" ") if column.width
  apply_cell_fixed_column_attributes!(attrs, column, header: true)
  attrs
end

#header_cell_classes(column) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'app/components/hakumi_components/table/concerns/cell_rendering.rb', line 124

def header_cell_classes(column)
  classes = T.let([ "hakumi-table-cell", "hakumi-table-header-cell" ], T::Array[String])
  classes.concat(responsive_classes(column))
  classes << "hakumi-table-cell-ellipsis" if cell_ellipsis_config(column)
  classes << "hakumi-table-cell-sortable" if column_sortable?(column)
  classes << "hakumi-table-cell-filterable" if column_filterable?(column)
  class_name = column.class_name
  classes << class_name if class_name
  classes.join(" ")
end

#render_cell_display(column, content, raw_value, row, row_index) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/components/hakumi_components/table/concerns/cell_rendering.rb', line 98

def render_cell_display(column, content, raw_value, row, row_index)
  config = cell_ellipsis_config(column)

  value_fragment = if content.is_a?(ViewComponent::Base)
    T.unsafe(self).render(content)
  else
    content
  end
  value_fragment = raw_value if value_fragment.nil?

  span_options = T.let({ class: "hakumi-table-cell-content" }, SpanAttributes)
  title_text = cell_ellipsis_title(config, raw_value, value_fragment)
  span_options[:title] = title_text if title_text.present?

  display_value = if value_fragment.is_a?(String) || value_fragment.is_a?(ActiveSupport::SafeBuffer)
    value_fragment
  else
    value_fragment.to_s
  end
  inner = T.unsafe(self).tag.span(**span_options) { display_value }

  tooltip_component = cell_ellipsis_tooltip_component(config, raw_value, row.source, row_index)
  tooltip_component ? T.unsafe(self).render(tooltip_component) { inner } : inner
end