Class: HakumiComponents::Table::Component

Constant Summary collapse

TableContent =
T.type_alias { T.nilable(T.any(Types::Renderable, Proc)) }
RowSource =
T.type_alias { HakumiComponents::Table::RowRecord::Source }
RowKeyConfig =
T.type_alias { HakumiComponents::Table::RowRecord::RowKeyConfig }
FilterModeValue =
T.type_alias { T.nilable(Types::HtmlKey) }
FilterSearchValue =
T.type_alias { HakumiComponents::Table::DefinitionTypes::SearchValue }
ScrollAxisValue =
T.type_alias { T.nilable(T.any(String, Numeric)) }
ScrollInput =
T.type_alias { T.nilable(T::Hash[Types::HtmlKey, ScrollAxisValue]) }
RowClassName =
T.type_alias { T.nilable(T.any(String, Proc)) }
PaginationValue =
T.type_alias { HakumiComponents::Table::Configs::Pagination::Value }
ControllerLocalScalar =
T.type_alias { T.nilable(Types::ValidationPrimitive) }
ControllerLocalCollection =
T.type_alias { T.any(HakumiComponents::Table::DefinitionTypes::RawCollection, HakumiComponents::Table::DefinitionTypes::RawRecord) }
DisplayRow =
T.type_alias { { row: HakumiComponents::Table::RowRecord, depth: Integer } }
ScrollConfig =
T.type_alias { { x: T.nilable(String), y: T.nilable(String) } }
ControllerLocalValue =
T.type_alias { T.nilable(T.any(ControllerLocalScalar, ControllerLocalCollection)) }
ColumnInput =
T.type_alias { Concerns::Columns::ColumnInput }
ROW_SELECTION_TYPES =
T.let(%i[checkbox radio].freeze, T::Array[Symbol])
SORT_DIRECTIONS =
T.let(%i[ascend descend].freeze, T::Array[Symbol])
FIXED_SIDES =
T.let(%i[left right].freeze, T::Array[Symbol])
RESPONSIVE_BREAKPOINTS =
T.let(%i[xs sm md lg xl xxl].freeze, T::Array[Symbol])
ROW_BUTTON_ACTIONS =
T.let(%i[edit save cancel].freeze, T::Array[Symbol])
SELECTION_COLUMN =
:selection_column
EXPAND_COLUMN =
:expand_column

Constants included from HakumiComponents::Table::Concerns::SurfaceRendering

HakumiComponents::Table::Concerns::SurfaceRendering::ScrollValue, HakumiComponents::Table::Concerns::SurfaceRendering::TableDisplayRow

Constants included from HakumiComponents::Table::Concerns::CellRendering

HakumiComponents::Table::Concerns::CellRendering::CellContent, HakumiComponents::Table::Concerns::CellRendering::CellState, HakumiComponents::Table::Concerns::CellRendering::DragHandleCellState, HakumiComponents::Table::Concerns::CellRendering::ExpandCellState, HakumiComponents::Table::Concerns::CellRendering::SelectionCellState, HakumiComponents::Table::Concerns::CellRendering::SpanAttributes

Constants included from HakumiComponents::Table::Concerns::Columns

HakumiComponents::Table::Concerns::Columns::ColumnInputHash, HakumiComponents::Table::Concerns::Columns::ColumnInputValue

Constants inherited from BaseComponent

BaseComponent::ControllerOptions, BaseComponent::DateInput, BaseComponent::DateLikeValue, BaseComponent::DimensionInput, BaseComponent::HtmlPayloadInput, BaseComponent::I18nOptionValue, BaseComponent::PresenceArray, BaseComponent::PresenceScalar, BaseComponent::PresenceValue, BaseComponent::RawHtmlInput, BaseComponent::SIZES, BaseComponent::SizeValue, BaseComponent::SymbolInput

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HakumiComponents::Table::Concerns::SurfaceRendering

#ajax_pagination_component, #clear_text, #column_filter_multiple?, #container_classes, #current_sort_state, #empty_state, #header_filter_options, #invert_text, #pagination_component, #reset_text, #scroll_styles, #search_placeholder_text, #select_all_text, #selection_actions?, #selection_all_enabled?, #selection_header_state, #summary_content, #table_attributes, #table_classes, #table_container_attributes, #table_style, #wrapper_attributes

Methods included from HakumiComponents::Table::Concerns::CellRendering

#cell_attributes, #cell_classes, #cell_content, #header_cell_attributes, #header_cell_classes, #render_cell_display

