Sha256: da8050bdd29b42bb81d51de5c201cbfcfedf2ddc3b9bb9a331b9d1d931114d07

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Turbo
    # Renders a table that uses turbo stream replacement when sorting or
    # paginating.
    class TableComponent < ::Katalyst::TableComponent
      include Tables::TurboReplaceable

      attr_reader :id

      def initialize(collection:, id:, header: true, **options)
        header = if header.is_a?(Hash)
                   default_header_options.merge(header)
                 elsif header
                   default_header_options
                 end

        @id = id

        super(collection: collection, header: header, id: id, **options)
      end

      private

      def default_html_attributes
        {
          data: {
            controller:                           "tables--turbo-collection",
            tables__turbo_collection_query_value: current_query,
            tables__turbo_collection_sort_value:  collection.sort,
          },
        }
      end

      def current_query
        Rack::Utils.build_nested_query(collection.to_params)
      end

      def default_header_options
        { link: { data: { turbo_stream: "" } } }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
katalyst-tables-2.3.0 app/components/katalyst/turbo/table_component.rb
katalyst-tables-2.2.12 app/components/katalyst/turbo/table_component.rb
katalyst-tables-2.2.11 app/components/katalyst/turbo/table_component.rb
katalyst-tables-2.2.10 app/components/katalyst/turbo/table_component.rb