Class: FComponents::Table::Mobile::Component

Inherits:
Base
  • Object
show all
Defined in:
app/components/f_components/table/mobile/component.rb

Instance Method Summary collapse

Methods inherited from Base

call

Methods included from ComponentsHelper

#component, #fcomponent

Constructor Details

#initialize(**options) ⇒ Component

Returns a new instance of Component.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/components/f_components/table/mobile/component.rb', line 7

def initialize(**options)
  @rows = options.delete(:rows)
  @actions = options.delete(:actions)
  @main_column = options.delete(:main_column)
  @check_boxes = options.delete(:check_boxes)
  @columns = options.delete(:columns)
  @id = ['mobile-table', options.delete(:id_suffix)].filter_map(&:presence).join('-')
  @class = options.delete(:class)
  @resources = options.delete(:resources)
  add_target(options)
  @options = options
end

Instance Method Details

#actions(resource_index:) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/components/f_components/table/mobile/component.rb', line 61

def actions(resource_index:)
  return if @actions.blank?

  fcomponent :dropdown, label: 'Opções', padding: false, icon: 'plus', class: 'mt-5' do
    safe_join(
      @actions[resource_index].map do |action|
        tag.div(class: 'text-sm py-3 px-5 even:bg-gray-lt') do
          action
        end
      end
    )
  end
end

#add_target(options) ⇒ Object



92
93
94
95
96
97
98
# File 'app/components/f_components/table/mobile/component.rb', line 92

def add_target(options)
  options ||= {}
  options[:data] ||= {}
  options[:data][:f_table_component_target] = 'mobileTable'

  nil
end

#format_cell(value) ⇒ Object



75
76
77
78
79
80
81
82
# File 'app/components/f_components/table/mobile/component.rb', line 75

def format_cell(value)
  cell_content = value.is_a?(Hash) ? value[:cell_content] : value
  return cell_content if cell_content.present? && html?(cell_content)

  cell_id = value[:id].present? ? "#{value[:id]}-mobile-#{value[:column_index]}" : nil

  tag.p(id: cell_id, class: 'max-w-5/6') { cell_content }
end

#html?(test_target) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'app/components/f_components/table/mobile/component.rb', line 84

def html?(test_target)
  Nokogiri::XML.parse(test_target.to_s).errors.empty?
end

#invalid_column_error!Object



88
89
90
# File 'app/components/f_components/table/mobile/component.rb', line 88

def invalid_column_error!
  raise "Main column '#{@main_column}' not found in #{@columns}. Maybe you forgot to specify a main column?"
end

#rowsObject



43
44
45
46
47
# File 'app/components/f_components/table/mobile/component.rb', line 43

def rows
  @rows.map do |row|
    row.map { |cell| format_cell(cell) }
  end
end

#table_body(row) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'app/components/f_components/table/mobile/component.rb', line 49

def table_body(row)
  tag.div class: 'flex flex-col space-y-5 mt-5' do
    safe_join(
      row.each_with_index.map do |value, position|
        tag.div(class: 'flex items-center text-sm', data: { mobile_col: true }) do
          tag.strong(class: 'w-20 min-w-20 mr-8') { @columns[position] } + format_cell(value)
        end
      end
    )
  end
end

#table_head(resource_index:, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/components/f_components/table/mobile/component.rb', line 20

def table_head(resource_index:, &block)
  current_resource = @resources[resource_index]

  if @main_column.is_a?(Proc) && current_resource.present?
    main_header = @main_column.call(current_resource)
  else
    main_header_index = @columns.index(@main_column) or invalid_column_error!
    main_header = tag.strong(class: 'text-sm') do
      value = @rows[resource_index][main_header_index]

      value.is_a?(Hash) ? value[:cell_content] : value
    end
  end

  title = if @check_boxes.present?
            @check_boxes[resource_index] + main_header
          else
            main_header
          end

  fcomponent(:collapsible, summary: title, class: 'bg-white border-b border-gray-lt', &block)
end