Methods included from HakumiComponents::Table::Concerns::FixedColumns

#fixed_columns?

Methods included from HakumiComponents::Table::Concerns::Editable

#editable_config_json

Methods included from HakumiComponents::Table::Concerns::Columns

#compute_fixed_offsets, #dimension_to_css, #expand_column?, #expandable?, #generate_id, #leaf_column_count, #leaf_columns, #normalized_columns, #selection_column?

Methods inherited from BaseComponent

#append_data_token, boolean_html_param, #build_inline_style, cast_boolean, #cast_boolean, #class_names, #component_classes, #data_attributes_from, #dimension_to_css, #ensure_dom_id!, float_html_param, #generate_id, #html_classes, html_param, html_primitive_param, #html_style, #i18n_scope, integer_html_param, #merge_attributes, #render_value, #size_to_pixels, #stimulus_attrs, string_html_param, string_or_symbol_array_html_param, symbol_html_param, #t_default, #translate_with_default, #validate_inclusion!, #validate_required!, #value_present?

Constructor Details

#initialize(columns: nil, data_source: [], row_key: :key, size: :default, bordered: false, title: nil, footer: nil, row_selection: nil, expandable: nil, pagination: nil, summary: nil, empty_text: nil, sort_directions: SORT_DIRECTIONS, filter_on_close: true, filter_mode: :menu, filter_search: false, children_column_name: :children, indent_size: 16, scroll: nil, sticky: false, row_class_name: nil, editable: nil, row_drag: nil, column_drag: false, data_url: nil, **html_options) ⇒ Component

Returns a new instance of Component.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/components/hakumi_components/table/component.rb', line 148

def initialize(
  columns: nil,
  data_source: [],
  row_key: :key,
  size: :default,
  bordered: false,
  title: nil,
  footer: nil,
  row_selection: nil,
  expandable: nil,
  pagination: nil,
  summary: nil,
  empty_text: nil,
  sort_directions: SORT_DIRECTIONS,
  filter_on_close: true,
  filter_mode: :menu,
  filter_search: false,
  children_column_name: :children,
  indent_size: 16,
  scroll: nil,
  sticky: false,
  row_class_name: nil,
  editable: nil,
  row_drag: nil,
  column_drag: false,
  data_url: nil,
  **html_options
)
  @columns_data = T.let(columns, T.nilable(T::Array[ColumnInput]))
  @data_source = T.let(
    Array(data_source).map { |row| HakumiComponents::Table::RowRecord.new(source: row, children_column_name: children_column_name) },
    T::Array[HakumiComponents::Table::RowRecord]
  )
  @row_key = T.let(row_key, RowKeyConfig)
  @size = T.let(size || :default, Symbol)
  @bordered = T.let(bordered == true, T::Boolean)
  @title = T.let(title, TableContent)
  @footer = T.let(footer, TableContent)
  @row_selection = T.let(HakumiComponents::Table::Configs::RowSelection.coerce(row_selection), T.nilable(HakumiComponents::Table::Configs::RowSelection))
  @expandable = T.let(HakumiComponents::Table::Configs::Expandable.coerce(expandable), T.nilable(HakumiComponents::Table::Configs::Expandable))
  @summary = T.let(summary, TableContent)
  @empty_text = T.let(empty_text, TableContent)
  @sort_directions = T.let(Array(sort_directions).presence || SORT_DIRECTIONS, T::Array[Symbol])
  @filter_on_close = T.let(filter_on_close.nil? ? true : filter_on_close, T::Boolean)
  @filter_mode = T.let(normalize_component_symbol(filter_mode, fallback: :menu), Symbol)
  @filter_search = T.let(normalize_filter_search(filter_search), HakumiComponents::Table::DefinitionTypes::SearchValue)
  @children_column_name = T.let(children_column_name, HakumiComponents::Table::DefinitionTypes::ColumnKey)
  @indent_size = T.let(indent_size || 16, Integer)
  @scroll = T.let(normalize_scroll(scroll), T.nilable(ScrollConfig))
  @scroll_x = T.let(@scroll&.dig(:x), T.nilable(String))
  @scroll_y = T.let(@scroll&.dig(:y), T.nilable(String))
  @sticky = T.let(sticky == true, T::Boolean)
  @row_class_name = T.let(row_class_name, RowClassName)
  @editable = T.let(HakumiComponents::Table::Configs::Editable.coerce(editable), T.nilable(HakumiComponents::Table::Configs::Editable))
  @row_drag = T.let(HakumiComponents::Table::Configs::RowDrag.coerce(row_drag), T.nilable(HakumiComponents::Table::Configs::RowDrag))
  @column_drag = T.let(column_drag == true, T::Boolean)
  @data_url = T.let(data_url, T.nilable(String))
  @html_options = T.let(html_options, Types::HtmlAttributes)
  @pagination = T.let(HakumiComponents::Table::Configs::Pagination.coerce(pagination, total: @data_source.size), PaginationValue)
  @header_rows = T.let(nil, T.nilable(T::Array[T::Array[HakumiComponents::Table::HeaderCell]]))
  @display_rows = T.let(nil, T.nilable(T::Array[DisplayRow]))
  @row_render_state = T.let(nil, T.nilable(HakumiComponents::Table::RowRenderState))

  validate_props!
