Class: Plushie::Widget::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/plushie/widget/table.rb

Overview

Table -- data table with column definitions and optional sorting.

A container widget: child nodes represent row content. Use #push to add children immutably.

Props:

  • columns (array of hashes) -- column definitions (key, label, width).
  • rows (array) -- row data.
  • header (boolean) -- show header row.
  • separator (boolean) -- show row separators.
  • width (length) -- table width.
  • padding (number|hash) -- cell padding.
  • sort_by (string) -- column key to sort by.
  • sort_order (symbol) -- :asc or :desc.
  • header_text_size (number) -- header font size.
  • row_text_size (number) -- row font size.
  • cell_spacing (number) -- horizontal spacing between cells.
  • row_spacing (number) -- vertical spacing between rows.
  • separator_thickness (number) -- separator line thickness.
  • separator_color (string) -- separator colour.
  • a11y (hash) -- accessibility overrides.

Examples:

table = Plushie::Widget::Table.new("users",
  columns: [{ key: "name", label: "Name" }, { key: "email", label: "Email" }],
  sort_by: "name", sort_order: :asc)
node = table.build

Constant Summary collapse

PROPS =

Supported property keys for the table widget.

%i[columns rows header separator width padding sort_by sort_order
header_text_size row_text_size cell_spacing row_spacing
separator_thickness separator_color a11y].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, **opts) ⇒ Table

Returns a new instance of Table.

Parameters:

  • id (String)

    widget identifier

  • opts (Hash)

    optional properties matching PROPS keys



44
45
46
47
48
# File 'lib/plushie/widget/table.rb', line 44

def initialize(id, **opts)
  @id = id.to_s
  @children = opts.delete(:children) || []
  PROPS.each { |k| instance_variable_set(:"@#{k}", opts[k]) if opts.key?(k) }
end

Instance Attribute Details

#a11yObject (readonly)

Returns the value of attribute a11y.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def a11y
  @a11y
end

#cell_spacingObject (readonly)

Returns the value of attribute cell_spacing.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def cell_spacing
  @cell_spacing
end

#childrenObject (readonly)

Returns the value of attribute children.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def children
  @children
end

#columnsObject (readonly)

Returns the value of attribute columns.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def columns
  @columns
end

#headerObject (readonly)

Returns the value of attribute header.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def header
  @header
end

#header_text_sizeObject (readonly)

Returns the value of attribute header_text_size.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def header_text_size
  @header_text_size
end

#idObject (readonly)

Returns the value of attribute id.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def id
  @id
end

#paddingObject (readonly)

Returns the value of attribute padding.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def padding
  @padding
end

#row_spacingObject (readonly)

Returns the value of attribute row_spacing.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def row_spacing
  @row_spacing
end

#row_text_sizeObject (readonly)

Returns the value of attribute row_text_size.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def row_text_size
  @row_text_size
end

#rowsObject (readonly)

Returns the value of attribute rows.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def rows
  @rows
end

#separatorObject (readonly)

Returns the value of attribute separator.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def separator
  @separator
end

#separator_colorObject (readonly)

Returns the value of attribute separator_color.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def separator_color
  @separator_color
end

#separator_thicknessObject (readonly)

Returns the value of attribute separator_thickness.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def separator_thickness
  @separator_thickness
end

#sort_byObject (readonly)

Returns the value of attribute sort_by.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def sort_by
  @sort_by
end

#sort_orderObject (readonly)

Returns the value of attribute sort_order.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def sort_order
  @sort_order
end

#widthObject (readonly)

Returns the value of attribute width.



1
2
3
# File 'lib/plushie/widget/table.rb', line 1

def width
  @width
end

Instance Method Details

#buildPlushie::Node

Returns:



65
66
67
68
69
70
71
72
73
# File 'lib/plushie/widget/table.rb', line 65

def build
  props = {}
  PROPS.each do |key|
    val = instance_variable_get(:"@#{key}")
    Build.put_if(props, key, val)
  end
  Node.new(id: @id, type: "table", props: props,
    children: Build.children_to_nodes(@children))
end

#push(child) ⇒ Table

Append a child node. Returns a new Table (immutable).

Parameters:

  • child (Object)

    child widget or node

Returns:



60
61
62
# File 'lib/plushie/widget/table.rb', line 60

def push(child)
  dup.tap { _1.instance_variable_set(:@children, @children + [child]) }
end