Class: FComponents::Table::Desktop::Component

Inherits:
Base
  • Object
show all
Defined in:
app/components/f_components/table/desktop/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/desktop/component.rb', line 7

def initialize(**options)
  @rows = options.delete(:rows)
  @actions = options.delete(:actions)
  @check_boxes = options.delete(:check_boxes)
  @columns = options.delete(:columns)
  @columns.prepend('') if @check_boxes.present?
  @columns.push('') if @actions.present?
  @id = ['desktop-table', options.delete(:id_suffix)].filter_map(&:presence).join('-')
  @class = options.delete(:class)
  add_target(options)
  @options = options
end

Instance Method Details

#actions(resource_index:) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'app/components/f_components/table/desktop/component.rb', line 26

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

  resource_actions = @actions[resource_index]

  tag.td(class: 'py-3 px-4') do
    actions_for_resource(resource_actions)
  end
end

#actions_for_resource(resource_actions) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'app/components/f_components/table/desktop/component.rb', line 36

def actions_for_resource(resource_actions)
  fcomponent :dropdown, label: 'Opções', padding: false, icon: 'plus', class: 'w-fit mx-auto' do
    safe_join(
      resource_actions.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



61
62
63
64
65
66
67
# File 'app/components/f_components/table/desktop/component.rb', line 61

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

  nil
end

#check_box_for(resource_index:) ⇒ Object



20
21
22
23
24
# File 'app/components/f_components/table/desktop/component.rb', line 20

def check_box_for(resource_index:)
  return if @check_boxes.empty?

  tag.td(class: 'py-3 text-center') { @check_boxes[resource_index] }
end

#format_cell(value) ⇒ Object



48
49
50
51
52
53
54
55
# File 'app/components/f_components/table/desktop/component.rb', line 48

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]}-desktop-#{value[:column_index]}" : nil

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

#html?(test_target) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/components/f_components/table/desktop/component.rb', line 57

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