end

Class Method Details

.extract_controller_locals(params) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/components/hakumi_components/table/component.rb', line 81

def self.extract_controller_locals(params)
  {
    columns: parse_json_param(params[:columns]),
    data_source: parse_json_param(params[:data_source]),
    row_key: params[:row_key],
    size: params[:size],
    bordered: params[:bordered],
    title: params[:title],
    footer: params[:footer],
    row_selection: params[:row_selection],
    expandable: params[:expandable],
    pagination: params[:pagination],
    summary: params[:summary],
    empty_text: params[:empty_text],
    sort_directions: params[:sort_directions],
    filter_on_close: params[:filter_on_close],
    filter_mode: params[:filter_mode],
    filter_search: params[:filter_search],
    children_column_name: params[:children_column_name],
    indent_size: params[:indent_size],
    scroll: params[:scroll],
    sticky: params[:sticky],
    row_class_name: params[:row_class_name],
    editable: parse_json_param(params[:editable])
  }
end

.parse_json_param(value) ⇒ Object



109
110
111
112
113
114
115
116
# File 'app/components/hakumi_components/table/component.rb', line 109

def self.parse_json_param(value)
  return value.to_unsafe_h if value.is_a?(ActionController::Parameters)
  return value unless value.is_a?(String)

  JSON.parse(value)
rescue JSON::ParserError
  value
end

Instance Method Details

#column_filterable?(column) ⇒ Boolean

Returns:

  • (Boolean)


374
375
376
# File 'app/components/hakumi_components/table/component.rb', line 374

def column_filterable?(column)
  column.filters.present?
end

#column_sortable?(column) ⇒ Boolean

Returns:

  • (Boolean)


369
370
371
# File 'app/components/hakumi_components/table/component.rb', line 369

def column_sortable?(column)
  !column.sorter.nil?
end

#columns_dataObject



427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'app/components/hakumi_components/table/component.rb', line 427

def columns_data
  return @columns_data if @columns_data && !@columns_data.empty?

  slot_columns = respond_to?(:columns) ? public_send(:columns) : nil
  return [] unless slot_columns.is_a?(Array) && slot_columns.any?

  slot_columns.filter_map do |column|
    column_hash = column.respond_to?(:to_h) ? column.to_h : nil
    next unless column_hash.is_a?(Hash)

    column_hash
  end
end

#default_filtered_values(column) ⇒ Object



379
380
381
# File 'app/components/hakumi_components/table/component.rb', line 379

def default_filtered_values(column)
  Array(column.filtered_value || column.default_filtered_value)
end

#default_sort_order(column) ⇒ Object



384
385
386
# File 'app/components/hakumi_components/table/component.rb', line 384

def default_sort_order(column)
  column.sort_order || column.default_sort_order
end

#display_rowsObject



335
336
337
338
339
340
# File 'app/components/hakumi_components/table/component.rb', line 335

def display_rows
  @display_rows ||= HakumiComponents::Table::DisplayRows.build(
    rows: @data_source,
    pagination: @pagination
  )
end

#expandable_configObject



220
221
222
# File 'app/components/hakumi_components/table/component.rb', line 220

def expandable_config
  @expandable
end

#expanded_row?(row_key) ⇒ Boolean

Returns:

  • (Boolean)


358
359
360
# File 'app/components/hakumi_components/table/component.rb', line 358

def expanded_row?(row_key)
  row_render_state.expanded?(row_key)
end

#expanded_row_content(row, row_index) ⇒ Object



363
364
365
366
# File 'app/components/hakumi_components/table/component.rb', line 363

