# frozen_string_literal: true module FComponents module Table module Desktop class Component < Base 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 def check_box_for(resource_index:) return if @check_boxes.empty? tag.td(class: 'py-3 text-center') { @check_boxes[resource_index] } end 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 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 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 def html?(test_target) Nokogiri::XML.parse(test_target.to_s).errors.empty? end def add_target(options) options ||= {} options[:data] ||= {} options[:data][:f_table_component_target] = 'desktopTable' nil end end end end end