Module: Playbook::PbAdvancedTable::ColumnLayoutHelper

Included in:
TableHeader, TableRow
Defined in:
app/pb_kits/playbook/pb_advanced_table/column_layout_helper.rb

Overview

Mirrors React ColumnLayoutHelper: maps column_styling width keys to inline styles on header/body cells so columns stay stable when rows expand/collapse.

Instance Method Summary collapse

Instance Method Details

#build_column_layout_styles(styling) ⇒ Object

Inline width styles for / . Returns a hash with :width, :min_width, and/or :max_width (CSS length strings). KitBase converts snake_case keys to kebab-case CSS properties.

If only width is set (no min/max), all three are locked to that value.



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
69
70
71
72
73
# File 'app/pb_kits/playbook/pb_advanced_table/column_layout_helper.rb', line 42

def build_column_layout_styles(styling)
  return {} unless styling.is_a?(Hash) && styling.present?

  styling_width = read_styling_length(styling, :width, "width")
  styling_min = read_styling_length(styling, :min_width, "min_width", :minWidth, "minWidth")
  styling_max = read_styling_length(styling, :max_width, "max_width", :maxWidth, "maxWidth")

  has_width = !styling_width.nil? && styling_width != ""
  has_min = !styling_min.nil? && styling_min != ""
  has_max = !styling_max.nil? && styling_max != ""

  width_value = has_width ? styling_width : nil
  min_value = has_min ? styling_min : nil
  max_value = has_max ? styling_max : nil

  if has_width && !has_min && !has_max
    min_value = styling_width
    max_value = styling_width
    width_value = styling_width
  end

  styles = {}
  width_css = css_length(width_value)
  min_css = css_length(min_value)
  max_css = css_length(max_value)

  styles[:width] = width_css if width_css
  styles[:min_width] = min_css if min_css
  styles[:max_width] = max_css if max_css

  styles
end

#build_column_layout_styles_from_column(column) ⇒ Object

Builds layout styles from a column definition that may include :column_styling.



30
31
32
33
34
35
# File 'app/pb_kits/playbook/pb_advanced_table/column_layout_helper.rb', line 30

def build_column_layout_styles_from_column(column)
  return {} unless column.is_a?(Hash)

  styling = column[:column_styling] || column["column_styling"] || {}
  build_column_layout_styles(styling)
end

#css_length(value) ⇒ Object

Converts a Playbook column width value to a CSS length string. Numbers are treated as pixels; strings are passed through (e.g. "12rem", "200px").



10
11
12
13
14
15
16
# File 'app/pb_kits/playbook/pb_advanced_table/column_layout_helper.rb', line 10

def css_length(value)
  return nil if value.nil? || value == ""

  return "#{value}px" if value.is_a?(Numeric)

  value.to_s
end

#read_styling_length(styling, *keys) ⇒ Object

Reads a styling length from column_styling, accepting snake_case or camelCase keys.



19
20
21
22
23
24
25
26
27
# File 'app/pb_kits/playbook/pb_advanced_table/column_layout_helper.rb', line 19

def read_styling_length(styling, *keys)
  return nil unless styling.is_a?(Hash)

  keys.each do |key|
    value = styling[key] || styling[key.to_s] || styling[key.to_sym]
    return value unless value.nil?
  end
  nil
end