def expanded_row_content(row, row_index)
  renderer = @expandable&.expanded_row_render
  renderer&.call(row.source, row_index)
end

#header_rowsObject



330
331
332
# File 'app/components/hakumi_components/table/component.rb', line 330

def header_rows
  @header_rows ||= build_header_rows(normalized_columns)
end

#pagination_configObject



225
226
227
# File 'app/components/hakumi_components/table/component.rb', line 225

def pagination_config
  @pagination
end

#row_classes(row, row_index, row_key) ⇒ Object



353
354
355
# File 'app/components/hakumi_components/table/component.rb', line 353

def row_classes(row, row_index, row_key)
  row_render_state.row_classes(row, row_index, row_key)
end

#row_drag_configObject



235
236
237
# File 'app/components/hakumi_components/table/component.rb', line 235

def row_drag_config
  @row_drag
end

#row_key_for(row, index) ⇒ Object



343
344
345
# File 'app/components/hakumi_components/table/component.rb', line 343

def row_key_for(row, index)
  row_render_state.row_key_for(row, index)
end

#row_selected?(row_key) ⇒ Boolean

Returns:

  • (Boolean)


348
349
350
# File 'app/components/hakumi_components/table/component.rb', line 348

def row_selected?(row_key)
  row_render_state.selected?(row_key)
end

#row_selection_configObject



215
216
217
# File 'app/components/hakumi_components/table/component.rb', line 215

def row_selection_config
  @row_selection
end

#surface_bordered?Boolean

Returns:

  • (Boolean)


245
246
247
# File 'app/components/hakumi_components/table/component.rb', line 245

def surface_bordered?
  @bordered
end

#surface_column_drag?Boolean

Returns:

  • (Boolean)


305
306
307
# File 'app/components/hakumi_components/table/component.rb', line 305

def surface_column_drag?
  @column_drag
end

#surface_data_urlObject



315
316
317
# File 'app/components/hakumi_components/table/component.rb', line 315

def surface_data_url
  @data_url
end

#surface_editableObject



295
296
297
# File 'app/components/hakumi_components/table/component.rb', line 295

def surface_editable
  @editable
end

#surface_empty_textObject



320
321
322
# File 'app/components/hakumi_components/table/component.rb', line 320

def surface_empty_text
  @empty_text
end

#surface_filter_modeObject



285
286
287
# File 'app/components/hakumi_components/table/component.rb', line 285

def surface_filter_mode
  @filter_mode
end

#surface_filter_on_close?Boolean

Returns:

  • (Boolean)


280
281
282
# File 'app/components/hakumi_components/table/component.rb', line 280

def surface_filter_on_close?
  @filter_on_close
end

#surface_filter_searchObject



290
291
292
# File 'app/components/hakumi_components/table/component.rb', line 290

def surface_filter_search
  @filter_search
end

#surface_html_optionsObject



265
266
267
# File 'app/components/hakumi_components/table/component.rb', line 265

def surface_html_options
  @html_options
end

#surface_paginationObject



310
311
312
# File 'app/components/hakumi_components/table/component.rb', line 310

def surface_pagination
  @pagination
end

#surface_row_dragObject



300
301
302
# File 'app/components/hakumi_components/table/component.rb', line 300

def surface_row_drag
  @row_drag
end

#surface_row_selectionObject



270
271
272
# File 'app/components/hakumi_components/table/component.rb', line 270

def surface_row_selection
  @row_selection
end

#surface_scroll_xObject



255
256
257
# File 'app/components/hakumi_components/table/component.rb', line 255

def surface_scroll_x
  @scroll_x
end

#surface_scroll_yObject



260
261
262
# File 'app/components/hakumi_components/table/component.rb', line 260

def surface_scroll_y
  @scroll_y
end

#surface_sizeObject



240
241
242
# File 'app/components/hakumi_components/table/component.rb', line 240

def surface_size
  @size
end

#surface_sort_directionsObject



275
276
277
# File 'app/components/hakumi_components/table/component.rb', line 275

def surface_sort_directions
  @sort_directions
end

#surface_sticky?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'app/components/hakumi_components/table/component.rb', line 250

def surface_sticky?
  @sticky
end

#surface_summaryObject



325
326
327
# File 'app/components/hakumi_components/table/component.rb', line 325

def surface_summary
  @summary
end

#table_editable_configObject



230
231
232
# File 'app/components/hakumi_components/table/component.rb', line 230

def table_editable_config
  @editable
end