Sha256: 53659dabcbcfaafc5a8b1ecdccf7f392ab25299d0532ff045c0bebfdfd9a3ae2

Contents?: true

Size: 1.92 KB

Versions: 24

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module Ariadne
  # `List` is used to show a list of items in a vertical format.
  class ListComponent < Ariadne::Component
    DEFAULT_TAG = :ul
    DEFAULT_UL_CLASSES = "ariadne-divide-y ariadne-divide-gray-300"

    renders_many :items, "ListItem"

    # @example Basic
    #   <% numbers = [1, 2, 3] %>
    #   <%= render(Ariadne::ListComponent.new) do |list| %>
    #     <% numbers.each do |number| %>
    #       <%= list.item do |item|  %>
    #         <%= item.entry { number } %>
    #       <% end %>
    #     <% end %>
    #   <% end %>
    #
    #
    # @param classes [String] <%= link_to_classes_docs %>
    # @param attributes [Hash] <%= link_to_attributes_docs %>
    def initialize(classes: "", attributes: {})
      @tag = DEFAULT_TAG
      @classes = class_names(DEFAULT_UL_CLASSES, classes)
      @attributes = attributes
    end

    def render?
      items.any?
    end

    # This component is part of `ListComponent` and should not be
    # used as a standalone component.
    class ListItem < Ariadne::Component
      DEFAULT_ITEM_CLASSES = "ariadne-relative ariadne-p-1.5 focus:ariadne-ring-2 focus:ariadne-ring-offset-2 focus:ariadne-ring-purple-500 hover:ariadne-bg-button-hover-color"

      renders_one :entry, lambda { |static_content = nil, &block|
        next static_content if static_content.present?

        render(Ariadne::BaseComponent.new(tag: :div)) do
          view_context.capture { block&.call }
        end
      }

      attr_reader :link, :classes, :attributes

      def initialize(link: {}, classes: "", attributes: {})
        @link = link
        @classes = class_names(DEFAULT_ITEM_CLASSES, classes)
        @attributes = attributes
      end

      def selected?
        @selected
      end

      def linked?
        @link.present?
      end

      def call
        Ariadne::BaseComponent.new(tag: :div, classes: @classes, attributes: @attributes)
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.12 app/components/ariadne/list_component.rb
ariadne_view_components-0.0.12-x86_64-linux app/components/ariadne/list_component.rb
ariadne_view_components-0.0.12-x86_64-darwin app/components/ariadne/list_component.rb
ariadne_view_components-0.0.12-x64-mingw32 app/components/ariadne/list_component.rb
ariadne_view_components-0.0.12-x64-mingw-ucrt app/components/ariadne/list_component.rb
ariadne_view_components-0.0.12-arm64-darwin app/components/ariadne/list_component.rb
ariadne_view_components-0.0.12-aarch64-linux app/components/ariadne/list_component.rb
ariadne_view_components-0.0.11 app/components/ariadne/list_component.rb
ariadne_view_components-0.0.11-x86_64-linux app/components/ariadne/list_component.rb
ariadne_view_components-0.0.11-x86_64-darwin app/components/ariadne/list_component.rb
ariadne_view_components-0.0.11-x64-mingw32 app/components/ariadne/list_component.rb
ariadne_view_components-0.0.11-x64-mingw-ucrt app/components/ariadne/list_component.rb
ariadne_view_components-0.0.11-arm64-darwin app/components/ariadne/list_component.rb
ariadne_view_components-0.0.11-aarch64-linux app/components/ariadne/list_component.rb
ariadne_view_components-0.0.10 app/components/ariadne/list_component.rb
ariadne_view_components-0.0.10-x86_64-linux app/components/ariadne/list_component.rb
ariadne_view_components-0.0.10-x86_64-darwin app/components/ariadne/list_component.rb
ariadne_view_components-0.0.10-x64-mingw32 app/components/ariadne/list_component.rb
ariadne_view_components-0.0.10-x64-mingw-ucrt app/components/ariadne/list_component.rb
ariadne_view_components-0.0.10-arm64-darwin app/components/ariadne/list_component.rb